PluginProbe
Github Embed / 2.2.0
Github Embed v2.2.0
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 2.2.0, at github-api.php

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