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 +164 -126 1.71.2 View file →
@@ -1,19 +1,20 @@
1 1 <?php
2 +
2 3 /*
3 4 Plugin Name: Github Embed
4 5 Plugin URI: http://www.leewillis.co.uk/wordpress-plugins
5 6 Description: Paste the URL to a Github project into your posts or pages, and have the project information pulled in and displayed automatically
6 -Version: 1.7
7 +Version: 1.2
7 8 Author: Lee Willis
8 9 Author URI: http://www.leewillis.co.uk/
9 10 */
10 11
11 12 /**
12 - * Copyright (c) 2013-2019 Lee Willis. All rights reserved.
13 + * Copyright (c) 2013 Lee Willis. All rights reserved.
13 14 *
14 - * Released under the GPL license v2
15 - * https://www.gnu.org/licenses/gpl-2.0.en.html
15 + * Released under the GPL license
16 + * http://www.opensource.org/licenses/gpl-license.php
16 17 *
17 18 * This is an add-on for WordPress
18 19 * http://wordpress.org/
19 20 *
@@ -31,54 +32,37 @@
31 32 */
32 33
33 34 /**
34 35 * This class handles being the oEmbed provider in terms of registering the URLs that
35 - * 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
36 37 * class to retrieve the information from the GitHub API.
37 38 * @uses class github_api
38 39 */
40 +class github_embed {
39 41
40 -class github_embed {
41 42
43 +
42 44 private $api;
43 45
46 +
47 +
44 48 /**
45 49 * Constructor. Registers hooks and filters
46 50 * @param class $api An instance of the github_api classs
47 51 */
48 - public function __construct( $api ) {
52 + public function __construct ( $api ) {
53 +
49 54 $this->api = $api;
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' ) );
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 +
55 60 // @TODO i18n
61 +
56 62 }
57 63
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 - }
67 - }
68 64
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 - }
81 65
82 66 /**
83 67 * Enqueue the frontend CSS
84 68 * @return void
@@ -83,12 +67,16 @@
83 67 * Enqueue the frontend CSS
84 68 * @return void
85 69 */
86 70 function enqueue_styles() {
87 - wp_register_style( 'github-embed', plugins_url( basename( dirname( __FILE__ ) ) . '/css/github-embed.css' ) );
88 - 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 +
89 75 }
90 76
77 +
78 +
91 79 /**
92 80 * Register the oEmbed provider, and point it at a local endpoint since github
93 81 * doesn't directly support oEmbed yet. Our local endpoint will use the github
94 82 * API to fulfil the request.
@@ -95,14 +83,18 @@
95 83 * @param array $providers The current list of providers
96 84 * @return array The list, with our new provider added
97 85 */
98 86 public function register_oembed_handler() {
99 - $oembed_url = home_url();
87 +
88 + $oembed_url = home_url ();
100 89 $key = $this->get_key();
101 - $oembed_url = add_query_arg( array( 'github_oembed' => $key ), $oembed_url );
102 - 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 +
103 93 }
104 94
95 +
96 +
105 97 /**
106 98 * Generate a unique key that can be used on our requests to stop others
107 99 * hijacking our internal oEmbed API
108 100 * @return string The site key
@@ -107,16 +99,22 @@
107 99 * hijacking our internal oEmbed API
108 100 * @return string The site key
109 101 */
110 102 private function get_key() {
111 - $key = get_option( 'github_oembed_key' );
103 +
104 + $key = get_option ( 'github_oembed_key' );
105 +
112 106 if ( ! $key ) {
113 - $key = md5( time() . rand( 0, 65535 ) );
114 - add_option( 'github_oembed_key', $key, '', 'yes' );
107 + $key = md5 ( time() . rand ( 0,65535 ) );
108 + add_option ( 'github_oembed_key', $key, '', 'yes' );
115 109 }
110 +
116 111 return $key;
112 +
117 113 }
118 114
115 +
116 +
119 117 /**
120 118 * Check whether this is an oembed request, handle if it is
121 119 * Ignore it if not.
122 120 * Insert rant here about WP's lack of a front-end AJAX handler.
@@ -121,70 +119,85 @@
121 119 * Ignore it if not.
122 120 * Insert rant here about WP's lack of a front-end AJAX handler.
123 121 */
124 122 public function maybe_handle_oembed() {
125 - if ( isset( $_GET['github_oembed'] ) ) {
123 +
124 + if ( isset ( $_GET['github_oembed'] ) ) {
126 125 return $this->handle_oembed();
127 126 }
127 +
128 128 }
129 129
130 +
131 +
130 132 /**
131 133 * Handle an oembed request
132 134 */
133 135 public function handle_oembed() {
136 +
134 137 // Check this request is valid
135 - if ( $_GET['github_oembed'] !== $this->get_key() ) {
136 - header( 'HTTP/1.0 403 Forbidden' );
137 - 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.' );
138 141 }
139 142
140 143 // Check we have the required information
141 - $url = isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
142 - $format = isset( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
144 + $url = isset ( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
145 + $format = isset ( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
143 146
144 - if ( ! empty( $format ) && 'json' !== $format ) {
145 - header( 'HTTP/1.0 501 Not implemented' );
146 - 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' );
147 150 }
148 151
149 152 if ( ! $url ) {
150 - header( 'HTTP/1.0 404 Not Found' );
151 - die( 'Octocat is lost, and afraid' );
153 + header ( 'HTTP/1.0 404 Not Found' );
154 + die ( 'Octocat is lost, and afraid' );
152 155 }
153 156
154 157 // Issues / 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 ) ) {
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 ) ) {
159 165 $milestone = $milestones[1];
160 166 } else {
161 167 $milestone = null;
162 168 }
169 +
163 170 if ( $milestone ) {
164 - $this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $milestone );
171 + $this->oembed_github_repo_milestone_summary ( $matches[1], $matches[2], $milestone );
165 172 }
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] );
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 +
175 184 }
185 +
176 186 }
177 187
188 +
189 +
178 190 /**
179 191 * Retrieve a list of contributors for a project
180 192 * @param string $owner The owner of the repository
181 193 * @param string $repository The repository name
182 194 */
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 );
195 + private function oembed_github_repo_contributors ( $owner, $repository ) {
186 196
197 + $repo = $this->api->get_repo ( $owner, $repository );
198 + $contributors = $this->api->get_repo_contributors ( $owner, $repository );
199 +
187 200 $response = new stdClass();
188 201 $response->type = 'rich';
189 202 $response->width = '10';
190 203 $response->height = '10';
@@ -190,41 +203,53 @@
190 203 $response->height = '10';
191 204 $response->version = '1.0';
192 205 $response->title = $repo->description;
193 206
194 - $gravatar_size = apply_filters( 'github_oembed_gravatar_size', 64 );
207 + $gravatar_size = apply_filters ( 'github_oembed_gravatar_size', 64 );
195 208
196 209 // @TODO This should all be templated
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/>';
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 +
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';
@@ -231,43 +256,45 @@
231 256 $response->version = '1.0';
232 257 $response->title = $repo->description;
233 258
234 259 // @TODO This should all be templated
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 -
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 +
239 263 $response->html .= '<span class="github-heading">Milestone: </span>';
240 - $response->html .= '<span class="github-milestone-title">' . esc_html( $summary->title ) . '</span><br>';
241 -
264 + $response->html .= '<span class="github-milestone-title">'.esc_html($summary->title)."</span><br>";
265 +
242 266 $response->html .= '<span class="github-heading">Issues: </span>';
243 267 $response->html .= '<span class="github-milestone-issues">';
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 ) ) {
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 ) ) {
248 272 $response->html .= '<span class="github-heading">Due: </span>';
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>';
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>';
251 275 }
252 -
253 - $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>";
254 278 $response->html .= '</div>';
255 279
256 - header( 'Content-Type: application/json' );
257 - echo json_encode( $response );
280 + header ( 'Content-Type: application/json' );
281 + echo json_encode ( $response );
258 282 die();
259 283
260 - }
284 + }
261 285
286 +
287 +
262 288 /**
263 289 * Retrieve the information from github for a repo, and
264 290 * output it as an oembed response
265 291 */
266 292 private function oembed_github_repo ( $owner, $repository ) {
267 - $repo = $this->api->get_repo( $owner, $repository );
268 - $commits =$this->api->get_repo_commits( $owner, $repository );
269 293
294 + $repo = $this->api->get_repo ( $owner, $repository );
295 + $commits =$this->api->get_repo_commits ( $owner, $repository );
296 +
270 297 $response = new stdClass();
271 298 $response->type = 'rich';
272 299 $response->width = '10';
273 300 $response->height = '10';
@@ -274,39 +301,48 @@
274 301 $response->version = '1.0';
275 302 $response->title = $repo->description;
276 303
277 304 // @TODO This should all be templated
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/>';
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/>";
285 310
286 - if ( count( $commits ) ) {
311 + if ( count ( $commits ) ) {
312 +
287 313 $cnt = 0;
288 314 $response->html .= 'Recent commits:';
289 315 $response->html .= '<ul class="github_commits">';
316 +
290 317 foreach ( $commits as $commit ) {
291 - if ( $cnt > 4 ) {
318 +
319 + if ($cnt > 4)
292 320 break;
293 - }
321 +
294 322 $response->html .= '<li class="github_commit">';
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 );
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);
297 325 $response->html .= '</li>';
326 +
298 327 $cnt++;
328 +
299 329 }
330 +
300 331 $response->html .= '</ul>';
332 +
301 333 }
302 334 $response->html .= '</p>';
303 335 $response->html .= '</div>';
304 - header( 'Content-Type: application/json' );
305 - echo json_encode( $response );
336 +
337 + header ( 'Content-Type: application/json' );
338 + echo json_encode ( $response );
306 339 die();
340 +
307 341 }
308 342
343 +
344 +
309 345 /**
310 346 * Retrieve the information from github for an author, and output
311 347 * it as an oembed response
312 348 */
@@ -311,9 +347,9 @@
311 347 * it as an oembed response
312 348 */
313 349 private function oembed_github_author ( $owner ) {
314 350
315 - $owner_info = $this->api->get_user( $owner );
351 + $owner_info = $this->api->get_user ( $owner );
316 352
317 353 $response = new stdClass();
318 354 $response->type = 'rich';
319 355 $response->width = '10';
@@ -321,20 +357,22 @@
321 357 $response->version = '1.0';
322 358 $response->title = $owner_info->name;
323 359
324 360 // @TODO This should all be templated
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>';
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>';
330 365 $response->html .= '</div>';
331 - header( 'Content-Type: application/json' );
332 - echo json_encode( $response );
366 +
367 + header ( 'Content-Type: application/json' );
368 + echo json_encode ( $response );
333 369 die();
370 +
334 371 }
372 +
335 373 }
336 374
337 -require_once( 'github-api.php' );
375 +require_once ( 'github-api.php' );
338 376
339 377 $github_api = new github_api();
340 -$github_embed = new github_embed( $github_api );
378 +$github_embed = new github_embed ( $github_api );