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