暫無描述

class-wc-rest-data-controller.php 4.7KB

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