PluginProbe
Github Embed / 2.0
Github Embed v2.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
github-embed / github-api.php

github-api.php in Github Embed 2.0, at github-api.php

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