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