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 +156 -183 2.01.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: 2.0
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-2020 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 - */
39 34 class github_embed {
40 35
41 - private $api;
42 36
37 +
43 38 /**
44 39 * Constructor. Registers hooks and filters
45 - *
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,28 +54,34 @@
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 - *
96 - * @param array $providers The current list of providers
97 - *
70 + * @param array $providers The current list of providers
98 71 * @return array The list, with our new provider added
99 72 */
100 73 public function register_oembed_handler() {
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 );
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 +
105 80 }
106 81
82 +
83 +
107 84 /**
108 85 * Generate a unique key that can be used on our requests to stop others
109 86 * hijacking our internal oEmbed API
110 87 * @return string The site key
@@ -109,17 +86,22 @@
109 86 * hijacking our internal oEmbed API
110 87 * @return string The site key
111 88 */
112 89 private function get_key() {
113 - $key = get_option( 'github_oembed_key' );
90 +
91 + $key = get_option ( 'github_oembed_key' );
92 +
114 93 if ( ! $key ) {
115 - $key = md5( time() . rand( 0, 65535 ) );
116 - add_option( 'github_oembed_key', $key, '', 'yes' );
94 + $key = md5 ( time() . rand ( 0,65535 ) );
95 + add_option ( 'github_oembed_key', $key, '', 'yes' );
117 96 }
118 97
119 98 return $key;
99 +
120 100 }
121 101
102 +
103 +
122 104 /**
123 105 * Check whether this is an oembed request, handle if it is
124 106 * Ignore it if not.
125 107 * Insert rant here about WP's lack of a front-end AJAX handler.
@@ -124,184 +106,175 @@
124 106 * Ignore it if not.
125 107 * Insert rant here about WP's lack of a front-end AJAX handler.
126 108 */
127 109 public function maybe_handle_oembed() {
128 - if ( isset( $_GET['github_oembed'] ) ) {
110 +
111 + if ( isset ( $_GET['github_oembed'] ) ) {
129 112 return $this->handle_oembed();
130 113 }
114 +
131 115 }
132 116
117 +
118 +
133 119 /**
134 120 * Handle an oembed request
135 121 */
136 122 public function handle_oembed() {
123 +
137 124 // Check this request is valid
138 - if ( $_GET['github_oembed'] !== $this->get_key() ) {
139 - header( 'HTTP/1.0 403 Forbidden' );
140 - 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.' );
141 128 }
142 129
143 130 // Check we have the required information
144 - $url = isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
145 - $format = isset( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
131 + $url = isset ( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
132 + $format = isset ( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
146 133
147 - if ( ! empty( $format ) && 'json' !== $format ) {
148 - header( 'HTTP/1.0 501 Not implemented' );
149 - 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' );
150 137 }
151 138
152 139 if ( ! $url ) {
153 - header( 'HTTP/1.0 404 Not Found' );
154 - die( 'Octocat is lost, and afraid' );
140 + header ( 'HTTP/1.0 404 Not Found' );
141 + die ( 'Octocat is lost, and afraid' );
155 142 }
156 143
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] );
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 +
178 152 }
153 +
179 154 }
180 155
156 +
157 +
181 158 /**
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
159 + * Retrieve the information from github for a repo, and
160 + * output it as an oembed response
188 161 */
189 - private function process_template( $template, $data ) {
190 - ob_start();
191 - if ( ! locate_template( 'wp-github-oembed/' . $template, true ) ) {
192 - require_once 'templates/' . $template;
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' );
193 174 }
194 175
195 - return ob_get_clean();
196 - }
176 + $repo = json_decode ( $results['body'] );
197 177
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' );
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' ) );
210 180
211 - $response = new stdClass();
212 - $response->type = 'rich';
213 - $response->width = '10';
214 - $response->height = '10';
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';
215 194 $response->version = '1.0';
216 - $response->title = $data['repo']->description;
217 - $response->html = $this->process_template(
218 - 'repository_contributors.php', $data );
195 + $response->title = $repo->description;
219 196
220 - header( 'Content-Type: application/json' );
221 - echo json_encode( $response );
222 - die();
223 - }
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/>";
224 203
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' );
204 + if ( count ( $commits ) ) {
234 205
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 );
206 + $cnt = 0;
207 + $response->html .= 'Recent commits:';
208 + $response->html .= '<ul class="github_commits">';
243 209
244 - header( 'Content-Type: application/json' );
245 - echo json_encode( $response );
246 - die();
210 + foreach ( $commits as $commit ) {
247 211
248 - }
212 + if ($cnt > 4)
213 + break;
249 214
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' );
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++;
262 221
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 );
222 + }
271 223
224 + $response->html .= '</ul>';
272 225
273 - header( 'Content-Type: application/json' );
274 - echo json_encode( $response );
226 + }
227 + $response->html .= '</p>';
228 + $response->html .= '</div>';
229 +
230 + header ( 'Content-Type: application/json' );
231 + echo json_encode ( $response );
275 232 die();
233 +
276 234 }
277 235
236 +
237 +
278 238 /**
279 239 * Retrieve the information from github for an author, and output
280 240 * it as an oembed response
281 241 */
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' );
242 + private function oembed_github_author ( $owner ) {
288 243
289 - $response = new stdClass();
290 - $response->type = 'rich';
291 - $response->width = '10';
292 - $response->height = '10';
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';
293 262 $response->version = '1.0';
294 - $response->title = $data['owner_info']->name;
295 - $response->html = $this->process_template(
296 - 'author.php', $data );
263 + $response->title = $owner_info->name;
297 264
298 - header( 'Content-Type: application/json' );
299 - echo json_encode( $response );
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 );
300 274 die();
275 +
301 276 }
277 +
302 278 }
303 279
304 -require_once( 'github-api.php' );
305 -
306 -$github_api = new github_api();
307 -$github_embed = new github_embed( $github_api );
280 +$github_embed = new github_embed();