PluginProbe
Github Embed / 1.0
Github Embed v1.0
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 +46 -198 1.31.0 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.3
7 +Version: 1.0
8 8 Author: Lee Willis
9 9 Author URI: http://www.leewillis.co.uk/
10 10 */
11 11
@@ -30,35 +30,19 @@
30 30 * GNU General Public License for more details.
31 31 * **********************************************************************
32 32 */
33 33
34 -/**
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
37 - * class to retrieve the information from the GitHub API.
38 - * @uses class github_api
39 - */
40 34 class github_embed {
41 35
42 36
43 37
44 - private $api;
45 -
46 -
47 -
48 38 /**
49 39 * Constructor. Registers hooks and filters
50 - * @param class $api An instance of the github_api classs
51 40 */
52 - public function __construct ( $api ) {
41 + public function __construct() {
53 42
54 - $this->api = $api;
55 -
56 43 add_action ( 'init', array ( $this, 'register_oembed_handler' ) );
57 44 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 45
62 46 // @TODO i18n
63 47
64 48 }
@@ -64,56 +48,9 @@
64 48 }
65 49
66 50
67 51
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 -
102 52 /**
103 - * Enqueue the frontend CSS
104 - * @return void
105 - */
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 -
111 - }
112 -
113 -
114 -
115 - /**
116 53 * Register the oEmbed provider, and point it at a local endpoint since github
117 54 * doesn't directly support oEmbed yet. Our local endpoint will use the github
118 55 * API to fulfil the request.
119 56 * @param array $providers The current list of providers
@@ -137,9 +74,9 @@
137 74 */
138 75 private function get_key() {
139 76
140 77 $key = get_option ( 'github_oembed_key' );
141 -
78 +
142 79 if ( ! $key ) {
143 80 $key = md5 ( time() . rand ( 0,65535 ) );
144 81 add_option ( 'github_oembed_key', $key, '', 'yes' );
145 82 }
@@ -189,31 +126,12 @@
189 126 header ( 'HTTP/1.0 404 Not Found' );
190 127 die ( 'Octocat is lost, and afraid' );
191 128 }
192 129
193 - // Issues / Milestones
194 - if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/graphs/contributors/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) {
130 + if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) {
195 131
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 ) ) {
201 - $milestone = $milestones[1];
202 - } else {
203 - $milestone = null;
204 - }
205 -
206 - if ( $milestone ) {
207 - $this->oembed_github_repo_milestone_summary ( $matches[1], $matches[2], $milestone );
208 - }
209 -
210 - // Repository
211 - } elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) {
212 -
213 132 $this->oembed_github_repo ( $matches[1], $matches[2] );
214 133
215 - // User
216 134 } elseif ( preg_match ( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) {
217 135
218 136 $this->oembed_github_author ( $matches[1] );
219 137
@@ -223,114 +141,39 @@
223 141
224 142
225 143
226 144 /**
227 - * Retrieve a list of contributors for a project
228 - * @param string $owner The owner of the repository
229 - * @param string $repository The repository name
145 + * Retrieve the information from github for a repo, and
146 + * output it as an oembed response
230 147 */
231 - private function oembed_github_repo_contributors ( $owner, $repository ) {
148 + private function oembed_github_repo ( $owner, $repository ) {
232 149
233 - $repo = $this->api->get_repo ( $owner, $repository );
234 - $contributors = $this->api->get_repo_contributors ( $owner, $repository );
150 + $repository = trim ( $repository, '/' );
235 151
236 - $response = new stdClass();
237 - $response->type = 'rich';
238 - $response->width = '10';
239 - $response->height = '10';
240 - $response->version = '1.0';
241 - $response->title = $repo->description;
152 + $results = wp_remote_get( "https://api.github.com/repos/$owner/$repository", $args = array (
153 + 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) );
242 154
243 - $gravatar_size = apply_filters ( 'github_oembed_gravatar_size', 64 );
244 -
245 - // @TODO This should all be templated
246 - $response->html = '<div class="github-embed github-embed-repo-contributors">';
247 - $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>";
248 -
249 - $response->html .= '<span class="github-heading">Contributors: </span>';
250 -
251 - $response->html .= '<ul class="github-repo-contributors">';
252 -
253 - foreach ( $contributors as $contributor ) {
254 -
255 - $details = $this->api->get_user ($contributor->login);
256 - $response->html .= '<li class="github-repo-contributor">';
257 - $response->html .= '<img class="github-repo-contributor-avatar" src="';
258 - $response->html .= esc_url(add_query_arg(array('s'=>$gravatar_size), $contributor->avatar_url));
259 - $response->html .= '" alt="Picture of '.esc_attr($contributor->login).'">';
260 - if ( ! empty ( $details->name ) ) {
261 - $response->html .= '<span class="github-repo-contributor-name">'.esc_html($details->name)."</span><br>";
262 - }
263 - $response->html .= '<span class="github-repo-contributor-login">';
264 - $response->html .= '<a href="https://github.com/'.esc_attr($contributor->login).'">'.esc_attr($contributor->login).'</a></span>';
155 + if ( is_wp_error( $results ) ||
156 + ! isset ( $results['response']['code'] ) ||
157 + $results['response']['code'] != '200' ) {
158 + header ( 'HTTP/1.0 404 Not Found' );
159 + die ( 'Octocat is lost, and afraid' );
265 160 }
266 161
267 - $response->html .= '</ul>';
268 - $response->html .= '<div style="clear: both;"></div>';
269 - $response->html .= '</div>';
162 + $repo = json_decode ( $results['body'] );
270 163
271 - header ( 'Content-Type: application/json' );
272 - echo json_encode ( $response );
273 - die();
164 + $results = wp_remote_get( "https://api.github.com/repos/$owner/$repository/commits", $args = array (
165 + 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) );
274 166
275 - }
276 -
277 -
278 -
279 - /**
280 - * Retrieve the summary information for a repo's milestone, and
281 - * output it as an oembed response
282 - */
283 - private function oembed_github_repo_milestone_summary ( $owner, $repository, $milestone ) {
284 -
285 - $repo = $this->api->get_repo ( $owner, $repository );
286 - $summary = $this->api->get_repo_milestone_summary ( $owner, $repository, $milestone );
287 -
288 - $response = new stdClass();
289 - $response->type = 'rich';
290 - $response->width = '10';
291 - $response->height = '10';
292 - $response->version = '1.0';
293 - $response->title = $repo->description;
294 -
295 - // @TODO This should all be templated
296 - $response->html = '<div class="github-embed github-embed-milestone-summary">';
297 - $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>";
298 -
299 - $response->html .= '<span class="github-heading">Milestone: </span>';
300 - $response->html .= '<span class="github-milestone-title">'.esc_html($summary->title)."</span><br>";
301 -
302 - $response->html .= '<span class="github-heading">Issues: </span>';
303 - $response->html .= '<span class="github-milestone-issues">';
304 - $response->html .= esc_html ( number_format_i18n ( $summary->open_issues ) )." open, ";
305 - $response->html .= esc_html ( number_format_i18n ( $summary->closed_issues ) )." closed.</span><br>";
306 -
307 - if ( ! empty ( $summary->due_on ) ) {
308 - $response->html .= '<span class="github-heading">Due: </span>';
309 - $due_date = date_format ( date_create ( $summary->due_on ), 'jS F Y' );
310 - $response->html .= '<span class="github-milestone-due-date">'.esc_html($due_date).'</span><br>';
167 + if ( is_wp_error( $results ) ||
168 + ! isset ( $results['response']['code'] ) ||
169 + $results['response']['code'] != '200' ) {
170 + header ( 'HTTP/1.0 404 Not Found' );
171 + die ( 'Octocat is lost, and afraid' );
311 172 }
312 173
313 - $response->html .= '<p class="github-milestone-description">'.nl2br(esc_html($summary->description))."</p><br>";
314 - $response->html .= '</div>';
174 + $commits = json_decode ( $results['body'] );
315 175
316 - header ( 'Content-Type: application/json' );
317 - echo json_encode ( $response );
318 - die();
319 -
320 - }
321 -
322 -
323 -
324 - /**
325 - * Retrieve the information from github for a repo, and
326 - * output it as an oembed response
327 - */
328 - private function oembed_github_repo ( $owner, $repository ) {
329 -
330 - $repo = $this->api->get_repo ( $owner, $repository );
331 - $commits =$this->api->get_repo_commits ( $owner, $repository );
332 -
333 176 $response = new stdClass();
334 177 $response->type = 'rich';
335 178 $response->width = '10';
336 179 $response->height = '10';
@@ -337,13 +180,12 @@
337 180 $response->version = '1.0';
338 181 $response->title = $repo->description;
339 182
340 183 // @TODO This should all be templated
341 - $response->html = '<div class="github-embed github-embed-repository">';
342 - $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>";
343 - $response->html .= '<a href="'.esc_attr($repo->html_url).'" target="_blank">'.esc_html($repo->html_url)."</a><br/>";
344 - $response->html .= esc_html ( number_format_i18n ( $repo->forks_count ) )." forks.<br/>";
345 - $response->html .= esc_html ( number_format_i18n ( $repo->open_issues_count ) )." open issues.<br/>";
184 + $response->html = '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>";
185 + $response->html .= esc_html($repo->html_url)."<br/>";
186 + $response->html .= esc_html($repo->forks_count)." forks.<br/>";
187 + $response->html .= esc_html($repo->open_issues_count)." open issues.<br/>";
346 188
347 189 if ( count ( $commits ) ) {
348 190
349 191 $cnt = 0;
@@ -358,9 +200,9 @@
358 200 $response->html .= '<li class="github_commit">';
359 201 $response->html .= '<a href="https://github.com/'.$owner.'/'.$repository.'/commit/'.esc_attr($commit->sha).'" target="_blank">'.esc_html($commit->commit->message)."</a>, ";
360 202 $response->html .= esc_html($commit->commit->committer->name);
361 203 $response->html .= '</li>';
362 -
204 +
363 205 $cnt++;
364 206
365 207 }
366 208
@@ -367,9 +209,8 @@
367 209 $response->html .= '</ul>';
368 210
369 211 }
370 212 $response->html .= '</p>';
371 - $response->html .= '</div>';
372 213
373 214 header ( 'Content-Type: application/json' );
374 215 echo json_encode ( $response );
375 216 die();
@@ -383,10 +224,22 @@
383 224 * it as an oembed response
384 225 */
385 226 private function oembed_github_author ( $owner ) {
386 227
387 - $owner_info = $this->api->get_user ( $owner );
228 + $owner = trim ( $owner, '/' );
388 229
230 + $results = wp_remote_get( "https://api.github.com/users/$owner", $args = array (
231 + 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) );
232 +
233 + if ( is_wp_error( $results ) ||
234 + ! isset ( $results['response']['code'] ) ||
235 + $results['response']['code'] != '200' ) {
236 + header ( 'HTTP/1.0 404 Not Found' );
237 + die ( 'Octocat is lost, and afraid' );
238 + }
239 +
240 + $owner_info = json_decode ( $results['body'] );
241 +
389 242 $response = new stdClass();
390 243 $response->type = 'rich';
391 244 $response->width = '10';
392 245 $response->height = '10';
@@ -393,13 +246,11 @@
393 246 $response->version = '1.0';
394 247 $response->title = $owner_info->name;
395 248
396 249 // @TODO This should all be templated
397 - $response->html = '<div class="github-embed github-embed-user">';
398 - $response->html .= '<p><a href="https://github.com/'.esc_attr($owner).'" target="_blank"><strong>'.esc_html($owner)."</strong></a><br/>";
399 - $response->html .= esc_html ( number_format_i18n ( $owner_info->public_repos ) ).' repositories, ';
400 - $response->html .= esc_html ( number_format_i18n ( $owner_info->followers ) ).' followers.</p>';
401 - $response->html .= '</div>';
250 + $response->html = '<p><a href="https://github.com/'.esc_attr($owner).'" target="_blank"><strong>'.esc_html($owner)."</strong></a><br/>";
251 + $response->html .= esc_html($owner_info->public_repos).' repositories, ';
252 + $response->html .= esc_html($owner_info->followers).' followers.</p>';
402 253
403 254 header ( 'Content-Type: application/json' );
404 255 echo json_encode ( $response );
405 256 die();
@@ -407,8 +258,5 @@
407 258 }
408 259
409 260 }
410 261
411 -require_once ( 'github-api.php' );
412 -
413 -$github_api = new github_api();
414 -$github_embed = new github_embed ( $github_api );
262 +$github_embed = new github_embed();