From ab3d13bdd652e8a8b0810786d718b6d87d4c65e1 Mon Sep 17 00:00:00 2001 From: Lee Willis Date: Sun, 10 Feb 2013 15:54:42 +0000 Subject: [PATCH] Bit of refactoring, and introduce support for milestones --- github-embed.php | 100 ++++++++++++++++++++++++++++++----------------- 1 file changed, 65 insertions(+), 35 deletions(-) diff --git a/github-embed.php b/github-embed.php index c4ac382..2a1d547 100644 --- a/github-embed.php +++ b/github-embed.php @@ -31,6 +31,12 @@ Author URI: http://www.leewillis.co.uk/ * ********************************************************************** */ +/** + * This class handles being the oEmbed provider in terms of registering the URLs that + * we can embed, and handling the actual oEmbed calls. It relies on the github_api + * class to retrieve the information from the GitHub API. + * @uses class github_api + */ class github_embed { @@ -148,10 +154,25 @@ class github_embed { die ( 'Octocat is lost, and afraid' ); } - if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { + // Issues / Milestones + if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/issues.*$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { + + if ( preg_match ( '#issues.?milestone=([0-9]*)#i', $url, $milestones ) ) { + $milestone = $milestones[1]; + } else { + $milestone = null; + } + + if ( $milestone ) { + $this->oembed_github_repo_milestone_summary ( $matches[1], $matches[2], $milestone ); + } + + // Repository + } elseif ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { $this->oembed_github_repo ( $matches[1], $matches[2] ); + // User } elseif ( preg_match ( '#https?://github.com/([^/]*)/?$#i', $url, $matches ) ) { $this->oembed_github_author ( $matches[1] ); @@ -161,38 +182,59 @@ class github_embed { } - /** - * Retrieve the information from github for a repo, and + * Retrieve the summary information for a repo's milestone, and * output it as an oembed response */ - private function oembed_github_repo ( $owner, $repository ) { + private function oembed_github_repo_milestone_summary ( $owner, $repository, $milestone ) { - $repository = trim ( $repository, '/' ); + $repo = $this->api->get_repo ( $owner, $repository ); + $summary = $this->api->get_repo_milestone_summary ( $owner, $repository, $milestone ); - $results = wp_remote_get( "https://api.github.com/repos/$owner/$repository", $args = array ( - 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) ); + $response = new stdClass(); + $response->type = 'rich'; + $response->width = '10'; + $response->height = '10'; + $response->version = '1.0'; + $response->title = $repo->description; - if ( is_wp_error( $results ) || - ! isset ( $results['response']['code'] ) || - $results['response']['code'] != '200' ) { - header ( 'HTTP/1.0 404 Not Found' ); - die ( 'Octocat is lost, and afraid' ); + // @TODO This should all be templated + $response->html = '
'; + $response->html .= '

'.esc_html($repo->description)."
"; + + $response->html .= 'Milestone: '; + $response->html .= ''.esc_html($summary->title)."
"; + + $response->html .= 'Issues: '; + $response->html .= ''; + $response->html .= esc_html($summary->open_issues)." open, "; + $response->html .= esc_html($summary->closed_issues)." closed.
"; + + if ( ! empty ( $summary->due_on ) ) { + $response->html .= 'Due: '; + $due_date = date_format ( date_create ( $summary->due_on ), 'jS F Y' ); + $response->html .= ''.esc_html($due_date).'
'; } + + $response->html .= '

'.nl2br(esc_html($summary->description))."


"; + $response->html .= '
'; - $repo = json_decode ( $results['body'] ); + header ( 'Content-Type: application/json' ); + echo json_encode ( $response ); + die(); - $results = wp_remote_get( "https://api.github.com/repos/$owner/$repository/commits", $args = array ( - 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) ); + } - if ( is_wp_error( $results ) || - ! isset ( $results['response']['code'] ) || - $results['response']['code'] != '200' ) { - header ( 'HTTP/1.0 404 Not Found' ); - die ( 'Octocat is lost, and afraid' ); - } - $commits = json_decode ( $results['body'] ); + + /** + * Retrieve the information from github for a repo, and + * output it as an oembed response + */ + private function oembed_github_repo ( $owner, $repository ) { + + $repo = $this->api->get_repo ( $owner, $repository ); + $commits =$this->api->get_repo_commits ( $owner, $repository ); $response = new stdClass(); $response->type = 'rich'; @@ -248,19 +290,7 @@ class github_embed { */ private function oembed_github_author ( $owner ) { - $owner = trim ( $owner, '/' ); - - $results = wp_remote_get( "https://api.github.com/users/$owner", $args = array ( - 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) ); - - if ( is_wp_error( $results ) || - ! isset ( $results['response']['code'] ) || - $results['response']['code'] != '200' ) { - header ( 'HTTP/1.0 404 Not Found' ); - die ( 'Octocat is lost, and afraid' ); - } - - $owner_info = json_decode ( $results['body'] ); + $owner_info = $this->api->get_user ( $owner ); $response = new stdClass(); $response->type = 'rich';