PluginProbe
Github Embed / 1.6
Github Embed v1.6
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 +120 -92 2.2.01.6 View file →
@@ -1,19 +1,20 @@
1 1 <?php
2 +
2 3 /*
3 4 Plugin Name: Github Embed
4 -Plugin URI: https://wordpress.org/plugins/github-embed/
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.2.0
7 -Author: Ademti Software Ltd.
8 -Author URI: https://www.ademti-software.co.uk/
7 +Version: 1.6
8 +Author: Lee Willis
9 +Author URI: http://www.leewillis.co.uk/
9 10 */
10 11
11 12 /**
12 - * Copyright (c) 2013-2025 Ademti Software. 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 *
@@ -35,8 +36,9 @@
35 36 * we can embed, and handling the actual oEmbed calls. It relies on the github_api
36 37 * class to retrieve the information from the GitHub API.
37 38 * @uses class github_api
38 39 */
40 +
39 41 class github_embed {
40 42
41 43 private $api;
42 44
@@ -41,9 +43,8 @@
41 43 private $api;
42 44
43 45 /**
44 46 * Constructor. Registers hooks and filters
45 - *
46 47 * @param class $api An instance of the github_api classs
47 48 */
48 49 public function __construct( $api ) {
49 50 $this->api = $api;
@@ -58,9 +59,9 @@
58 59 /**
59 60 * Make sure we have a scheduled event set to clear down the oEmbed cache until
60 61 * WordPress supports cache_age in oEmbed responses.
61 62 */
62 - public function schedule_expiry() {
63 + function schedule_expiry() {
63 64 if ( ! wp_next_scheduled( 'github_embed_cron' ) ) {
64 65 $frequency = apply_filters( 'github_embed_cache_frequency', 'daily' );
65 66 wp_schedule_event( time(), $frequency, 'github_embed_cron' );
66 67 }
@@ -70,11 +71,11 @@
70 71 * Expire old oEmbeds.
71 72 * Note: This is a bit sledgehammer-to-crack-a-nut hence why I'm only running it
72 73 * daily. Ideally WP should honour cache_age in oEmbed responses properly
73 74 */
74 - public function cron() {
75 + function cron() {
75 76 global $wpdb, $table_prefix;
76 - $sql = "DELETE
77 + $sql = "DELETE
77 78 FROM {$table_prefix}postmeta
78 79 WHERE meta_key LIKE '_oembed_%'";
79 80 $results = $wpdb->get_results( $sql );
80 81 }
@@ -82,9 +83,9 @@
82 83 /**
83 84 * Enqueue the frontend CSS
84 85 * @return void
85 86 */
86 - public function enqueue_styles() {
87 + function enqueue_styles() {
87 88 wp_register_style( 'github-embed', plugins_url( basename( dirname( __FILE__ ) ) . '/css/github-embed.css' ) );
88 89 wp_enqueue_style( 'github-embed' );
89 90 }
90 91
@@ -91,16 +92,14 @@
91 92 /**
92 93 * Register the oEmbed provider, and point it at a local endpoint since github
93 94 * doesn't directly support oEmbed yet. Our local endpoint will use the github
94 95 * API to fulfil the request.
95 - *
96 - * @param array $providers The current list of providers
97 - *
96 + * @param array $providers The current list of providers
98 97 * @return array The list, with our new provider added
99 98 */
100 99 public function register_oembed_handler() {
101 100 $oembed_url = home_url();
102 - $key = $this->get_key();
101 + $key = $this->get_key();
103 102 $oembed_url = add_query_arg( array( 'github_oembed' => $key ), $oembed_url );
104 103 wp_oembed_add_provider( '#https?://github.com/.*#i', $oembed_url, true );
105 104 }
106 105
@@ -114,9 +113,8 @@
114 113 if ( ! $key ) {
115 114 $key = md5( time() . rand( 0, 65535 ) );
116 115 add_option( 'github_oembed_key', $key, '', 'yes' );
117 116 }
118 -
119 117 return $key;
120 118 }
121 119
122 120 /**
@@ -140,9 +138,9 @@
140 138 die( 'Sad Octocat is sad.' );
141 139 }
142 140
143 141 // Check we have the required information
144 - $url = isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
142 + $url = isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
145 143 $format = isset( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
146 144
147 145 if ( ! empty( $format ) && 'json' !== $format ) {
148 146 header( 'HTTP/1.0 501 Not implemented' );
@@ -178,47 +176,42 @@
178 176 }
179 177 }
180 178
181 179 /**
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
188 - */
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;
193 - }
194 -
195 - return ob_get_clean();
196 - }
197 -
198 - /**
199 180 * 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
181 + * @param string $owner The owner of the repository
182 + * @param string $repository The repository name
203 183 */
204 184 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 );
185 + $repo = $this->api->get_repo( $owner, $repository );
186 + $contributors = $this->api->get_repo_contributors( $owner, $repository );
211 187
212 - $response = new stdClass();
213 - $response->type = 'rich';
214 - $response->width = '10';
215 - $response->height = '10';
188 + $response = new stdClass();
189 + $response->type = 'rich';
190 + $response->width = '10';
191 + $response->height = '10';
216 192 $response->version = '1.0';
217 - $response->title = $data['repo']->description;
218 - $response->html = $this->process_template(
219 - 'repository_contributors.php', $data );
193 + $response->title = $repo->description;
220 194
195 + $gravatar_size = apply_filters( 'github_oembed_gravatar_size', 64 );
196 +
197 + // @TODO This should all be templated
198 + $response->html = '<div class="github-embed github-embed-repo-contributors">';
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 + }
211 + $response->html .= '</ul>';
212 + $response->html .= '<div style="clear: both;"></div>';
213 + $response->html .= '</div>';
221 214 header( 'Content-Type: application/json' );
222 215 echo json_encode( $response );
223 216 die();
224 217 }
@@ -227,22 +220,39 @@
227 220 * Retrieve the summary information for a repo's milestone, and
228 221 * output it as an oembed response
229 222 */
230 223 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' );
224 + $repo = $this->api->get_repo( $owner, $repository );
225 + $summary = $this->api->get_repo_milestone_summary( $owner, $repository, $milestone );
235 226
236 - $response = new stdClass();
237 - $response->type = 'rich';
238 - $response->width = '10';
239 - $response->height = '10';
227 + $response = new stdClass();
228 + $response->type = 'rich';
229 + $response->width = '10';
230 + $response->height = '10';
240 231 $response->version = '1.0';
241 - $response->title = $data['repo']->description;
242 - $response->html = $this->process_template(
243 - 'repository_milestone_summary.php', $data );
232 + $response->title = $repo->description;
244 233
234 + // @TODO This should all be templated
235 + $response->html = '<div class="github-embed github-embed-milestone-summary">';
236 + $response->html .= '<p><a href="' . esc_attr( $repo->html_url ) . '" target="_blank"><strong>' . esc_html( $repo->description ) . '</strong></a><br/>';
237 +
238 + $response->html .= '<span class="github-heading">Milestone: </span>';
239 + $response->html .= '<span class="github-milestone-title">' . esc_html( $summary->title ) . '</span><br>';
240 +
241 + $response->html .= '<span class="github-heading">Issues: </span>';
242 + $response->html .= '<span class="github-milestone-issues">';
243 + $response->html .= esc_html( number_format_i18n( $summary->open_issues ) ) . ' open, ';
244 + $response->html .= esc_html( number_format_i18n( $summary->closed_issues ) ) . ' closed.</span><br>';
245 +
246 + if ( ! empty( $summary->due_on ) ) {
247 + $response->html .= '<span class="github-heading">Due: </span>';
248 + $due_date = date_format( date_create( $summary->due_on ), 'jS F Y' );
249 + $response->html .= '<span class="github-milestone-due-date">' . esc_html( $due_date ) . '</span><br>';
250 + }
251 +
252 + $response->html .= '<p class="github-milestone-description">' . nl2br( esc_html( $summary->description ) ) . '</p><br>';
253 + $response->html .= '</div>';
254 +
245 255 header( 'Content-Type: application/json' );
246 256 echo json_encode( $response );
247 257 die();
248 258
@@ -251,28 +261,45 @@
251 261 /**
252 262 * Retrieve the information from github for a repo, and
253 263 * output it as an oembed response
254 264 */
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 );
265 + private function oembed_github_repo ( $owner, $repository ) {
266 + $repo = $this->api->get_repo( $owner, $repository );
267 + $commits =$this->api->get_repo_commits( $owner, $repository );
264 268
265 - $response = new stdClass();
266 - $response->type = 'rich';
267 - $response->width = '10';
268 - $response->height = '10';
269 + $response = new stdClass();
270 + $response->type = 'rich';
271 + $response->width = '10';
272 + $response->height = '10';
269 273 $response->version = '1.0';
270 - $response->title = $data['repo']->description;
271 - $response->html = $this->process_template(
272 - 'repository.php', $data );
274 + $response->title = $repo->description;
273 275
276 + // @TODO This should all be templated
277 + $response->html = '<div class="github-embed github-embed-repository">';
278 + $response->html .= '<p><a href="' . esc_attr( $repo->html_url ) . '" target="_blank"><strong>' . esc_html( $repo->description ) . '</strong></a><br/>';
279 + $response->html .= '<a href="' . esc_attr( $repo->html_url ) . '" target="_blank">' . esc_html( $repo->html_url ) . '</a><br/>';
280 + $response->html .= '<a href="' . esc_attr( $repo->html_url . '/network' ) . '" target="_blank">' . esc_html( number_format_i18n( $repo->forks_count ) ) . '</a> forks.<br/>';
281 + $response->html .= '<a href="' . esc_attr( $repo->html_url . '/stargazers' ) . '" target="_blank">' . esc_html( number_format_i18n( $repo->stargazers_count ) ) . '</a> stars.<br/>';
282 + $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/>';
274 283
284 + if ( count( $commits ) ) {
285 + $cnt = 0;
286 + $response->html .= 'Recent commits:';
287 + $response->html .= '<ul class="github_commits">';
288 + foreach ( $commits as $commit ) {
289 + if ( $cnt > 4 ) {
290 + break;
291 + }
292 + $response->html .= '<li class="github_commit">';
293 + $response->html .= '<a href="https://github.com/' . $owner . '/' . $repository . '/commit/' . esc_attr( $commit->sha ) . '" target="_blank">' . esc_html( $commit->commit->message ) . '</a>, ';
294 + $response->html .= esc_html( $commit->commit->committer->name );
295 + $response->html .= '</li>';
296 + $cnt++;
297 + }
298 + $response->html .= '</ul>';
299 + }
300 + $response->html .= '</p>';
301 + $response->html .= '</div>';
275 302 header( 'Content-Type: application/json' );
276 303 echo json_encode( $response );
277 304 die();
278 305 }
@@ -280,24 +307,25 @@
280 307 /**
281 308 * Retrieve the information from github for an author, and output
282 309 * it as an oembed response
283 310 */
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' );
311 + private function oembed_github_author ( $owner ) {
290 312
291 - $response = new stdClass();
292 - $response->type = 'rich';
293 - $response->width = '10';
294 - $response->height = '10';
313 + $owner_info = $this->api->get_user( $owner );
314 +
315 + $response = new stdClass();
316 + $response->type = 'rich';
317 + $response->width = '10';
318 + $response->height = '10';
295 319 $response->version = '1.0';
296 - $response->title = $data['owner_info']->name;
297 - $response->html = $this->process_template(
298 - 'author.php', $data );
320 + $response->title = $owner_info->name;
299 321
322 + // @TODO This should all be templated
323 + $response->html = '<div class="github-embed github-embed-user">';
324 + $response->html .= '<p><a href="https://github.com/' . esc_attr( $owner ) . '" target="_blank"><strong>' . esc_html( $owner ) . '</strong></a><br/>';
325 + $response->html .= esc_html( number_format_i18n( $owner_info->public_repos ) ) . ' repositories, ';
326 + $response->html .= esc_html( number_format_i18n( $owner_info->followers ) ) . ' followers.</p>';
327 + $response->html .= '</div>';
300 328 header( 'Content-Type: application/json' );
301 329 echo json_encode( $response );
302 330 die();
303 331 }
@@ -304,6 +332,6 @@
304 332 }
305 333
306 334 require_once( 'github-api.php' );
307 335
308 -$github_api = new github_api();
336 +$github_api = new github_api();
309 337 $github_embed = new github_embed( $github_api );