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