| 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 25; |
| 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_id ); |
| 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(array ( 'client_id' => $this->client_id, |
| 71 |
'client_secret' => $this->client_secret), |
| 72 |
$url ); |
| 73 |
} |
| 74 |
|
| 75 |
$args = array ( 'user-agent' => 'WordPress Github oEmbed plugin - https://github.com/leewillis77/wp-github-oembed'); |
| 76 |
|
| 77 |
$this->log ( __FUNCTION__." : $url", GEDEBUG_CALL ); |
| 78 |
|
| 79 |
$results = wp_remote_get ( $url, $args ); |
| 80 |
|
| 81 |
$this->log ( __FUNCTION__." : ".print_r($results,1), GEDEBUG_RESP ); |
| 82 |
|
| 83 |
if ( is_wp_error( $results ) || |
| 84 |
! isset ( $results['response']['code'] ) || |
| 85 |
$results['response']['code'] != '200' ) { |
| 86 |
header ( 'HTTP/1.0 404 Not Found' ); |
| 87 |
die ( 'Octocat is lost, and afraid' ); |
| 88 |
} |
| 89 |
|
| 90 |
return $results; |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
/** |
| 97 |
* Get a repository from the GitHub API |
| 98 |
* @param string $owner The repository's owner |
| 99 |
* @param string $repository The respository name |
| 100 |
* @return object The response from the GitHub API |
| 101 |
*/ |
| 102 |
public function get_repo ( $owner, $repository ) { |
| 103 |
|
| 104 |
$this->log ( "get_repo ( $owner, $repository )", GEDEBUG_CALL ); |
| 105 |
|
| 106 |
$owner = trim ( $owner, '/' ); |
| 107 |
$repository = trim ( $repository, '/' ); |
| 108 |
|
| 109 |
$results = $this->call_api ( "https://api.github.com/repos/$owner/$repository" ); |
| 110 |
|
| 111 |
return json_decode ( $results['body'] ); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
/** |
| 118 |
* Get commit information for a repository from the GitHub API |
| 119 |
* @param string $owner The repository's owner |
| 120 |
* @param string $repository The respository name |
| 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 |
$owner = trim ( $owner, '/' ); |
| 128 |
$repository = trim ( $repository, '/' ); |
| 129 |
|
| 130 |
$results = $this->call_api ( "https://api.github.com/repos/$owner/$repository/commits" ); |
| 131 |
|
| 132 |
return json_decode ( $results['body'] ); |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* Get a milestone summary from the GitHub API |
| 140 |
* @param string $owner The repository's owner |
| 141 |
* @param string $repository The respository name |
| 142 |
* @param string $milestone The milestone ID |
| 143 |
* @return object The response from the GitHub API |
| 144 |
*/ |
| 145 |
public function get_repo_milestone_summary ( $owner, $repository, $milestone ) { |
| 146 |
|
| 147 |
$this->log ( "get_repo_milestone_summary ( $owner, $repository, $milestone )", GEDEBUG_CALL ); |
| 148 |
|
| 149 |
$owner = trim ( $owner, '/' ); |
| 150 |
$repo = trim ( $repo, '/' ); |
| 151 |
|
| 152 |
$results = $this->call_api ( "https://api.github.com/repos/$owner/$repository/milestones/$milestone" ); |
| 153 |
|
| 154 |
return json_decode ( $results['body'] ); |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public function get_repo_contributors ( $owner, $repository ) { |
| 161 |
|
| 162 |
$this->log ( "get_repo_contributors ( $owner, $repository )", GEDEBUG_CALL ); |
| 163 |
|
| 164 |
$owner = trim ( $owner, '/' ); |
| 165 |
$repo = trim ( $repo, '/' ); |
| 166 |
|
| 167 |
$results = $this->call_api( "https://api.github.com/repos/$owner/$repository/collaborators" ); |
| 168 |
|
| 169 |
return json_decode ( $results['body'] ); |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
/** |
| 176 |
* Get a user from the GitHub API |
| 177 |
* @param string $user The username |
| 178 |
* @return object The response from the GitHub API |
| 179 |
*/ |
| 180 |
public function get_user ( $user ) { |
| 181 |
|
| 182 |
$this->log ( "get_user ( $user )", GEDEBUG_CALL ); |
| 183 |
|
| 184 |
$user = trim ( $user, '/' ); |
| 185 |
$repository = trim ( $repository, '/' ); |
| 186 |
|
| 187 |
$results = $this->call_api ( "https://api.github.com/users/$user" ); |
| 188 |
|
| 189 |
return json_decode ( $results['body'] ); |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
private function log ( $msg, $level ) { |
| 196 |
if ( GITHUB_API_LEVEL >= $level ) { |
| 197 |
error_log ( "[GE$level]: ".$msg ); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
} |