This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-wpengine-api.php
391 lines (342 loc) · 11.4 KB
/
wp-wpengine-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
/**
* Library for accessing the WPengine API on WordPress
*
* @package WP-API-Libraries\WP-WPengine-API
*/
/*
* Plugin Name: WP WPengine API
* Plugin URI: https://wp-api-libraries.com/
* Description: Perform API requests.
* Author: WP API Libraries
* Version: 1.0.0
* Author URI: https://wp-api-libraries.com
* GitHub Plugin URI: https://github.com/imforza
* GitHub Branch: master
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'WPWPengineAPI' ) ) {
/**
* A WordPress API library for accessing the WPengine API.
*
* @version 1.1.0
* @link https://wpengineapi.com/reference API Documentation
* @package WP-API-Libraries\WP-WPengine-API
* @author Santiago Garza <https://github.com/sfgarza>
* @author imFORZA <https://github.com/imforza>
*/
class WPWPengineAPI {
/**
* Basic auth username.
*
* @var string
*/
protected $username;
/**
* Basic auth password.
*
* @var string
*/
protected $password;
/**
* WPengine BaseAPI Endpoint
*
* @var string
* @access protected
*/
protected $base_uri = 'https://api.wpengineapi.com/v0/';
/**
* Route being called.
*
* @var string
*/
protected $route = '';
/**
* Class constructor.
*
* @param string $username Auth username.
* @param string $password Auth password.
*/
public function __construct( $username, $password ) {
$this->username = $username;
$this->password = $password;
}
/**
* Prepares API request.
*
* @param string $route API route to make the call to.
* @param array $args Arguments to pass into the API call.
* @param array $method HTTP Method to use for request.
* @return self Returns an instance of itself so it can be chained to the fetch method.
*/
protected function build_request( $route, $args = array(), $method = 'GET' ) {
// Start building query.
$this->set_headers();
$this->args['method'] = $method;
$this->route = $route;
// Generate query string for GET requests.
if ( 'GET' === $method ) {
$this->route = add_query_arg( array_filter( $args ), $route );
} elseif ( 'application/json' === $this->args['headers']['Content-Type'] ) {
$this->args['body'] = wp_json_encode( $args );
} else {
$this->args['body'] = $args;
}
$this->args['timeout'] = 20;
return $this;
}
/**
* Fetch the request from the API.
*
* @access private
* @return array|WP_Error Request results or WP_Error on request failure.
*/
protected function fetch() {
// Make the request.
$response = wp_remote_request( $this->base_uri . $this->route, $this->args );
// Retrieve Status code & body.
$code = wp_remote_retrieve_response_code( $response );
$body = json_decode( wp_remote_retrieve_body( $response ) );
$this->clear();
// Return WP_Error if request is not successful.
if ( ! $this->is_status_ok( $code ) ) {
return new WP_Error( 'response-error', sprintf( __( 'Status: %d', 'wp-postmark-api' ), $code ), $body );
}
return $body;
}
/**
* Set request headers.
*/
protected function set_headers() {
// Set request headers.
$this->args['headers'] = array(
'Content-Type' => 'application/json',
'Authorization' => 'Basic ' . base64_encode( "{$this->username}:{$this->password}" ),
);
}
/**
* Clear query data.
*/
protected function clear() {
$this->args = array();
$this->query_args = array();
}
/**
* Check if HTTP status code is a success.
*
* @param int $code HTTP status code.
* @return boolean True if status is within valid range.
*/
protected function is_status_ok( $code ) {
return ( 200 <= $code && 300 > $code );
}
/*****************************************************************************************
Status
******************************************************************************************/
public function get_api_status() {
return $this->build_request( 'status' )->fetch();
}
/*****************************************************************************************
Swagger
******************************************************************************************/
public function get_swagger_spec() {
return $this->build_request( 'swagger' )->fetch();
}
/*****************************************************************************************
Accounts
******************************************************************************************/
/**
* List your WP Engine accounts
*
* @api GET
* @access public
* @param string $args Additional query arguments.
* @return array List of your WP Engine accounts
*/
public function get_accounts( $args = array() ) {
return $this->build_request( 'accounts', $args )->fetch();
}
/**
* Get an account by ID
*
* @param string $id Account ID
* @return mixed Returns a single Account
*/
public function get_account_by_id( $id ) {
return $this->build_request( "accounts/$id" )->fetch();
}
/*****************************************************************************************
Sites
******************************************************************************************/
/**
* List your sites
*
* @param array $args Additional arguments
* @return mixed List of sites
*/
public function get_sites( $args = array() ) {
return $this->build_request( 'sites', $args )->fetch();
}
/**
* Create a new site
*
* @param string $name The name of the site.
* @param string $account_id The ID of the account that the site will belong to.
* @return mixed The new site info.
*/
public function create_site( $name, $account_id ) {
$args = compact( $name, $account_id );
return $this->build_request( 'sites', $args, 'POST' )->fetch();
}
/**
* Get a site by ID
*
* @param string $id The site ID.
* @return mixed Returns a single site
*/
public function get_site_by_id( $id ) {
return $this->build_request( "sites/$id" )->fetch();
}
/**
* Change a sites name.
*
* @param string $id The ID of the site to change the name of (For accounts with sites enabled).
* @param string $name The new name for the site.
* @return mixed The new site data.
*/
public function update_site( $id, $name ) {
$args = compact( $name );
return $this->build_request( "sites/$id", $args, 'PATCH' )->fetch();
}
/**
* This will delete the site and any installs associated with this site. This delete is permanent and there is no confirmation prompt.
*
* @param string $id The ID of the site to delete (For accounts with sites enabled).
* @return string Deleted
*/
public function delete_site( $id ) {
return $this->build_request( "sites/$id", array(), 'DELETE' )->fetch();
}
/*****************************************************************************************
Installs
******************************************************************************************/
/**
* List your WordPress installations
*
* @param array $args Additional query arguments.
* @return mixed List of WordPress installs.
*/
public function get_installs( $args = array() ) {
return $this->build_request( 'installs', $args )->fetch();
}
/**
* Create a new WordPress installation
*
* @param string $name The name of the install.
* @param string $account_id The ID of the account that the install will belong to.
* @param string $site_id The ID of the site that the install will belong to.
* @param string $environment The site environment that the install will fill.
* @return mixed New install info
*/
public function create_install( $name, $account_id, $site_id, $environment ) {
$args = compact( $name, $account_id, $site_id, $environment );
return $this->build_request( 'installs', $args, 'POST' )->fetch();
}
/**
* Get an install by ID.
*
* @param string $id Install ID.
* @return mixed Returns a single Install
*/
public function get_install_by_id( $id ) {
return $this->build_request( "installs/$id" )->fetch();
}
/**
* Update a WordPress installation
*
* @param string $install_id Install ID.
* @param array $args Fields to update.
* @return mixed Updated install info
*/
public function update_install( $install_id, $args ) {
return $this->build_request( "installs/$install_id", $args, 'PATCH' )->fetch();
}
/**
* Delete an install by ID
*
* @param string $id Install ID.
* @return mixed Deleted
*/
public function delete_install( $id ) {
return $this->build_request( "installs/$id", array(), 'DELETE' )->fetch();
}
/*****************************************************************************************
Domains
******************************************************************************************/
/**
* Get the domains for an install by install id.
*
* @param string $install_id Install ID.
* @param array $args Additional query arguments.
* @return mixed Returns domains for a specific install
*/
public function get_domains( $install_id, $args = array() ) {
return $this->build_request( "installs/$install_id/domains", $args )->fetch();
}
/**
* Add a new domain to an existing install.
*
* @param string $install_id ID of install.
* @param string $name The name of the new domain.
* @param boolean $primary Sets the domain as the primary domain on the install.
* @return mixed New domain info.
*/
public function create_domain( $install_id, $name, $primary = false ) {
$args = compact( $name, $primary );
return $this->build_request( "installs/$install_id/domains", $args, 'POST' )->fetch();
}
/**
* Get a specific domain for an install
*
* @param string $install_id ID of install.
* @param string $domain_id ID of domain.
* @return mixed Returns specific domain for an install.
*/
public function get_domain_by_id( $install_id, $domain_id ) {
return $this->build_request( "installs/$install_id/domains/$domain_id" )->fetch();
}
/**
* Set an existing domain as primary.
*
* @param string $install_id ID of install.
* @param string $domain_id ID of domain.
* @param array $args Fields to update.
* @return mixed Updated domain info.
*/
public function update_domain( $install_id, $domain_id, $args ) {
return $this->build_request( "installs/$install_id/domains/$domain_id", $args, 'PATCH' )->fetch();
}
/**
* Delete a specific domain for an install.
*
* @param string $install_id ID of install.
* @param string $domain_id ID of domain.
* @return mixed Deleted.
*/
public function delete_domain( $install_id, $domain_id ) {
return $this->build_request( "installs/$install_id/domains/$domain_id", array(), 'DELETE' )->fetch();
}
/*****************************************************************************************
User
******************************************************************************************/
/**
* Get the current user.
*
* @return mixed Returns the currently authenticated user
*/
public function get_user() {
return $this->build_request( 'user' )->fetch();
}
}
}