client_id = apply_filters( 'github-embed-client-id', $this->client_id ); $this->client_secret = apply_filters( 'github-embed-client-secret', $this->client_secret ); } 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'] ) || $results['response']['code'] != '200' ) { header( 'HTTP/1.0 404 Not Found' ); 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'] ); } /** * Get commit information for 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_commits( $owner, $repository ) { $this->log( "get_repo_commits( $owner, $repository )", GEDEBUG_CALL ); $owner = trim( $owner, '/' ); $repository = trim( $repository, '/' ); $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/commits" ); return json_decode( $results['body'] ); } /** * Get a milestone summary from the GitHub API * @param string $owner The repository's owner * @param string $repository The respository name * @param string $milestone The milestone ID * @return object The response from the 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 = $this->call_api( "https://api.github.com/repos/$owner/$repository/milestones/$milestone" ); 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( $repository, '/' ); $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/stats/contributors" ); return json_decode( $results['body'] ); } /** * Get a user from the GitHub API * @param string $user The username * @return object The response from the GitHub API */ public function get_user( $user ) { $this->log( "get_user( $user )", GEDEBUG_CALL ); $user = trim( $user, '/' ); $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 ); } } }