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