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