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