| @@ -3,9 +3,9 @@ | ||
| 3 | 3 | /* |
| 4 | 4 | Plugin Name: Github Embed |
| 5 | 5 | Plugin URI: http://www.leewillis.co.uk/wordpress-plugins |
| 6 | 6 | 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.1 | |
| 7 | +Version: 1.5 | |
| 8 | 8 | Author: Lee Willis |
| 9 | 9 | Author URI: http://www.leewillis.co.uk/ |
| 10 | 10 | */ |
| 11 | 11 | |
| @@ -30,21 +30,36 @@ | ||
| 30 | 30 | * GNU General Public License for more details. |
| 31 | 31 | * ********************************************************************** |
| 32 | 32 | */ |
| 33 | 33 | |
| 34 | +/** | |
| 35 | + * This class handles being the oEmbed provider in terms of registering the URLs that | |
| 36 | + * we can embed, and handling the actual oEmbed calls. It relies on the github_api | |
| 37 | + * class to retrieve the information from the GitHub API. | |
| 38 | + * @uses class github_api | |
| 39 | + */ | |
| 34 | 40 | class github_embed { |
| 35 | 41 | |
| 36 | 42 | |
| 37 | 43 | |
| 44 | + private $api; | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 38 | 48 | /** |
| 39 | 49 | * Constructor. Registers hooks and filters |
| 50 | + * @param class $api An instance of the github_api classs | |
| 40 | 51 | */ |
| 41 | - public function __construct() { | |
| 52 | + public function __construct ( $api ) { | |
| 42 | 53 | |
| 43 | - add_action ( 'init', array ( $this, 'register_oembed_handler' ) ); | |
| 44 | - add_action ( 'init', array ( $this, 'maybe_handle_oembed' ) ); | |
| 45 | - add_action ( 'wp_enqueue_scripts', array ( $this, 'enqueue_styles' ) ); | |
| 46 | - | |
| 54 | + $this->api = $api; | |
| 55 | + | |
| 56 | + add_action( 'init', array ( $this, 'register_oembed_handler' ) ); | |
| 57 | + add_action( 'init', array( $this, 'maybe_handle_oembed' ) ); | |
| 58 | + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); | |
| 59 | + add_action( 'admin_init', array( $this, 'schedule_expiry' ) ); | |
| 60 | + add_action( 'github_embed_cron', array( $this, 'cron' ) ); | |
| 61 | + | |
| 47 | 62 | // @TODO i18n |
| 48 | 63 | |
| 49 | 64 | } |
| 50 | 65 | |
| @@ -50,8 +65,42 @@ | ||
| 50 | 65 | |
| 51 | 66 | |
| 52 | 67 | |
| 53 | 68 | /** |
| 69 | + * Make sure we have a scheduled event set to clear down the oEmbed cache until | |
| 70 | + * WordPress supports cache_age in oEmbed responses. | |
| 71 | + */ | |
| 72 | + function schedule_expiry() { | |
| 73 | + | |
| 74 | + if( ! wp_next_scheduled( 'github_embed_cron' ) ) { | |
| 75 | + $frequency = apply_filters ( 'github_embed_cache_frequency', 'daily' ); | |
| 76 | + wp_schedule_event( time(), $frequency, 'github_embed_cron' ); | |
| 77 | + } | |
| 78 | + | |
| 79 | + } | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Expire old oEmbeds. | |
| 85 | + * Note: This is a bit sledgehammer-to-crack-a-nut hence why I'm only running it | |
| 86 | + * daily. Ideally WP should honour cache_age in oEmbed responses properly | |
| 87 | + */ | |
| 88 | + function cron() { | |
| 89 | + | |
| 90 | + global $wpdb, $table_prefix; | |
| 91 | + | |
| 92 | + $sql = "DELETE | |
| 93 | + FROM {$table_prefix}postmeta | |
| 94 | + WHERE meta_key LIKE '_oembed_%'"; | |
| 95 | + | |
| 96 | + $results = $wpdb->get_results ( $sql ); | |
| 97 | + | |
| 98 | + } | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + /** | |
| 54 | 103 | * Enqueue the frontend CSS |
| 55 | 104 | * @return void |
| 56 | 105 | */ |
| 57 | 106 | function enqueue_styles() { |
| @@ -56,10 +105,10 @@ | ||
| 56 | 105 | */ |
| 57 | 106 | function enqueue_styles() { |
| 58 | 107 | |
| 59 | 108 | wp_register_style ( 'github-embed', plugins_url(basename(dirname(__FILE__)).'/css/github-embed.css' ) ); |
| 60 | - wp_enqueue_style ( 'github-embed' ); | |
| 61 | - | |
| 109 | + wp_enqueue_style ( 'github-embed' ); | |
| 110 | + | |
| 62 | 111 | } |
| 63 | 112 | |
| 64 | 113 | |
| 65 | 114 | |
| @@ -71,12 +120,12 @@ | ||
| 71 | 120 | * @return array The list, with our new provider added |
| 72 | 121 | */ |
| 73 | 122 | public function register_oembed_handler() { |
| 74 | 123 | |
| 75 | - $oembed_url = home_url (); | |
| 124 | + $oembed_url = home_url(); | |
| 76 | 125 | $key = $this->get_key(); |
| 77 | - $oembed_url = add_query_arg ( array ( 'github_oembed' => $key ), $oembed_url); | |
| 78 | - wp_oembed_add_provider ( '#https?://github.com/.*#i', $oembed_url, true ); | |
| 126 | + $oembed_url = add_query_arg( array( 'github_oembed' => $key ), $oembed_url ); | |
| 127 | + wp_oembed_add_provider( '#https?://github.com/.*#i', $oembed_url, true ); | |
| 79 | 128 | |
| 80 | 129 | } |
| 81 | 130 | |
| 82 | 131 | |
| @@ -88,9 +137,9 @@ | ||
| 88 | 137 | */ |
| 89 | 138 | private function get_key() { |
| 90 | 139 | |
| 91 | 140 | $key = get_option ( 'github_oembed_key' ); |
| 92 | - | |
| 141 | + | |
| 93 | 142 | if ( ! $key ) { |
| 94 | 143 | $key = md5 ( time() . rand ( 0,65535 ) ); |
| 95 | 144 | add_option ( 'github_oembed_key', $key, '', 'yes' ); |
| 96 | 145 | } |
| @@ -122,9 +171,9 @@ | ||
| 122 | 171 | public function handle_oembed() { |
| 123 | 172 | |
| 124 | 173 | // Check this request is valid |
| 125 | 174 | if ( $_GET['github_oembed'] != $this->get_key() ) { |
| 126 | - header ( 'HTTP/1.0 403 Forbidden' ); | |
| 175 | + header ( 'HTTP/1.0 403 Forbidden' ); | |
| 127 | 176 | die ( 'Sad Octocat is sad.' ); |
| 128 | 177 | } |
| 129 | 178 | |
| 130 | 179 | // Check we have the required information |
| @@ -140,12 +189,36 @@ | ||
| 140 | 189 | header ( 'HTTP/1.0 404 Not Found' ); |
| 141 | 190 | die ( 'Octocat is lost, and afraid' ); |
| 142 | 191 | } |
| 143 | 192 | |
| 144 | - if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { | |
| 193 | + // Issues / Milestones | |
| 194 | + if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/graphs/contributors/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { | |
| 145 | 195 | |
| 196 | + $this->oembed_github_repo_contributors ( $matches[1], $matches[2] ); | |
| 197 | + | |
| 198 | + } elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/issues.*$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { | |
| 199 | + | |
| 200 | + if ( preg_match ( '#issues.?milestone=([0-9]*)#i', $url, $milestones ) ) { | |
| 201 | + $milestone = $milestones[1]; | |
| 202 | + } else { | |
| 203 | + $milestone = null; | |
| 204 | + } | |
| 205 | + | |
| 206 | + if ( $milestone ) { | |
| 207 | + $this->oembed_github_repo_milestone_summary ( $matches[1], $matches[2], $milestone ); | |
| 208 | + } | |
| 209 | + | |
| 210 | + // New style milestone URL, e.g. https://github.com/example/example/milestone/1 | |
| 211 | + } elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/milestone/([0-9]*)$#i', $url, $matches ) ) { | |
| 212 | + | |
| 213 | + $this->oembed_github_repo_milestone_summary( $matches[1], $matches[2], $matches[3] ); | |
| 214 | + | |
| 215 | + // Repository | |
| 216 | + } elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { | |
| 217 | + | |
| 146 | 218 | $this->oembed_github_repo ( $matches[1], $matches[2] ); |
| 147 | 219 | |
| 220 | + // User | |
| 148 | 221 | } elseif ( preg_match ( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) { |
| 149 | 222 | |
| 150 | 223 | $this->oembed_github_author ( $matches[1] ); |
| 151 | 224 | |
| @@ -155,39 +228,111 @@ | ||
| 155 | 228 | |
| 156 | 229 | |
| 157 | 230 | |
| 158 | 231 | /** |
| 159 | - * Retrieve the information from github for a repo, and | |
| 232 | + * Retrieve a list of contributors for a project | |
| 233 | + * @param string $owner The owner of the repository | |
| 234 | + * @param string $repository The repository name | |
| 235 | + */ | |
| 236 | + private function oembed_github_repo_contributors ( $owner, $repository ) { | |
| 237 | + | |
| 238 | + $repo = $this->api->get_repo ( $owner, $repository ); | |
| 239 | + $contributors = $this->api->get_repo_contributors ( $owner, $repository ); | |
| 240 | + | |
| 241 | + $response = new stdClass(); | |
| 242 | + $response->type = 'rich'; | |
| 243 | + $response->width = '10'; | |
| 244 | + $response->height = '10'; | |
| 245 | + $response->version = '1.0'; | |
| 246 | + $response->title = $repo->description; | |
| 247 | + | |
| 248 | + $gravatar_size = apply_filters ( 'github_oembed_gravatar_size', 64 ); | |
| 249 | + | |
| 250 | + // @TODO This should all be templated | |
| 251 | + $response->html = '<div class="github-embed github-embed-repo-contributors">'; | |
| 252 | + $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; | |
| 253 | + | |
| 254 | + $response->html .= '<span class="github-heading">Contributors: </span>'; | |
| 255 | + | |
| 256 | + $response->html .= '<ul class="github-repo-contributors">'; | |
| 257 | + | |
| 258 | + foreach ( $contributors as $contributor ) { | |
| 259 | + | |
| 260 | + $response->html .= '<li class="github-repo-contributor">'; | |
| 261 | + $response->html .= '<img class="github-repo-contributor-avatar" src="'; | |
| 262 | + $response->html .= esc_url(add_query_arg(array('s'=>$gravatar_size), $contributor->author->avatar_url)); | |
| 263 | + $response->html .= '" alt="Picture of '.esc_attr($contributor->author->login).'">'; | |
| 264 | + $response->html .= '<span class="github-repo-contributor-login">'; | |
| 265 | + $response->html .= '<a href="https://github.com/'.esc_attr($contributor->author->login).'">'.esc_attr($contributor->author->login).'</a></span>'; | |
| 266 | + } | |
| 267 | + | |
| 268 | + $response->html .= '</ul>'; | |
| 269 | + $response->html .= '<div style="clear: both;"></div>'; | |
| 270 | + $response->html .= '</div>'; | |
| 271 | + | |
| 272 | + header ( 'Content-Type: application/json' ); | |
| 273 | + echo json_encode ( $response ); | |
| 274 | + die(); | |
| 275 | + | |
| 276 | + } | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + /** | |
| 281 | + * Retrieve the summary information for a repo's milestone, and | |
| 160 | 282 | * output it as an oembed response |
| 161 | 283 | */ |
| 162 | - private function oembed_github_repo ( $owner, $repository ) { | |
| 284 | + private function oembed_github_repo_milestone_summary ( $owner, $repository, $milestone ) { | |
| 163 | 285 | |
| 164 | - $repository = trim ( $repository, '/' ); | |
| 286 | + $repo = $this->api->get_repo ( $owner, $repository ); | |
| 287 | + $summary = $this->api->get_repo_milestone_summary ( $owner, $repository, $milestone ); | |
| 165 | 288 | |
| 166 | - $results = wp_remote_get( "https://api.github.com/repos/$owner/$repository", $args = array ( | |
| 167 | - 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) ); | |
| 289 | + $response = new stdClass(); | |
| 290 | + $response->type = 'rich'; | |
| 291 | + $response->width = '10'; | |
| 292 | + $response->height = '10'; | |
| 293 | + $response->version = '1.0'; | |
| 294 | + $response->title = $repo->description; | |
| 168 | 295 | |
| 169 | - if ( is_wp_error( $results ) || | |
| 170 | - ! isset ( $results['response']['code'] ) || | |
| 171 | - $results['response']['code'] != '200' ) { | |
| 172 | - header ( 'HTTP/1.0 404 Not Found' ); | |
| 173 | - die ( 'Octocat is lost, and afraid' ); | |
| 296 | + // @TODO This should all be templated | |
| 297 | + $response->html = '<div class="github-embed github-embed-milestone-summary">'; | |
| 298 | + $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; | |
| 299 | + | |
| 300 | + $response->html .= '<span class="github-heading">Milestone: </span>'; | |
| 301 | + $response->html .= '<span class="github-milestone-title">'.esc_html($summary->title)."</span><br>"; | |
| 302 | + | |
| 303 | + $response->html .= '<span class="github-heading">Issues: </span>'; | |
| 304 | + $response->html .= '<span class="github-milestone-issues">'; | |
| 305 | + $response->html .= esc_html ( number_format_i18n ( $summary->open_issues ) )." open, "; | |
| 306 | + $response->html .= esc_html ( number_format_i18n ( $summary->closed_issues ) )." closed.</span><br>"; | |
| 307 | + | |
| 308 | + if ( ! empty ( $summary->due_on ) ) { | |
| 309 | + $response->html .= '<span class="github-heading">Due: </span>'; | |
| 310 | + $due_date = date_format ( date_create ( $summary->due_on ), 'jS F Y' ); | |
| 311 | + $response->html .= '<span class="github-milestone-due-date">'.esc_html($due_date).'</span><br>'; | |
| 174 | 312 | } |
| 175 | 313 | |
| 176 | - $repo = json_decode ( $results['body'] ); | |
| 314 | + $response->html .= '<p class="github-milestone-description">'.nl2br(esc_html($summary->description))."</p><br>"; | |
| 315 | + $response->html .= '</div>'; | |
| 177 | 316 | |
| 178 | - $results = wp_remote_get( "https://api.github.com/repos/$owner/$repository/commits", $args = array ( | |
| 179 | - 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) ); | |
| 317 | + error_log( basename( __FILE__ ) . ': (' . __LINE__ . ') : response is ' . print_r( $response, 1 ) ); | |
| 318 | + header ( 'Content-Type: application/json' ); | |
| 319 | + echo json_encode ( $response ); | |
| 320 | + die(); | |
| 180 | 321 | |
| 181 | - if ( is_wp_error( $results ) || | |
| 182 | - ! isset ( $results['response']['code'] ) || | |
| 183 | - $results['response']['code'] != '200' ) { | |
| 184 | - header ( 'HTTP/1.0 404 Not Found' ); | |
| 185 | - die ( 'Octocat is lost, and afraid' ); | |
| 186 | - } | |
| 322 | + } | |
| 187 | 323 | |
| 188 | - $commits = json_decode ( $results['body'] ); | |
| 189 | 324 | |
| 325 | + | |
| 326 | + /** | |
| 327 | + * Retrieve the information from github for a repo, and | |
| 328 | + * output it as an oembed response | |
| 329 | + */ | |
| 330 | + private function oembed_github_repo ( $owner, $repository ) { | |
| 331 | + | |
| 332 | + $repo = $this->api->get_repo ( $owner, $repository ); | |
| 333 | + $commits =$this->api->get_repo_commits ( $owner, $repository ); | |
| 334 | + | |
| 190 | 335 | $response = new stdClass(); |
| 191 | 336 | $response->type = 'rich'; |
| 192 | 337 | $response->width = '10'; |
| 193 | 338 | $response->height = '10'; |
| @@ -197,10 +342,11 @@ | ||
| 197 | 342 | // @TODO This should all be templated |
| 198 | 343 | $response->html = '<div class="github-embed github-embed-repository">'; |
| 199 | 344 | $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; |
| 200 | 345 | $response->html .= '<a href="'.esc_attr($repo->html_url).'" target="_blank">'.esc_html($repo->html_url)."</a><br/>"; |
| 201 | - $response->html .= esc_html($repo->forks_count)." forks.<br/>"; | |
| 202 | - $response->html .= esc_html($repo->open_issues_count)." open issues.<br/>"; | |
| 346 | + $response->html .= '<a href="'.esc_attr($repo->html_url . '/network') . '" target="_blank">'.esc_html ( number_format_i18n ( $repo->forks_count ) ) . "</a> forks.<br/>"; | |
| 347 | + $response->html .= '<a href="'.esc_attr($repo->html_url . '/stargazers') . '" target="_blank">'.esc_html ( number_format_i18n ( $repo->stargazers_count ) ) . "</a> stars.<br/>"; | |
| 348 | + $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/>"; | |
| 203 | 349 | |
| 204 | 350 | if ( count ( $commits ) ) { |
| 205 | 351 | |
| 206 | 352 | $cnt = 0; |
| @@ -215,9 +361,9 @@ | ||
| 215 | 361 | $response->html .= '<li class="github_commit">'; |
| 216 | 362 | $response->html .= '<a href="https://github.com/'.$owner.'/'.$repository.'/commit/'.esc_attr($commit->sha).'" target="_blank">'.esc_html($commit->commit->message)."</a>, "; |
| 217 | 363 | $response->html .= esc_html($commit->commit->committer->name); |
| 218 | 364 | $response->html .= '</li>'; |
| 219 | - | |
| 365 | + | |
| 220 | 366 | $cnt++; |
| 221 | 367 | |
| 222 | 368 | } |
| 223 | 369 | |
| @@ -240,22 +386,10 @@ | ||
| 240 | 386 | * it as an oembed response |
| 241 | 387 | */ |
| 242 | 388 | private function oembed_github_author ( $owner ) { |
| 243 | 389 | |
| 244 | - $owner = trim ( $owner, '/' ); | |
| 390 | + $owner_info = $this->api->get_user ( $owner ); | |
| 245 | 391 | |
| 246 | - $results = wp_remote_get( "https://api.github.com/users/$owner", $args = array ( | |
| 247 | - 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) ); | |
| 248 | - | |
| 249 | - if ( is_wp_error( $results ) || | |
| 250 | - ! isset ( $results['response']['code'] ) || | |
| 251 | - $results['response']['code'] != '200' ) { | |
| 252 | - header ( 'HTTP/1.0 404 Not Found' ); | |
| 253 | - die ( 'Octocat is lost, and afraid' ); | |
| 254 | - } | |
| 255 | - | |
| 256 | - $owner_info = json_decode ( $results['body'] ); | |
| 257 | - | |
| 258 | 392 | $response = new stdClass(); |
| 259 | 393 | $response->type = 'rich'; |
| 260 | 394 | $response->width = '10'; |
| 261 | 395 | $response->height = '10'; |
| @@ -264,10 +398,10 @@ | ||
| 264 | 398 | |
| 265 | 399 | // @TODO This should all be templated |
| 266 | 400 | $response->html = '<div class="github-embed github-embed-user">'; |
| 267 | 401 | $response->html .= '<p><a href="https://github.com/'.esc_attr($owner).'" target="_blank"><strong>'.esc_html($owner)."</strong></a><br/>"; |
| 268 | - $response->html .= esc_html($owner_info->public_repos).' repositories, '; | |
| 269 | - $response->html .= esc_html($owner_info->followers).' followers.</p>'; | |
| 402 | + $response->html .= esc_html ( number_format_i18n ( $owner_info->public_repos ) ).' repositories, '; | |
| 403 | + $response->html .= esc_html ( number_format_i18n ( $owner_info->followers ) ).' followers.</p>'; | |
| 270 | 404 | $response->html .= '</div>'; |
| 271 | 405 | |
| 272 | 406 | header ( 'Content-Type: application/json' ); |
| 273 | 407 | echo json_encode ( $response ); |
| @@ -276,5 +410,8 @@ | ||
| 276 | 410 | } |
| 277 | 411 | |
| 278 | 412 | } |
| 279 | 413 | |
| 280 | -$github_embed = new github_embed(); | |
| 414 | +require_once ( 'github-api.php' ); | |
| 415 | + | |
| 416 | +$github_api = new github_api(); | |
| 417 | +$github_embed = new github_embed ( $github_api ); | |