PluginProbe
Github Embed / 2.0
Github Embed v2.0
trunk 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.0.1 2.0.2 2.1.0 2.2.0 2.2.1
← All changes | github-api.php +45 -44 1.92.0 View file →
@@ -22,26 +22,24 @@
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;
26 28
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 -
34 33 add_action( 'plugins_loaded', array( $this, 'set_credentials' ) );
35 34 add_filter( 'http_request_timeout', array( $this, 'http_request_timeout' ) );
36 -
37 35 }
38 36
39 -
40 -
41 37 /**
42 38 * Extend the timeout since API calls can easily exceed 5 seconds
43 - * @param int $seconds The current timeout setting
39 + *
40 + * @param int $seconds The current timeout setting
41 + *
44 42 * @return int The revised timeout setting
45 43 */
46 44 function http_request_timeout( $seconds ) {
47 45 return $seconds < 25 ? 25 : $seconds;
@@ -46,10 +44,8 @@
46 44 function http_request_timeout( $seconds ) {
47 45 return $seconds < 25 ? 25 : $seconds;
48 46 }
49 47
50 -
51 -
52 48 /**
53 49 * If you find yourself hitting rate limits, then you can register an application
54 50 * with GitHub(http://developer.github.com/v3/oauth/) use the filters here to
55 51 * provide the credentials.
@@ -54,37 +50,41 @@
54 50 * with GitHub(http://developer.github.com/v3/oauth/) use the filters here to
55 51 * provide the credentials.
56 52 */
57 53 public function set_credentials() {
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 );
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 );
60 58 }
61 59
62 -
63 -
64 60 private function call_api( $url ) {
65 -
66 - // Allow users to supply auth details to enable a higher rate limit
61 + // Allow users to supply auth details to enable a higher rate limit [Deprecated]
67 62 if ( ! empty( $this->client_id ) && ! empty( $this->client_secret ) ) {
68 63 $url = add_query_arg(
69 - array(
70 - 'client_id' => $this->client_id,
71 - 'client_secret' => $this->client_secret ),
72 - $url
73 - );
64 + array(
65 + 'client_id' => $this->client_id,
66 + 'client_secret' => $this->client_secret
67 + ),
68 + $url
69 + );
74 70 }
75 71
76 - $args = array( 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed');
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 );
77 81
78 - $this->log( __FUNCTION__." : $url", GEDEBUG_CALL );
79 -
80 82 $results = wp_remote_get( $url, $args );
81 83
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' ) {
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,13 +92,14 @@
92 92
93 93 }
94 94
95 95
96 -
97 96 /**
98 97 * Get a repository from the GitHub API
99 - * @param string $owner The repository's owner
100 - * @param string $repository The respository name
98 + *
99 + * @param string $owner The repository's owner
100 + * @param string $repository The respository name
101 + *
101 102 * @return object The response from the GitHub API
102 103 */
103 104 public function get_repo( $owner, $repository ) {
104 105
@@ -110,13 +111,14 @@
110 111
111 112 }
112 113
113 114
114 -
115 115 /**
116 116 * Get commit information for a repository from the GitHub API
117 - * @param string $owner The repository's owner
118 - * @param string $repository The respository name
117 + *
118 + * @param string $owner The repository's owner
119 + * @param string $repository The respository name
120 + *
119 121 * @return object The response from the GitHub API
120 122 */
121 123 public function get_repo_commits( $owner, $repository ) {
122 124
@@ -128,14 +130,15 @@
128 130
129 131 }
130 132
131 133
132 -
133 134 /**
134 135 * Get a milestone summary from the GitHub API
135 - * @param string $owner The repository's owner
136 - * @param string $repository The respository name
137 - * @param string $milestone The milestone ID
136 + *
137 + * @param string $owner The repository's owner
138 + * @param string $repository The respository name
139 + * @param string $milestone The milestone ID
140 + *
138 141 * @return object The response from the GitHub API
139 142 */
140 143 public function get_repo_milestone_summary( $owner, $repository, $milestone ) {
141 144
@@ -147,9 +150,8 @@
147 150
148 151 }
149 152
150 153
151 -
152 154 public function get_repo_contributors( $owner, $repository ) {
153 155
154 156 $this->log( "get_repo_contributors( $owner, $repository )", GEDEBUG_CALL );
155 157
@@ -159,12 +161,13 @@
159 161
160 162 }
161 163
162 164
163 -
164 165 /**
165 166 * Get a user from the GitHub API
166 - * @param string $user The username
167 + *
168 + * @param string $user The username
169 + *
167 170 * @return object The response from the GitHub API
168 171 */
169 172 public function get_user( $user ) {
170 173
@@ -176,14 +179,12 @@
176 179
177 180 }
178 181
179 182
180 -
181 183 private function log( $msg, $level ) {
182 184 if ( GITHUB_API_LEVEL >= $level ) {
183 - error_log( "[GE$level]: ".$msg );
185 + error_log( "[GE$level]: " . $msg );
184 186 }
185 187 }
186 -
187 188
188 189
189 190 }