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