| @@ -1,55 +1,49 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - | |
| 4 | - | |
| 5 | -// 0 - none | |
| 3 | +// 0 - none. | |
| 6 | 4 | define( 'GEDEBUG_NONE', 0 ); |
| 7 | 5 | |
| 8 | -// 1 - call logging only | |
| 6 | +// 1 - call logging only. | |
| 9 | 7 | define( 'GEDEBUG_CALL', 1 ); |
| 10 | 8 | |
| 11 | -// 2 - calls, and responses | |
| 9 | +// 2 - calls, and responses. | |
| 12 | 10 | define( 'GEDEBUG_RESP', 2 ); |
| 13 | 11 | |
| 14 | -// Selected debug level | |
| 15 | -define( 'GITHUB_API_LEVEL', GEDEBUG_NONE ); | |
| 12 | +// Selected debug level. | |
| 13 | +if ( ! defined( 'GITHUB_API_LEVEL' ) ) { | |
| 14 | + define( 'GITHUB_API_LEVEL', GEDEBUG_NONE ); | |
| 15 | +} | |
| 16 | 16 | |
| 17 | - | |
| 18 | 17 | /** |
| 19 | 18 | * This class contains all the functions that actually retrieve information from the GitHub API |
| 20 | 19 | */ |
| 21 | 20 | class github_api { |
| 22 | 21 | |
| 23 | - | |
| 24 | 22 | private $client_id = null; |
| 25 | 23 | private $client_secret = null; |
| 24 | + private $access_token = null; | |
| 25 | + private $access_token_username = null; | |
| 26 | 26 | |
| 27 | - | |
| 28 | - | |
| 29 | 27 | /** |
| 30 | 28 | * Allow the client ID / secret to be set, and used for subsequent calls |
| 31 | 29 | */ |
| 32 | - function __construct() { | |
| 33 | - | |
| 30 | + public function __construct() { | |
| 34 | 31 | add_action( 'plugins_loaded', array( $this, 'set_credentials' ) ); |
| 35 | 32 | add_filter( 'http_request_timeout', array( $this, 'http_request_timeout' ) ); |
| 36 | - | |
| 37 | 33 | } |
| 38 | 34 | |
| 39 | - | |
| 40 | - | |
| 41 | 35 | /** |
| 42 | 36 | * Extend the timeout since API calls can easily exceed 5 seconds |
| 43 | - * @param int $seconds The current timeout setting | |
| 37 | + * | |
| 38 | + * @param int $seconds The current timeout setting | |
| 39 | + * | |
| 44 | 40 | * @return int The revised timeout setting |
| 45 | 41 | */ |
| 46 | - function http_request_timeout( $seconds ) { | |
| 42 | + public function http_request_timeout( $seconds ) { | |
| 47 | 43 | return $seconds < 25 ? 25 : $seconds; |
| 48 | 44 | } |
| 49 | 45 | |
| 50 | - | |
| 51 | - | |
| 52 | 46 | /** |
| 53 | 47 | * If you find yourself hitting rate limits, then you can register an application |
| 54 | 48 | * with GitHub(http://developer.github.com/v3/oauth/) use the filters here to |
| 55 | 49 | * provide the credentials. |
| @@ -54,39 +48,41 @@ | ||
| 54 | 48 | * with GitHub(http://developer.github.com/v3/oauth/) use the filters here to |
| 55 | 49 | * provide the credentials. |
| 56 | 50 | */ |
| 57 | 51 | public function set_credentials() { |
| 58 | - | |
| 59 | - $this->client_id = apply_filters( 'github-embed-client-id', $this->client_id ); | |
| 60 | - $this->client_secret = apply_filters( 'github-embed-client-secret', $this->client_secret ); | |
| 61 | - | |
| 52 | + $this->client_id = apply_filters( 'github-embed-client-id', $this->client_id ); | |
| 53 | + $this->client_secret = apply_filters( 'github-embed-client-secret', $this->client_secret ); | |
| 54 | + $this->access_token = apply_filters( 'github-embed-access-token', $this->access_token ); | |
| 55 | + $this->access_token_username = apply_filters( 'github-embed-access-token-username', $this->access_token_username ); | |
| 62 | 56 | } |
| 63 | 57 | |
| 64 | - | |
| 65 | - | |
| 66 | 58 | private function call_api( $url ) { |
| 67 | - | |
| 68 | - // Allow users to supply auth details to enable a higher rate limit | |
| 59 | + // Allow users to supply auth details to enable a higher rate limit [Deprecated] | |
| 69 | 60 | if ( ! empty( $this->client_id ) && ! empty( $this->client_secret ) ) { |
| 70 | 61 | $url = add_query_arg( |
| 71 | - array( | |
| 72 | - 'client_id' => $this->client_id, | |
| 73 | - 'client_secret' => $this->client_secret ), | |
| 74 | - $url | |
| 75 | - ); | |
| 62 | + array( | |
| 63 | + 'client_id' => $this->client_id, | |
| 64 | + 'client_secret' => $this->client_secret | |
| 65 | + ), | |
| 66 | + $url | |
| 67 | + ); | |
| 76 | 68 | } |
| 77 | 69 | |
| 78 | - $args = array( 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed'); | |
| 70 | + $args = array( | |
| 71 | + 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed' | |
| 72 | + ); | |
| 73 | + if ( ! empty( $this->access_token_username ) && ! empty ( $this->access_token ) ) { | |
| 74 | + $args['headers'] = [ | |
| 75 | + 'Authorization' => 'Basic ' . base64_encode( $this->access_token_username . ':' . $this->access_token ), | |
| 76 | + ]; | |
| 77 | + } | |
| 78 | + $this->log( __FUNCTION__ . " : $url", GEDEBUG_CALL ); | |
| 79 | 79 | |
| 80 | - $this->log( __FUNCTION__." : $url", GEDEBUG_CALL ); | |
| 81 | - | |
| 82 | 80 | $results = wp_remote_get( $url, $args ); |
| 83 | 81 | |
| 84 | - $this->log( __FUNCTION__ . " : " . print_r( $results,1 ), GEDEBUG_RESP ); | |
| 85 | - | |
| 86 | - if( is_wp_error( $results ) || | |
| 87 | - ! isset( $results['response']['code'] ) || | |
| 88 | - $results['response']['code'] != '200' ) { | |
| 82 | + if ( is_wp_error( $results ) || | |
| 83 | + ! isset( $results['response']['code'] ) || | |
| 84 | + $results['response']['code'] != '200' ) { | |
| 89 | 85 | header( 'HTTP/1.0 404 Not Found' ); |
| 90 | 86 | die( 'Octocat is lost, and afraid' ); |
| 91 | 87 | } |
| 92 | 88 | |
| @@ -93,23 +89,18 @@ | ||
| 93 | 89 | return $results; |
| 94 | 90 | |
| 95 | 91 | } |
| 96 | 92 | |
| 97 | - | |
| 98 | - | |
| 99 | 93 | /** |
| 100 | 94 | * Get a repository from the GitHub API |
| 101 | - * @param string $owner The repository's owner | |
| 102 | - * @param string $repository The respository name | |
| 95 | + * | |
| 96 | + * @param string $owner The repository's owner | |
| 97 | + * @param string $repository The respository name | |
| 98 | + * | |
| 103 | 99 | * @return object The response from the GitHub API |
| 104 | 100 | */ |
| 105 | 101 | public function get_repo( $owner, $repository ) { |
| 106 | - | |
| 107 | 102 | $this->log( "get_repo( $owner, $repository )", GEDEBUG_CALL ); |
| 108 | - | |
| 109 | - $owner = trim( $owner, '/' ); | |
| 110 | - $repository = trim( $repository, '/' ); | |
| 111 | - | |
| 112 | 103 | $results = $this->call_api( "https://api.github.com/repos/$owner/$repository" ); |
| 113 | 104 | |
| 114 | 105 | return json_decode( $results['body'] ); |
| 115 | 106 | |
| @@ -114,92 +105,62 @@ | ||
| 114 | 105 | return json_decode( $results['body'] ); |
| 115 | 106 | |
| 116 | 107 | } |
| 117 | 108 | |
| 118 | - | |
| 119 | - | |
| 120 | 109 | /** |
| 121 | 110 | * Get commit information for a repository from the GitHub API |
| 122 | - * @param string $owner The repository's owner | |
| 123 | - * @param string $repository The respository name | |
| 111 | + * | |
| 112 | + * @param string $owner The repository's owner | |
| 113 | + * @param string $repository The respository name | |
| 114 | + * | |
| 124 | 115 | * @return object The response from the GitHub API |
| 125 | 116 | */ |
| 126 | 117 | public function get_repo_commits( $owner, $repository ) { |
| 127 | - | |
| 128 | 118 | $this->log( "get_repo_commits( $owner, $repository )", GEDEBUG_CALL ); |
| 129 | - | |
| 130 | - $owner = trim( $owner, '/' ); | |
| 131 | - $repository = trim( $repository, '/' ); | |
| 132 | - | |
| 133 | 119 | $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/commits" ); |
| 134 | 120 | |
| 135 | 121 | return json_decode( $results['body'] ); |
| 136 | - | |
| 137 | 122 | } |
| 138 | 123 | |
| 139 | - | |
| 140 | - | |
| 141 | 124 | /** |
| 142 | 125 | * Get a milestone summary from the GitHub API |
| 143 | - * @param string $owner The repository's owner | |
| 144 | - * @param string $repository The respository name | |
| 145 | - * @param string $milestone The milestone ID | |
| 126 | + * | |
| 127 | + * @param string $owner The repository's owner | |
| 128 | + * @param string $repository The respository name | |
| 129 | + * @param string $milestone The milestone ID | |
| 130 | + * | |
| 146 | 131 | * @return object The response from the GitHub API |
| 147 | 132 | */ |
| 148 | 133 | public function get_repo_milestone_summary( $owner, $repository, $milestone ) { |
| 149 | - | |
| 150 | 134 | $this->log( "get_repo_milestone_summary( $owner, $repository, $milestone )", GEDEBUG_CALL ); |
| 151 | - | |
| 152 | - $owner = trim( $owner, '/' ); | |
| 153 | - $repo = trim( $repository, '/' ); | |
| 154 | - | |
| 155 | 135 | $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/milestones/$milestone" ); |
| 156 | 136 | |
| 157 | 137 | return json_decode( $results['body'] ); |
| 158 | - | |
| 159 | 138 | } |
| 160 | 139 | |
| 161 | - | |
| 162 | - | |
| 163 | 140 | public function get_repo_contributors( $owner, $repository ) { |
| 164 | - | |
| 165 | 141 | $this->log( "get_repo_contributors( $owner, $repository )", GEDEBUG_CALL ); |
| 166 | - | |
| 167 | - $owner = trim( $owner, '/' ); | |
| 168 | - $repo = trim( $repository, '/' ); | |
| 169 | - | |
| 170 | 142 | $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/stats/contributors" ); |
| 171 | 143 | |
| 172 | 144 | return json_decode( $results['body'] ); |
| 173 | - | |
| 174 | 145 | } |
| 175 | 146 | |
| 176 | - | |
| 177 | - | |
| 178 | 147 | /** |
| 179 | 148 | * Get a user from the GitHub API |
| 180 | - * @param string $user The username | |
| 149 | + * | |
| 150 | + * @param string $user The username | |
| 151 | + * | |
| 181 | 152 | * @return object The response from the GitHub API |
| 182 | 153 | */ |
| 183 | 154 | public function get_user( $user ) { |
| 184 | - | |
| 185 | 155 | $this->log( "get_user( $user )", GEDEBUG_CALL ); |
| 186 | - | |
| 187 | - $user = trim( $user, '/' ); | |
| 188 | - | |
| 189 | 156 | $results = $this->call_api( "https://api.github.com/users/$user" ); |
| 190 | 157 | |
| 191 | 158 | return json_decode( $results['body'] ); |
| 192 | - | |
| 193 | 159 | } |
| 194 | 160 | |
| 195 | - | |
| 196 | - | |
| 197 | 161 | private function log( $msg, $level ) { |
| 198 | 162 | if ( GITHUB_API_LEVEL >= $level ) { |
| 199 | - error_log( "[GE$level]: ".$msg ); | |
| 163 | + error_log( "[GE$level]: " . $msg ); | |
| 200 | 164 | } |
| 201 | 165 | } |
| 202 | - | |
| 203 | - | |
| 204 | - | |
| 205 | 166 | } |