| @@ -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,37 @@ | ||
| 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 | + $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 ); | |
| 58 | 60 | } |
| 59 | 61 | |
| 62 | + | |
| 63 | + | |
| 60 | 64 | private function call_api( $url ) { |
| 61 | - // Allow users to supply auth details to enable a higher rate limit [Deprecated] | |
| 65 | + | |
| 66 | + // Allow users to supply auth details to enable a higher rate limit | |
| 62 | 67 | if ( ! empty( $this->client_id ) && ! empty( $this->client_secret ) ) { |
| 63 | 68 | $url = add_query_arg( |
| 64 | - array( | |
| 65 | - 'client_id' => $this->client_id, | |
| 66 | - 'client_secret' => $this->client_secret | |
| 67 | - ), | |
| 68 | - $url | |
| 69 | - ); | |
| 69 | + array( | |
| 70 | + 'client_id' => $this->client_id, | |
| 71 | + 'client_secret' => $this->client_secret ), | |
| 72 | + $url | |
| 73 | + ); | |
| 70 | 74 | } |
| 71 | 75 | |
| 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 ); | |
| 76 | + $args = array( 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed'); | |
| 81 | 77 | |
| 78 | + $this->log( __FUNCTION__." : $url", GEDEBUG_CALL ); | |
| 79 | + | |
| 82 | 80 | $results = wp_remote_get( $url, $args ); |
| 83 | 81 | |
| 84 | - if ( is_wp_error( $results ) || | |
| 85 | - ! isset( $results['response']['code'] ) || | |
| 86 | - $results['response']['code'] != '200' ) { | |
| 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' ) { | |
| 87 | 87 | header( 'HTTP/1.0 404 Not Found' ); |
| 88 | 88 | die( 'Octocat is lost, and afraid' ); |
| 89 | 89 | } |
| 90 | 90 | |
| @@ -92,14 +92,13 @@ | ||
| 92 | 92 | |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | |
| 96 | + | |
| 96 | 97 | /** |
| 97 | 98 | * Get a repository from the GitHub API |
| 98 | - * | |
| 99 | - * @param string $owner The repository's owner | |
| 100 | - * @param string $repository The respository name | |
| 101 | - * | |
| 99 | + * @param string $owner The repository's owner | |
| 100 | + * @param string $repository The respository name | |
| 102 | 101 | * @return object The response from the GitHub API |
| 103 | 102 | */ |
| 104 | 103 | public function get_repo( $owner, $repository ) { |
| 105 | 104 | |
| @@ -111,14 +110,13 @@ | ||
| 111 | 110 | |
| 112 | 111 | } |
| 113 | 112 | |
| 114 | 113 | |
| 114 | + | |
| 115 | 115 | /** |
| 116 | 116 | * 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 | - * | |
| 117 | + * @param string $owner The repository's owner | |
| 118 | + * @param string $repository The respository name | |
| 121 | 119 | * @return object The response from the GitHub API |
| 122 | 120 | */ |
| 123 | 121 | public function get_repo_commits( $owner, $repository ) { |
| 124 | 122 | |
| @@ -130,15 +128,14 @@ | ||
| 130 | 128 | |
| 131 | 129 | } |
| 132 | 130 | |
| 133 | 131 | |
| 132 | + | |
| 134 | 133 | /** |
| 135 | 134 | * 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 | - * | |
| 135 | + * @param string $owner The repository's owner | |
| 136 | + * @param string $repository The respository name | |
| 137 | + * @param string $milestone The milestone ID | |
| 141 | 138 | * @return object The response from the GitHub API |
| 142 | 139 | */ |
| 143 | 140 | public function get_repo_milestone_summary( $owner, $repository, $milestone ) { |
| 144 | 141 | |
| @@ -150,8 +147,9 @@ | ||
| 150 | 147 | |
| 151 | 148 | } |
| 152 | 149 | |
| 153 | 150 | |
| 151 | + | |
| 154 | 152 | public function get_repo_contributors( $owner, $repository ) { |
| 155 | 153 | |
| 156 | 154 | $this->log( "get_repo_contributors( $owner, $repository )", GEDEBUG_CALL ); |
| 157 | 155 | |
| @@ -161,13 +159,12 @@ | ||
| 161 | 159 | |
| 162 | 160 | } |
| 163 | 161 | |
| 164 | 162 | |
| 163 | + | |
| 165 | 164 | /** |
| 166 | 165 | * Get a user from the GitHub API |
| 167 | - * | |
| 168 | - * @param string $user The username | |
| 169 | - * | |
| 166 | + * @param string $user The username | |
| 170 | 167 | * @return object The response from the GitHub API |
| 171 | 168 | */ |
| 172 | 169 | public function get_user( $user ) { |
| 173 | 170 | |
| @@ -179,12 +176,14 @@ | ||
| 179 | 176 | |
| 180 | 177 | } |
| 181 | 178 | |
| 182 | 179 | |
| 180 | + | |
| 183 | 181 | private function log( $msg, $level ) { |
| 184 | 182 | if ( GITHUB_API_LEVEL >= $level ) { |
| 185 | - error_log( "[GE$level]: " . $msg ); | |
| 183 | + error_log( "[GE$level]: ".$msg ); | |
| 186 | 184 | } |
| 187 | 185 | } |
| 186 | + | |
| 188 | 187 | |
| 189 | 188 | |
| 190 | 189 | } |