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