| @@ -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.2 | |
| 7 | +Version: 1.0 | |
| 8 | 8 | Author: Lee Willis |
| 9 | 9 | Author URI: http://www.leewillis.co.uk/ |
| 10 | 10 | */ |
| 11 | 11 | |
| @@ -30,34 +30,20 @@ | ||
| 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 | - */ | |
| 40 | 34 | class github_embed { |
| 41 | 35 | |
| 42 | 36 | |
| 43 | 37 | |
| 44 | - private $api; | |
| 45 | - | |
| 46 | - | |
| 47 | - | |
| 48 | 38 | /** |
| 49 | 39 | * Constructor. Registers hooks and filters |
| 50 | - * @param class $api An instance of the github_api classs | |
| 51 | 40 | */ |
| 52 | - public function __construct ( $api ) { | |
| 41 | + public function __construct() { | |
| 53 | 42 | |
| 54 | - $this->api = $api; | |
| 55 | - | |
| 56 | 43 | add_action ( 'init', array ( $this, 'register_oembed_handler' ) ); |
| 57 | 44 | add_action ( 'init', array ( $this, 'maybe_handle_oembed' ) ); |
| 58 | - add_action ( 'wp_enqueue_scripts', array ( $this, 'enqueue_styles' ) ); | |
| 59 | - | |
| 45 | + | |
| 60 | 46 | // @TODO i18n |
| 61 | 47 | |
| 62 | 48 | } |
| 63 | 49 | |
| @@ -63,21 +49,8 @@ | ||
| 63 | 49 | |
| 64 | 50 | |
| 65 | 51 | |
| 66 | 52 | /** |
| 67 | - * Enqueue the frontend CSS | |
| 68 | - * @return void | |
| 69 | - */ | |
| 70 | - function enqueue_styles() { | |
| 71 | - | |
| 72 | - wp_register_style ( 'github-embed', plugins_url(basename(dirname(__FILE__)).'/css/github-embed.css' ) ); | |
| 73 | - wp_enqueue_style ( 'github-embed' ); | |
| 74 | - | |
| 75 | - } | |
| 76 | - | |
| 77 | - | |
| 78 | - | |
| 79 | - /** | |
| 80 | 53 | * Register the oEmbed provider, and point it at a local endpoint since github |
| 81 | 54 | * doesn't directly support oEmbed yet. Our local endpoint will use the github |
| 82 | 55 | * API to fulfil the request. |
| 83 | 56 | * @param array $providers The current list of providers |
| @@ -153,31 +126,12 @@ | ||
| 153 | 126 | header ( 'HTTP/1.0 404 Not Found' ); |
| 154 | 127 | die ( 'Octocat is lost, and afraid' ); |
| 155 | 128 | } |
| 156 | 129 | |
| 157 | - // Issues / Milestones | |
| 158 | - if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/graphs/contributors/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { | |
| 130 | + if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { | |
| 159 | 131 | |
| 160 | - $this->oembed_github_repo_contributors ( $matches[1], $matches[2] ); | |
| 161 | - | |
| 162 | - } elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/issues.*$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { | |
| 163 | - | |
| 164 | - if ( preg_match ( '#issues.?milestone=([0-9]*)#i', $url, $milestones ) ) { | |
| 165 | - $milestone = $milestones[1]; | |
| 166 | - } else { | |
| 167 | - $milestone = null; | |
| 168 | - } | |
| 169 | - | |
| 170 | - if ( $milestone ) { | |
| 171 | - $this->oembed_github_repo_milestone_summary ( $matches[1], $matches[2], $milestone ); | |
| 172 | - } | |
| 173 | - | |
| 174 | - // Repository | |
| 175 | - } elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { | |
| 176 | - | |
| 177 | 132 | $this->oembed_github_repo ( $matches[1], $matches[2] ); |
| 178 | 133 | |
| 179 | - // User | |
| 180 | 134 | } elseif ( preg_match ( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) { |
| 181 | 135 | |
| 182 | 136 | $this->oembed_github_author ( $matches[1] ); |
| 183 | 137 | |
| @@ -187,114 +141,39 @@ | ||
| 187 | 141 | |
| 188 | 142 | |
| 189 | 143 | |
| 190 | 144 | /** |
| 191 | - * Retrieve a list of contributors for a project | |
| 192 | - * @param string $owner The owner of the repository | |
| 193 | - * @param string $repository The repository name | |
| 145 | + * Retrieve the information from github for a repo, and | |
| 146 | + * output it as an oembed response | |
| 194 | 147 | */ |
| 195 | - private function oembed_github_repo_contributors ( $owner, $repository ) { | |
| 148 | + private function oembed_github_repo ( $owner, $repository ) { | |
| 196 | 149 | |
| 197 | - $repo = $this->api->get_repo ( $owner, $repository ); | |
| 198 | - $contributors = $this->api->get_repo_contributors ( $owner, $repository ); | |
| 150 | + $repository = trim ( $repository, '/' ); | |
| 199 | 151 | |
| 200 | - $response = new stdClass(); | |
| 201 | - $response->type = 'rich'; | |
| 202 | - $response->width = '10'; | |
| 203 | - $response->height = '10'; | |
| 204 | - $response->version = '1.0'; | |
| 205 | - $response->title = $repo->description; | |
| 152 | + $results = wp_remote_get( "https://api.github.com/repos/$owner/$repository", $args = array ( | |
| 153 | + 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) ); | |
| 206 | 154 | |
| 207 | - $gravatar_size = apply_filters ( 'github_oembed_gravatar_size', 64 ); | |
| 208 | - | |
| 209 | - // @TODO This should all be templated | |
| 210 | - $response->html = '<div class="github-embed github-embed-repo-contributors">'; | |
| 211 | - $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; | |
| 212 | - | |
| 213 | - $response->html .= '<span class="github-heading">Contributors: </span>'; | |
| 214 | - | |
| 215 | - $response->html .= '<ul class="github-repo-contributors">'; | |
| 216 | - | |
| 217 | - foreach ( $contributors as $contributor ) { | |
| 218 | - | |
| 219 | - $details = $this->api->get_user ($contributor->login); | |
| 220 | - $response->html .= '<li class="github-repo-contributor">'; | |
| 221 | - $response->html .= '<img class="github-repo-contributor-avatar" src="'; | |
| 222 | - $response->html .= esc_url(add_query_arg(array('s'=>$gravatar_size), $contributor->avatar_url)); | |
| 223 | - $response->html .= '" alt="Picture of '.esc_attr($contributor->login).'">'; | |
| 224 | - if ( ! empty ( $details->name ) ) { | |
| 225 | - $response->html .= '<span class="github-repo-contributor-name">'.esc_html($details->name)."</span><br>"; | |
| 226 | - } | |
| 227 | - $response->html .= '<span class="github-repo-contributor-login">'; | |
| 228 | - $response->html .= '<a href="https://github.com/'.esc_attr($contributor->login).'">'.esc_attr($contributor->login).'</a></span>'; | |
| 155 | + if ( is_wp_error( $results ) || | |
| 156 | + ! isset ( $results['response']['code'] ) || | |
| 157 | + $results['response']['code'] != '200' ) { | |
| 158 | + header ( 'HTTP/1.0 404 Not Found' ); | |
| 159 | + die ( 'Octocat is lost, and afraid' ); | |
| 229 | 160 | } |
| 230 | 161 | |
| 231 | - $response->html .= '</ul>'; | |
| 232 | - $response->html .= '<div style="clear: both;"></div>'; | |
| 233 | - $response->html .= '</div>'; | |
| 162 | + $repo = json_decode ( $results['body'] ); | |
| 234 | 163 | |
| 235 | - header ( 'Content-Type: application/json' ); | |
| 236 | - echo json_encode ( $response ); | |
| 237 | - die(); | |
| 164 | + $results = wp_remote_get( "https://api.github.com/repos/$owner/$repository/commits", $args = array ( | |
| 165 | + 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) ); | |
| 238 | 166 | |
| 239 | - } | |
| 240 | - | |
| 241 | - | |
| 242 | - | |
| 243 | - /** | |
| 244 | - * Retrieve the summary information for a repo's milestone, and | |
| 245 | - * output it as an oembed response | |
| 246 | - */ | |
| 247 | - private function oembed_github_repo_milestone_summary ( $owner, $repository, $milestone ) { | |
| 248 | - | |
| 249 | - $repo = $this->api->get_repo ( $owner, $repository ); | |
| 250 | - $summary = $this->api->get_repo_milestone_summary ( $owner, $repository, $milestone ); | |
| 251 | - | |
| 252 | - $response = new stdClass(); | |
| 253 | - $response->type = 'rich'; | |
| 254 | - $response->width = '10'; | |
| 255 | - $response->height = '10'; | |
| 256 | - $response->version = '1.0'; | |
| 257 | - $response->title = $repo->description; | |
| 258 | - | |
| 259 | - // @TODO This should all be templated | |
| 260 | - $response->html = '<div class="github-embed github-embed-milestone-summary">'; | |
| 261 | - $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; | |
| 262 | - | |
| 263 | - $response->html .= '<span class="github-heading">Milestone: </span>'; | |
| 264 | - $response->html .= '<span class="github-milestone-title">'.esc_html($summary->title)."</span><br>"; | |
| 265 | - | |
| 266 | - $response->html .= '<span class="github-heading">Issues: </span>'; | |
| 267 | - $response->html .= '<span class="github-milestone-issues">'; | |
| 268 | - $response->html .= esc_html($summary->open_issues)." open, "; | |
| 269 | - $response->html .= esc_html($summary->closed_issues)." closed.</span><br>"; | |
| 270 | - | |
| 271 | - if ( ! empty ( $summary->due_on ) ) { | |
| 272 | - $response->html .= '<span class="github-heading">Due: </span>'; | |
| 273 | - $due_date = date_format ( date_create ( $summary->due_on ), 'jS F Y' ); | |
| 274 | - $response->html .= '<span class="github-milestone-due-date">'.esc_html($due_date).'</span><br>'; | |
| 167 | + if ( is_wp_error( $results ) || | |
| 168 | + ! isset ( $results['response']['code'] ) || | |
| 169 | + $results['response']['code'] != '200' ) { | |
| 170 | + header ( 'HTTP/1.0 404 Not Found' ); | |
| 171 | + die ( 'Octocat is lost, and afraid' ); | |
| 275 | 172 | } |
| 276 | - | |
| 277 | - $response->html .= '<p class="github-milestone-description">'.nl2br(esc_html($summary->description))."</p><br>"; | |
| 278 | - $response->html .= '</div>'; | |
| 279 | 173 | |
| 280 | - header ( 'Content-Type: application/json' ); | |
| 281 | - echo json_encode ( $response ); | |
| 282 | - die(); | |
| 174 | + $commits = json_decode ( $results['body'] ); | |
| 283 | 175 | |
| 284 | - } | |
| 285 | - | |
| 286 | - | |
| 287 | - | |
| 288 | - /** | |
| 289 | - * Retrieve the information from github for a repo, and | |
| 290 | - * output it as an oembed response | |
| 291 | - */ | |
| 292 | - private function oembed_github_repo ( $owner, $repository ) { | |
| 293 | - | |
| 294 | - $repo = $this->api->get_repo ( $owner, $repository ); | |
| 295 | - $commits =$this->api->get_repo_commits ( $owner, $repository ); | |
| 296 | - | |
| 297 | 176 | $response = new stdClass(); |
| 298 | 177 | $response->type = 'rich'; |
| 299 | 178 | $response->width = '10'; |
| 300 | 179 | $response->height = '10'; |
| @@ -301,11 +180,10 @@ | ||
| 301 | 180 | $response->version = '1.0'; |
| 302 | 181 | $response->title = $repo->description; |
| 303 | 182 | |
| 304 | 183 | // @TODO This should all be templated |
| 305 | - $response->html = '<div class="github-embed github-embed-repository">'; | |
| 306 | - $response->html .= '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; | |
| 307 | - $response->html .= '<a href="'.esc_attr($repo->html_url).'" target="_blank">'.esc_html($repo->html_url)."</a><br/>"; | |
| 184 | + $response->html = '<p><a href="'.esc_attr($repo->html_url).'" target="_blank"><strong>'.esc_html($repo->description)."</strong></a><br/>"; | |
| 185 | + $response->html .= esc_html($repo->html_url)."<br/>"; | |
| 308 | 186 | $response->html .= esc_html($repo->forks_count)." forks.<br/>"; |
| 309 | 187 | $response->html .= esc_html($repo->open_issues_count)." open issues.<br/>"; |
| 310 | 188 | |
| 311 | 189 | if ( count ( $commits ) ) { |
| @@ -331,9 +209,8 @@ | ||
| 331 | 209 | $response->html .= '</ul>'; |
| 332 | 210 | |
| 333 | 211 | } |
| 334 | 212 | $response->html .= '</p>'; |
| 335 | - $response->html .= '</div>'; | |
| 336 | 213 | |
| 337 | 214 | header ( 'Content-Type: application/json' ); |
| 338 | 215 | echo json_encode ( $response ); |
| 339 | 216 | die(); |
| @@ -347,10 +224,22 @@ | ||
| 347 | 224 | * it as an oembed response |
| 348 | 225 | */ |
| 349 | 226 | private function oembed_github_author ( $owner ) { |
| 350 | 227 | |
| 351 | - $owner_info = $this->api->get_user ( $owner ); | |
| 228 | + $owner = trim ( $owner, '/' ); | |
| 352 | 229 | |
| 230 | + $results = wp_remote_get( "https://api.github.com/users/$owner", $args = array ( | |
| 231 | + 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) ); | |
| 232 | + | |
| 233 | + if ( is_wp_error( $results ) || | |
| 234 | + ! isset ( $results['response']['code'] ) || | |
| 235 | + $results['response']['code'] != '200' ) { | |
| 236 | + header ( 'HTTP/1.0 404 Not Found' ); | |
| 237 | + die ( 'Octocat is lost, and afraid' ); | |
| 238 | + } | |
| 239 | + | |
| 240 | + $owner_info = json_decode ( $results['body'] ); | |
| 241 | + | |
| 353 | 242 | $response = new stdClass(); |
| 354 | 243 | $response->type = 'rich'; |
| 355 | 244 | $response->width = '10'; |
| 356 | 245 | $response->height = '10'; |
| @@ -357,13 +246,11 @@ | ||
| 357 | 246 | $response->version = '1.0'; |
| 358 | 247 | $response->title = $owner_info->name; |
| 359 | 248 | |
| 360 | 249 | // @TODO This should all be templated |
| 361 | - $response->html = '<div class="github-embed github-embed-user">'; | |
| 362 | - $response->html .= '<p><a href="https://github.com/'.esc_attr($owner).'" target="_blank"><strong>'.esc_html($owner)."</strong></a><br/>"; | |
| 250 | + $response->html = '<p><a href="https://github.com/'.esc_attr($owner).'" target="_blank"><strong>'.esc_html($owner)."</strong></a><br/>"; | |
| 363 | 251 | $response->html .= esc_html($owner_info->public_repos).' repositories, '; |
| 364 | 252 | $response->html .= esc_html($owner_info->followers).' followers.</p>'; |
| 365 | - $response->html .= '</div>'; | |
| 366 | 253 | |
| 367 | 254 | header ( 'Content-Type: application/json' ); |
| 368 | 255 | echo json_encode ( $response ); |
| 369 | 256 | die(); |
| @@ -371,8 +258,5 @@ | ||
| 371 | 258 | } |
| 372 | 259 | |
| 373 | 260 | } |
| 374 | 261 | |
| 375 | -require_once ( 'github-api.php' ); | |
| 376 | - | |
| 377 | -$github_api = new github_api(); | |
| 378 | -$github_embed = new github_embed ( $github_api ); | |
| 262 | +$github_embed = new github_embed(); | |