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