| @@ -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.2 | |
| 7 | +Version: 1.3 | |
| 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 { |
| @@ -55,9 +55,11 @@ | ||
| 55 | 55 | |
| 56 | 56 | add_action ( 'init', array ( $this, 'register_oembed_handler' ) ); |
| 57 | 57 | add_action ( 'init', array ( $this, 'maybe_handle_oembed' ) ); |
| 58 | 58 | add_action ( 'wp_enqueue_scripts', array ( $this, 'enqueue_styles' ) ); |
| 59 | - | |
| 59 | + add_action ( 'admin_init', array ( $this, 'schedule_expiry' ) ); | |
| 60 | + add_action ( 'github_embed_cron', array ( $this, 'cron' ) ); | |
| 61 | + | |
| 60 | 62 | // @TODO i18n |
| 61 | 63 | |
| 62 | 64 | } |
| 63 | 65 | |
| @@ -62,8 +64,42 @@ | ||
| 62 | 64 | } |
| 63 | 65 | |
| 64 | 66 | |
| 65 | 67 | |
| 68 | + /** | |
| 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 | + | |
| 66 | 102 | /** |
| 67 | 103 | * Enqueue the frontend CSS |
| 68 | 104 | * @return void |
| 69 | 105 | */ |
| @@ -70,9 +106,9 @@ | ||
| 70 | 106 | function enqueue_styles() { |
| 71 | 107 | |
| 72 | 108 | wp_register_style ( 'github-embed', plugins_url(basename(dirname(__FILE__)).'/css/github-embed.css' ) ); |
| 73 | 109 | wp_enqueue_style ( 'github-embed' ); |
| 74 | - | |
| 110 | + | |
| 75 | 111 | } |
| 76 | 112 | |
| 77 | 113 | |
| 78 | 114 | |
| @@ -101,9 +137,9 @@ | ||
| 101 | 137 | */ |
| 102 | 138 | private function get_key() { |
| 103 | 139 | |
| 104 | 140 | $key = get_option ( 'github_oembed_key' ); |
| 105 | - | |
| 141 | + | |
| 106 | 142 | if ( ! $key ) { |
| 107 | 143 | $key = md5 ( time() . rand ( 0,65535 ) ); |
| 108 | 144 | add_option ( 'github_oembed_key', $key, '', 'yes' ); |
| 109 | 145 | } |
| @@ -208,13 +244,13 @@ | ||
| 208 | 244 | |
| 209 | 245 | // @TODO This should all be templated |
| 210 | 246 | $response->html = '<div class="github-embed github-embed-repo-contributors">'; |
| 211 | 247 | $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; |
| 212 | - | |
| 248 | + | |
| 213 | 249 | $response->html .= '<span class="github-heading">Contributors: </span>'; |
| 214 | 250 | |
| 215 | 251 | $response->html .= '<ul class="github-repo-contributors">'; |
| 216 | - | |
| 252 | + | |
| 217 | 253 | foreach ( $contributors as $contributor ) { |
| 218 | 254 | |
| 219 | 255 | $details = $this->api->get_user ($contributor->login); |
| 220 | 256 | $response->html .= '<li class="github-repo-contributor">'; |
| @@ -258,23 +294,23 @@ | ||
| 258 | 294 | |
| 259 | 295 | // @TODO This should all be templated |
| 260 | 296 | $response->html = '<div class="github-embed github-embed-milestone-summary">'; |
| 261 | 297 | $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; |
| 262 | - | |
| 298 | + | |
| 263 | 299 | $response->html .= '<span class="github-heading">Milestone: </span>'; |
| 264 | 300 | $response->html .= '<span class="github-milestone-title">'.esc_html($summary->title)."</span><br>"; |
| 265 | - | |
| 301 | + | |
| 266 | 302 | $response->html .= '<span class="github-heading">Issues: </span>'; |
| 267 | 303 | $response->html .= '<span class="github-milestone-issues">'; |
| 268 | - $response->html .= esc_html($summary->open_issues)." open, "; | |
| 269 | - $response->html .= esc_html($summary->closed_issues)." closed.</span><br>"; | |
| 270 | - | |
| 304 | + $response->html .= esc_html ( number_format_i18n ( $summary->open_issues ) )." open, "; | |
| 305 | + $response->html .= esc_html ( number_format_i18n ( $summary->closed_issues ) )." closed.</span><br>"; | |
| 306 | + | |
| 271 | 307 | if ( ! empty ( $summary->due_on ) ) { |
| 272 | 308 | $response->html .= '<span class="github-heading">Due: </span>'; |
| 273 | 309 | $due_date = date_format ( date_create ( $summary->due_on ), 'jS F Y' ); |
| 274 | 310 | $response->html .= '<span class="github-milestone-due-date">'.esc_html($due_date).'</span><br>'; |
| 275 | 311 | } |
| 276 | - | |
| 312 | + | |
| 277 | 313 | $response->html .= '<p class="github-milestone-description">'.nl2br(esc_html($summary->description))."</p><br>"; |
| 278 | 314 | $response->html .= '</div>'; |
| 279 | 315 | |
| 280 | 316 | header ( 'Content-Type: application/json' ); |
| @@ -280,9 +316,9 @@ | ||
| 280 | 316 | header ( 'Content-Type: application/json' ); |
| 281 | 317 | echo json_encode ( $response ); |
| 282 | 318 | die(); |
| 283 | 319 | |
| 284 | - } | |
| 320 | + } | |
| 285 | 321 | |
| 286 | 322 | |
| 287 | 323 | |
| 288 | 324 | /** |
| @@ -304,10 +340,10 @@ | ||
| 304 | 340 | // @TODO This should all be templated |
| 305 | 341 | $response->html = '<div class="github-embed github-embed-repository">'; |
| 306 | 342 | $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; |
| 307 | 343 | $response->html .= '<a href="'.esc_attr($repo->html_url).'" target="_blank">'.esc_html($repo->html_url)."</a><br/>"; |
| 308 | - $response->html .= esc_html($repo->forks_count)." forks.<br/>"; | |
| 309 | - $response->html .= esc_html($repo->open_issues_count)." open issues.<br/>"; | |
| 344 | + $response->html .= esc_html ( number_format_i18n ( $repo->forks_count ) )." forks.<br/>"; | |
| 345 | + $response->html .= esc_html ( number_format_i18n ( $repo->open_issues_count ) )." open issues.<br/>"; | |
| 310 | 346 | |
| 311 | 347 | if ( count ( $commits ) ) { |
| 312 | 348 | |
| 313 | 349 | $cnt = 0; |
| @@ -322,9 +358,9 @@ | ||
| 322 | 358 | $response->html .= '<li class="github_commit">'; |
| 323 | 359 | $response->html .= '<a href="https://github.com/'.$owner.'/'.$repository.'/commit/'.esc_attr($commit->sha).'" target="_blank">'.esc_html($commit->commit->message)."</a>, "; |
| 324 | 360 | $response->html .= esc_html($commit->commit->committer->name); |
| 325 | 361 | $response->html .= '</li>'; |
| 326 | - | |
| 362 | + | |
| 327 | 363 | $cnt++; |
| 328 | 364 | |
| 329 | 365 | } |
| 330 | 366 | |
| @@ -359,10 +395,10 @@ | ||
| 359 | 395 | |
| 360 | 396 | // @TODO This should all be templated |
| 361 | 397 | $response->html = '<div class="github-embed github-embed-user">'; |
| 362 | 398 | $response->html .= '<p><a href="https://github.com/'.esc_attr($owner).'" target="_blank"><strong>'.esc_html($owner)."</strong></a><br/>"; |
| 363 | - $response->html .= esc_html($owner_info->public_repos).' repositories, '; | |
| 364 | - $response->html .= esc_html($owner_info->followers).' followers.</p>'; | |
| 399 | + $response->html .= esc_html ( number_format_i18n ( $owner_info->public_repos ) ).' repositories, '; | |
| 400 | + $response->html .= esc_html ( number_format_i18n ( $owner_info->followers ) ).' followers.</p>'; | |
| 365 | 401 | $response->html .= '</div>'; |
| 366 | 402 | |
| 367 | 403 | header ( 'Content-Type: application/json' ); |
| 368 | 404 | echo json_encode ( $response ); |