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