PluginProbe
Github Embed / 1.0
Github Embed v1.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 +144 -189 1.91.0 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.9
7 +Version: 1.0
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,82 +30,44 @@
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 +
55 46 // @TODO i18n
56 - }
57 47
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 48 }
68 49
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 50
82 - /**
83 - * Enqueue the frontend CSS
84 - * @return void
85 - */
86 - 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 - }
90 51
91 52 /**
92 53 * Register the oEmbed provider, and point it at a local endpoint since github
93 54 * doesn't directly support oEmbed yet. Our local endpoint will use the github
94 55 * API to fulfil the request.
95 - *
96 - * @param array $providers The current list of providers
97 - *
56 + * @param array $providers The current list of providers
98 57 * @return array The list, with our new provider added
99 58 */
100 59 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 );
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 +
105 66 }
106 67
68 +
69 +
107 70 /**
108 71 * Generate a unique key that can be used on our requests to stop others
109 72 * hijacking our internal oEmbed API
110 73 * @return string The site key
@@ -109,17 +72,22 @@
109 72 * hijacking our internal oEmbed API
110 73 * @return string The site key
111 74 */
112 75 private function get_key() {
113 - $key = get_option( 'github_oembed_key' );
76 +
77 + $key = get_option ( 'github_oembed_key' );
78 +
114 79 if ( ! $key ) {
115 - $key = md5( time() . rand( 0, 65535 ) );
116 - add_option( 'github_oembed_key', $key, '', 'yes' );
80 + $key = md5 ( time() . rand ( 0,65535 ) );
81 + add_option ( 'github_oembed_key', $key, '', 'yes' );
117 82 }
118 83
119 84 return $key;
85 +
120 86 }
121 87
88 +
89 +
122 90 /**
123 91 * Check whether this is an oembed request, handle if it is
124 92 * Ignore it if not.
125 93 * Insert rant here about WP's lack of a front-end AJAX handler.
@@ -124,184 +92,171 @@
124 92 * Ignore it if not.
125 93 * Insert rant here about WP's lack of a front-end AJAX handler.
126 94 */
127 95 public function maybe_handle_oembed() {
128 - if ( isset( $_GET['github_oembed'] ) ) {
96 +
97 + if ( isset ( $_GET['github_oembed'] ) ) {
129 98 return $this->handle_oembed();
130 99 }
100 +
131 101 }
132 102
103 +
104 +
133 105 /**
134 106 * Handle an oembed request
135 107 */
136 108 public function handle_oembed() {
109 +
137 110 // 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.' );
111 + if ( $_GET['github_oembed'] != $this->get_key() ) {
112 + header ( 'HTTP/1.0 403 Forbidden' );
113 + die ( 'Sad Octocat is sad.' );
141 114 }
142 115
143 116 // Check we have the required information
144 - $url = isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
145 - $format = isset( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
117 + $url = isset ( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
118 + $format = isset ( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
146 119
147 - if ( ! empty( $format ) && 'json' !== $format ) {
148 - header( 'HTTP/1.0 501 Not implemented' );
149 - die( 'This octocat only does json' );
120 + if ( ! empty ( $format ) && $format != 'json' ) {
121 + header ( 'HTTP/1.0 501 Not implemented' );
122 + die ( 'This octocat only does json' );
150 123 }
151 124
152 125 if ( ! $url ) {
153 - header( 'HTTP/1.0 404 Not Found' );
154 - die( 'Octocat is lost, and afraid' );
126 + header ( 'HTTP/1.0 404 Not Found' );
127 + die ( 'Octocat is lost, and afraid' );
155 128 }
156 129
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] );
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 +
178 138 }
139 +
179 140 }
180 141
142 +
143 +
181 144 /**
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
145 + * Retrieve the information from github for a repo, and
146 + * output it as an oembed response
188 147 */
189 - private function process_template( $template, $data ) {
190 - ob_start();
191 - if ( ! locate_template( 'wp-github-oembed/' . $template, true ) ) {
192 - require_once 'templates/' . $template;
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' );
193 160 }
194 161
195 - return ob_get_clean();
196 - }
162 + $repo = json_decode ( $results['body'] );
197 163
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' );
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' ) );
210 166
211 - $response = new stdClass();
212 - $response->type = 'rich';
213 - $response->width = '10';
214 - $response->height = '10';
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';
215 180 $response->version = '1.0';
216 - $response->title = $data['repo']->description;
217 - $response->html = $this->process_template(
218 - 'repository_contributors.php', $data );
181 + $response->title = $repo->description;
219 182
220 - header( 'Content-Type: application/json' );
221 - echo json_encode( $response );
222 - die();
223 - }
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/>";
224 188
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' );
189 + if ( count ( $commits ) ) {
234 190
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 );
191 + $cnt = 0;
192 + $response->html .= 'Recent commits:';
193 + $response->html .= '<ul class="github_commits">';
243 194
244 - header( 'Content-Type: application/json' );
245 - echo json_encode( $response );
246 - die();
195 + foreach ( $commits as $commit ) {
247 196
248 - }
197 + if ($cnt > 4)
198 + break;
249 199
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' );
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++;
262 206
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 );
207 + }
271 208
209 + $response->html .= '</ul>';
272 210
273 - header( 'Content-Type: application/json' );
274 - echo json_encode( $response );
211 + }
212 + $response->html .= '</p>';
213 +
214 + header ( 'Content-Type: application/json' );
215 + echo json_encode ( $response );
275 216 die();
217 +
276 218 }
277 219
220 +
221 +
278 222 /**
279 223 * Retrieve the information from github for an author, and output
280 224 * it as an oembed response
281 225 */
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' );
226 + private function oembed_github_author ( $owner ) {
288 227
289 - $response = new stdClass();
290 - $response->type = 'rich';
291 - $response->width = '10';
292 - $response->height = '10';
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';
293 246 $response->version = '1.0';
294 - $response->title = $data['owner_info']->name;
295 - $response->html = $this->process_template(
296 - 'author.php', $data );
247 + $response->title = $owner_info->name;
297 248
298 - header( 'Content-Type: application/json' );
299 - echo json_encode( $response );
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 256 die();
257 +
301 258 }
259 +
302 260 }
303 261
304 -require_once( 'github-api.php' );
305 -
306 -$github_api = new github_api();
307 -$github_embed = new github_embed( $github_api );
262 +$github_embed = new github_embed();