PluginProbe
Github Embed / 1.8
Github Embed v1.8
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 +58 -45 2.0.11.8 View file →
@@ -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
@@ -104,8 +103,11 @@
104 103 public function get_repo( $owner, $repository ) {
105 104
106 105 $this->log( "get_repo( $owner, $repository )", GEDEBUG_CALL );
107 106
107 + $owner = trim( $owner, '/' );
108 + $repository = trim( $repository, '/' );
109 +
108 110 $results = $this->call_api( "https://api.github.com/repos/$owner/$repository" );
109 111
110 112 return json_decode( $results['body'] );
111 113
@@ -111,14 +113,13 @@
111 113
112 114 }
113 115
114 116
117 +
115 118 /**
116 119 * 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 - *
120 + * @param string $owner The repository's owner
121 + * @param string $repository The respository name
121 122 * @return object The response from the GitHub API
122 123 */
123 124 public function get_repo_commits( $owner, $repository ) {
124 125
@@ -123,8 +124,11 @@
123 124 public function get_repo_commits( $owner, $repository ) {
124 125
125 126 $this->log( "get_repo_commits( $owner, $repository )", GEDEBUG_CALL );
126 127
128 + $owner = trim( $owner, '/' );
129 + $repository = trim( $repository, '/' );
130 +
127 131 $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/commits" );
128 132
129 133 return json_decode( $results['body'] );
130 134
@@ -130,15 +134,14 @@
130 134
131 135 }
132 136
133 137
138 +
134 139 /**
135 140 * 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 - *
141 + * @param string $owner The repository's owner
142 + * @param string $repository The respository name
143 + * @param string $milestone The milestone ID
141 144 * @return object The response from the GitHub API
142 145 */
143 146 public function get_repo_milestone_summary( $owner, $repository, $milestone ) {
144 147
@@ -143,8 +146,11 @@
143 146 public function get_repo_milestone_summary( $owner, $repository, $milestone ) {
144 147
145 148 $this->log( "get_repo_milestone_summary( $owner, $repository, $milestone )", GEDEBUG_CALL );
146 149
150 + $owner = trim( $owner, '/' );
151 + $repo = trim( $repository, '/' );
152 +
147 153 $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/milestones/$milestone" );
148 154
149 155 return json_decode( $results['body'] );
150 156
@@ -150,12 +156,16 @@
150 156
151 157 }
152 158
153 159
160 +
154 161 public function get_repo_contributors( $owner, $repository ) {
155 162
156 163 $this->log( "get_repo_contributors( $owner, $repository )", GEDEBUG_CALL );
157 164
165 + $owner = trim( $owner, '/' );
166 + $repo = trim( $repository, '/' );
167 +
158 168 $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/stats/contributors" );
159 169
160 170 return json_decode( $results['body'] );
161 171
@@ -161,13 +171,12 @@
161 171
162 172 }
163 173
164 174
175 +
165 176 /**
166 177 * Get a user from the GitHub API
167 - *
168 - * @param string $user The username
169 - *
178 + * @param string $user The username
170 179 * @return object The response from the GitHub API
171 180 */
172 181 public function get_user( $user ) {
173 182
@@ -172,8 +181,10 @@
172 181 public function get_user( $user ) {
173 182
174 183 $this->log( "get_user( $user )", GEDEBUG_CALL );
175 184
185 + $user = trim( $user, '/' );
186 +
176 187 $results = $this->call_api( "https://api.github.com/users/$user" );
177 188
178 189 return json_decode( $results['body'] );
179 190
@@ -179,12 +190,14 @@
179 190
180 191 }
181 192
182 193
194 +
183 195 private function log( $msg, $level ) {
184 196 if ( GITHUB_API_LEVEL >= $level ) {
185 - error_log( "[GE$level]: " . $msg );
197 + error_log( "[GE$level]: ".$msg );
186 198 }
187 199 }
200 +
188 201
189 202
190 203 }