| @@ -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.6 | |
| 8 | -Author: Lee Willis | |
| 9 | -Author URI: http://www.leewillis.co.uk/ | |
| 6 | +Version: 2.0.2 | |
| 7 | +Author: Ademti Software | |
| 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 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 | * |
| @@ -36,9 +35,8 @@ | ||
| 36 | 35 | * we can embed, and handling the actual oEmbed calls. It relies on the github_api |
| 37 | 36 | * class to retrieve the information from the GitHub API. |
| 38 | 37 | * @uses class github_api |
| 39 | 38 | */ |
| 40 | - | |
| 41 | 39 | class github_embed { |
| 42 | 40 | |
| 43 | 41 | private $api; |
| 44 | 42 | |
| @@ -43,8 +41,9 @@ | ||
| 43 | 41 | private $api; |
| 44 | 42 | |
| 45 | 43 | /** |
| 46 | 44 | * Constructor. Registers hooks and filters |
| 45 | + * | |
| 47 | 46 | * @param class $api An instance of the github_api classs |
| 48 | 47 | */ |
| 49 | 48 | public function __construct( $api ) { |
| 50 | 49 | $this->api = $api; |
| @@ -59,9 +58,9 @@ | ||
| 59 | 58 | /** |
| 60 | 59 | * Make sure we have a scheduled event set to clear down the oEmbed cache until |
| 61 | 60 | * WordPress supports cache_age in oEmbed responses. |
| 62 | 61 | */ |
| 63 | - function schedule_expiry() { | |
| 62 | + public function schedule_expiry() { | |
| 64 | 63 | if ( ! wp_next_scheduled( 'github_embed_cron' ) ) { |
| 65 | 64 | $frequency = apply_filters( 'github_embed_cache_frequency', 'daily' ); |
| 66 | 65 | wp_schedule_event( time(), $frequency, 'github_embed_cron' ); |
| 67 | 66 | } |
| @@ -71,11 +70,11 @@ | ||
| 71 | 70 | * Expire old oEmbeds. |
| 72 | 71 | * Note: This is a bit sledgehammer-to-crack-a-nut hence why I'm only running it |
| 73 | 72 | * daily. Ideally WP should honour cache_age in oEmbed responses properly |
| 74 | 73 | */ |
| 75 | - function cron() { | |
| 74 | + public function cron() { | |
| 76 | 75 | global $wpdb, $table_prefix; |
| 77 | - $sql = "DELETE | |
| 76 | + $sql = "DELETE | |
| 78 | 77 | FROM {$table_prefix}postmeta |
| 79 | 78 | WHERE meta_key LIKE '_oembed_%'"; |
| 80 | 79 | $results = $wpdb->get_results( $sql ); |
| 81 | 80 | } |
| @@ -83,9 +82,9 @@ | ||
| 83 | 82 | /** |
| 84 | 83 | * Enqueue the frontend CSS |
| 85 | 84 | * @return void |
| 86 | 85 | */ |
| 87 | - function enqueue_styles() { | |
| 86 | + public function enqueue_styles() { | |
| 88 | 87 | wp_register_style( 'github-embed', plugins_url( basename( dirname( __FILE__ ) ) . '/css/github-embed.css' ) ); |
| 89 | 88 | wp_enqueue_style( 'github-embed' ); |
| 90 | 89 | } |
| 91 | 90 | |
| @@ -92,14 +91,16 @@ | ||
| 92 | 91 | /** |
| 93 | 92 | * Register the oEmbed provider, and point it at a local endpoint since github |
| 94 | 93 | * doesn't directly support oEmbed yet. Our local endpoint will use the github |
| 95 | 94 | * API to fulfil the request. |
| 96 | - * @param array $providers The current list of providers | |
| 95 | + * | |
| 96 | + * @param array $providers The current list of providers | |
| 97 | + * | |
| 97 | 98 | * @return array The list, with our new provider added |
| 98 | 99 | */ |
| 99 | 100 | public function register_oembed_handler() { |
| 100 | 101 | $oembed_url = home_url(); |
| 101 | - $key = $this->get_key(); | |
| 102 | + $key = $this->get_key(); | |
| 102 | 103 | $oembed_url = add_query_arg( array( 'github_oembed' => $key ), $oembed_url ); |
| 103 | 104 | wp_oembed_add_provider( '#https?://github.com/.*#i', $oembed_url, true ); |
| 104 | 105 | } |
| 105 | 106 | |
| @@ -113,8 +114,9 @@ | ||
| 113 | 114 | if ( ! $key ) { |
| 114 | 115 | $key = md5( time() . rand( 0, 65535 ) ); |
| 115 | 116 | add_option( 'github_oembed_key', $key, '', 'yes' ); |
| 116 | 117 | } |
| 118 | + | |
| 117 | 119 | return $key; |
| 118 | 120 | } |
| 119 | 121 | |
| 120 | 122 | /** |
| @@ -138,9 +140,9 @@ | ||
| 138 | 140 | die( 'Sad Octocat is sad.' ); |
| 139 | 141 | } |
| 140 | 142 | |
| 141 | 143 | // Check we have the required information |
| 142 | - $url = isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : null; | |
| 144 | + $url = isset( $_REQUEST['url'] ) ? $_REQUEST['url'] : null; | |
| 143 | 145 | $format = isset( $_REQUEST['format'] ) ? $_REQUEST['format'] : null; |
| 144 | 146 | |
| 145 | 147 | if ( ! empty( $format ) && 'json' !== $format ) { |
| 146 | 148 | header( 'HTTP/1.0 501 Not implemented' ); |
| @@ -176,42 +178,46 @@ | ||
| 176 | 178 | } |
| 177 | 179 | } |
| 178 | 180 | |
| 179 | 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 | + /** | |
| 180 | 199 | * Retrieve a list of contributors for a project |
| 181 | - * @param string $owner The owner of the repository | |
| 182 | - * @param string $repository The repository name | |
| 200 | + * | |
| 201 | + * @param string $owner The owner of the repository | |
| 202 | + * @param string $repository The repository name | |
| 183 | 203 | */ |
| 184 | 204 | private function oembed_github_repo_contributors( $owner, $repository ) { |
| 185 | - $repo = $this->api->get_repo( $owner, $repository ); | |
| 186 | - $contributors = $this->api->get_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' ); | |
| 187 | 210 | |
| 188 | - $response = new stdClass(); | |
| 189 | - $response->type = 'rich'; | |
| 190 | - $response->width = '10'; | |
| 191 | - $response->height = '10'; | |
| 211 | + $response = new stdClass(); | |
| 212 | + $response->type = 'rich'; | |
| 213 | + $response->width = '10'; | |
| 214 | + $response->height = '10'; | |
| 192 | 215 | $response->version = '1.0'; |
| 193 | - $response->title = $repo->description; | |
| 216 | + $response->title = $data['repo']->description; | |
| 217 | + $response->html = $this->process_template( | |
| 218 | + 'repository_contributors.php', $data ); | |
| 194 | 219 | |
| 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>'; | |
| 214 | 220 | header( 'Content-Type: application/json' ); |
| 215 | 221 | echo json_encode( $response ); |
| 216 | 222 | die(); |
| 217 | 223 | } |
| @@ -220,39 +226,22 @@ | ||
| 220 | 226 | * Retrieve the summary information for a repo's milestone, and |
| 221 | 227 | * output it as an oembed response |
| 222 | 228 | */ |
| 223 | 229 | private function oembed_github_repo_milestone_summary( $owner, $repository, $milestone ) { |
| 224 | - $repo = $this->api->get_repo( $owner, $repository ); | |
| 225 | - $summary = $this->api->get_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' ); | |
| 226 | 234 | |
| 227 | - $response = new stdClass(); | |
| 228 | - $response->type = 'rich'; | |
| 229 | - $response->width = '10'; | |
| 230 | - $response->height = '10'; | |
| 235 | + $response = new stdClass(); | |
| 236 | + $response->type = 'rich'; | |
| 237 | + $response->width = '10'; | |
| 238 | + $response->height = '10'; | |
| 231 | 239 | $response->version = '1.0'; |
| 232 | - $response->title = $repo->description; | |
| 240 | + $response->title = $data['repo']->description; | |
| 241 | + $response->html = $this->process_template( | |
| 242 | + 'repository_milestone_summary.php', $data ); | |
| 233 | 243 | |
| 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 | - | |
| 255 | 244 | header( 'Content-Type: application/json' ); |
| 256 | 245 | echo json_encode( $response ); |
| 257 | 246 | die(); |
| 258 | 247 | |
| @@ -261,45 +250,27 @@ | ||
| 261 | 250 | /** |
| 262 | 251 | * Retrieve the information from github for a repo, and |
| 263 | 252 | * output it as an oembed response |
| 264 | 253 | */ |
| 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 ); | |
| 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' ); | |
| 268 | 262 | |
| 269 | - $response = new stdClass(); | |
| 270 | - $response->type = 'rich'; | |
| 271 | - $response->width = '10'; | |
| 272 | - $response->height = '10'; | |
| 263 | + $response = new stdClass(); | |
| 264 | + $response->type = 'rich'; | |
| 265 | + $response->width = '10'; | |
| 266 | + $response->height = '10'; | |
| 273 | 267 | $response->version = '1.0'; |
| 274 | - $response->title = $repo->description; | |
| 268 | + $response->title = $data['repo']->description; | |
| 269 | + $response->html = $this->process_template( | |
| 270 | + 'repository.php', $data ); | |
| 275 | 271 | |
| 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/>'; | |
| 283 | 272 | |
| 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>'; | |
| 302 | 273 | header( 'Content-Type: application/json' ); |
| 303 | 274 | echo json_encode( $response ); |
| 304 | 275 | die(); |
| 305 | 276 | } |
| @@ -307,25 +278,24 @@ | ||
| 307 | 278 | /** |
| 308 | 279 | * Retrieve the information from github for an author, and output |
| 309 | 280 | * it as an oembed response |
| 310 | 281 | */ |
| 311 | - private function oembed_github_author ( $owner ) { | |
| 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' ); | |
| 312 | 288 | |
| 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'; | |
| 289 | + $response = new stdClass(); | |
| 290 | + $response->type = 'rich'; | |
| 291 | + $response->width = '10'; | |
| 292 | + $response->height = '10'; | |
| 319 | 293 | $response->version = '1.0'; |
| 320 | - $response->title = $owner_info->name; | |
| 294 | + $response->title = $data['owner_info']->name; | |
| 295 | + $response->html = $this->process_template( | |
| 296 | + 'author.php', $data ); | |
| 321 | 297 | |
| 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>'; | |
| 328 | 298 | header( 'Content-Type: application/json' ); |
| 329 | 299 | echo json_encode( $response ); |
| 330 | 300 | die(); |
| 331 | 301 | } |
| @@ -332,6 +302,6 @@ | ||
| 332 | 302 | } |
| 333 | 303 | |
| 334 | 304 | require_once( 'github-api.php' ); |
| 335 | 305 | |
| 336 | -$github_api = new github_api(); | |
| 306 | +$github_api = new github_api(); | |
| 337 | 307 | $github_embed = new github_embed( $github_api ); |