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