| @@ -3,9 +3,9 @@ | ||
| 3 | 3 | /* |
| 4 | 4 | Plugin Name: Github Embed |
| 5 | 5 | Plugin URI: http://www.leewillis.co.uk/wordpress-plugins |
| 6 | 6 | Description: Paste the URL to a Github project into your posts or pages, and have the project information pulled in and displayed automatically |
| 7 | -Version: 1.5 | |
| 7 | +Version: 1.2 | |
| 8 | 8 | Author: Lee Willis |
| 9 | 9 | Author URI: http://www.leewillis.co.uk/ |
| 10 | 10 | */ |
| 11 | 11 | |
| @@ -32,9 +32,9 @@ | ||
| 32 | 32 | */ |
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * This class handles being the oEmbed provider in terms of registering the URLs that |
| 36 | - * we can embed, and handling the actual oEmbed calls. It relies on the github_api | |
| 36 | + * we can embed, and handling the actual oEmbed calls. It relies on the github_api | |
| 37 | 37 | * class to retrieve the information from the GitHub API. |
| 38 | 38 | * @uses class github_api |
| 39 | 39 | */ |
| 40 | 40 | class github_embed { |
| @@ -52,14 +52,12 @@ | ||
| 52 | 52 | public function __construct ( $api ) { |
| 53 | 53 | |
| 54 | 54 | $this->api = $api; |
| 55 | 55 | |
| 56 | - add_action( 'init', array ( $this, 'register_oembed_handler' ) ); | |
| 57 | - add_action( 'init', array( $this, 'maybe_handle_oembed' ) ); | |
| 58 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); | |
| 59 | - add_action( 'admin_init', array( $this, 'schedule_expiry' ) ); | |
| 60 | - add_action( 'github_embed_cron', array( $this, 'cron' ) ); | |
| 61 | - | |
| 56 | + add_action ( 'init', array ( $this, 'register_oembed_handler' ) ); | |
| 57 | + add_action ( 'init', array ( $this, 'maybe_handle_oembed' ) ); | |
| 58 | + add_action ( 'wp_enqueue_scripts', array ( $this, 'enqueue_styles' ) ); | |
| 59 | + | |
| 62 | 60 | // @TODO i18n |
| 63 | 61 | |
| 64 | 62 | } |
| 65 | 63 | |
| @@ -65,42 +63,8 @@ | ||
| 65 | 63 | |
| 66 | 64 | |
| 67 | 65 | |
| 68 | 66 | /** |
| 69 | - * Make sure we have a scheduled event set to clear down the oEmbed cache until | |
| 70 | - * WordPress supports cache_age in oEmbed responses. | |
| 71 | - */ | |
| 72 | - function schedule_expiry() { | |
| 73 | - | |
| 74 | - if( ! wp_next_scheduled( 'github_embed_cron' ) ) { | |
| 75 | - $frequency = apply_filters ( 'github_embed_cache_frequency', 'daily' ); | |
| 76 | - wp_schedule_event( time(), $frequency, 'github_embed_cron' ); | |
| 77 | - } | |
| 78 | - | |
| 79 | - } | |
| 80 | - | |
| 81 | - | |
| 82 | - | |
| 83 | - /** | |
| 84 | - * Expire old oEmbeds. | |
| 85 | - * Note: This is a bit sledgehammer-to-crack-a-nut hence why I'm only running it | |
| 86 | - * daily. Ideally WP should honour cache_age in oEmbed responses properly | |
| 87 | - */ | |
| 88 | - function cron() { | |
| 89 | - | |
| 90 | - global $wpdb, $table_prefix; | |
| 91 | - | |
| 92 | - $sql = "DELETE | |
| 93 | - FROM {$table_prefix}postmeta | |
| 94 | - WHERE meta_key LIKE '_oembed_%'"; | |
| 95 | - | |
| 96 | - $results = $wpdb->get_results ( $sql ); | |
| 97 | - | |
| 98 | - } | |
| 99 | - | |
| 100 | - | |
| 101 | - | |
| 102 | - /** | |
| 103 | 67 | * Enqueue the frontend CSS |
| 104 | 68 | * @return void |
| 105 | 69 | */ |
| 106 | 70 | function enqueue_styles() { |
| @@ -105,10 +69,10 @@ | ||
| 105 | 69 | */ |
| 106 | 70 | function enqueue_styles() { |
| 107 | 71 | |
| 108 | 72 | wp_register_style ( 'github-embed', plugins_url(basename(dirname(__FILE__)).'/css/github-embed.css' ) ); |
| 109 | - wp_enqueue_style ( 'github-embed' ); | |
| 110 | - | |
| 73 | + wp_enqueue_style ( 'github-embed' ); | |
| 74 | + | |
| 111 | 75 | } |
| 112 | 76 | |
| 113 | 77 | |
| 114 | 78 | |
| @@ -120,12 +84,12 @@ | ||
| 120 | 84 | * @return array The list, with our new provider added |
| 121 | 85 | */ |
| 122 | 86 | public function register_oembed_handler() { |
| 123 | 87 | |
| 124 | - $oembed_url = home_url(); | |
| 88 | + $oembed_url = home_url (); | |
| 125 | 89 | $key = $this->get_key(); |
| 126 | - $oembed_url = add_query_arg( array( 'github_oembed' => $key ), $oembed_url ); | |
| 127 | - wp_oembed_add_provider( '#https?://github.com/.*#i', $oembed_url, true ); | |
| 90 | + $oembed_url = add_query_arg ( array ( 'github_oembed' => $key ), $oembed_url); | |
| 91 | + wp_oembed_add_provider ( '#https?://github.com/.*#i', $oembed_url, true ); | |
| 128 | 92 | |
| 129 | 93 | } |
| 130 | 94 | |
| 131 | 95 | |
| @@ -137,9 +101,9 @@ | ||
| 137 | 101 | */ |
| 138 | 102 | private function get_key() { |
| 139 | 103 | |
| 140 | 104 | $key = get_option ( 'github_oembed_key' ); |
| 141 | - | |
| 105 | + | |
| 142 | 106 | if ( ! $key ) { |
| 143 | 107 | $key = md5 ( time() . rand ( 0,65535 ) ); |
| 144 | 108 | add_option ( 'github_oembed_key', $key, '', 'yes' ); |
| 145 | 109 | } |
| @@ -171,9 +135,9 @@ | ||
| 171 | 135 | public function handle_oembed() { |
| 172 | 136 | |
| 173 | 137 | // Check this request is valid |
| 174 | 138 | if ( $_GET['github_oembed'] != $this->get_key() ) { |
| 175 | - header ( 'HTTP/1.0 403 Forbidden' ); | |
| 139 | + header ( 'HTTP/1.0 403 Forbidden' ); | |
| 176 | 140 | die ( 'Sad Octocat is sad.' ); |
| 177 | 141 | } |
| 178 | 142 | |
| 179 | 143 | // Check we have the required information |
| @@ -206,13 +170,8 @@ | ||
| 206 | 170 | if ( $milestone ) { |
| 207 | 171 | $this->oembed_github_repo_milestone_summary ( $matches[1], $matches[2], $milestone ); |
| 208 | 172 | } |
| 209 | 173 | |
| 210 | - // New style milestone URL, e.g. https://github.com/example/example/milestone/1 | |
| 211 | - } elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/milestone/([0-9]*)$#i', $url, $matches ) ) { | |
| 212 | - | |
| 213 | - $this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $matches[3] ); | |
| 214 | - | |
| 215 | 174 | // Repository |
| 216 | 175 | } elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { |
| 217 | 176 | |
| 218 | 177 | $this->oembed_github_repo ( $matches[1], $matches[2] ); |
| @@ -249,21 +208,25 @@ | ||
| 249 | 208 | |
| 250 | 209 | // @TODO This should all be templated |
| 251 | 210 | $response->html = '<div class="github-embed github-embed-repo-contributors">'; |
| 252 | 211 | $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; |
| 253 | - | |
| 212 | + | |
| 254 | 213 | $response->html .= '<span class="github-heading">Contributors: </span>'; |
| 255 | 214 | |
| 256 | 215 | $response->html .= '<ul class="github-repo-contributors">'; |
| 257 | - | |
| 216 | + | |
| 258 | 217 | foreach ( $contributors as $contributor ) { |
| 259 | 218 | |
| 219 | + $details = $this->api->get_user ($contributor->login); | |
| 260 | 220 | $response->html .= '<li class="github-repo-contributor">'; |
| 261 | 221 | $response->html .= '<img class="github-repo-contributor-avatar" src="'; |
| 262 | - $response->html .= esc_url(add_query_arg(array('s'=>$gravatar_size), $contributor->author->avatar_url)); | |
| 263 | - $response->html .= '" alt="Picture of '.esc_attr($contributor->author->login).'">'; | |
| 222 | + $response->html .= esc_url(add_query_arg(array('s'=>$gravatar_size), $contributor->avatar_url)); | |
| 223 | + $response->html .= '" alt="Picture of '.esc_attr($contributor->login).'">'; | |
| 224 | + if ( ! empty ( $details->name ) ) { | |
| 225 | + $response->html .= '<span class="github-repo-contributor-name">'.esc_html($details->name)."</span><br>"; | |
| 226 | + } | |
| 264 | 227 | $response->html .= '<span class="github-repo-contributor-login">'; |
| 265 | - $response->html .= '<a href="https://github.com/'.esc_attr($contributor->author->login).'">'.esc_attr($contributor->author->login).'</a></span>'; | |
| 228 | + $response->html .= '<a href="https://github.com/'.esc_attr($contributor->login).'">'.esc_attr($contributor->login).'</a></span>'; | |
| 266 | 229 | } |
| 267 | 230 | |
| 268 | 231 | $response->html .= '</ul>'; |
| 269 | 232 | $response->html .= '<div style="clear: both;"></div>'; |
| @@ -295,32 +258,31 @@ | ||
| 295 | 258 | |
| 296 | 259 | // @TODO This should all be templated |
| 297 | 260 | $response->html = '<div class="github-embed github-embed-milestone-summary">'; |
| 298 | 261 | $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; |
| 299 | - | |
| 262 | + | |
| 300 | 263 | $response->html .= '<span class="github-heading">Milestone: </span>'; |
| 301 | 264 | $response->html .= '<span class="github-milestone-title">'.esc_html($summary->title)."</span><br>"; |
| 302 | - | |
| 265 | + | |
| 303 | 266 | $response->html .= '<span class="github-heading">Issues: </span>'; |
| 304 | 267 | $response->html .= '<span class="github-milestone-issues">'; |
| 305 | - $response->html .= esc_html ( number_format_i18n ( $summary->open_issues ) )." open, "; | |
| 306 | - $response->html .= esc_html ( number_format_i18n ( $summary->closed_issues ) )." closed.</span><br>"; | |
| 307 | - | |
| 268 | + $response->html .= esc_html($summary->open_issues)." open, "; | |
| 269 | + $response->html .= esc_html($summary->closed_issues)." closed.</span><br>"; | |
| 270 | + | |
| 308 | 271 | if ( ! empty ( $summary->due_on ) ) { |
| 309 | 272 | $response->html .= '<span class="github-heading">Due: </span>'; |
| 310 | 273 | $due_date = date_format ( date_create ( $summary->due_on ), 'jS F Y' ); |
| 311 | 274 | $response->html .= '<span class="github-milestone-due-date">'.esc_html($due_date).'</span><br>'; |
| 312 | 275 | } |
| 313 | - | |
| 276 | + | |
| 314 | 277 | $response->html .= '<p class="github-milestone-description">'.nl2br(esc_html($summary->description))."</p><br>"; |
| 315 | 278 | $response->html .= '</div>'; |
| 316 | 279 | |
| 317 | - error_log( basename( __FILE__ ) . ': (' . __LINE__ . ') : response is ' . print_r( $response, 1 ) ); | |
| 318 | 280 | header ( 'Content-Type: application/json' ); |
| 319 | 281 | echo json_encode ( $response ); |
| 320 | 282 | die(); |
| 321 | 283 | |
| 322 | - } | |
| 284 | + } | |
| 323 | 285 | |
| 324 | 286 | |
| 325 | 287 | |
| 326 | 288 | /** |
| @@ -342,11 +304,10 @@ | ||
| 342 | 304 | // @TODO This should all be templated |
| 343 | 305 | $response->html = '<div class="github-embed github-embed-repository">'; |
| 344 | 306 | $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; |
| 345 | 307 | $response->html .= '<a href="'.esc_attr($repo->html_url).'" target="_blank">'.esc_html($repo->html_url)."</a><br/>"; |
| 346 | - $response->html .= '<a href="'.esc_attr($repo->html_url . '/network') . '" target="_blank">'.esc_html ( number_format_i18n ( $repo->forks_count ) ) . "</a> forks.<br/>"; | |
| 347 | - $response->html .= '<a href="'.esc_attr($repo->html_url . '/stargazers') . '" target="_blank">'.esc_html ( number_format_i18n ( $repo->stargazers_count ) ) . "</a> stars.<br/>"; | |
| 348 | - $response->html .= '<a href="'.esc_attr($repo->html_url . '/issues') . '" target="_blank">'.esc_html ( number_format_i18n ( $repo->open_issues_count ) ) . "</a> open issues.<br/>"; | |
| 308 | + $response->html .= esc_html($repo->forks_count)." forks.<br/>"; | |
| 309 | + $response->html .= esc_html($repo->open_issues_count)." open issues.<br/>"; | |
| 349 | 310 | |
| 350 | 311 | if ( count ( $commits ) ) { |
| 351 | 312 | |
| 352 | 313 | $cnt = 0; |
| @@ -361,9 +322,9 @@ | ||
| 361 | 322 | $response->html .= '<li class="github_commit">'; |
| 362 | 323 | $response->html .= '<a href="https://github.com/'.$owner.'/'.$repository.'/commit/'.esc_attr($commit->sha).'" target="_blank">'.esc_html($commit->commit->message)."</a>, "; |
| 363 | 324 | $response->html .= esc_html($commit->commit->committer->name); |
| 364 | 325 | $response->html .= '</li>'; |
| 365 | - | |
| 326 | + | |
| 366 | 327 | $cnt++; |
| 367 | 328 | |
| 368 | 329 | } |
| 369 | 330 | |
| @@ -398,10 +359,10 @@ | ||
| 398 | 359 | |
| 399 | 360 | // @TODO This should all be templated |
| 400 | 361 | $response->html = '<div class="github-embed github-embed-user">'; |
| 401 | 362 | $response->html .= '<p><a href="https://github.com/'.esc_attr($owner).'" target="_blank"><strong>'.esc_html($owner)."</strong></a><br/>"; |
| 402 | - $response->html .= esc_html ( number_format_i18n ( $owner_info->public_repos ) ).' repositories, '; | |
| 403 | - $response->html .= esc_html ( number_format_i18n ( $owner_info->followers ) ).' followers.</p>'; | |
| 363 | + $response->html .= esc_html($owner_info->public_repos).' repositories, '; | |
| 364 | + $response->html .= esc_html($owner_info->followers).' followers.</p>'; | |
| 404 | 365 | $response->html .= '</div>'; |
| 405 | 366 | |
| 406 | 367 | header ( 'Content-Type: application/json' ); |
| 407 | 368 | echo json_encode ( $response ); |
| @@ -413,5 +374,5 @@ | ||
| 413 | 374 | |
| 414 | 375 | require_once ( 'github-api.php' ); |
| 415 | 376 | |
| 416 | 377 | $github_api = new github_api(); |
| 417 | -$github_embed = new github_embed ( $github_api ); | |
| 378 | +$github_embed = new github_embed ( $github_api ); | |