PluginProbe
Github Embed / 1.1
Github Embed v1.1
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 +45 -175 1.41.1 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.4
7 +Version: 1.1
8 8 Author: Lee Willis
9 9 Author URI: http://www.leewillis.co.uk/
10 10 */
11 11
@@ -30,36 +30,21 @@
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 45 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 -
46 +
62 47 // @TODO i18n
63 48
64 49 }
65 50
@@ -64,42 +49,8 @@
64 49 }
65 50
66 51
67 52
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 53 /**
103 54 * Enqueue the frontend CSS
104 55 * @return void
105 56 */
@@ -106,9 +57,9 @@
106 57 function enqueue_styles() {
107 58
108 59 wp_register_style ( 'github-embed', plugins_url(basename(dirname(__FILE__)).'/css/github-embed.css' ) );
109 60 wp_enqueue_style ( 'github-embed' );
110 -
61 +
111 62 }
112 63
113 64
114 65
@@ -137,9 +88,9 @@
137 88 */
138 89 private function get_key() {
139 90
140 91 $key = get_option ( 'github_oembed_key' );
141 -
92 +
142 93 if ( ! $key ) {
143 94 $key = md5 ( time() . rand ( 0,65535 ) );
144 95 add_option ( 'github_oembed_key', $key, '', 'yes' );
145 96 }
@@ -189,31 +140,12 @@
189 140 header ( 'HTTP/1.0 404 Not Found' );
190 141 die ( 'Octocat is lost, and afraid' );
191 142 }
192 143
193 - // Issues / Milestones
194 - if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/graphs/contributors/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) {
144 + if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) {
195 145
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 146 $this->oembed_github_repo ( $matches[1], $matches[2] );
214 147
215 - // User
216 148 } elseif ( preg_match ( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) {
217 149
218 150 $this->oembed_github_author ( $matches[1] );
219 151
@@ -223,110 +155,39 @@
223 155
224 156
225 157
226 158 /**
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
159 + * Retrieve the information from github for a repo, and
160 + * output it as an oembed response
230 161 */
231 - private function oembed_github_repo_contributors ( $owner, $repository ) {
162 + private function oembed_github_repo ( $owner, $repository ) {
232 163
233 - $repo = $this->api->get_repo ( $owner, $repository );
234 - $contributors = $this->api->get_repo_contributors ( $owner, $repository );
164 + $repository = trim ( $repository, '/' );
235 165
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;
166 + $results = wp_remote_get( "https://api.github.com/repos/$owner/$repository", $args = array (
167 + 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) );
242 168
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 - $response->html .= '<li class="github-repo-contributor">';
256 - $response->html .= '<img class="github-repo-contributor-avatar" src="';
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).'">';
259 - $response->html .= '<span class="github-repo-contributor-login">';
260 - $response->html .= '<a href="https://github.com/'.esc_attr($contributor->author->login).'">'.esc_attr($contributor->author->login).'</a></span>';
169 + if ( is_wp_error( $results ) ||
170 + ! isset ( $results['response']['code'] ) ||
171 + $results['response']['code'] != '200' ) {
172 + header ( 'HTTP/1.0 404 Not Found' );
173 + die ( 'Octocat is lost, and afraid' );
261 174 }
262 175
263 - $response->html .= '</ul>';
264 - $response->html .= '<div style="clear: both;"></div>';
265 - $response->html .= '</div>';
176 + $repo = json_decode ( $results['body'] );
266 177
267 - header ( 'Content-Type: application/json' );
268 - echo json_encode ( $response );
269 - die();
178 + $results = wp_remote_get( "https://api.github.com/repos/$owner/$repository/commits", $args = array (
179 + 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) );
270 180
271 - }
272 -
273 -
274 -
275 - /**
276 - * Retrieve the summary information for a repo's milestone, and
277 - * output it as an oembed response
278 - */
279 - private function oembed_github_repo_milestone_summary ( $owner, $repository, $milestone ) {
280 -
281 - $repo = $this->api->get_repo ( $owner, $repository );
282 - $summary = $this->api->get_repo_milestone_summary ( $owner, $repository, $milestone );
283 -
284 - $response = new stdClass();
285 - $response->type = 'rich';
286 - $response->width = '10';
287 - $response->height = '10';
288 - $response->version = '1.0';
289 - $response->title = $repo->description;
290 -
291 - // @TODO This should all be templated
292 - $response->html = '<div class="github-embed github-embed-milestone-summary">';
293 - $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>";
294 -
295 - $response->html .= '<span class="github-heading">Milestone: </span>';
296 - $response->html .= '<span class="github-milestone-title">'.esc_html($summary->title)."</span><br>";
297 -
298 - $response->html .= '<span class="github-heading">Issues: </span>';
299 - $response->html .= '<span class="github-milestone-issues">';
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>";
302 -
303 - if ( ! empty ( $summary->due_on ) ) {
304 - $response->html .= '<span class="github-heading">Due: </span>';
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>';
181 + if ( is_wp_error( $results ) ||
182 + ! isset ( $results['response']['code'] ) ||
183 + $results['response']['code'] != '200' ) {
184 + header ( 'HTTP/1.0 404 Not Found' );
185 + die ( 'Octocat is lost, and afraid' );
307 186 }
308 187
309 - $response->html .= '<p class="github-milestone-description">'.nl2br(esc_html($summary->description))."</p><br>";
310 - $response->html .= '</div>';
188 + $commits = json_decode ( $results['body'] );
311 189
312 - header ( 'Content-Type: application/json' );
313 - echo json_encode ( $response );
314 - die();
315 -
316 - }
317 -
318 -
319 -
320 - /**
321 - * Retrieve the information from github for a repo, and
322 - * output it as an oembed response
323 - */
324 - private function oembed_github_repo ( $owner, $repository ) {
325 -
326 - $repo = $this->api->get_repo ( $owner, $repository );
327 - $commits =$this->api->get_repo_commits ( $owner, $repository );
328 -
329 190 $response = new stdClass();
330 191 $response->type = 'rich';
331 192 $response->width = '10';
332 193 $response->height = '10';
@@ -336,10 +197,10 @@
336 197 // @TODO This should all be templated
337 198 $response->html = '<div class="github-embed github-embed-repository">';
338 199 $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>";
339 200 $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/>";
201 + $response->html .= esc_html($repo->forks_count)." forks.<br/>";
202 + $response->html .= esc_html($repo->open_issues_count)." open issues.<br/>";
342 203
343 204 if ( count ( $commits ) ) {
344 205
345 206 $cnt = 0;
@@ -354,9 +215,9 @@
354 215 $response->html .= '<li class="github_commit">';
355 216 $response->html .= '<a href="https://github.com/'.$owner.'/'.$repository.'/commit/'.esc_attr($commit->sha).'" target="_blank">'.esc_html($commit->commit->message)."</a>, ";
356 217 $response->html .= esc_html($commit->commit->committer->name);
357 218 $response->html .= '</li>';
358 -
219 +
359 220 $cnt++;
360 221
361 222 }
362 223
@@ -379,10 +240,22 @@
379 240 * it as an oembed response
380 241 */
381 242 private function oembed_github_author ( $owner ) {
382 243
383 - $owner_info = $this->api->get_user ( $owner );
244 + $owner = trim ( $owner, '/' );
384 245
246 + $results = wp_remote_get( "https://api.github.com/users/$owner", $args = array (
247 + 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) );
248 +
249 + if ( is_wp_error( $results ) ||
250 + ! isset ( $results['response']['code'] ) ||
251 + $results['response']['code'] != '200' ) {
252 + header ( 'HTTP/1.0 404 Not Found' );
253 + die ( 'Octocat is lost, and afraid' );
254 + }
255 +
256 + $owner_info = json_decode ( $results['body'] );
257 +
385 258 $response = new stdClass();
386 259 $response->type = 'rich';
387 260 $response->width = '10';
388 261 $response->height = '10';
@@ -391,10 +264,10 @@
391 264
392 265 // @TODO This should all be templated
393 266 $response->html = '<div class="github-embed github-embed-user">';
394 267 $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>';
268 + $response->html .= esc_html($owner_info->public_repos).' repositories, ';
269 + $response->html .= esc_html($owner_info->followers).' followers.</p>';
397 270 $response->html .= '</div>';
398 271
399 272 header ( 'Content-Type: application/json' );
400 273 echo json_encode ( $response );
@@ -403,8 +276,5 @@
403 276 }
404 277
405 278 }
406 279
407 -require_once ( 'github-api.php' );
408 -
409 -$github_api = new github_api();
410 -$github_embed = new github_embed ( $github_api );
280 +$github_embed = new github_embed();