1 | <?php |
||
10 | class WordPress_GitHub_Sync_Base_Client { |
||
11 | |||
12 | const TOKEN_OPTION_KEY = 'wpghs_oauth_token'; |
||
13 | const REPO_OPTION_KEY = 'wpghs_repository'; |
||
14 | const HOST_OPTION_KEY = 'wpghs_host'; |
||
15 | |||
16 | /** |
||
17 | * Application container. |
||
18 | * |
||
19 | * @var WordPress_GitHub_Sync |
||
20 | */ |
||
21 | protected $app; |
||
22 | |||
23 | /** |
||
24 | * Instantiates a new Api object. |
||
25 | * |
||
26 | * @param WordPress_GitHub_Sync $app Application container. |
||
27 | */ |
||
28 | 19 | public function __construct( WordPress_GitHub_Sync $app ) { |
|
31 | |||
32 | /** |
||
33 | * Generic GitHub API interface and response handler |
||
34 | * |
||
35 | * @param string $method HTTP method. |
||
36 | * @param string $endpoint API endpoint. |
||
37 | * @param array $body Request body. |
||
38 | * |
||
39 | * @return stdClass|WP_Error |
||
40 | */ |
||
41 | 16 | protected function call( $method, $endpoint, $body = array() ) { |
|
42 | 16 | if ( is_wp_error( $error = $this->can_call() ) ) { |
|
|
|||
43 | 3 | return $error; |
|
44 | } |
||
45 | |||
46 | $args = array( |
||
47 | 13 | 'method' => $method, |
|
48 | 'headers' => array( |
||
49 | 13 | 'Authorization' => 'token ' . $this->oauth_token(), |
|
50 | 13 | ), |
|
51 | 13 | ); |
|
52 | |||
53 | 13 | if ( 'GET' !== $method ) { |
|
54 | 5 | $args['body'] = function_exists( 'wp_json_encode' ) ? |
|
55 | 5 | wp_json_encode( $body ) : |
|
56 | 5 | json_encode( $body ); |
|
57 | 5 | } |
|
58 | |||
59 | 13 | $response = wp_remote_request( $endpoint, $args ); |
|
60 | 13 | $status = wp_remote_retrieve_header( $response, 'status' ); |
|
61 | 13 | $body = json_decode( wp_remote_retrieve_body( $response ) ); |
|
62 | |||
63 | 13 | if ( '2' !== substr( $status, 0, 1 ) && '3' !== substr( $status, 0, 1 ) ) { |
|
64 | 7 | return new WP_Error( |
|
65 | 7 | strtolower( str_replace( ' ', '_', $status ) ), |
|
66 | 7 | sprintf( |
|
67 | 7 | __( 'Method %s to endpoint %s failed with error: %s', 'wp-github-sync' ), |
|
68 | 7 | $method, |
|
69 | 7 | $endpoint, |
|
70 | 7 | $body && $body->message ? $body->message : 'Unknown error' |
|
71 | 7 | ) |
|
72 | 7 | ); |
|
73 | } |
||
74 | |||
75 | 11 | return $body; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Validates whether the Api object can make a call. |
||
80 | * |
||
81 | * @return true|WP_Error |
||
82 | */ |
||
83 | 16 | protected function can_call() { |
|
111 | |||
112 | /** |
||
113 | * Returns the repository to sync with |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | 16 | public function repository() { |
|
120 | |||
121 | /** |
||
122 | * Returns the user's oauth token |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 16 | public function oauth_token() { |
|
129 | |||
130 | /** |
||
131 | * Returns the GitHub host to sync with (for GitHub Enterprise support) |
||
132 | */ |
||
133 | 16 | public function api_base() { |
|
136 | |||
137 | /** |
||
138 | * API endpoint for the master branch reference |
||
139 | */ |
||
140 | 14 | public function reference_endpoint() { |
|
152 | |||
153 | /** |
||
154 | * Api to get and create commits |
||
155 | */ |
||
156 | 10 | public function commit_endpoint() { |
|
162 | |||
163 | /** |
||
164 | * Api to get and create trees |
||
165 | */ |
||
166 | 9 | public function tree_endpoint() { |
|
172 | |||
173 | /** |
||
174 | * Builds the proper blob API endpoint for a given post |
||
175 | * |
||
176 | * Returns String the relative API call path |
||
177 | */ |
||
178 | 2 | public function blob_endpoint() { |
|
179 | 2 | $url = $this->api_base() . '/repos/'; |
|
180 | 2 | $url = $url . $this->repository() . '/git/blobs'; |
|
181 | |||
182 | 2 | return $url; |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * Builds the proper content API endpoint for a given post |
||
187 | * |
||
188 | * Returns String the relative API call path |
||
189 | */ |
||
190 | public function content_endpoint() { |
||
196 | } |
||
197 |