PluginProbe
Github Embed / 2.0.1
Github Embed v2.0.1
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 +88 -101 1.32.0.1 View file →
@@ -1,21 +1,21 @@
1 -<?php
1 +<?php
2 2
3 +// 0 - none.
4 +define( 'GEDEBUG_NONE', 0 );
3 5
6 +// 1 - call logging only.
7 +define( 'GEDEBUG_CALL', 1 );
4 8
5 -// 0 - none
6 -define ( 'GEDEBUG_NONE', 0 );
9 +// 2 - calls, and responses.
10 +define( 'GEDEBUG_RESP', 2 );
7 11
8 -// 1 - call logging only
9 -define ( 'GEDEBUG_CALL', 1 );
12 +// Selected debug level.
13 +if ( ! defined( 'GITHUB_API_LEVEL' ) ) {
14 + define( 'GITHUB_API_LEVEL', GEDEBUG_NONE );
15 +}
10 16
11 -// 2 - calls, and responses
12 -define ( 'GEDEBUG_RESP', 2 );
13 17
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,70 +22,71 @@
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 - add_action ( 'plugins_loaded', array ( $this, 'set_credentials' ) );
35 - add_filter ( 'http_request_timeout', array ( $this, 'http_request_timeout' ) );
36 -
33 + add_action( 'plugins_loaded', array( $this, 'set_credentials' ) );
34 + add_filter( 'http_request_timeout', array( $this, 'http_request_timeout' ) );
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 - function http_request_timeout ( $seconds ) {
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 - * with GitHub (http://developer.github.com/v3/oauth/) use the filters here to
50 + * with GitHub(http://developer.github.com/v3/oauth/) use the filters here to
55 51 * provide the credentials.
56 52 */
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 -
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 );
62 58 }
63 59
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 + );
70 + }
64 71
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 );
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 + ];
73 79 }
80 + $this->log( __FUNCTION__ . " : $url", GEDEBUG_CALL );
74 81
75 - $args = array ( 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed');
76 -
77 - $this->log ( __FUNCTION__." : $url", GEDEBUG_CALL );
82 + $results = wp_remote_get( $url, $args );
78 83
79 - $results = wp_remote_get ( $url, $args );
80 -
81 - $this->log ( __FUNCTION__." : ".print_r($results,1), GEDEBUG_RESP );
82 -
83 84 if ( is_wp_error( $results ) ||
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' );
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' );
88 89 }
89 90
90 91 return $results;
91 92
@@ -91,113 +92,99 @@
91 92
92 93 }
93 94
94 95
95 -
96 96 /**
97 97 * Get a repository from the GitHub API
98 - * @param string $owner The repository's owner
99 - * @param string $repository The respository name
98 + *
99 + * @param string $owner The repository's owner
100 + * @param string $repository The respository name
101 + *
100 102 * @return object The response from the GitHub API
101 103 */
102 - public function get_repo ( $owner, $repository ) {
104 + public function get_repo( $owner, $repository ) {
103 105
104 - $this->log ( "get_repo ( $owner, $repository )", GEDEBUG_CALL );
106 + $this->log( "get_repo( $owner, $repository )", GEDEBUG_CALL );
105 107
106 - $owner = trim ( $owner, '/' );
107 - $repository = trim ( $repository, '/' );
108 + $results = $this->call_api( "https://api.github.com/repos/$owner/$repository" );
108 109
109 - $results = $this->call_api ( "https://api.github.com/repos/$owner/$repository" );
110 + return json_decode( $results['body'] );
110 111
111 - return json_decode ( $results['body'] );
112 -
113 112 }
114 113
115 114
116 -
117 115 /**
118 116 * Get commit information for a repository from the GitHub API
119 - * @param string $owner The repository's owner
120 - * @param string $repository The respository name
117 + *
118 + * @param string $owner The repository's owner
119 + * @param string $repository The respository name
120 + *
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 - $owner = trim ( $owner, '/' );
128 - $repository = trim ( $repository, '/' );
127 + $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/commits" );
129 128
130 - $results = $this->call_api ( "https://api.github.com/repos/$owner/$repository/commits" );
129 + return json_decode( $results['body'] );
131 130
132 - return json_decode ( $results['body'] );
133 -
134 131 }
135 132
136 133
137 -
138 134 /**
139 135 * Get a milestone summary from the GitHub API
140 - * @param string $owner The repository's owner
141 - * @param string $repository The respository name
142 - * @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 + *
143 141 * @return object The response from the GitHub API
144 142 */
145 - public function get_repo_milestone_summary ( $owner, $repository, $milestone ) {
143 + public function get_repo_milestone_summary( $owner, $repository, $milestone ) {
146 144
147 - $this->log ( "get_repo_milestone_summary ( $owner, $repository, $milestone )", GEDEBUG_CALL );
145 + $this->log( "get_repo_milestone_summary( $owner, $repository, $milestone )", GEDEBUG_CALL );
148 146
149 - $owner = trim ( $owner, '/' );
150 - $repo = trim ( $repo, '/' );
147 + $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/milestones/$milestone" );
151 148
152 - $results = $this->call_api ( "https://api.github.com/repos/$owner/$repository/milestones/$milestone" );
149 + return json_decode( $results['body'] );
153 150
154 - return json_decode ( $results['body'] );
155 -
156 151 }
157 152
158 153
154 + public function get_repo_contributors( $owner, $repository ) {
159 155
160 - public function get_repo_contributors ( $owner, $repository ) {
156 + $this->log( "get_repo_contributors( $owner, $repository )", GEDEBUG_CALL );
161 157
162 - $this->log ( "get_repo_contributors ( $owner, $repository )", GEDEBUG_CALL );
158 + $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/stats/contributors" );
163 159
164 - $owner = trim ( $owner, '/' );
165 - $repo = trim ( $repo, '/' );
160 + return json_decode( $results['body'] );
166 161
167 - $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/collaborators" );
168 -
169 - return json_decode ( $results['body'] );
170 -
171 162 }
172 163
173 164
174 -
175 165 /**
176 166 * Get a user from the GitHub API
177 - * @param string $user The username
167 + *
168 + * @param string $user The username
169 + *
178 170 * @return object The response from the GitHub API
179 171 */
180 - public function get_user ( $user ) {
172 + public function get_user( $user ) {
181 173
182 - $this->log ( "get_user ( $user )", GEDEBUG_CALL );
174 + $this->log( "get_user( $user )", GEDEBUG_CALL );
183 175
184 - $user = trim ( $user, '/' );
185 - $repository = trim ( $repository, '/' );
176 + $results = $this->call_api( "https://api.github.com/users/$user" );
186 177
187 - $results = $this->call_api ( "https://api.github.com/users/$user" );
178 + return json_decode( $results['body'] );
188 179
189 - return json_decode ( $results['body'] );
190 -
191 180 }
192 -
193 181
194 182
195 - private function log ( $msg, $level ) {
183 + private function log( $msg, $level ) {
196 184 if ( GITHUB_API_LEVEL >= $level ) {
197 - error_log ( "[GE$level]: ".$msg );
185 + error_log( "[GE$level]: " . $msg );
198 186 }
199 187 }
200 188
201 189
202 -
203 -}
190 +}