PluginProbe
Github Embed / 1.4
Github Embed v1.4
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.4, at github-api.php

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