diff --git a/css/github-embed.css b/css/github-embed.css index 980f670..43ba1f9 100644 --- a/css/github-embed.css +++ b/css/github-embed.css @@ -11,3 +11,14 @@ -moz-box-shadow: 2px 2px 2px #ddd; box-shadow: 2px 2px 2px #ddd; } + +li.github-repo-contributor { + clear: both; + display: block; +} + +.github-repo-contributor-avatar { + float: left; + margin-right: 1em; + margin-bottom: 0.5em; +} \ No newline at end of file diff --git a/github-api.php b/github-api.php index c7edfc9..27a3aeb 100644 --- a/github-api.php +++ b/github-api.php @@ -1,25 +1,84 @@ 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) ); + + /** + * If you find yourself hitting rate limits, then you can register an application + * with GitHub (http://developer.github.com/v3/oauth/) use the filters here to + * provide the credentials. + */ + public function set_credentials () { + + $this->client_id = apply_filters ( 'github-embed-client-id', $this->client_id ); + $this->client_secret = apply_filters ( 'github-embed-client-secret', $this->client_id ); + + } + + + + private function call_api ( $url ) { + + // Allow users to supply auth details to enable a higher rate limit + if ( ! empty ( $this->client_id ) && ! empty ( $this->client_secret ) ) { + $url = add_query_arg(array ( 'client_id' => $this->client_id, + 'client_secret' => $this->client_secret), + $url ); + } + + $args = array ( 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed'); + + $this->log ( __FUNCTION__." : $url", GEDEBUG_CALL ); + + $results = wp_remote_get ( $url, $args ); + + $this->log ( __FUNCTION__." : ".print_r($results,1), GEDEBUG_RESP ); if ( is_wp_error( $results ) || ! isset ( $results['response']['code'] ) || @@ -28,6 +87,27 @@ class github_api { die ( 'Octocat is lost, and afraid' ); } + return $results; + + } + + + + /** + * Get a repository from the GitHub API + * @param string $owner The repository's owner + * @param string $repository The respository name + * @return object The response from the GitHub API + */ + public function get_repo ( $owner, $repository ) { + + $this->log ( "get_repo ( $owner, $repository )", GEDEBUG_CALL ); + + $owner = trim ( $owner, '/' ); + $repository = trim ( $repository, '/' ); + + $results = $this->call_api ( "https://api.github.com/repos/$owner/$repository" ); + return json_decode ( $results['body'] ); } @@ -42,18 +122,12 @@ class github_api { */ public function get_repo_commits ( $owner, $repository ) { + $this->log ( "get_repo_commits ( $owner, $repository )", GEDEBUG_CALL ); + $owner = trim ( $owner, '/' ); $repository = trim ( $repository, '/' ); - $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' ); - } + $results = $this->call_api ( "https://api.github.com/repos/$owner/$repository/commits" ); return json_decode ( $results['body'] ); @@ -70,18 +144,27 @@ class github_api { */ public function get_repo_milestone_summary ( $owner, $repository, $milestone ) { + $this->log ( "get_repo_milestone_summary ( $owner, $repository, $milestone )", GEDEBUG_CALL ); + $owner = trim ( $owner, '/' ); $repo = trim ( $repo, '/' ); - $results = wp_remote_get( "https://api.github.com/repos/$owner/$repository/milestones/$milestone", $args = array ( - 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' ) ); + $results = $this->call_api ( "https://api.github.com/repos/$owner/$repository/milestones/$milestone" ); - 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' ); - } + return json_decode ( $results['body'] ); + + } + + + + public function get_repo_contributors ( $owner, $repository ) { + + $this->log ( "get_repo_contributors ( $owner, $repository )", GEDEBUG_CALL ); + + $owner = trim ( $owner, '/' ); + $repo = trim ( $repo, '/' ); + + $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/collaborators" ); return json_decode ( $results['body'] ); @@ -96,21 +179,25 @@ class github_api { */ public function get_user ( $user ) { + $this->log ( "get_user ( $user )", GEDEBUG_CALL ); + $user = trim ( $user, '/' ); $repository = trim ( $repository, '/' ); - $results = wp_remote_get( "https://api.github.com/users/$user", $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' ); - } + $results = $this->call_api ( "https://api.github.com/users/$user" ); return json_decode ( $results['body'] ); } + + + private function log ( $msg, $level ) { + if ( GITHUB_API_LEVEL >= $level ) { + error_log ( "[GE$level]: ".$msg ); + } + } + + + } \ No newline at end of file diff --git a/github-embed.php b/github-embed.php index 2a1d547..e0c6ef4 100644 --- a/github-embed.php +++ b/github-embed.php @@ -155,7 +155,11 @@ class github_embed { } // Issues / Milestones - if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/issues.*$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { + if ( preg_match ( '#https?://github.com/([^/]*)/([^/]*)/graphs/contributors/?$#i', $url, $matches ) && ! empty ( $matches[2] ) ) { + + $this->oembed_github_repo_contributors ( $matches[1], $matches[2] ); + + } elseif ( 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]; @@ -182,6 +186,52 @@ class github_embed { } + + private function oembed_github_repo_contributors ( $owner, $repository ) { + + + $repo = $this->api->get_repo ( $owner, $repository ); + $contributors = $this->api->get_repo_contributors ( $owner, $repository ); + + $response = new stdClass(); + $response->type = 'rich'; + $response->width = '10'; + $response->height = '10'; + $response->version = '1.0'; + $response->title = $repo->description; + + $gravatar_size = apply_filters ( 'github_oembed_gravatar_size', 64 ); + + // @TODO This should all be templated + $response->html = '
'; + $response->html .= '

'.esc_html($repo->description)."
"; + + $response->html .= 'Contributors: '; + + $response->html .= '

'; + $response->html .= '
'; + + header ( 'Content-Type: application/json' ); + echo json_encode ( $response ); + die(); + + } /** * Retrieve the summary information for a repo's milestone, and * output it as an oembed response