| 1 |
<?php |
| 2 |
/** |
| 3 |
* The Rest API class. |
| 4 |
* |
| 5 |
* @link http://wpmudev.com |
| 6 |
* @since 1.0.0 |
| 7 |
* @author Joel James <[email protected]> |
| 8 |
* @package WPMUDEV\Hub\Connector |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace WPMUDEV\Hub\Connector; |
| 12 |
|
| 13 |
use WP_REST_Server; |
| 14 |
use WP_REST_Response; |
| 15 |
use WP_REST_Request; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class Rest |
| 19 |
*/ |
| 20 |
class Rest { |
| 21 |
|
| 22 |
use Singleton; |
| 23 |
|
| 24 |
/** |
| 25 |
* API namespace. |
| 26 |
* |
| 27 |
* @var string $namespace |
| 28 |
*/ |
| 29 |
protected $namespace = 'hub-connector/v1'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Initialize Rest class. |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
*/ |
| 36 |
protected function __construct() { |
| 37 |
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Register the routes for rest API. |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function register_routes() { |
| 48 |
// Sync endpoint. |
| 49 |
register_rest_route( |
| 50 |
$this->namespace, |
| 51 |
'sync', |
| 52 |
array( |
| 53 |
array( |
| 54 |
'methods' => WP_REST_Server::EDITABLE, |
| 55 |
'callback' => array( $this, 'sync' ), |
| 56 |
'permission_callback' => function () { |
| 57 |
return current_user_can( 'manage_options' ); |
| 58 |
}, |
| 59 |
'args' => array( |
| 60 |
'force' => array( |
| 61 |
'required' => false, |
| 62 |
'description' => __( 'Should do a force sync.', 'wpmudev' ), |
| 63 |
'type' => 'boolean', |
| 64 |
'default' => false, |
| 65 |
), |
| 66 |
), |
| 67 |
), |
| 68 |
) |
| 69 |
); |
| 70 |
|
| 71 |
// Sync endpoint. |
| 72 |
register_rest_route( |
| 73 |
$this->namespace, |
| 74 |
'logout', |
| 75 |
array( |
| 76 |
array( |
| 77 |
'methods' => WP_REST_Server::EDITABLE, |
| 78 |
'callback' => array( $this, 'logout' ), |
| 79 |
'permission_callback' => function () { |
| 80 |
return current_user_can( 'manage_options' ); |
| 81 |
}, |
| 82 |
'args' => array(), |
| 83 |
), |
| 84 |
) |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Callback for sync API request. |
| 90 |
* |
| 91 |
* @since 1.0.0 |
| 92 |
* |
| 93 |
* @param WP_REST_Request $request Request object. |
| 94 |
* |
| 95 |
* @return WP_REST_Response |
| 96 |
*/ |
| 97 |
public function sync( $request ) { |
| 98 |
// Should we do a force sync?. |
| 99 |
$force = $request->get_param( 'force' ); |
| 100 |
|
| 101 |
// Do hub sync. |
| 102 |
$response = API::get()->sync_site( $force ); |
| 103 |
|
| 104 |
// If error, get error message. |
| 105 |
if ( is_wp_error( $response ) ) { |
| 106 |
return $this->get_response( |
| 107 |
$response->get_error_message(), |
| 108 |
false |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
return $this->get_response(); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Callback for logout API request. |
| 117 |
* |
| 118 |
* @since 1.0.0 |
| 119 |
* |
| 120 |
* @return WP_REST_Response |
| 121 |
*/ |
| 122 |
public function logout() { |
| 123 |
// Logout. |
| 124 |
$response = API::get()->logout(); |
| 125 |
|
| 126 |
// If error, get error message. |
| 127 |
if ( is_wp_error( $response ) ) { |
| 128 |
return $this->get_response( |
| 129 |
$response->get_error_message(), |
| 130 |
false |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
return $this->get_response(); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get API response data. |
| 139 |
* |
| 140 |
* @since 1.0.0 |
| 141 |
* |
| 142 |
* @param array $data Data to return. |
| 143 |
* @param bool $success Success?. |
| 144 |
* @param int $code Status code. |
| 145 |
* |
| 146 |
* @return WP_REST_Response |
| 147 |
*/ |
| 148 |
private function get_response( $data = array(), $success = true, $code = 200 ) { |
| 149 |
$response = array( |
| 150 |
'success' => $success, |
| 151 |
); |
| 152 |
|
| 153 |
// Response data. |
| 154 |
if ( $success ) { |
| 155 |
$response['data'] = $data; |
| 156 |
} else { |
| 157 |
$response['error'] = $data; |
| 158 |
} |
| 159 |
|
| 160 |
return new WP_REST_Response( $response, $code ); |
| 161 |
} |
| 162 |
} |
| 163 |
|