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