PluginProbe
Github Embed / 2.2.1
Github Embed v2.2.1
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 -70 1.92.2.1 View file →
@@ -13,43 +13,37 @@
13 13 if ( ! defined( 'GITHUB_API_LEVEL' ) ) {
14 14 define( 'GITHUB_API_LEVEL', GEDEBUG_NONE );
15 15 }
16 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 -
30 + public function __construct() {
34 31 add_action( 'plugins_loaded', array( $this, 'set_credentials' ) );
35 32 add_filter( 'http_request_timeout', array( $this, 'http_request_timeout' ) );
36 -
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 ) {
42 + public function http_request_timeout( $seconds ) {
47 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 48 * with GitHub(http://developer.github.com/v3/oauth/) use the filters here to
55 49 * provide the credentials.
@@ -54,37 +48,41 @@
54 48 * with GitHub(http://developer.github.com/v3/oauth/) use the filters here to
55 49 * provide the credentials.
56 50 */
57 51 public function set_credentials() {
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 );
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 56 }
61 57
62 -
63 -
64 58 private function call_api( $url ) {
65 -
66 - // 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]
67 60 if ( ! empty( $this->client_id ) && ! empty( $this->client_secret ) ) {
68 61 $url = add_query_arg(
69 - array(
70 - 'client_id' => $this->client_id,
71 - 'client_secret' => $this->client_secret ),
72 - $url
73 - );
62 + array(
63 + 'client_id' => $this->client_id,
64 + 'client_secret' => $this->client_secret
65 + ),
66 + $url
67 + );
74 68 }
75 69
76 - $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 );
77 79
78 - $this->log( __FUNCTION__." : $url", GEDEBUG_CALL );
79 -
80 80 $results = wp_remote_get( $url, $args );
81 81
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' ) {
82 + if ( is_wp_error( $results ) ||
83 + ! isset( $results['response']['code'] ) ||
84 + $results['response']['code'] != '200' ) {
87 85 header( 'HTTP/1.0 404 Not Found' );
88 86 die( 'Octocat is lost, and afraid' );
89 87 }
90 88
@@ -91,20 +89,18 @@
91 89 return $results;
92 90
93 91 }
94 92
95 -
96 -
97 93 /**
98 94 * Get a repository from the GitHub API
99 - * @param string $owner The repository's owner
100 - * @param string $repository The respository name
95 + *
96 + * @param string $owner The repository's owner
97 + * @param string $repository The respository name
98 + *
101 99 * @return object The response from the GitHub API
102 100 */
103 101 public function get_repo( $owner, $repository ) {
104 -
105 102 $this->log( "get_repo( $owner, $repository )", GEDEBUG_CALL );
106 -
107 103 $results = $this->call_api( "https://api.github.com/repos/$owner/$repository" );
108 104
109 105 return json_decode( $results['body'] );
110 106
@@ -109,81 +105,62 @@
109 105 return json_decode( $results['body'] );
110 106
111 107 }
112 108
113 -
114 -
115 109 /**
116 110 * Get commit information for a repository from the GitHub API
117 - * @param string $owner The repository's owner
118 - * @param string $repository The respository name
111 + *
112 + * @param string $owner The repository's owner
113 + * @param string $repository The respository name
114 + *
119 115 * @return object The response from the GitHub API
120 116 */
121 117 public function get_repo_commits( $owner, $repository ) {
122 -
123 118 $this->log( "get_repo_commits( $owner, $repository )", GEDEBUG_CALL );
124 -
125 119 $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/commits" );
126 120
127 121 return json_decode( $results['body'] );
128 -
129 122 }
130 123
131 -
132 -
133 124 /**
134 125 * Get a milestone summary from the GitHub API
135 - * @param string $owner The repository's owner
136 - * @param string $repository The respository name
137 - * @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 + *
138 131 * @return object The response from the GitHub API
139 132 */
140 133 public function get_repo_milestone_summary( $owner, $repository, $milestone ) {
141 -
142 134 $this->log( "get_repo_milestone_summary( $owner, $repository, $milestone )", GEDEBUG_CALL );
143 -
144 135 $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/milestones/$milestone" );
145 136
146 137 return json_decode( $results['body'] );
147 -
148 138 }
149 139
150 -
151 -
152 140 public function get_repo_contributors( $owner, $repository ) {
153 -
154 141 $this->log( "get_repo_contributors( $owner, $repository )", GEDEBUG_CALL );
155 -
156 142 $results = $this->call_api( "https://api.github.com/repos/$owner/$repository/stats/contributors" );
157 143
158 144 return json_decode( $results['body'] );
159 -
160 145 }
161 146
162 -
163 -
164 147 /**
165 148 * Get a user from the GitHub API
166 - * @param string $user The username
149 + *
150 + * @param string $user The username
151 + *
167 152 * @return object The response from the GitHub API
168 153 */
169 154 public function get_user( $user ) {
170 -
171 155 $this->log( "get_user( $user )", GEDEBUG_CALL );
172 -
173 156 $results = $this->call_api( "https://api.github.com/users/$user" );
174 157
175 158 return json_decode( $results['body'] );
176 -
177 159 }
178 160
179 -
180 -
181 161 private function log( $msg, $level ) {
182 162 if ( GITHUB_API_LEVEL >= $level ) {
183 - error_log( "[GE$level]: ".$msg );
163 + error_log( "[GE$level]: " . $msg );
184 164 }
185 165 }
186 -
187 -
188 -
189 166 }