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