Skip to content

Instantly share code, notes, and snippets.

@norcross
Created December 24, 2011 03:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save norcross/1516135 to your computer and use it in GitHub Desktop.
Save norcross/1516135 to your computer and use it in GitHub Desktop.
Public Gist Sidebar Widget
<?php
/*
Plugin Name: GitHub Gists Sidebar Widget
Plugin URI: http://andrewnorcross.com/plugins
Description: A sidebar widget to display your public gists from GitHub.
Version: 1.0
Author: norcross
Author URI: http://andrewnorcross.com
*/
/* Copyright 2012 Andrew Norcross
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License (GPL v2) only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* construct widget
*/
class ListGistsWidget extends WP_Widget {
/** constructor */
function ListGistsWidget() {
$widget_ops = array( 'classname' => 'list_gists', 'description' => 'Displays a list of gists hosted on GitHub' );
$this->WP_Widget( 'list_gists', 'Public GitHub Gists', $widget_ops );
}
function gist_transient_store() {
// grab plugin variables
$user = $instance['github_user'];
$number = $instance['gists_num'];
if( false == get_transient( 'public_github_gists' ) ) {
if( !class_exists( 'WP_Http' ) )
include_once( ABSPATH . WPINC. '/class-http.php' );
// set number of items to return
if (!empty ($number) ) { $max = $number; } else { $max = 100; } // 100 is the max return in the GitHub API
$request = new WP_Http;
$url = 'https://api.github.com/users/'.urlencode($user).'/gists?&per_page='.$max.'';
$response = wp_remote_get ( $url );
// Save a transient to the database
set_transient('public_github_gists', $response, 60*60*12 );
} // end transient check
}
/** @see WP_Widget::widget */
function widget($args, $instance) {
extract( $args, EXTR_SKIP );
echo $before_widget;
$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
echo '<ul>';
// check for transient cache'd result
$response = get_transient( 'public_github_gists' );
if( is_wp_error( $response ) ) {
echo '<p>Sorry, there was an error with your request.</p>';
} else {
$gist_list = json_decode( $response['body'] );
// set variable options for plugin call
$user = $instance['github_user'];
$number = $instance['gists_num'];
$date = isset($instance['show_date']) ? $instance['show_date'] : true;
$link = isset($instance['show_link']) ? $instance['show_link'] : true;
$text = $instance['link_text'];
foreach ( $gist_list as $gist ) {
// get gist values for display
$desc = $gist->description;
$gistid = $gist->id;
$url = $gist->html_url;
// grab date and convert it to a readable format
$create = $gist->created_at;
$create = strtotime($create);
$create = date('n/j/Y', $create);
// check for missing values and replace them if necessary
( $desc == null) ? $title = 'Gist ID: '.$gistid : $title = $desc;
( empty ($text) ) ? $text = 'Github Profile' : $text = $text;
// display list of gists
echo '<li class="gist_item">';
echo '<a class="gist_title" href="'.$url.'" title="'.$title.'" target="_blank">'.$title.'</a>';
if ($date) {
echo '<br /><span class="gist_date">Created: '.$create.'</span>';
}
echo '</li>';
} // end foreach
echo '</ul>';
// display optional github profile link
if ($link) {
echo '<p class="github_link"><a href="https://github.com/'.$user.'" title="'.$text.'" target="_blank">'.$text.'</a></p>';
}
} // end error check
echo $after_widget;
?>
<?php }
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['github_user'] = strip_tags($new_instance['github_user']);
$instance['gists_num'] = strip_tags($new_instance['gists_num']);
$instance['link_text'] = strip_tags($new_instance['link_text']);
$instance['show_date'] = !empty($new_instance['show_date']) ? 1 : 0;
$instance['show_link'] = !empty($new_instance['show_link']) ? 1 : 0;
return $instance;
}
/** @see WP_Widget::form */
function form($instance) {
$instance = wp_parse_args( (array) $instance, array(
'title' => '',
'github_user' => '',
'gists_num' => '',
'link_text' => 'See my GitHub profile',
'show_date' => 0,
'show_link' => 0,
));
foreach ( $instance as $field => $val ) {
if ( isset($new_instance[$field]) )
$instance[$field] = 1;
}
$title = strip_tags($instance['title']);
$github_user = strip_tags($instance['github_user']);
$gists_num = strip_tags($instance['gists_num']);
$link_text = strip_tags($instance['link_text']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title'); ?><input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('github_user'); ?>"><?php _e('GitHub username'); ?>
<input class="widefat" id="<?php echo $this->get_field_id('github_user'); ?>" name="<?php echo $this->get_field_name('github_user'); ?>" type="text" value="<?php echo esc_attr($github_user); ?>" /></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('gists_num'); ?>"><?php _e('Gists to display'); ?><input class="widefat" id="<?php echo $this->get_field_id('gists_num'); ?>" name="<?php echo $this->get_field_name('gists_num'); ?>" type="text" value="<?php echo esc_attr($gists_num); ?>" /></label>
</p>
<p><strong>Optional Values</strong></p>
<p>
<input class="checkbox" type="checkbox" <?php checked($instance['show_date'], true) ?> id="<?php echo $this->get_field_id('show_date'); ?>" name="<?php echo $this->get_field_name('show_date'); ?>" />
<label for="<?php echo $this->get_field_id('show_date'); ?>"><?php _e('Display creation date'); ?></label>
</p>
<p>
<input class="checkbox" type="checkbox" <?php checked($instance['show_link'], true) ?> id="<?php echo $this->get_field_id('show_link'); ?>" name="<?php echo $this->get_field_name('show_link'); ?>" />
<label for="<?php echo $this->get_field_id('show_link'); ?>"><?php _e('Include link to Github profile'); ?></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('link_text'); ?>"><?php _e('Profile link text'); ?><input class="widefat" id="<?php echo $this->get_field_id('link_text'); ?>" name="<?php echo $this->get_field_name('link_text'); ?>" type="text" value="<?php echo esc_attr($link_text); ?>" /></label>
</p>
<?php }
} // class
// register widget
add_action( 'widgets_init', create_function( '', "register_widget('ListGistsWidget');" ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment