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