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