PluginProbe
Github Embed / 1.6
Github Embed v1.6
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 +175 -118 1.11.6 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.1
7 +Version: 1.6
8 8 Author: Lee Willis
9 9 Author URI: http://www.leewillis.co.uk/
10 10 */
11 11
@@ -30,40 +30,66 @@
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 41 class github_embed {
35 42
43 + private $api;
36 44
37 -
38 45 /**
39 46 * Constructor. Registers hooks and filters
47 + * @param class $api An instance of the github_api classs
40 48 */
41 - public function __construct() {
42 -
43 - add_action ( 'init', array ( $this, 'register_oembed_handler' ) );
44 - add_action ( 'init', array ( $this, 'maybe_handle_oembed' ) );
45 - add_action ( 'wp_enqueue_scripts', array ( $this, 'enqueue_styles' ) );
46 -
49 + public function __construct( $api ) {
50 + $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' ) );
47 56 // @TODO i18n
57 + }
48 58
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 + }
49 68 }
50 69
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 + }
51 82
52 -
53 83 /**
54 84 * Enqueue the frontend CSS
55 85 * @return void
56 86 */
57 87 function enqueue_styles() {
58 -
59 - wp_register_style ( 'github-embed', plugins_url(basename(dirname(__FILE__)).'/css/github-embed.css' ) );
60 - wp_enqueue_style ( 'github-embed' );
61 -
88 + wp_register_style( 'github-embed', plugins_url( basename( dirname( __FILE__ ) ) . '/css/github-embed.css' ) );
89 + wp_enqueue_style( 'github-embed' );
62 90 }
63 91
64 -
65 -
66 92 /**
67 93 * Register the oEmbed provider, and point it at a local endpoint since github
68 94 * doesn't directly support oEmbed yet. Our local endpoint will use the github
69 95 * API to fulfil the request.
@@ -70,18 +96,14 @@
70 96 * @param array $providers The current list of providers
71 97 * @return array The list, with our new provider added
72 98 */
73 99 public function register_oembed_handler() {
74 -
75 - $oembed_url = home_url ();
100 + $oembed_url = home_url();
76 101 $key = $this->get_key();
77 - $oembed_url = add_query_arg ( array ( 'github_oembed' => $key ), $oembed_url);
78 - wp_oembed_add_provider ( '#https?://github.com/.*#i', $oembed_url, true );
79 -
102 + $oembed_url = add_query_arg( array( 'github_oembed' => $key ), $oembed_url );
103 + wp_oembed_add_provider( '#https?://github.com/.*#i', $oembed_url, true );
80 104 }
81 105
82 -
83 -
84 106 /**
85 107 * Generate a unique key that can be used on our requests to stop others
86 108 * hijacking our internal oEmbed API
87 109 * @return string The site key
@@ -86,22 +108,16 @@
86 108 * hijacking our internal oEmbed API
87 109 * @return string The site key
88 110 */
89 111 private function get_key() {
90 -
91 - $key = get_option ( 'github_oembed_key' );
92 -
112 + $key = get_option( 'github_oembed_key' );
93 113 if ( ! $key ) {
94 - $key = md5 ( time() . rand ( 0,65535 ) );
95 - add_option ( 'github_oembed_key', $key, '', 'yes' );
114 + $key = md5( time() . rand( 0, 65535 ) );
115 + add_option( 'github_oembed_key', $key, '', 'yes' );
96 116 }
97 -
98 117 return $key;
99 -
100 118 }
101 119
102 -
103 -
104 120 /**
105 121 * Check whether this is an oembed request, handle if it is
106 122 * Ignore it if not.
107 123 * Insert rant here about WP's lack of a front-end AJAX handler.
@@ -106,87 +122,150 @@
106 122 * Ignore it if not.
107 123 * Insert rant here about WP's lack of a front-end AJAX handler.
108 124 */
109 125 public function maybe_handle_oembed() {
110 -
111 - if ( isset ( $_GET['github_oembed'] ) ) {
126 + if ( isset( $_GET['github_oembed'] ) ) {
112 127 return $this->handle_oembed();
113 128 }
114 -
115 129 }
116 130
117 -
118 -
119 131 /**
120 132 * Handle an oembed request
121 133 */
122 134 public function handle_oembed() {
123 -
124 135 // Check this request is valid
125 - if ( $_GET['github_oembed'] != $this->get_key() ) {
126 - header ( 'HTTP/1.0 403 Forbidden' );
127 - die ( 'Sad Octocat is sad.' );
136 + if ( $_GET['github_oembed'] !== $this->get_key() ) {
137 + header( 'HTTP/1.0 403 Forbidden' );
138 + die( 'Sad Octocat is sad.' );
128 139 }
129 140
130 141 // Check we have the required information
131 - $url = isset ( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
132 - $format = isset ( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
142 + $url = isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
143 + $format = isset( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
133 144
134 - if ( ! empty ( $format ) && $format != 'json' ) {
135 - header ( 'HTTP/1.0 501 Not implemented' );
136 - die ( 'This octocat only does json' );
145 + if ( ! empty( $format ) && 'json' !== $format ) {
146 + header( 'HTTP/1.0 501 Not implemented' );
147 + die( 'This octocat only does json' );
137 148 }
138 149
139 150 if ( ! $url ) {
140 - header ( 'HTTP/1.0 404 Not Found' );
141 - die ( 'Octocat is lost, and afraid' );
151 + header( 'HTTP/1.0 404 Not Found' );
152 + die( 'Octocat is lost, and afraid' );
142 153 }
143 154
144 - if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) {
155 + // 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 ) ) {
160 + $milestone = $milestones[1];
161 + } else {
162 + $milestone = null;
163 + }
164 + if ( $milestone ) {
165 + $this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $milestone );
166 + }
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] );
176 + }
177 + }
145 178
146 - $this->oembed_github_repo ( $matches[1], $matches[2] );
179 + /**
180 + * Retrieve a list of contributors for a project
181 + * @param string $owner The owner of the repository
182 + * @param string $repository The repository name
183 + */
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 );
147 187
148 - } elseif ( preg_match ( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) {
188 + $response = new stdClass();
189 + $response->type = 'rich';
190 + $response->width = '10';
191 + $response->height = '10';
192 + $response->version = '1.0';
193 + $response->title = $repo->description;
149 194
150 - $this->oembed_github_author ( $matches[1] );
195 + $gravatar_size = apply_filters( 'github_oembed_gravatar_size', 64 );
151 196
197 + // @TODO This should all be templated
198 + $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/>';
201 + $response->html .= '<span class="github-heading">Contributors: </span>';
202 + $response->html .= '<ul class="github-repo-contributors">';
203 + foreach ( $contributors as $contributor ) {
204 + $response->html .= '<li class="github-repo-contributor">';
205 + $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 ) . '">';
208 + $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>';
152 210 }
153 -
211 + $response->html .= '</ul>';
212 + $response->html .= '<div style="clear: both;"></div>';
213 + $response->html .= '</div>';
214 + header( 'Content-Type: application/json' );
215 + echo json_encode( $response );
216 + die();
154 217 }
155 218
156 -
157 -
158 219 /**
159 - * Retrieve the information from github for a repo, and
220 + * Retrieve the summary information for a repo's milestone, and
160 221 * output it as an oembed response
161 222 */
162 - private function oembed_github_repo ( $owner, $repository ) {
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 );
163 226
164 - $repository = trim ( $repository, '/' );
227 + $response = new stdClass();
228 + $response->type = 'rich';
229 + $response->width = '10';
230 + $response->height = '10';
231 + $response->version = '1.0';
232 + $response->title = $repo->description;
165 233
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' ) );
234 + // @TODO This should all be templated
235 + $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/>';
168 237
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' );
238 + $response->html .= '<span class="github-heading">Milestone: </span>';
239 + $response->html .= '<span class="github-milestone-title">' . esc_html( $summary->title ) . '</span><br>';
240 +
241 + $response->html .= '<span class="github-heading">Issues: </span>';
242 + $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 ) ) {
247 + $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>';
174 250 }
175 251
176 - $repo = json_decode ( $results['body'] );
252 + $response->html .= '<p class="github-milestone-description">' . nl2br( esc_html( $summary->description ) ) . '</p><br>';
253 + $response->html .= '</div>';
177 254
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' ) );
255 + header( 'Content-Type: application/json' );
256 + echo json_encode( $response );
257 + die();
180 258
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' );
186 - }
259 + }
187 260
188 - $commits = json_decode ( $results['body'] );
261 + /**
262 + * Retrieve the information from github for a repo, and
263 + * output it as an oembed response
264 + */
265 + private function oembed_github_repo ( $owner, $repository ) {
266 + $repo = $this->api->get_repo( $owner, $repository );
267 + $commits =$this->api->get_repo_commits( $owner, $repository );
189 268
190 269 $response = new stdClass();
191 270 $response->type = 'rich';
192 271 $response->width = '10';
@@ -195,47 +274,37 @@
195 274 $response->title = $repo->description;
196 275
197 276 // @TODO This should all be templated
198 277 $response->html = '<div class="github-embed github-embed-repository">';
199 - $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>";
200 - $response->html .= '<a href="'.esc_attr($repo->html_url).'" target="_blank">'.esc_html($repo->html_url)."</a><br/>";
201 - $response->html .= esc_html($repo->forks_count)." forks.<br/>";
202 - $response->html .= esc_html($repo->open_issues_count)." open issues.<br/>";
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/>';
203 283
204 - if ( count ( $commits ) ) {
205 -
284 + if ( count( $commits ) ) {
206 285 $cnt = 0;
207 286 $response->html .= 'Recent commits:';
208 287 $response->html .= '<ul class="github_commits">';
209 -
210 288 foreach ( $commits as $commit ) {
211 -
212 - if ($cnt > 4)
289 + if ( $cnt > 4 ) {
213 290 break;
214 -
291 + }
215 292 $response->html .= '<li class="github_commit">';
216 - $response->html .= '<a href="https://github.com/'.$owner.'/'.$repository.'/commit/'.esc_attr($commit->sha).'" target="_blank">'.esc_html($commit->commit->message)."</a>, ";
217 - $response->html .= esc_html($commit->commit->committer->name);
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 );
218 295 $response->html .= '</li>';
219 -
220 296 $cnt++;
221 -
222 297 }
223 -
224 298 $response->html .= '</ul>';
225 -
226 299 }
227 300 $response->html .= '</p>';
228 301 $response->html .= '</div>';
229 -
230 - header ( 'Content-Type: application/json' );
231 - echo json_encode ( $response );
302 + header( 'Content-Type: application/json' );
303 + echo json_encode( $response );
232 304 die();
233 -
234 305 }
235 306
236 -
237 -
238 307 /**
239 308 * Retrieve the information from github for an author, and output
240 309 * it as an oembed response
241 310 */
@@ -240,22 +309,10 @@
240 309 * it as an oembed response
241 310 */
242 311 private function oembed_github_author ( $owner ) {
243 312
244 - $owner = trim ( $owner, '/' );
313 + $owner_info = $this->api->get_user( $owner );
245 314
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 -
258 315 $response = new stdClass();
259 316 $response->type = 'rich';
260 317 $response->width = '10';
261 318 $response->height = '10';
@@ -263,18 +320,18 @@
263 320 $response->title = $owner_info->name;
264 321
265 322 // @TODO This should all be templated
266 323 $response->html = '<div class="github-embed github-embed-user">';
267 - $response->html .= '<p><a href="https://github.com/'.esc_attr($owner).'" target="_blank"><strong>'.esc_html($owner)."</strong></a><br/>";
268 - $response->html .= esc_html($owner_info->public_repos).' repositories, ';
269 - $response->html .= esc_html($owner_info->followers).' followers.</p>';
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>';
270 327 $response->html .= '</div>';
271 -
272 - header ( 'Content-Type: application/json' );
273 - echo json_encode ( $response );
328 + header( 'Content-Type: application/json' );
329 + echo json_encode( $response );
274 330 die();
275 -
276 331 }
332 +}
277 333
278 -}
334 +require_once( 'github-api.php' );
279 335
280 -$github_embed = new github_embed();
336 +$github_api = new github_api();
337 +$github_embed = new github_embed( $github_api );