| 1 |
<?php |
| 2 |
/** |
| 3 |
* REST API Data controller. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\API\V1; |
| 9 |
|
| 10 |
\defined( 'ABSPATH' ) || die; |
| 11 |
|
| 12 |
if ( ! class_exists( 'WC_REST_Data_Controller' ) ) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
use WC_REST_Data_Controller; |
| 17 |
use WP_REST_Server; |
| 18 |
use WP_REST_Request; |
| 19 |
use WP_Error; |
| 20 |
use WP_REST_Response; |
| 21 |
|
| 22 |
/** |
| 23 |
* REST API Data controller. |
| 24 |
* |
| 25 |
* Seems overkill to create a new class for this, but WC doesn't provide a way to get the list of order statuses. |
| 26 |
*/ |
| 27 |
class Data_Order_Statuses_Controller extends WC_REST_Data_Controller { |
| 28 |
|
| 29 |
/** |
| 30 |
* Endpoint namespace. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $namespace = 'wcpos/v1'; |
| 35 |
|
| 36 |
/** |
| 37 |
* Route base. |
| 38 |
* |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
protected $rest_base = 'data/order_statuses'; |
| 42 |
|
| 43 |
/** |
| 44 |
* Return the list of order statuses. |
| 45 |
* |
| 46 |
* @param WP_REST_Request $request Request data. |
| 47 |
* @return WP_Error|WP_REST_Response |
| 48 |
*/ |
| 49 |
public function get_items( $request ) { |
| 50 |
$statuses = wc_get_order_statuses(); |
| 51 |
$data = array(); |
| 52 |
|
| 53 |
foreach ( $statuses as $status => $label ) { |
| 54 |
// Remove the 'wc-' prefix from the status. |
| 55 |
$status = str_replace( 'wc-', '', $status ); |
| 56 |
|
| 57 |
$resource = array( |
| 58 |
'status' => $status, |
| 59 |
'label' => $label, |
| 60 |
); |
| 61 |
|
| 62 |
$item = $this->prepare_item_for_response( (object) $resource, $request ); |
| 63 |
$data[] = $this->prepare_response_for_collection( $item ); |
| 64 |
} |
| 65 |
|
| 66 |
return rest_ensure_response( $data ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Prepare a data resource object for serialization. |
| 71 |
* |
| 72 |
* @param \stdClass $resource Resource data. |
| 73 |
* @param WP_REST_Request $request Request object. |
| 74 |
* @return WP_REST_Response $response Response data. |
| 75 |
*/ |
| 76 |
public function prepare_item_for_response( $resource, $request ) { |
| 77 |
$data = array( |
| 78 |
'status' => $resource->status, |
| 79 |
'label' => $resource->label, |
| 80 |
); |
| 81 |
|
| 82 |
$data = $this->add_additional_fields_to_object( $data, $request ); |
| 83 |
$data = $this->filter_response_by_context( $data, 'view' ); |
| 84 |
|
| 85 |
// Wrap the data in a response object. |
| 86 |
$response = rest_ensure_response( $data ); |
| 87 |
$response->add_links( $this->prepare_links( $resource ) ); |
| 88 |
|
| 89 |
return $response; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Prepare links for the request. |
| 94 |
* |
| 95 |
* @param object $item Data object. |
| 96 |
* @return array Links for the given country. |
| 97 |
*/ |
| 98 |
protected function prepare_links( $item ) { |
| 99 |
$links = array( |
| 100 |
// 'self' => array( |
| 101 |
// 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $item->status ) ), |
| 102 |
// ), |
| 103 |
'collection' => array( |
| 104 |
'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), |
| 105 |
), |
| 106 |
); |
| 107 |
|
| 108 |
return $links; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get the data index schema, conforming to JSON Schema. |
| 113 |
* |
| 114 |
* @since 3.5.0 |
| 115 |
* @return array |
| 116 |
*/ |
| 117 |
public function get_item_schema() { |
| 118 |
$schema = array( |
| 119 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 120 |
'title' => 'data_index', |
| 121 |
'type' => 'object', |
| 122 |
'properties' => array( |
| 123 |
'status' => array( |
| 124 |
'description' => /* translators: REST API schema field label or error message. */ __( 'Order Status.', 'woocommerce-pos' ), |
| 125 |
'type' => 'string', |
| 126 |
'context' => array( 'view' ), |
| 127 |
'readonly' => true, |
| 128 |
), |
| 129 |
'label' => array( |
| 130 |
'description' => /* translators: REST API schema field label or error message. */ __( 'Order Status Label.', 'woocommerce-pos' ), |
| 131 |
'type' => 'string', |
| 132 |
'context' => array( 'view' ), |
| 133 |
'readonly' => true, |
| 134 |
), |
| 135 |
), |
| 136 |
); |
| 137 |
|
| 138 |
return $this->add_additional_fields_schema( $schema ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Check whether a given request has permission to view order statuses. |
| 143 |
* |
| 144 |
* @param WP_REST_Request $request Full details about the request. |
| 145 |
* @return WP_Error|boolean |
| 146 |
*/ |
| 147 |
public function get_items_permissions_check( $request ) { |
| 148 |
if ( is_user_logged_in() ) { |
| 149 |
return true; |
| 150 |
} |
| 151 |
return parent::get_items_permissions_check( $request ); |
| 152 |
} |
| 153 |
} |
| 154 |
|