public
Created

Public Gist Sidebar Widget

  • Download Gist
gist-sidebar-widget
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
<?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
*/
 
// add admin CSS for my error message
function rkv_gist_widget_css() {
echo "
<style type='text/css'>
span.gist_error_message {color:#CD0000;padding-top:5px;display:block;text-align:right;font-weight:bold;}
</style>
";
}
 
add_action('admin_head', 'rkv_gist_widget_css');
 
/**
* construct widget
*/
class rkv_ListGistsWidget extends WP_Widget {
/** constructor */
function rkv_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 );
}
 
/** @see WP_Widget::widget */
function widget($args, $instance) {
extract( $args, EXTR_SKIP );
 
// first check for a username. can't do much without it
$user = $instance['github_user'];
if (empty ($user) ) {
echo '<p>Please enter a username in the widget settings</p>';
} else {
 
// check for stored transient. if none present, create one
if( false == get_transient( 'public_github_gists' ) ) {
// grab username and total gists to grab
$user = $instance['github_user'];
$number = $instance['gists_num'];
 
// 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
 
 
// set all variable options for plugin call
$user = $instance['github_user'];
$number = $instance['gists_num'];
$date = $instance['show_date'];
$link = $instance['show_link'];
$text = $instance['link_text'];
// check for transient cache'd result
$response = get_transient( 'public_github_gists' );
 
// check for bad response from GitHub
if( is_wp_error( $response ) ) {
echo '<p>Sorry, there was an error with your request.</p>';
} else {
$gist_list = json_decode( $response['body'] );
 
// start output of actual widget
echo $before_widget;
$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
echo '<ul>';
 
// list individual items
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>';
// include optional date
if ($date == 1) : echo '<br /><span class="gist_date">Created: '.$create.'</span>'; endif;
 
echo '</li>';
} // end foreach
echo '</ul>';
// display optional github profile link
if ($link == 1) : echo '<p class="github_link"><a href="https://github.com/'.$user.'" title="'.$text.'" target="_blank">'.$text.'</a></p>'; endif;
 
} // end error check
echo $after_widget;
} // end username check
?>
 
<?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;
 
// Remove our saved transient (in case we changed something)
delete_transient('public_github_gists');
 
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'); ?></label>
<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); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('github_user'); ?>"><?php _e('GitHub username'); ?></label>
<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); ?>" />
<?php if (empty ($github_user) ) : echo '<span class="gist_error_message">Username is required!</span>'; endif; ?>
</p>
<p>
<label for="<?php echo $this->get_field_id('gists_num'); ?>"><?php _e('Gists to display'); ?></label>
<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); ?>" />
</p>
<br />
<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'); ?></label>
<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); ?>" />
</p>
<?php }
 
} // class
 
// register widget
add_action( 'widgets_init', create_function( '', "register_widget('rkv_ListGistsWidget');" ) );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.