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