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
github-embed / github-api.php

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

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