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