PluginProbe
Github Embed / 1.5
Github Embed v1.5
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 +93 -54 2.0.21.5 View file →
@@ -1,49 +1,55 @@
1 1 <?php
2 2
3 -// 0 - none.
3 +
4 +
5 +// 0 - none
4 6 define( 'GEDEBUG_NONE', 0 );
5 7
6 -// 1 - call logging only.
8 +// 1 - call logging only
7 9 define( 'GEDEBUG_CALL', 1 );
8 10
9 -// 2 - calls, and responses.
11 +// 2 - calls, and responses
10 12 define( 'GEDEBUG_RESP', 2 );
11 13
12 -// Selected debug level.
13 -if ( ! defined( 'GITHUB_API_LEVEL' ) ) {
14 - define( 'GITHUB_API_LEVEL', GEDEBUG_NONE );
15 -}
14 +// Selected debug level
15 +define( 'GITHUB_API_LEVEL', GEDEBUG_NONE );
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,39 @@
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 +
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_secret );
61 +
56 62 }
57 63
64 +
65 +
58 66 private function call_api( $url ) {
59 - // Allow users to supply auth details to enable a higher rate limit [Deprecated]
67 +
68 + // Allow users to supply auth details to enable a higher rate limit
60 69 if ( ! empty( $this->client_id ) && ! empty( $this->client_secret ) ) {
61 70 $url = add_query_arg(
62 - array(
63 - 'client_id' => $this->client_id,
64 - 'client_secret' => $this->client_secret
65 - ),
66 - $url
67 - );
71 + array(
72 + 'client_id' => $this->client_id,
73 + 'client_secret' => $this->client_secret ),
74 + $url
75 + );
68 76 }
69 77
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 );
78 + $args = array( 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed');
79 79
80 + $this->log( __FUNCTION__." : $url", GEDEBUG_CALL );
81 +
80 82 $results = wp_remote_get( $url, $args );
81 83
82 - if ( is_wp_error( $results ) ||
83 - ! isset( $results['response']['code'] ) ||
84 - $results['response']['code'] != '200' ) {
84 + $this->log( __FUNCTION__ . " : " . print_r( $results,1 ), GEDEBUG_RESP );
85 +
86 + if( is_wp_error( $results ) ||
87 + ! isset( $results['response']['code'] ) ||
88 + $results['response']['code'] != '200' ) {
85 89 header( 'HTTP/1.0 404 Not Found' );
86 90 die( 'Octocat is lost, and afraid' );
87 91 }
88 92
@@ -89,18 +93,23 @@
89 93 return $results;
90 94
91 95 }
92 96
97 +
98 +
93 99 /**
94 100 * Get a repository from the GitHub API
95 - *
96 - * @param string $owner The repository's owner
97 - * @param string $repository The respository name
98 - *
101 + * @param string $owner The repository's owner
102 + * @param string $repository The respository name
99 103 * @return object The response from the GitHub API
100 104 */
101 105 public function get_repo( $owner, $repository ) {
106 +
102 107 $this->log( "get_repo( $owner, $repository )", GEDEBUG_CALL );
108 +
109 + $owner = trim( $owner, '/' );
110 + $repository = trim( $repository, '/' );
111 +
103 112 $results = $this->call_api( "https://api.github.com/repos/$owner/$repository" );
104 113
105 114 return json_decode( $results['body'] );
106 115
@@ -105,62 +114,92 @@
105 114 return json_decode( $results['body'] );
106 115
107 116 }
108 117
118 +
119 +
109 120 /**
110 121 * 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 - *
122 + * @param string $owner The repository's owner
123 + * @param string $repository The respository name
115 124 * @return object The response from the GitHub API
116 125 */
117 126 public function get_repo_commits( $owner, $repository ) {
127 +
118 128 $this->log( "get_repo_commits( $owner, $repository )", GEDEBUG_CALL );
129 +
130 + $owner = trim( $owner, '/' );
131 + $repository = trim( $repository, '/' );
132 +
119 133 $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/commits" );
120 134
121 135 return json_decode( $results['body'] );
136 +
122 137 }
123 138
139 +
140 +
124 141 /**
125 142 * 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 - *
143 + * @param string $owner The repository's owner
144 + * @param string $repository The respository name
145 + * @param string $milestone The milestone ID
131 146 * @return object The response from the GitHub API
132 147 */
133 148 public function get_repo_milestone_summary( $owner, $repository, $milestone ) {
149 +
134 150 $this->log( "get_repo_milestone_summary( $owner, $repository, $milestone )", GEDEBUG_CALL );
151 +
152 + $owner = trim( $owner, '/' );
153 + $repo = trim( $repository, '/' );
154 +
135 155 $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/milestones/$milestone" );
136 156
137 157 return json_decode( $results['body'] );
158 +
138 159 }
139 160
161 +
162 +
140 163 public function get_repo_contributors( $owner, $repository ) {
164 +
141 165 $this->log( "get_repo_contributors( $owner, $repository )", GEDEBUG_CALL );
166 +
167 + $owner = trim( $owner, '/' );
168 + $repo = trim( $repository, '/' );
169 +
142 170 $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/stats/contributors" );
143 171
144 172 return json_decode( $results['body'] );
173 +
145 174 }
146 175
176 +
177 +
147 178 /**
148 179 * Get a user from the GitHub API
149 - *
150 - * @param string $user The username
151 - *
180 + * @param string $user The username
152 181 * @return object The response from the GitHub API
153 182 */
154 183 public function get_user( $user ) {
184 +
155 185 $this->log( "get_user( $user )", GEDEBUG_CALL );
186 +
187 + $user = trim( $user, '/' );
188 +
156 189 $results = $this->call_api( "https://api.github.com/users/$user" );
157 190
158 191 return json_decode( $results['body'] );
192 +
159 193 }
160 194
195 +
196 +
161 197 private function log( $msg, $level ) {
162 198 if ( GITHUB_API_LEVEL >= $level ) {
163 - error_log( "[GE$level]: " . $msg );
199 + error_log( "[GE$level]: ".$msg );
164 200 }
165 201 }
202 +
203 +
204 +
166 205 }