| @@ -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_RESP ); | |
| 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,9 +42,9 @@ | ||
| 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 ) { | |
| 46 | + function http_request_timeout ( $seconds ) { | |
| 47 | 47 | return $seconds < 25 ? 25 : $seconds; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | |
| @@ -50,45 +50,42 @@ | ||
| 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_secret ); | |
| 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 | 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( | |
| 71 | - array( | |
| 72 | - 'client_id' => $this->client_id, | |
| 73 | - 'client_secret' => $this->client_secret ), | |
| 74 | - $url | |
| 75 | - ); | |
| 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 ); | |
| 76 | 73 | } |
| 77 | 74 | |
| 78 | - $args = array( 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed'); | |
| 75 | + $args = array ( 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed'); | |
| 76 | + | |
| 77 | + $this->log ( __FUNCTION__." : $url", GEDEBUG_CALL ); | |
| 79 | 78 | |
| 80 | - $this->log( __FUNCTION__." : $url", GEDEBUG_CALL ); | |
| 79 | + $results = wp_remote_get ( $url, $args ); | |
| 81 | 80 | |
| 82 | - $results = wp_remote_get( $url, $args ); | |
| 81 | + $this->log ( __FUNCTION__." : ".print_r($results,1), GEDEBUG_RESP ); | |
| 83 | 82 | |
| 84 | - $this->log( __FUNCTION__ . " : " . print_r( $results,1 ), GEDEBUG_RESP ); | |
| 85 | - | |
| 86 | - if( is_wp_error( $results ) || | |
| 87 | - ! isset( $results['response']['code'] ) || | |
| 83 | + if ( is_wp_error( $results ) || | |
| 84 | + ! isset ( $results['response']['code'] ) || | |
| 88 | 85 | $results['response']['code'] != '200' ) { |
| 89 | - header( 'HTTP/1.0 404 Not Found' ); | |
| 90 | - die( 'Octocat is lost, and afraid' ); | |
| 86 | + header ( 'HTTP/1.0 404 Not Found' ); | |
| 87 | + die ( 'Octocat is lost, and afraid' ); | |
| 91 | 88 | } |
| 92 | 89 | |
| 93 | 90 | return $results; |
| 94 | 91 | |
| @@ -101,18 +98,18 @@ | ||
| 101 | 98 | * @param string $owner The repository's owner |
| 102 | 99 | * @param string $repository The respository name |
| 103 | 100 | * @return object The response from the GitHub API |
| 104 | 101 | */ |
| 105 | - public function get_repo( $owner, $repository ) { | |
| 102 | + public function get_repo ( $owner, $repository ) { | |
| 106 | 103 | |
| 107 | - $this->log( "get_repo( $owner, $repository )", GEDEBUG_CALL ); | |
| 104 | + $this->log ( "get_repo ( $owner, $repository )", GEDEBUG_CALL ); | |
| 108 | 105 | |
| 109 | - $owner = trim( $owner, '/' ); | |
| 110 | - $repository = trim( $repository, '/' ); | |
| 106 | + $owner = trim ( $owner, '/' ); | |
| 107 | + $repository = trim ( $repository, '/' ); | |
| 111 | 108 | |
| 112 | - $results = $this->call_api( "https://api.github.com/repos/$owner/$repository" ); | |
| 109 | + $results = $this->call_api ( "https://api.github.com/repos/$owner/$repository" ); | |
| 113 | 110 | |
| 114 | - return json_decode( $results['body'] ); | |
| 111 | + return json_decode ( $results['body'] ); | |
| 115 | 112 | |
| 116 | 113 | } |
| 117 | 114 | |
| 118 | 115 | |
| @@ -122,18 +119,18 @@ | ||
| 122 | 119 | * @param string $owner The repository's owner |
| 123 | 120 | * @param string $repository The respository name |
| 124 | 121 | * @return object The response from the GitHub API |
| 125 | 122 | */ |
| 126 | - public function get_repo_commits( $owner, $repository ) { | |
| 123 | + public function get_repo_commits ( $owner, $repository ) { | |
| 127 | 124 | |
| 128 | - $this->log( "get_repo_commits( $owner, $repository )", GEDEBUG_CALL ); | |
| 125 | + $this->log ( "get_repo_commits ( $owner, $repository )", GEDEBUG_CALL ); | |
| 129 | 126 | |
| 130 | - $owner = trim( $owner, '/' ); | |
| 131 | - $repository = trim( $repository, '/' ); | |
| 127 | + $owner = trim ( $owner, '/' ); | |
| 128 | + $repository = trim ( $repository, '/' ); | |
| 132 | 129 | |
| 133 | - $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/commits" ); | |
| 130 | + $results = $this->call_api ( "https://api.github.com/repos/$owner/$repository/commits" ); | |
| 134 | 131 | |
| 135 | - return json_decode( $results['body'] ); | |
| 132 | + return json_decode ( $results['body'] ); | |
| 136 | 133 | |
| 137 | 134 | } |
| 138 | 135 | |
| 139 | 136 | |
| @@ -144,33 +141,33 @@ | ||
| 144 | 141 | * @param string $repository The respository name |
| 145 | 142 | * @param string $milestone The milestone ID |
| 146 | 143 | * @return object The response from the GitHub API |
| 147 | 144 | */ |
| 148 | - public function get_repo_milestone_summary( $owner, $repository, $milestone ) { | |
| 145 | + public function get_repo_milestone_summary ( $owner, $repository, $milestone ) { | |
| 149 | 146 | |
| 150 | - $this->log( "get_repo_milestone_summary( $owner, $repository, $milestone )", GEDEBUG_CALL ); | |
| 147 | + $this->log ( "get_repo_milestone_summary ( $owner, $repository, $milestone )", GEDEBUG_CALL ); | |
| 151 | 148 | |
| 152 | - $owner = trim( $owner, '/' ); | |
| 153 | - $repo = trim( $repo, '/' ); | |
| 149 | + $owner = trim ( $owner, '/' ); | |
| 150 | + $repo = trim ( $repo, '/' ); | |
| 154 | 151 | |
| 155 | - $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/milestones/$milestone" ); | |
| 152 | + $results = $this->call_api ( "https://api.github.com/repos/$owner/$repository/milestones/$milestone" ); | |
| 156 | 153 | |
| 157 | - return json_decode( $results['body'] ); | |
| 154 | + return json_decode ( $results['body'] ); | |
| 158 | 155 | |
| 159 | 156 | } |
| 160 | 157 | |
| 161 | 158 | |
| 162 | 159 | |
| 163 | - public function get_repo_contributors( $owner, $repository ) { | |
| 160 | + public function get_repo_contributors ( $owner, $repository ) { | |
| 164 | 161 | |
| 165 | - $this->log( "get_repo_contributors( $owner, $repository )", GEDEBUG_CALL ); | |
| 162 | + $this->log ( "get_repo_contributors ( $owner, $repository )", GEDEBUG_CALL ); | |
| 166 | 163 | |
| 167 | - $owner = trim( $owner, '/' ); | |
| 168 | - $repo = trim( $repository, '/' ); | |
| 164 | + $owner = trim ( $owner, '/' ); | |
| 165 | + $repo = trim ( $repo, '/' ); | |
| 169 | 166 | |
| 170 | - $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/stats/contributors" ); | |
| 167 | + $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/collaborators" ); | |
| 171 | 168 | |
| 172 | - return json_decode( $results['body'] ); | |
| 169 | + return json_decode ( $results['body'] ); | |
| 173 | 170 | |
| 174 | 171 | } |
| 175 | 172 | |
| 176 | 173 | |
| @@ -179,27 +176,28 @@ | ||
| 179 | 176 | * Get a user from the GitHub API |
| 180 | 177 | * @param string $user The username |
| 181 | 178 | * @return object The response from the GitHub API |
| 182 | 179 | */ |
| 183 | - public function get_user( $user ) { | |
| 180 | + public function get_user ( $user ) { | |
| 184 | 181 | |
| 185 | - $this->log( "get_user( $user )", GEDEBUG_CALL ); | |
| 182 | + $this->log ( "get_user ( $user )", GEDEBUG_CALL ); | |
| 186 | 183 | |
| 187 | - $user = trim( $user, '/' ); | |
| 184 | + $user = trim ( $user, '/' ); | |
| 185 | + $repository = trim ( $repository, '/' ); | |
| 188 | 186 | |
| 189 | - $results = $this->call_api( "https://api.github.com/users/$user" ); | |
| 187 | + $results = $this->call_api ( "https://api.github.com/users/$user" ); | |
| 190 | 188 | |
| 191 | - return json_decode( $results['body'] ); | |
| 189 | + return json_decode ( $results['body'] ); | |
| 192 | 190 | |
| 193 | 191 | } |
| 192 | + | |
| 194 | 193 | |
| 195 | 194 | |
| 196 | - | |
| 197 | - private function log( $msg, $level ) { | |
| 195 | + private function log ( $msg, $level ) { | |
| 198 | 196 | if ( GITHUB_API_LEVEL >= $level ) { |
| 199 | - error_log( "[GE$level]: ".$msg ); | |
| 197 | + error_log ( "[GE$level]: ".$msg ); | |
| 200 | 198 | } |
| 201 | 199 | } |
| 202 | 200 | |
| 203 | 201 | |
| 204 | 202 | |
| 205 | -} | |
| 203 | +} | |