| 1 |
<?php |
| 2 |
|
| 3 |
namespace FlyWP; |
| 4 |
|
| 5 |
class FlyApi { |
| 6 |
|
| 7 |
/** |
| 8 |
* Get the site's info. |
| 9 |
* |
| 10 |
* @return array|false |
| 11 |
*/ |
| 12 |
public function site_info() { |
| 13 |
return $this->get( '/info' ); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Set the site's cache status. |
| 18 |
* |
| 19 |
* @param string $action |
| 20 |
* |
| 21 |
* @return array|false |
| 22 |
*/ |
| 23 |
public function cache_toggle( $action = 'enable' ) { |
| 24 |
return $this->post( |
| 25 |
'/cache-toggle', |
| 26 |
[ |
| 27 |
'action' => $action, |
| 28 |
] |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get the API endpoint. |
| 34 |
* |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
protected function get_endpoint() { |
| 38 |
return apply_filters( 'flywp_api_endpoint', 'https://app.flywp.com/api/site-api' ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Send a GET request to the API. |
| 43 |
* |
| 44 |
* @param string $path |
| 45 |
* |
| 46 |
* @return array|false |
| 47 |
*/ |
| 48 |
public function get( $path ) { |
| 49 |
$url = $this->get_endpoint() . $path; |
| 50 |
|
| 51 |
$response = wp_remote_get( |
| 52 |
$url, |
| 53 |
[ |
| 54 |
'headers' => [ |
| 55 |
'Authorization' => 'Bearer ' . flywp()->get_key(), |
| 56 |
], |
| 57 |
] |
| 58 |
); |
| 59 |
|
| 60 |
if ( is_wp_error( $response ) ) { |
| 61 |
// Handle error if needed |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
$body = wp_remote_retrieve_body( $response ); |
| 66 |
$data = json_decode( $body, true ); |
| 67 |
|
| 68 |
return $data; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Send a POST request to the API. |
| 73 |
* |
| 74 |
* @param string $path |
| 75 |
* @param array $data |
| 76 |
* |
| 77 |
* @return array|false |
| 78 |
*/ |
| 79 |
public function post( $path, $data = [] ) { |
| 80 |
$url = $this->get_endpoint() . $path; |
| 81 |
|
| 82 |
$response = wp_remote_post( |
| 83 |
$url, |
| 84 |
[ |
| 85 |
'headers' => [ |
| 86 |
'Authorization' => 'Bearer ' . flywp()->get_key(), |
| 87 |
], |
| 88 |
'body' => $data, |
| 89 |
] |
| 90 |
); |
| 91 |
|
| 92 |
if ( is_wp_error( $response ) ) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
$body = wp_remote_retrieve_body( $response ); |
| 97 |
$data = json_decode( $body, true ); |
| 98 |
|
| 99 |
return $data; |
| 100 |
} |
| 101 |
} |
| 102 |
|