| @@ -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,37 +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 | - $this->client_id = apply_filters( 'github-embed-client-id', $this->client_id ); | |
| 59 | - $this->client_secret = apply_filters( 'github-embed-client-secret', $this->client_secret ); | |
| 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 ); | |
| 60 | 58 | } |
| 61 | 59 | |
| 62 | - | |
| 63 | - | |
| 64 | 60 | private function call_api( $url ) { |
| 65 | - | |
| 66 | - // 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] | |
| 67 | 62 | if ( ! empty( $this->client_id ) && ! empty( $this->client_secret ) ) { |
| 68 | 63 | $url = add_query_arg( |
| 69 | - array( | |
| 70 | - 'client_id' => $this->client_id, | |
| 71 | - 'client_secret' => $this->client_secret ), | |
| 72 | - $url | |
| 73 | - ); | |
| 64 | + array( | |
| 65 | + 'client_id' => $this->client_id, | |
| 66 | + 'client_secret' => $this->client_secret | |
| 67 | + ), | |
| 68 | + $url | |
| 69 | + ); | |
| 74 | 70 | } |
| 75 | 71 | |
| 76 | - $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 ); | |
| 77 | 81 | |
| 78 | - $this->log( __FUNCTION__." : $url", GEDEBUG_CALL ); | |
| 79 | - | |
| 80 | 82 | $results = wp_remote_get( $url, $args ); |
| 81 | 83 | |
| 82 | - $this->log( __FUNCTION__ . " : " . print_r( $results,1 ), GEDEBUG_RESP ); | |
| 83 | - | |
| 84 | - if( is_wp_error( $results ) || | |
| 85 | - ! isset( $results['response']['code'] ) || | |
| 86 | - $results['response']['code'] != '200' ) { | |
| 84 | + if ( is_wp_error( $results ) || | |
| 85 | + ! isset( $results['response']['code'] ) || | |
| 86 | + $results['response']['code'] != '200' ) { | |
| 87 | 87 | header( 'HTTP/1.0 404 Not Found' ); |
| 88 | 88 | die( 'Octocat is lost, and afraid' ); |
| 89 | 89 | } |
| 90 | 90 | |
| @@ -92,13 +92,14 @@ | ||
| 92 | 92 | |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | |
| 96 | - | |
| 97 | 96 | /** |
| 98 | 97 | * Get a repository from the GitHub API |
| 99 | - * @param string $owner The repository's owner | |
| 100 | - * @param string $repository The respository name | |
| 98 | + * | |
| 99 | + * @param string $owner The repository's owner | |
| 100 | + * @param string $repository The respository name | |
| 101 | + * | |
| 101 | 102 | * @return object The response from the GitHub API |
| 102 | 103 | */ |
| 103 | 104 | public function get_repo( $owner, $repository ) { |
| 104 | 105 | |
| @@ -103,11 +104,8 @@ | ||
| 103 | 104 | public function get_repo( $owner, $repository ) { |
| 104 | 105 | |
| 105 | 106 | $this->log( "get_repo( $owner, $repository )", GEDEBUG_CALL ); |
| 106 | 107 | |
| 107 | - $owner = trim( $owner, '/' ); | |
| 108 | - $repository = trim( $repository, '/' ); | |
| 109 | - | |
| 110 | 108 | $results = $this->call_api( "https://api.github.com/repos/$owner/$repository" ); |
| 111 | 109 | |
| 112 | 110 | return json_decode( $results['body'] ); |
| 113 | 111 | |
| @@ -113,13 +111,14 @@ | ||
| 113 | 111 | |
| 114 | 112 | } |
| 115 | 113 | |
| 116 | 114 | |
| 117 | - | |
| 118 | 115 | /** |
| 119 | 116 | * Get commit information for a repository from the GitHub API |
| 120 | - * @param string $owner The repository's owner | |
| 121 | - * @param string $repository The respository name | |
| 117 | + * | |
| 118 | + * @param string $owner The repository's owner | |
| 119 | + * @param string $repository The respository name | |
| 120 | + * | |
| 122 | 121 | * @return object The response from the GitHub API |
| 123 | 122 | */ |
| 124 | 123 | public function get_repo_commits( $owner, $repository ) { |
| 125 | 124 | |
| @@ -124,11 +123,8 @@ | ||
| 124 | 123 | public function get_repo_commits( $owner, $repository ) { |
| 125 | 124 | |
| 126 | 125 | $this->log( "get_repo_commits( $owner, $repository )", GEDEBUG_CALL ); |
| 127 | 126 | |
| 128 | - $owner = trim( $owner, '/' ); | |
| 129 | - $repository = trim( $repository, '/' ); | |
| 130 | - | |
| 131 | 127 | $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/commits" ); |
| 132 | 128 | |
| 133 | 129 | return json_decode( $results['body'] ); |
| 134 | 130 | |
| @@ -134,14 +130,15 @@ | ||
| 134 | 130 | |
| 135 | 131 | } |
| 136 | 132 | |
| 137 | 133 | |
| 138 | - | |
| 139 | 134 | /** |
| 140 | 135 | * Get a milestone summary from the GitHub API |
| 141 | - * @param string $owner The repository's owner | |
| 142 | - * @param string $repository The respository name | |
| 143 | - * @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 | + * | |
| 144 | 141 | * @return object The response from the GitHub API |
| 145 | 142 | */ |
| 146 | 143 | public function get_repo_milestone_summary( $owner, $repository, $milestone ) { |
| 147 | 144 | |
| @@ -146,11 +143,8 @@ | ||
| 146 | 143 | public function get_repo_milestone_summary( $owner, $repository, $milestone ) { |
| 147 | 144 | |
| 148 | 145 | $this->log( "get_repo_milestone_summary( $owner, $repository, $milestone )", GEDEBUG_CALL ); |
| 149 | 146 | |
| 150 | - $owner = trim( $owner, '/' ); | |
| 151 | - $repo = trim( $repository, '/' ); | |
| 152 | - | |
| 153 | 147 | $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/milestones/$milestone" ); |
| 154 | 148 | |
| 155 | 149 | return json_decode( $results['body'] ); |
| 156 | 150 | |
| @@ -156,16 +150,12 @@ | ||
| 156 | 150 | |
| 157 | 151 | } |
| 158 | 152 | |
| 159 | 153 | |
| 160 | - | |
| 161 | 154 | public function get_repo_contributors( $owner, $repository ) { |
| 162 | 155 | |
| 163 | 156 | $this->log( "get_repo_contributors( $owner, $repository )", GEDEBUG_CALL ); |
| 164 | 157 | |
| 165 | - $owner = trim( $owner, '/' ); | |
| 166 | - $repo = trim( $repository, '/' ); | |
| 167 | - | |
| 168 | 158 | $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/stats/contributors" ); |
| 169 | 159 | |
| 170 | 160 | return json_decode( $results['body'] ); |
| 171 | 161 | |
| @@ -171,12 +161,13 @@ | ||
| 171 | 161 | |
| 172 | 162 | } |
| 173 | 163 | |
| 174 | 164 | |
| 175 | - | |
| 176 | 165 | /** |
| 177 | 166 | * Get a user from the GitHub API |
| 178 | - * @param string $user The username | |
| 167 | + * | |
| 168 | + * @param string $user The username | |
| 169 | + * | |
| 179 | 170 | * @return object The response from the GitHub API |
| 180 | 171 | */ |
| 181 | 172 | public function get_user( $user ) { |
| 182 | 173 | |
| @@ -181,10 +172,8 @@ | ||
| 181 | 172 | public function get_user( $user ) { |
| 182 | 173 | |
| 183 | 174 | $this->log( "get_user( $user )", GEDEBUG_CALL ); |
| 184 | 175 | |
| 185 | - $user = trim( $user, '/' ); | |
| 186 | - | |
| 187 | 176 | $results = $this->call_api( "https://api.github.com/users/$user" ); |
| 188 | 177 | |
| 189 | 178 | return json_decode( $results['body'] ); |
| 190 | 179 | |
| @@ -190,14 +179,12 @@ | ||
| 190 | 179 | |
| 191 | 180 | } |
| 192 | 181 | |
| 193 | 182 | |
| 194 | - | |
| 195 | 183 | private function log( $msg, $level ) { |
| 196 | 184 | if ( GITHUB_API_LEVEL >= $level ) { |
| 197 | - error_log( "[GE$level]: ".$msg ); | |
| 185 | + error_log( "[GE$level]: " . $msg ); | |
| 198 | 186 | } |
| 199 | 187 | } |
| 200 | - | |
| 201 | 188 | |
| 202 | 189 | |
| 203 | 190 | } |