| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sync status REST API controller. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\API\V2 |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\API\V2; |
| 9 |
|
| 10 |
use WCPOS\WooCommercePOS\Sync\Api; |
| 11 |
use WCPOS\WooCommercePOS\Sync\Endpoint_Permissions; |
| 12 |
use WCPOS\WooCommercePOS\Sync\Health; |
| 13 |
use WP_REST_Controller; |
| 14 |
use WP_REST_Request; |
| 15 |
use WP_REST_Response; |
| 16 |
use WP_REST_Server; |
| 17 |
|
| 18 |
/** |
| 19 |
* Reports sync store health. |
| 20 |
*/ |
| 21 |
final class Status_Controller extends WP_REST_Controller { |
| 22 |
use Endpoint_Permissions; |
| 23 |
|
| 24 |
/** |
| 25 |
* Named features this server supports, which a client may test for instead of comparing versions. |
| 26 |
* |
| 27 |
* - `products_id_fast_path`: `GET /wcpos/v2/products?per_page=-1&_fields=id,date_modified_gmt,stock_quantity,stock_status` |
| 28 |
* is answered from one query (#2113). |
| 29 |
* |
| 30 |
* Append new names; never rename or remove one. |
| 31 |
*/ |
| 32 |
private const CAPABILITIES = array( 'products_id_fast_path' ); |
| 33 |
|
| 34 |
/** |
| 35 |
* Register the sync status route. |
| 36 |
*/ |
| 37 |
public function register_routes(): void { |
| 38 |
register_rest_route( |
| 39 |
Api::ROUTE_NAMESPACE, |
| 40 |
'/status', |
| 41 |
array( |
| 42 |
'methods' => WP_REST_Server::READABLE, |
| 43 |
'callback' => array( $this, 'get_status' ), |
| 44 |
'permission_callback' => array( $this, 'permissions_check' ), |
| 45 |
) |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get the current sync store health. |
| 51 |
* |
| 52 |
* @param WP_REST_Request $request Request object. |
| 53 |
*/ |
| 54 |
public function get_status( WP_REST_Request $request ): WP_REST_Response { |
| 55 |
$missing_tables = Health::missing_tables(); |
| 56 |
|
| 57 |
return new WP_REST_Response( |
| 58 |
array( |
| 59 |
'healthy' => array() === $missing_tables, |
| 60 |
'missing_tables' => $missing_tables, |
| 61 |
'schema_version' => get_option( Api::SCHEMA_OPTION, null ), |
| 62 |
'capabilities' => self::CAPABILITIES, |
| 63 |
), |
| 64 |
200 |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Allow the status endpoint to report an unhealthy sync store. |
| 70 |
*/ |
| 71 |
protected function health_gated(): bool { |
| 72 |
return false; |
| 73 |
} |
| 74 |
} |
| 75 |
|