PluginProbe
Github Embed / 2.0
Github Embed v2.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 +183 -156 1.12.0 View file →
@@ -1,20 +1,19 @@
1 1 <?php
2 -
3 2 /*
4 3 Plugin Name: Github Embed
5 4 Plugin URI: http://www.leewillis.co.uk/wordpress-plugins
6 5 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
6 +Version: 2.0
8 7 Author: Lee Willis
9 8 Author URI: http://www.leewillis.co.uk/
10 9 */
11 10
12 11 /**
13 - * Copyright (c) 2013 Lee Willis. All rights reserved.
12 + * Copyright (c) 2013-2020 Lee Willis. All rights reserved.
14 13 *
15 - * Released under the GPL license
16 - * http://www.opensource.org/licenses/gpl-license.php
14 + * Released under the GPL license v2
15 + * https://www.gnu.org/licenses/gpl-2.0.en.html
17 16 *
18 17 * This is an add-on for WordPress
19 18 * http://wordpress.org/
20 19 *
@@ -30,58 +29,82 @@
30 29 * GNU General Public License for more details.
31 30 * **********************************************************************
32 31 */
33 32
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 39 class github_embed {
35 40
41 + private $api;
36 42
37 -
38 43 /**
39 44 * Constructor. Registers hooks and filters
45 + *
46 + * @param class $api An instance of the github_api classs
40 47 */
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 -
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' ) );
47 55 // @TODO i18n
56 + }
48 57
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 + }
49 67 }
50 68
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 + }
51 81
52 -
53 82 /**
54 83 * Enqueue the frontend CSS
55 84 * @return void
56 85 */
57 86 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 -
87 + wp_register_style( 'github-embed', plugins_url( basename( dirname( __FILE__ ) ) . '/css/github-embed.css' ) );
88 + wp_enqueue_style( 'github-embed' );
62 89 }
63 90
64 -
65 -
66 91 /**
67 92 * Register the oEmbed provider, and point it at a local endpoint since github
68 93 * doesn't directly support oEmbed yet. Our local endpoint will use the github
69 94 * API to fulfil the request.
70 - * @param array $providers The current list of providers
95 + *
96 + * @param array $providers The current list of providers
97 + *
71 98 * @return array The list, with our new provider added
72 99 */
73 100 public function register_oembed_handler() {
74 -
75 - $oembed_url = home_url ();
76 - $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 -
101 + $oembed_url = home_url();
102 + $key = $this->get_key();
103 + $oembed_url = add_query_arg( array( 'github_oembed' => $key ), $oembed_url );
104 + wp_oembed_add_provider( '#https?://github.com/.*#i', $oembed_url, true );
80 105 }
81 106
82 -
83 -
84 107 /**
85 108 * Generate a unique key that can be used on our requests to stop others
86 109 * hijacking our internal oEmbed API
87 110 * @return string The site key
@@ -86,22 +109,17 @@
86 109 * hijacking our internal oEmbed API
87 110 * @return string The site key
88 111 */
89 112 private function get_key() {
90 -
91 - $key = get_option ( 'github_oembed_key' );
92 -
113 + $key = get_option( 'github_oembed_key' );
93 114 if ( ! $key ) {
94 - $key = md5 ( time() . rand ( 0,65535 ) );
95 - add_option ( 'github_oembed_key', $key, '', 'yes' );
115 + $key = md5( time() . rand( 0, 65535 ) );
116 + add_option( 'github_oembed_key', $key, '', 'yes' );
96 117 }
97 118
98 119 return $key;
99 -
100 120 }
101 121
102 -
103 -
104 122 /**
105 123 * Check whether this is an oembed request, handle if it is
106 124 * Ignore it if not.
107 125 * Insert rant here about WP's lack of a front-end AJAX handler.
@@ -106,175 +124,184 @@
106 124 * Ignore it if not.
107 125 * Insert rant here about WP's lack of a front-end AJAX handler.
108 126 */
109 127 public function maybe_handle_oembed() {
110 -
111 - if ( isset ( $_GET['github_oembed'] ) ) {
128 + if ( isset( $_GET['github_oembed'] ) ) {
112 129 return $this->handle_oembed();
113 130 }
114 -
115 131 }
116 132
117 -
118 -
119 133 /**
120 134 * Handle an oembed request
121 135 */
122 136 public function handle_oembed() {
123 -
124 137 // 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.' );
138 + if ( $_GET['github_oembed'] !== $this->get_key() ) {
139 + header( 'HTTP/1.0 403 Forbidden' );
140 + die( 'Sad Octocat is sad.' );
128 141 }
129 142
130 143 // Check we have the required information
131 - $url = isset ( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
132 - $format = isset ( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
144 + $url = isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
145 + $format = isset( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
133 146
134 - if ( ! empty ( $format ) && $format != 'json' ) {
135 - header ( 'HTTP/1.0 501 Not implemented' );
136 - die ( 'This octocat only does json' );
147 + if ( ! empty( $format ) && 'json' !== $format ) {
148 + header( 'HTTP/1.0 501 Not implemented' );
149 + die( 'This octocat only does json' );
137 150 }
138 151
139 152 if ( ! $url ) {
140 - header ( 'HTTP/1.0 404 Not Found' );
141 - die ( 'Octocat is lost, and afraid' );
153 + header( 'HTTP/1.0 404 Not Found' );
154 + die( 'Octocat is lost, and afraid' );
142 155 }
143 156
144 - if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) {
145 -
146 - $this->oembed_github_repo ( $matches[1], $matches[2] );
147 -
148 - } elseif ( preg_match ( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) {
149 -
150 - $this->oembed_github_author ( $matches[1] );
151 -
157 + // Issues / Milestones
158 + if ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/graphs/contributors/?$#i', $url, $matches ) && ! empty( $matches[2] ) ) {
159 + $this->oembed_github_repo_contributors( $matches[1], $matches[2] );
160 + } elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/issues.*$#i', $url, $matches ) && ! empty( $matches[2] ) ) {
161 + if ( preg_match( '#issues.?milestone=([0-9]*)#i', $url, $milestones ) ) {
162 + $milestone = $milestones[1];
163 + } else {
164 + $milestone = null;
165 + }
166 + if ( $milestone ) {
167 + $this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $milestone );
168 + }
169 + } elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/milestone/([0-9]*)$#i', $url, $matches ) ) {
170 + // New style milestone URL, e.g. https://github.com/example/example/milestone/1.
171 + $this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $matches[3] );
172 + } elseif ( preg_match( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty( $matches[2] ) ) {
173 + // Repository.
174 + $this->oembed_github_repo( $matches[1], $matches[2] );
175 + } elseif ( preg_match( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) {
176 + // User.
177 + $this->oembed_github_author( $matches[1] );
152 178 }
153 -
154 179 }
155 180
156 -
157 -
158 181 /**
159 - * Retrieve the information from github for a repo, and
160 - * output it as an oembed response
182 + * Capture then return output of template, provided theme or fallback to plugin default.
183 + *
184 + * @param string $template The template name to process.
185 + * @param string $data Array, object, or variable that the template needs.
186 + *
187 + * @return string
161 188 */
162 - private function oembed_github_repo ( $owner, $repository ) {
163 -
164 - $repository = trim ( $repository, '/' );
165 -
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' ) );
168 -
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' );
189 + private function process_template( $template, $data ) {
190 + ob_start();
191 + if ( ! locate_template( 'wp-github-oembed/' . $template, true ) ) {
192 + require_once 'templates/' . $template;
174 193 }
175 194
176 - $repo = json_decode ( $results['body'] );
195 + return ob_get_clean();
196 + }
177 197
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' ) );
198 + /**
199 + * Retrieve a list of contributors for a project
200 + *
201 + * @param string $owner The owner of the repository
202 + * @param string $repository The repository name
203 + */
204 + private function oembed_github_repo_contributors( $owner, $repository ) {
205 + $data = [];
206 + $data['repo'] = $this->api->get_repo( $owner, $repository );
207 + $data['contributors'] = $this->api->get_repo_contributors( $owner, $repository );
208 + $data['gravatar_size'] = apply_filters( 'github_oembed_gravatar_size', 64 );
209 + $data['logo_class'] = apply_filters( 'wp_github_oembed_logo_class', 'github-logo-octocat' );
180 210
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 - }
187 -
188 - $commits = json_decode ( $results['body'] );
189 -
190 - $response = new stdClass();
191 - $response->type = 'rich';
192 - $response->width = '10';
193 - $response->height = '10';
211 + $response = new stdClass();
212 + $response->type = 'rich';
213 + $response->width = '10';
214 + $response->height = '10';
194 215 $response->version = '1.0';
195 - $response->title = $repo->description;
216 + $response->title = $data['repo']->description;
217 + $response->html = $this->process_template(
218 + 'repository_contributors.php', $data );
196 219
197 - // @TODO This should all be templated
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/>";
220 + header( 'Content-Type: application/json' );
221 + echo json_encode( $response );
222 + die();
223 + }
203 224
204 - if ( count ( $commits ) ) {
225 + /**
226 + * Retrieve the summary information for a repo's milestone, and
227 + * output it as an oembed response
228 + */
229 + private function oembed_github_repo_milestone_summary( $owner, $repository, $milestone ) {
230 + $data = [];
231 + $data['repo'] = $this->api->get_repo( $owner, $repository );
232 + $data['summary'] = $this->api->get_repo_milestone_summary( $owner, $repository, $milestone );
233 + $data['logo_class'] = apply_filters( 'wp_github_oembed_logo_class', 'github-logo-octocat' );
205 234
206 - $cnt = 0;
207 - $response->html .= 'Recent commits:';
208 - $response->html .= '<ul class="github_commits">';
235 + $response = new stdClass();
236 + $response->type = 'rich';
237 + $response->width = '10';
238 + $response->height = '10';
239 + $response->version = '1.0';
240 + $response->title = $data['repo']->description;
241 + $response->html = $this->process_template(
242 + 'repository_milestone_summary.php', $data );
209 243
210 - foreach ( $commits as $commit ) {
244 + header( 'Content-Type: application/json' );
245 + echo json_encode( $response );
246 + die();
211 247
212 - if ($cnt > 4)
213 - break;
248 + }
214 249
215 - $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);
218 - $response->html .= '</li>';
219 -
220 - $cnt++;
250 + /**
251 + * Retrieve the information from github for a repo, and
252 + * output it as an oembed response
253 + */
254 + private function oembed_github_repo( $owner, $repository ) {
255 + $data = [
256 + 'owner_slug' => $owner,
257 + 'repo_slug' => $repository,
258 + ];
259 + $data['repo'] = $this->api->get_repo( $owner, $repository );
260 + $data['commits'] = $this->api->get_repo_commits( $owner, $repository );
261 + $data['logo_class'] = apply_filters( 'wp_github_oembed_logo_class', 'github-logo-mark' );
221 262
222 - }
263 + $response = new stdClass();
264 + $response->type = 'rich';
265 + $response->width = '10';
266 + $response->height = '10';
267 + $response->version = '1.0';
268 + $response->title = $data['repo']->description;
269 + $response->html = $this->process_template(
270 + 'repository.php', $data );
223 271
224 - $response->html .= '</ul>';
225 272
226 - }
227 - $response->html .= '</p>';
228 - $response->html .= '</div>';
229 -
230 - header ( 'Content-Type: application/json' );
231 - echo json_encode ( $response );
273 + header( 'Content-Type: application/json' );
274 + echo json_encode( $response );
232 275 die();
233 -
234 276 }
235 277
236 -
237 -
238 278 /**
239 279 * Retrieve the information from github for an author, and output
240 280 * it as an oembed response
241 281 */
242 - private function oembed_github_author ( $owner ) {
282 + private function oembed_github_author( $owner ) {
283 + $data = [];
284 + $data["owner"] = $owner;
285 + $data["owner_info"] = $this->api->get_user( $owner );
286 + $data["logo_class"] = apply_filters( 'wp_github_oembed_logo_class',
287 + 'github-logo-octocat' );
243 288
244 - $owner = trim ( $owner, '/' );
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 -
258 - $response = new stdClass();
259 - $response->type = 'rich';
260 - $response->width = '10';
261 - $response->height = '10';
289 + $response = new stdClass();
290 + $response->type = 'rich';
291 + $response->width = '10';
292 + $response->height = '10';
262 293 $response->version = '1.0';
263 - $response->title = $owner_info->name;
294 + $response->title = $data['owner_info']->name;
295 + $response->html = $this->process_template(
296 + 'author.php', $data );
264 297
265 - // @TODO This should all be templated
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>';
270 - $response->html .= '</div>';
271 -
272 - header ( 'Content-Type: application/json' );
273 - echo json_encode ( $response );
298 + header( 'Content-Type: application/json' );
299 + echo json_encode( $response );
274 300 die();
275 -
276 301 }
302 +}
277 303
278 -}
304 +require_once( 'github-api.php' );
279 305
280 -$github_embed = new github_embed();
306 +$github_api = new github_api();
307 +$github_embed = new github_embed( $github_api );