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