PluginProbe
Github Embed / 1.5
Github Embed v1.5
trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.0.1 2.0.2 2.1.0 2.2.0 2.2.1
← All changes | github-embed.php +74 -35 1.21.5 View file →
@@ -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.5
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,12 +52,14 @@
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 -
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 +
60 62 // @TODO i18n
61 63
62 64 }
63 65
@@ -63,8 +65,42 @@
63 65
64 66
65 67
66 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 +
102 + /**
67 103 * Enqueue the frontend CSS
68 104 * @return void
69 105 */
70 106 function enqueue_styles() {
@@ -69,10 +105,10 @@
69 105 */
70 106 function enqueue_styles() {
71 107
72 108 wp_register_style ( 'github-embed', plugins_url(basename(dirname(__FILE__)).'/css/github-embed.css' ) );
73 - wp_enqueue_style ( 'github-embed' );
74 -
109 + wp_enqueue_style ( 'github-embed' );
110 +
75 111 }
76 112
77 113
78 114
@@ -84,12 +120,12 @@
84 120 * @return array The list, with our new provider added
85 121 */
86 122 public function register_oembed_handler() {
87 123
88 - $oembed_url = home_url ();
124 + $oembed_url = home_url();
89 125 $key = $this->get_key();
90 - $oembed_url = add_query_arg ( array ( 'github_oembed' => $key ), $oembed_url);
91 - wp_oembed_add_provider ( '#https?://github.com/.*#i', $oembed_url, true );
126 + $oembed_url = add_query_arg( array( 'github_oembed' => $key ), $oembed_url );
127 + wp_oembed_add_provider( '#https?://github.com/.*#i', $oembed_url, true );
92 128
93 129 }
94 130
95 131
@@ -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 }
@@ -135,9 +171,9 @@
135 171 public function handle_oembed() {
136 172
137 173 // Check this request is valid
138 174 if ( $_GET['github_oembed'] != $this->get_key() ) {
139 - header ( 'HTTP/1.0 403 Forbidden' );
175 + header ( 'HTTP/1.0 403 Forbidden' );
140 176 die ( 'Sad Octocat is sad.' );
141 177 }
142 178
143 179 // Check we have the required information
@@ -170,8 +206,13 @@
170 206 if ( $milestone ) {
171 207 $this->oembed_github_repo_milestone_summary ( $matches[1], $matches[2], $milestone );
172 208 }
173 209
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 +
174 215 // Repository
175 216 } elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) {
176 217
177 218 $this->oembed_github_repo ( $matches[1], $matches[2] );
@@ -208,25 +249,21 @@
208 249
209 250 // @TODO This should all be templated
210 251 $response->html = '<div class="github-embed github-embed-repo-contributors">';
211 252 $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>";
212 -
253 +
213 254 $response->html .= '<span class="github-heading">Contributors: </span>';
214 255
215 256 $response->html .= '<ul class="github-repo-contributors">';
216 -
257 +
217 258 foreach ( $contributors as $contributor ) {
218 259
219 - $details = $this->api->get_user ($contributor->login);
220 260 $response->html .= '<li class="github-repo-contributor">';
221 261 $response->html .= '<img class="github-repo-contributor-avatar" src="';
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 - }
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).'">';
227 264 $response->html .= '<span class="github-repo-contributor-login">';
228 - $response->html .= '<a href="https://github.com/'.esc_attr($contributor->login).'">'.esc_attr($contributor->login).'</a></span>';
265 + $response->html .= '<a href="https://github.com/'.esc_attr($contributor->author->login).'">'.esc_attr($contributor->author->login).'</a></span>';
229 266 }
230 267
231 268 $response->html .= '</ul>';
232 269 $response->html .= '<div style="clear: both;"></div>';
@@ -258,31 +295,32 @@
258 295
259 296 // @TODO This should all be templated
260 297 $response->html = '<div class="github-embed github-embed-milestone-summary">';
261 298 $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>";
262 -
299 +
263 300 $response->html .= '<span class="github-heading">Milestone: </span>';
264 301 $response->html .= '<span class="github-milestone-title">'.esc_html($summary->title)."</span><br>";
265 -
302 +
266 303 $response->html .= '<span class="github-heading">Issues: </span>';
267 304 $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 -
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 +
271 308 if ( ! empty ( $summary->due_on ) ) {
272 309 $response->html .= '<span class="github-heading">Due: </span>';
273 310 $due_date = date_format ( date_create ( $summary->due_on ), 'jS F Y' );
274 311 $response->html .= '<span class="github-milestone-due-date">'.esc_html($due_date).'</span><br>';
275 312 }
276 -
313 +
277 314 $response->html .= '<p class="github-milestone-description">'.nl2br(esc_html($summary->description))."</p><br>";
278 315 $response->html .= '</div>';
279 316
317 + error_log( basename( __FILE__ ) . ': (' . __LINE__ . ') : response is ' . print_r( $response, 1 ) );
280 318 header ( 'Content-Type: application/json' );
281 319 echo json_encode ( $response );
282 320 die();
283 321
284 - }
322 + }
285 323
286 324
287 325
288 326 /**
@@ -304,10 +342,11 @@
304 342 // @TODO This should all be templated
305 343 $response->html = '<div class="github-embed github-embed-repository">';
306 344 $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>";
307 345 $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/>";
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/>";
310 349
311 350 if ( count ( $commits ) ) {
312 351
313 352 $cnt = 0;
@@ -322,9 +361,9 @@
322 361 $response->html .= '<li class="github_commit">';
323 362 $response->html .= '<a href="https://github.com/'.$owner.'/'.$repository.'/commit/'.esc_attr($commit->sha).'" target="_blank">'.esc_html($commit->commit->message)."</a>, ";
324 363 $response->html .= esc_html($commit->commit->committer->name);
325 364 $response->html .= '</li>';
326 -
365 +
327 366 $cnt++;
328 367
329 368 }
330 369
@@ -359,10 +398,10 @@
359 398
360 399 // @TODO This should all be templated
361 400 $response->html = '<div class="github-embed github-embed-user">';
362 401 $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>';
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>';
365 404 $response->html .= '</div>';
366 405
367 406 header ( 'Content-Type: application/json' );
368 407 echo json_encode ( $response );
@@ -374,5 +413,5 @@
374 413
375 414 require_once ( 'github-api.php' );
376 415
377 416 $github_api = new github_api();
378 -$github_embed = new github_embed ( $github_api );
417 +$github_embed = new github_embed ( $github_api );