PluginProbe
Github Embed / 1.9
Github Embed v1.9
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
github-embed / github-embed.php

github-embed.php in Github Embed 1.9, at github-embed.php

308 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Github Embed
4 Plugin URI: http://www.leewillis.co.uk/wordpress-plugins
5 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 Author: Lee Willis
8 Author URI: http://www.leewillis.co.uk/
9 */
10
11 /**
12 * Copyright (c) 2013-2020 Lee Willis. All rights reserved.
13 *
14 * Released under the GPL license v2
15 * https://www.gnu.org/licenses/gpl-2.0.en.html
16 *
17 * This is an add-on for WordPress
18 * http://wordpress.org/
19 *
20 * **********************************************************************
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 * **********************************************************************
31 */
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 */
39 class github_embed {
40
41 private $api;
42
43 /**
44 * Constructor. Registers hooks and filters
45 *
46 * @param class $api An instance of the github_api classs
47 */
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' ) );
55 // @TODO i18n
56 }
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 }
67 }
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 }
81
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
91 /**
92 * Register the oEmbed provider, and point it at a local endpoint since github
93 * doesn't directly support oEmbed yet. Our local endpoint will use the github
94 * API to fulfil the request.
95 *
96 * @param array $providers The current list of providers
97 *
98 * @return array The list, with our new provider added
99 */
100 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 );
105 }
106
107 /**
108 * Generate a unique key that can be used on our requests to stop others
109 * hijacking our internal oEmbed API
110 * @return string The site key
111 */
112 private function get_key() {
113 $key = get_option( 'github_oembed_key' );
114 if ( ! $key ) {
115 $key = md5( time() . rand( 0, 65535 ) );
116 add_option( 'github_oembed_key', $key, '', 'yes' );
117 }
118
119 return $key;
120 }
121
122 /**
123 * Check whether this is an oembed request, handle if it is
124 * Ignore it if not.
125 * Insert rant here about WP's lack of a front-end AJAX handler.
126 */
127 public function maybe_handle_oembed() {
128 if ( isset( $_GET['github_oembed'] ) ) {
129 return $this->handle_oembed();
130 }
131 }
132
133 /**
134 * Handle an oembed request
135 */
136 public function handle_oembed() {
137 // 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.' );
141 }
142
143 // Check we have the required information
144 $url = isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
145 $format = isset( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;
146
147 if ( ! empty( $format ) && 'json' !== $format ) {
148 header( 'HTTP/1.0 501 Not implemented' );
149 die( 'This octocat only does json' );
150 }
151
152 if ( ! $url ) {
153 header( 'HTTP/1.0 404 Not Found' );
154 die( 'Octocat is lost, and afraid' );
155 }
156
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] );
178 }
179 }
180
181 /**
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 ) ) {
192 require_once 'templates/' . $template;
193 }
194
195 return ob_get_clean();
196 }
197
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
211 $response = new stdClass();
212 $response->type = 'rich';
213 $response->width = '10';
214 $response->height = '10';
215 $response->version = '1.0';
216 $response->title = $data['repo']->description;
217 $response->html = $this->process_template(
218 'repository_contributors.php', $data );
219
220 header( 'Content-Type: application/json' );
221 echo json_encode( $response );
222 die();
223 }
224
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' );
234
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 );
243
244 header( 'Content-Type: application/json' );
245 echo json_encode( $response );
246 die();
247
248 }
249
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' );
262
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 );
271
272
273 header( 'Content-Type: application/json' );
274 echo json_encode( $response );
275 die();
276 }
277
278 /**
279 * Retrieve the information from github for an author, and output
280 * it as an oembed response
281 */
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' );
288
289 $response = new stdClass();
290 $response->type = 'rich';
291 $response->width = '10';
292 $response->height = '10';
293 $response->version = '1.0';
294 $response->title = $data['owner_info']->name;
295 $response->html = $this->process_template(
296 'author.php', $data );
297
298 header( 'Content-Type: application/json' );
299 echo json_encode( $response );
300 die();
301 }
302 }
303
304 require_once( 'github-api.php' );
305
306 $github_api = new github_api();
307 $github_embed = new github_embed( $github_api );
308