PluginProbe
Github Embed / 1.3
Github Embed v1.3
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 +101 -88 2.0.11.3 View file →
@@ -1,21 +1,21 @@
1 -<?php
1 +<?php
2 2
3 -// 0 - none.
4 -define( 'GEDEBUG_NONE', 0 );
5 3
6 -// 1 - call logging only.
7 -define( 'GEDEBUG_CALL', 1 );
8 4
9 -// 2 - calls, and responses.
10 -define( 'GEDEBUG_RESP', 2 );
5 +// 0 - none
6 +define ( 'GEDEBUG_NONE', 0 );
11 7
12 -// Selected debug level.
13 -if ( ! defined( 'GITHUB_API_LEVEL' ) ) {
14 - define( 'GITHUB_API_LEVEL', GEDEBUG_NONE );
15 -}
8 +// 1 - call logging only
9 +define ( 'GEDEBUG_CALL', 1 );
16 10
11 +// 2 - calls, and responses
12 +define ( 'GEDEBUG_RESP', 2 );
17 13
14 +// Selected debug level
15 +define ( 'GITHUB_API_LEVEL', GEDEBUG_RESP );
16 +
17 +
18 18 /**
19 19 * This class contains all the functions that actually retrieve information from the GitHub API
20 20 */
21 21 class github_api {
@@ -22,71 +22,70 @@
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 - add_action( 'plugins_loaded', array( $this, 'set_credentials' ) );
34 - add_filter( 'http_request_timeout', array( $this, 'http_request_timeout' ) );
33 +
34 + add_action ( 'plugins_loaded', array ( $this, 'set_credentials' ) );
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 - function http_request_timeout( $seconds ) {
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 - * with GitHub(http://developer.github.com/v3/oauth/) use the filters here to
54 + * with GitHub (http://developer.github.com/v3/oauth/) use the filters here to
51 55 * provide the credentials.
52 56 */
53 - 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 );
57 + public function set_credentials () {
58 +
59 + $this->client_id = apply_filters ( 'github-embed-client-id', $this->client_id );
60 + $this->client_secret = apply_filters ( 'github-embed-client-secret', $this->client_id );
61 +
58 62 }
59 63
60 - private function call_api( $url ) {
61 - // Allow users to supply auth details to enable a higher rate limit [Deprecated]
62 - if ( ! empty( $this->client_id ) && ! empty( $this->client_secret ) ) {
63 - $url = add_query_arg(
64 - array(
65 - 'client_id' => $this->client_id,
66 - 'client_secret' => $this->client_secret
67 - ),
68 - $url
69 - );
64 +
65 +
66 + private function call_api ( $url ) {
67 +
68 + // Allow users to supply auth details to enable a higher rate limit
69 + if ( ! empty ( $this->client_id ) && ! empty ( $this->client_secret ) ) {
70 + $url = add_query_arg(array ( 'client_id' => $this->client_id,
71 + 'client_secret' => $this->client_secret),
72 + $url );
70 73 }
71 74
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 );
75 + $args = array ( 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed');
76 +
77 + $this->log ( __FUNCTION__." : $url", GEDEBUG_CALL );
81 78
82 - $results = wp_remote_get( $url, $args );
79 + $results = wp_remote_get ( $url, $args );
83 80
81 + $this->log ( __FUNCTION__." : ".print_r($results,1), GEDEBUG_RESP );
82 +
84 83 if ( is_wp_error( $results ) ||
85 - ! isset( $results['response']['code'] ) ||
86 - $results['response']['code'] != '200' ) {
87 - header( 'HTTP/1.0 404 Not Found' );
88 - die( 'Octocat is lost, and afraid' );
84 + ! isset ( $results['response']['code'] ) ||
85 + $results['response']['code'] != '200' ) {
86 + header ( 'HTTP/1.0 404 Not Found' );
87 + die ( 'Octocat is lost, and afraid' );
89 88 }
90 89
91 90 return $results;
92 91
@@ -92,99 +91,113 @@
92 91
93 92 }
94 93
95 94
95 +
96 96 /**
97 97 * Get a repository from the GitHub API
98 - *
99 - * @param string $owner The repository's owner
100 - * @param string $repository The respository name
101 - *
98 + * @param string $owner The repository's owner
99 + * @param string $repository The respository name
102 100 * @return object The response from the GitHub API
103 101 */
104 - public function get_repo( $owner, $repository ) {
102 + public function get_repo ( $owner, $repository ) {
105 103
106 - $this->log( "get_repo( $owner, $repository )", GEDEBUG_CALL );
104 + $this->log ( "get_repo ( $owner, $repository )", GEDEBUG_CALL );
107 105
108 - $results = $this->call_api( "https://api.github.com/repos/$owner/$repository" );
106 + $owner = trim ( $owner, '/' );
107 + $repository = trim ( $repository, '/' );
109 108
110 - return json_decode( $results['body'] );
109 + $results = $this->call_api ( "https://api.github.com/repos/$owner/$repository" );
111 110
111 + return json_decode ( $results['body'] );
112 +
112 113 }
113 114
114 115
116 +
115 117 /**
116 118 * 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 - *
119 + * @param string $owner The repository's owner
120 + * @param string $repository The respository name
121 121 * @return object The response from the GitHub API
122 122 */
123 - public function get_repo_commits( $owner, $repository ) {
123 + public function get_repo_commits ( $owner, $repository ) {
124 124
125 - $this->log( "get_repo_commits( $owner, $repository )", GEDEBUG_CALL );
125 + $this->log ( "get_repo_commits ( $owner, $repository )", GEDEBUG_CALL );
126 126
127 - $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/commits" );
127 + $owner = trim ( $owner, '/' );
128 + $repository = trim ( $repository, '/' );
128 129
129 - return json_decode( $results['body'] );
130 + $results = $this->call_api ( "https://api.github.com/repos/$owner/$repository/commits" );
130 131
132 + return json_decode ( $results['body'] );
133 +
131 134 }
132 135
133 136
137 +
134 138 /**
135 139 * 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 - *
140 + * @param string $owner The repository's owner
141 + * @param string $repository The respository name
142 + * @param string $milestone The milestone ID
141 143 * @return object The response from the GitHub API
142 144 */
143 - public function get_repo_milestone_summary( $owner, $repository, $milestone ) {
145 + public function get_repo_milestone_summary ( $owner, $repository, $milestone ) {
144 146
145 - $this->log( "get_repo_milestone_summary( $owner, $repository, $milestone )", GEDEBUG_CALL );
147 + $this->log ( "get_repo_milestone_summary ( $owner, $repository, $milestone )", GEDEBUG_CALL );
146 148
147 - $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/milestones/$milestone" );
149 + $owner = trim ( $owner, '/' );
150 + $repo = trim ( $repo, '/' );
148 151
149 - return json_decode( $results['body'] );
152 + $results = $this->call_api ( "https://api.github.com/repos/$owner/$repository/milestones/$milestone" );
150 153
154 + return json_decode ( $results['body'] );
155 +
151 156 }
152 157
153 158
154 - public function get_repo_contributors( $owner, $repository ) {
155 159
156 - $this->log( "get_repo_contributors( $owner, $repository )", GEDEBUG_CALL );
160 + public function get_repo_contributors ( $owner, $repository ) {
157 161
158 - $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/stats/contributors" );
162 + $this->log ( "get_repo_contributors ( $owner, $repository )", GEDEBUG_CALL );
159 163
160 - return json_decode( $results['body'] );
164 + $owner = trim ( $owner, '/' );
165 + $repo = trim ( $repo, '/' );
161 166
167 + $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/collaborators" );
168 +
169 + return json_decode ( $results['body'] );
170 +
162 171 }
163 172
164 173
174 +
165 175 /**
166 176 * Get a user from the GitHub API
167 - *
168 - * @param string $user The username
169 - *
177 + * @param string $user The username
170 178 * @return object The response from the GitHub API
171 179 */
172 - public function get_user( $user ) {
180 + public function get_user ( $user ) {
173 181
174 - $this->log( "get_user( $user )", GEDEBUG_CALL );
182 + $this->log ( "get_user ( $user )", GEDEBUG_CALL );
175 183
176 - $results = $this->call_api( "https://api.github.com/users/$user" );
184 + $user = trim ( $user, '/' );
185 + $repository = trim ( $repository, '/' );
177 186
178 - return json_decode( $results['body'] );
187 + $results = $this->call_api ( "https://api.github.com/users/$user" );
179 188
189 + return json_decode ( $results['body'] );
190 +
180 191 }
192 +
181 193
182 194
183 - private function log( $msg, $level ) {
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 }
188 200
189 201
190 -}
202 +
203 +}