PluginProbe
Github Embed / 1.6
Github Embed v1.6
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 +83 -163 1.51.6 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.5
7 +Version: 1.6
8 8 Author: Lee Willis
9 9 Author URI: http://www.leewillis.co.uk/
10 10 */
11 11
@@ -36,51 +36,38 @@
36 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 41 class github_embed {
41 42
42 -
43 -
44 43 private $api;
45 44
46 -
47 -
48 45 /**
49 46 * Constructor. Registers hooks and filters
50 47 * @param class $api An instance of the github_api classs
51 48 */
52 - public function __construct ( $api ) {
53 -
49 + public function __construct( $api ) {
54 50 $this->api = $api;
55 -
56 - add_action( 'init', array ( $this, 'register_oembed_handler' ) );
51 + add_action( 'init', array( $this, 'register_oembed_handler' ) );
57 52 add_action( 'init', array( $this, 'maybe_handle_oembed' ) );
58 53 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
59 54 add_action( 'admin_init', array( $this, 'schedule_expiry' ) );
60 55 add_action( 'github_embed_cron', array( $this, 'cron' ) );
61 -
62 56 // @TODO i18n
63 -
64 57 }
65 58
66 -
67 -
68 59 /**
69 60 * Make sure we have a scheduled event set to clear down the oEmbed cache until
70 61 * WordPress supports cache_age in oEmbed responses.
71 62 */
72 63 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' );
64 + if ( ! wp_next_scheduled( 'github_embed_cron' ) ) {
65 + $frequency = apply_filters( 'github_embed_cache_frequency', 'daily' );
66 + wp_schedule_event( time(), $frequency, 'github_embed_cron' );
77 67 }
78 -
79 68 }
80 69
81 -
82 -
83 70 /**
84 71 * Expire old oEmbeds.
85 72 * Note: This is a bit sledgehammer-to-crack-a-nut hence why I'm only running it
86 73 * daily. Ideally WP should honour cache_age in oEmbed responses properly
@@ -85,34 +72,24 @@
85 72 * Note: This is a bit sledgehammer-to-crack-a-nut hence why I'm only running it
86 73 * daily. Ideally WP should honour cache_age in oEmbed responses properly
87 74 */
88 75 function cron() {
89 -
90 76 global $wpdb, $table_prefix;
91 -
92 77 $sql = "DELETE
93 78 FROM {$table_prefix}postmeta
94 79 WHERE meta_key LIKE '_oembed_%'";
95 -
96 - $results = $wpdb->get_results ( $sql );
97 -
80 + $results = $wpdb->get_results( $sql );
98 81 }
99 82
100 -
101 -
102 83 /**
103 84 * Enqueue the frontend CSS
104 85 * @return void
105 86 */
106 87 function enqueue_styles() {
107 -
108 - wp_register_style ( 'github-embed', plugins_url(basename(dirname(__FILE__)).'/css/github-embed.css' ) );
109 - wp_enqueue_style ( 'github-embed' );
110 -
88 + wp_register_style( 'github-embed', plugins_url( basename( dirname( __FILE__ ) ) . '/css/github-embed.css' ) );
89 + wp_enqueue_style( 'github-embed' );
111 90 }
112 91
113 -
114 -
115 92 /**
116 93 * Register the oEmbed provider, and point it at a local endpoint since github
117 94 * doesn't directly support oEmbed yet. Our local endpoint will use the github
118 95 * API to fulfil the request.
@@ -119,18 +96,14 @@
119 96 * @param array $providers The current list of providers
120 97 * @return array The list, with our new provider added
121 98 */
122 99 public function register_oembed_handler() {
123 -
124 100 $oembed_url = home_url();
125 101 $key = $this->get_key();
126 102 $oembed_url = add_query_arg( array( 'github_oembed' => $key ), $oembed_url );
127 103 wp_oembed_add_provider( '#https?://github.com/.*#i', $oembed_url, true );
128 -
129 104 }
130 105
131 -
132 -
133 106 /**
134 107 * Generate a unique key that can be used on our requests to stop others
135 108 * hijacking our internal oEmbed API
136 109 * @return string The site key
@@ -135,22 +108,16 @@
135 108 * hijacking our internal oEmbed API
136 109 * @return string The site key
137 110 */
138 111 private function get_key() {
139 -
140 - $key = get_option ( 'github_oembed_key' );
141 -
112 + $key = get_option( 'github_oembed_key' );
142 113 if ( ! $key ) {
143 - $key = md5 ( time() . rand ( 0,65535 ) );
144 - add_option ( 'github_oembed_key', $key, '', 'yes' );
114 + $key = md5( time() . rand( 0, 65535 ) );
115 + add_option( 'github_oembed_key', $key, '', 'yes' );
145 116 }
146 -
147 117 return $key;
148 -
149 118 }
150 119
151 -
152 -
153 120 /**
154 121 * Check whether this is an oembed request, handle if it is
155 122 * Ignore it if not.
156 123 * Insert rant here about WP's lack of a front-end AJAX handler.
@@ -155,90 +122,70 @@
155 122 * Ignore it if not.
156 123 * Insert rant here about WP's lack of a front-end AJAX handler.
157 124 */
158 125 public function maybe_handle_oembed() {
159 -
160 - if ( isset ( $_GET['github_oembed'] ) ) {
126 + if ( isset( $_GET['github_oembed'] ) ) {
161 127 return $this->handle_oembed();
162 128 }
163 -
164 129 }
165 130
166 -
167 -
168 131 /**
169 132 * Handle an oembed request
170 133 */
171 134 public function handle_oembed() {
172 -
173 135 // Check this request is valid
174 - if ( $_GET['github_oembed'] != $this->get_key() ) {
175 - header ( 'HTTP/1.0 403 Forbidden' );
176 - die ( 'Sad Octocat is sad.' );
136 + if ( $_GET['github_oembed'] !== $this->get_key() ) {
137 + header( 'HTTP/1.0 403 Forbidden' );
138 + die( 'Sad Octocat is sad.' );
177 139 }
178 140
179 141 // Check we have the required information
180 - $url = isset ( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
181 - $format = isset ( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
142 + $url = isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
143 + $format = isset( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
182 144
183 - if ( ! empty ( $format ) && $format != 'json' ) {
184 - header ( 'HTTP/1.0 501 Not implemented' );
185 - die ( 'This octocat only does json' );
145 + if ( ! empty( $format ) && 'json' !== $format ) {
146 + header( 'HTTP/1.0 501 Not implemented' );
147 + die( 'This octocat only does json' );
186 148 }
187 149
188 150 if ( ! $url ) {
189 - header ( 'HTTP/1.0 404 Not Found' );
190 - die ( 'Octocat is lost, and afraid' );
151 + header( 'HTTP/1.0 404 Not Found' );
152 + die( 'Octocat is lost, and afraid' );
191 153 }
192 154
193 155 // Issues / Milestones
194 - if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/graphs/contributors/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) {
195 -
196 - $this->oembed_github_repo_contributors ( $matches[1], $matches[2] );
197 -
198 - } elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/issues.*$#i', $url, $matches ) && ! empty ( $matches[2] ) ) {
199 -
200 - if ( preg_match ( '#issues.?milestone=([0-9]*)#i', $url, $milestones ) ) {
156 + if ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/graphs/contributors/?$#i', $url, $matches ) && ! empty( $matches[2] ) ) {
157 + $this->oembed_github_repo_contributors( $matches[1], $matches[2] );
158 + } elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/issues.*$#i', $url, $matches ) && ! empty( $matches[2] ) ) {
159 + if ( preg_match( '#issues.?milestone=([0-9]*)#i', $url, $milestones ) ) {
201 160 $milestone = $milestones[1];
202 161 } else {
203 162 $milestone = null;
204 163 }
205 -
206 164 if ( $milestone ) {
207 - $this->oembed_github_repo_milestone_summary ( $matches[1], $matches[2], $milestone );
165 + $this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $milestone );
208 166 }
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 -
167 + } elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/milestone/([0-9]*)$#i', $url, $matches ) ) {
168 + // New style milestone URL, e.g. https://github.com/example/example/milestone/1.
213 169 $this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $matches[3] );
214 -
215 - // Repository
216 - } elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) {
217 -
218 - $this->oembed_github_repo ( $matches[1], $matches[2] );
219 -
220 - // User
221 - } elseif ( preg_match ( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) {
222 -
223 - $this->oembed_github_author ( $matches[1] );
224 -
170 + } elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty( $matches[2] ) ) {
171 + // Repository.
172 + $this->oembed_github_repo( $matches[1], $matches[2] );
173 + } elseif ( preg_match( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) {
174 + // User.
175 + $this->oembed_github_author( $matches[1] );
225 176 }
226 -
227 177 }
228 178
229 -
230 -
231 179 /**
232 180 * Retrieve a list of contributors for a project
233 181 * @param string $owner The owner of the repository
234 182 * @param string $repository The repository name
235 183 */
236 - private function oembed_github_repo_contributors ( $owner, $repository ) {
184 + private function oembed_github_repo_contributors( $owner, $repository ) {
185 + $repo = $this->api->get_repo( $owner, $repository );
186 + $contributors = $this->api->get_repo_contributors( $owner, $repository );
237 187
238 - $repo = $this->api->get_repo ( $owner, $repository );
239 - $contributors = $this->api->get_repo_contributors ( $owner, $repository );
240 -
241 188 $response = new stdClass();
242 189 $response->type = 'rich';
243 190 $response->width = '10';
244 191 $response->height = '10';
@@ -244,49 +191,40 @@
244 191 $response->height = '10';
245 192 $response->version = '1.0';
246 193 $response->title = $repo->description;
247 194
248 - $gravatar_size = apply_filters ( 'github_oembed_gravatar_size', 64 );
195 + $gravatar_size = apply_filters( 'github_oembed_gravatar_size', 64 );
249 196
250 197 // @TODO This should all be templated
251 198 $response->html = '<div class="github-embed github-embed-repo-contributors">';
252 - $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>";
253 -
199 + $response->html .= '<p><a href="' . esc_attr( $repo->html_url ) . '" target="_blank">';
200 + $response->html .= '<strong>' . esc_html( $repo->description ) . '</strong></a><br/>';
254 201 $response->html .= '<span class="github-heading">Contributors: </span>';
255 -
256 202 $response->html .= '<ul class="github-repo-contributors">';
257 -
258 203 foreach ( $contributors as $contributor ) {
259 -
260 204 $response->html .= '<li class="github-repo-contributor">';
261 205 $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).'">';
206 + $response->html .= esc_url( add_query_arg( array( 's' => $gravatar_size ), $contributor->author->avatar_url ) );
207 + $response->html .= '" alt="Picture of ' . esc_attr( $contributor->author->login ) . '">';
264 208 $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>';
209 + $response->html .= '<a href="https://github.com/' . esc_attr( $contributor->author->login ) . '">' . esc_attr( $contributor->author->login ) . '</a></span>';
266 210 }
267 -
268 211 $response->html .= '</ul>';
269 212 $response->html .= '<div style="clear: both;"></div>';
270 213 $response->html .= '</div>';
271 -
272 - header ( 'Content-Type: application/json' );
273 - echo json_encode ( $response );
214 + header( 'Content-Type: application/json' );
215 + echo json_encode( $response );
274 216 die();
275 -
276 217 }
277 218
278 -
279 -
280 219 /**
281 220 * Retrieve the summary information for a repo's milestone, and
282 221 * output it as an oembed response
283 222 */
284 - private function oembed_github_repo_milestone_summary ( $owner, $repository, $milestone ) {
223 + private function oembed_github_repo_milestone_summary( $owner, $repository, $milestone ) {
224 + $repo = $this->api->get_repo( $owner, $repository );
225 + $summary = $this->api->get_repo_milestone_summary( $owner, $repository, $milestone );
285 226
286 - $repo = $this->api->get_repo ( $owner, $repository );
287 - $summary = $this->api->get_repo_milestone_summary ( $owner, $repository, $milestone );
288 -
289 227 $response = new stdClass();
290 228 $response->type = 'rich';
291 229 $response->width = '10';
292 230 $response->height = '10';
@@ -294,45 +232,41 @@
294 232 $response->title = $repo->description;
295 233
296 234 // @TODO This should all be templated
297 235 $response->html = '<div class="github-embed github-embed-milestone-summary">';
298 - $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>";
236 + $response->html .= '<p><a href="' . esc_attr( $repo->html_url ) . '" target="_blank"><strong>' . esc_html( $repo->description ) . '</strong></a><br/>';
299 237
300 238 $response->html .= '<span class="github-heading">Milestone: </span>';
301 - $response->html .= '<span class="github-milestone-title">'.esc_html($summary->title)."</span><br>";
239 + $response->html .= '<span class="github-milestone-title">' . esc_html( $summary->title ) . '</span><br>';
302 240
303 241 $response->html .= '<span class="github-heading">Issues: </span>';
304 242 $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>";
243 + $response->html .= esc_html( number_format_i18n( $summary->open_issues ) ) . ' open, ';
244 + $response->html .= esc_html( number_format_i18n( $summary->closed_issues ) ) . ' closed.</span><br>';
307 245
308 - if ( ! empty ( $summary->due_on ) ) {
246 + if ( ! empty( $summary->due_on ) ) {
309 247 $response->html .= '<span class="github-heading">Due: </span>';
310 - $due_date = date_format ( date_create ( $summary->due_on ), 'jS F Y' );
311 - $response->html .= '<span class="github-milestone-due-date">'.esc_html($due_date).'</span><br>';
248 + $due_date = date_format( date_create( $summary->due_on ), 'jS F Y' );
249 + $response->html .= '<span class="github-milestone-due-date">' . esc_html( $due_date ) . '</span><br>';
312 250 }
313 251
314 - $response->html .= '<p class="github-milestone-description">'.nl2br(esc_html($summary->description))."</p><br>";
252 + $response->html .= '<p class="github-milestone-description">' . nl2br( esc_html( $summary->description ) ) . '</p><br>';
315 253 $response->html .= '</div>';
316 254
317 - error_log( basename( __FILE__ ) . ': (' . __LINE__ . ') : response is ' . print_r( $response, 1 ) );
318 - header ( 'Content-Type: application/json' );
319 - echo json_encode ( $response );
255 + header( 'Content-Type: application/json' );
256 + echo json_encode( $response );
320 257 die();
321 258
322 259 }
323 260
324 -
325 -
326 261 /**
327 262 * Retrieve the information from github for a repo, and
328 263 * output it as an oembed response
329 264 */
330 265 private function oembed_github_repo ( $owner, $repository ) {
266 + $repo = $this->api->get_repo( $owner, $repository );
267 + $commits =$this->api->get_repo_commits( $owner, $repository );
331 268
332 - $repo = $this->api->get_repo ( $owner, $repository );
333 - $commits =$this->api->get_repo_commits ( $owner, $repository );
334 -
335 269 $response = new stdClass();
336 270 $response->type = 'rich';
337 271 $response->width = '10';
338 272 $response->height = '10';
@@ -340,48 +274,37 @@
340 274 $response->title = $repo->description;
341 275
342 276 // @TODO This should all be templated
343 277 $response->html = '<div class="github-embed github-embed-repository">';
344 - $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>";
345 - $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/>";
278 + $response->html .= '<p><a href="' . esc_attr( $repo->html_url ) . '" target="_blank"><strong>' . esc_html( $repo->description ) . '</strong></a><br/>';
279 + $response->html .= '<a href="' . esc_attr( $repo->html_url ) . '" target="_blank">' . esc_html( $repo->html_url ) . '</a><br/>';
280 + $response->html .= '<a href="' . esc_attr( $repo->html_url . '/network' ) . '" target="_blank">' . esc_html( number_format_i18n( $repo->forks_count ) ) . '</a> forks.<br/>';
281 + $response->html .= '<a href="' . esc_attr( $repo->html_url . '/stargazers' ) . '" target="_blank">' . esc_html( number_format_i18n( $repo->stargazers_count ) ) . '</a> stars.<br/>';
282 + $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/>';
349 283
350 - if ( count ( $commits ) ) {
351 -
284 + if ( count( $commits ) ) {
352 285 $cnt = 0;
353 286 $response->html .= 'Recent commits:';
354 287 $response->html .= '<ul class="github_commits">';
355 -
356 288 foreach ( $commits as $commit ) {
357 -
358 - if ($cnt > 4)
289 + if ( $cnt > 4 ) {
359 290 break;
360 -
291 + }
361 292 $response->html .= '<li class="github_commit">';
362 - $response->html .= '<a href="https://github.com/'.$owner.'/'.$repository.'/commit/'.esc_attr($commit->sha).'" target="_blank">'.esc_html($commit->commit->message)."</a>, ";
363 - $response->html .= esc_html($commit->commit->committer->name);
293 + $response->html .= '<a href="https://github.com/' . $owner . '/' . $repository . '/commit/' . esc_attr( $commit->sha ) . '" target="_blank">' . esc_html( $commit->commit->message ) . '</a>, ';
294 + $response->html .= esc_html( $commit->commit->committer->name );
364 295 $response->html .= '</li>';
365 -
366 296 $cnt++;
367 -
368 297 }
369 -
370 298 $response->html .= '</ul>';
371 -
372 299 }
373 300 $response->html .= '</p>';
374 301 $response->html .= '</div>';
375 -
376 - header ( 'Content-Type: application/json' );
377 - echo json_encode ( $response );
302 + header( 'Content-Type: application/json' );
303 + echo json_encode( $response );
378 304 die();
379 -
380 305 }
381 306
382 -
383 -
384 307 /**
385 308 * Retrieve the information from github for an author, and output
386 309 * it as an oembed response
387 310 */
@@ -386,9 +309,9 @@
386 309 * it as an oembed response
387 310 */
388 311 private function oembed_github_author ( $owner ) {
389 312
390 - $owner_info = $this->api->get_user ( $owner );
313 + $owner_info = $this->api->get_user( $owner );
391 314
392 315 $response = new stdClass();
393 316 $response->type = 'rich';
394 317 $response->width = '10';
@@ -397,21 +320,18 @@
397 320 $response->title = $owner_info->name;
398 321
399 322 // @TODO This should all be templated
400 323 $response->html = '<div class="github-embed github-embed-user">';
401 - $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>';
324 + $response->html .= '<p><a href="https://github.com/' . esc_attr( $owner ) . '" target="_blank"><strong>' . esc_html( $owner ) . '</strong></a><br/>';
325 + $response->html .= esc_html( number_format_i18n( $owner_info->public_repos ) ) . ' repositories, ';
326 + $response->html .= esc_html( number_format_i18n( $owner_info->followers ) ) . ' followers.</p>';
404 327 $response->html .= '</div>';
405 -
406 - header ( 'Content-Type: application/json' );
407 - echo json_encode ( $response );
328 + header( 'Content-Type: application/json' );
329 + echo json_encode( $response );
408 330 die();
409 -
410 331 }
411 -
412 332 }
413 333
414 -require_once ( 'github-api.php' );
334 +require_once( 'github-api.php' );
415 335
416 336 $github_api = new github_api();
417 -$github_embed = new github_embed ( $github_api );
337 +$github_embed = new github_embed( $github_api );