class-wc-rest-data-controller.php
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Data controller. |
| 4 | * |
| 5 | * Handles requests to the /data endpoint. |
| 6 | * |
| 7 | * @package WooCommerce\RestApi |
| 8 | * @since 3.5.0 |
| 9 | */ |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * REST API Data controller class. |
| 15 | * |
| 16 | * @package WooCommerce\RestApi |
| 17 | * @extends WC_REST_Controller |
| 18 | */ |
| 19 | class WC_REST_Data_Controller extends WC_REST_Controller { |
| 20 | |
| 21 | /** |
| 22 | * Endpoint namespace. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $namespace = 'wc/v3'; |
| 27 | |
| 28 | /** |
| 29 | * Route base. |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | protected $rest_base = 'data'; |
| 34 | |
| 35 | /** |
| 36 | * Register routes. |
| 37 | * |
| 38 | * @since 3.5.0 |
| 39 | */ |
| 40 | public function register_routes() { |
| 41 | register_rest_route( |
| 42 | $this->namespace, '/' . $this->rest_base, array( |
| 43 | array( |
| 44 | 'methods' => WP_REST_Server::READABLE, |
| 45 | 'callback' => array( $this, 'get_items' ), |
| 46 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 47 | ), |
| 48 | 'schema' => array( $this, 'get_public_item_schema' ), |
| 49 | ) |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Check whether a given request has permission to read site data. |
| 55 | * |
| 56 | * @param WP_REST_Request $request Full details about the request. |
| 57 | * @return WP_Error|boolean |
| 58 | */ |
| 59 | public function get_items_permissions_check( $request ) { |
| 60 | if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) { |
| 61 | return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
| 62 | } |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Check whether a given request has permission to read site settings. |
| 69 | * |
| 70 | * @param WP_REST_Request $request Full details about the request. |
| 71 | * @return WP_Error|boolean |
| 72 | */ |
| 73 | public function get_item_permissions_check( $request ) { |
| 74 | if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) { |
| 75 | return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Return the list of data resources. |
| 83 | * |
| 84 | * @since 3.5.0 |
| 85 | * @param WP_REST_Request $request Request data. |
| 86 | * @return WP_Error|WP_REST_Response |
| 87 | */ |
| 88 | public function get_items( $request ) { |
| 89 | $data = array(); |
| 90 | $resources = array( |
| 91 | array( |
| 92 | 'slug' => 'continents', |
| 93 | 'description' => __( 'List of supported continents, countries, and states.', 'woocommerce' ), |
| 94 | ), |
| 95 | array( |
| 96 | 'slug' => 'countries', |
| 97 | 'description' => __( 'List of supported states in a given country.', 'woocommerce' ), |
| 98 | ), |
| 99 | array( |
| 100 | 'slug' => 'currencies', |
| 101 | 'description' => __( 'List of supported currencies.', 'woocommerce' ), |
| 102 | ), |
| 103 | ); |
| 104 | |
| 105 | foreach ( $resources as $resource ) { |
| 106 | $item = $this->prepare_item_for_response( (object) $resource, $request ); |
| 107 | $data[] = $this->prepare_response_for_collection( $item ); |
| 108 | } |
| 109 | |
| 110 | return rest_ensure_response( $data ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Prepare a data resource object for serialization. |
| 115 | * |
| 116 | * @param stdClass $resource Resource data. |
| 117 | * @param WP_REST_Request $request Request object. |
| 118 | * @return WP_REST_Response $response Response data. |
| 119 | */ |
| 120 | public function prepare_item_for_response( $resource, $request ) { |
| 121 | $data = array( |
| 122 | 'slug' => $resource->slug, |
| 123 | 'description' => $resource->description, |
| 124 | ); |
| 125 | |
| 126 | $data = $this->add_additional_fields_to_object( $data, $request ); |
| 127 | $data = $this->filter_response_by_context( $data, 'view' ); |
| 128 | |
| 129 | // Wrap the data in a response object. |
| 130 | $response = rest_ensure_response( $data ); |
| 131 | $response->add_links( $this->prepare_links( $resource ) ); |
| 132 | |
| 133 | return $response; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Prepare links for the request. |
| 138 | * |
| 139 | * @param object $item Data object. |
| 140 | * @return array Links for the given country. |
| 141 | */ |
| 142 | protected function prepare_links( $item ) { |
| 143 | $links = array( |
| 144 | 'self' => array( |
| 145 | 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $item->slug ) ), |
| 146 | ), |
| 147 | 'collection' => array( |
| 148 | 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ), |
| 149 | ), |
| 150 | ); |
| 151 | |
| 152 | return $links; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Get the data index schema, conforming to JSON Schema. |
| 157 | * |
| 158 | * @since 3.5.0 |
| 159 | * @return array |
| 160 | */ |
| 161 | public function get_item_schema() { |
| 162 | $schema = array( |
| 163 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 164 | 'title' => 'data_index', |
| 165 | 'type' => 'object', |
| 166 | 'properties' => array( |
| 167 | 'slug' => array( |
| 168 | 'description' => __( 'Data resource ID.', 'woocommerce' ), |
| 169 | 'type' => 'string', |
| 170 | 'context' => array( 'view' ), |
| 171 | 'readonly' => true, |
| 172 | ), |
| 173 | 'description' => array( |
| 174 | 'description' => __( 'Data resource description.', 'woocommerce' ), |
| 175 | 'type' => 'string', |
| 176 | 'context' => array( 'view' ), |
| 177 | 'readonly' => true, |
| 178 | ), |
| 179 | ), |
| 180 | ); |
| 181 | |
| 182 | return $this->add_additional_fields_schema( $schema ); |
| 183 | } |
| 184 | } |
| 185 |