| 1 |
<?php |
| 2 |
/** |
| 3 |
* Extensions REST API controller. |
| 4 |
* |
| 5 |
* Serves the extension catalog with local plugin status. |
| 6 |
* |
| 7 |
* @package WCPOS\WooCommercePOS\API\V1 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace WCPOS\WooCommercePOS\API\V1; |
| 11 |
|
| 12 |
use WCPOS\WooCommercePOS\Services\Extensions as ExtensionsService; |
| 13 |
use WP_REST_Controller; |
| 14 |
use WP_REST_Request; |
| 15 |
use WP_REST_Response; |
| 16 |
use WP_REST_Server; |
| 17 |
|
| 18 |
use const WCPOS\WooCommercePOS\SHORT_NAME; |
| 19 |
|
| 20 |
/** |
| 21 |
* Extensions controller class. |
| 22 |
*/ |
| 23 |
class Extensions extends WP_REST_Controller { |
| 24 |
|
| 25 |
/** |
| 26 |
* Route namespace. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $namespace = SHORT_NAME . '/v1'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Route base. |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
protected $rest_base = 'extensions'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Register routes. |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function register_routes(): void { |
| 45 |
register_rest_route( |
| 46 |
$this->namespace, |
| 47 |
'/' . $this->rest_base, |
| 48 |
array( |
| 49 |
'methods' => WP_REST_Server::READABLE, |
| 50 |
'callback' => array( $this, 'get_items' ), |
| 51 |
'permission_callback' => array( $this, 'check_permissions' ), |
| 52 |
'args' => array( |
| 53 |
'force' => array( |
| 54 |
'type' => 'boolean', |
| 55 |
'default' => false, |
| 56 |
'sanitize_callback' => 'rest_sanitize_boolean', |
| 57 |
), |
| 58 |
), |
| 59 |
) |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Get all extensions with status. |
| 65 |
* |
| 66 |
* @param WP_REST_Request $request Request object. |
| 67 |
* |
| 68 |
* @return WP_REST_Response |
| 69 |
*/ |
| 70 |
public function get_items( $request ): WP_REST_Response { |
| 71 |
$service = ExtensionsService::instance(); |
| 72 |
$force = $request->get_param( 'force' ); |
| 73 |
if ( $force && ! $service->clear_cache() ) { |
| 74 |
return new WP_REST_Response( |
| 75 |
array( |
| 76 |
'code' => 'wcpos_extensions_cache_clear_failed', |
| 77 |
'message' => __( 'Could not clear the extensions cache.', 'woocommerce-pos' ), |
| 78 |
), |
| 79 |
500 |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
$extensions = $service->get_extensions(); |
| 84 |
if ( $force && empty( $extensions ) ) { |
| 85 |
return new WP_REST_Response( |
| 86 |
array( |
| 87 |
'code' => 'wcpos_extensions_refresh_failed', |
| 88 |
'message' => __( 'Could not refresh extensions. Please try again.', 'woocommerce-pos' ), |
| 89 |
), |
| 90 |
502 |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
$response = new WP_REST_Response( $extensions ); |
| 95 |
$response->header( 'X-WP-Total', (string) \count( $extensions ) ); |
| 96 |
|
| 97 |
return $response; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Check if the current user has permission. |
| 102 |
* |
| 103 |
* @param WP_REST_Request $request Request object. |
| 104 |
* |
| 105 |
* @return bool|\WP_Error |
| 106 |
*/ |
| 107 |
public function check_permissions( WP_REST_Request $request ) { |
| 108 |
if ( ! current_user_can( 'manage_woocommerce_pos' ) ) { |
| 109 |
return new \WP_Error( |
| 110 |
'rest_forbidden', |
| 111 |
__( 'You do not have permission to view extensions.', 'woocommerce-pos' ), |
| 112 |
array( 'status' => rest_authorization_required_code() ) |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
return true; |
| 117 |
} |
| 118 |
} |
| 119 |
|