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