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