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