class-wc-rest-shipping-methods-controller.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * REST API WC Shipping Methods controller
  4. *
  5. * Handles requests to the /shipping_methods endpoint.
  6. *
  7. * @package WooCommerce/API
  8. * @since 3.0.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Shipping methods controller class.
  13. *
  14. * @package WooCommerce/API
  15. * @extends WC_REST_Controller
  16. */
  17. class WC_REST_Shipping_Methods_Controller extends WC_REST_Controller {
  18. /**
  19. * Endpoint namespace.
  20. *
  21. * @var string
  22. */
  23. protected $namespace = 'wc/v2';
  24. /**
  25. * Route base.
  26. *
  27. * @var string
  28. */
  29. protected $rest_base = 'shipping_methods';
  30. /**
  31. * Register the route for /shipping_methods and /shipping_methods/<method>
  32. */
  33. public function register_routes() {
  34. register_rest_route(
  35. $this->namespace, '/' . $this->rest_base, array(
  36. array(
  37. 'methods' => WP_REST_Server::READABLE,
  38. 'callback' => array( $this, 'get_items' ),
  39. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  40. 'args' => $this->get_collection_params(),
  41. ),
  42. 'schema' => array( $this, 'get_public_item_schema' ),
  43. )
  44. );
  45. register_rest_route(
  46. $this->namespace, '/' . $this->rest_base . '/(?P<id>[\w-]+)', array(
  47. 'args' => array(
  48. 'id' => array(
  49. 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
  50. 'type' => 'string',
  51. ),
  52. ),
  53. array(
  54. 'methods' => WP_REST_Server::READABLE,
  55. 'callback' => array( $this, 'get_item' ),
  56. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  57. 'args' => array(
  58. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  59. ),
  60. ),
  61. 'schema' => array( $this, 'get_public_item_schema' ),
  62. )
  63. );
  64. }
  65. /**
  66. * Check whether a given request has permission to view shipping methods.
  67. *
  68. * @param WP_REST_Request $request Full details about the request.
  69. * @return WP_Error|boolean
  70. */
  71. public function get_items_permissions_check( $request ) {
  72. if ( ! wc_rest_check_manager_permissions( 'shipping_methods', 'read' ) ) {
  73. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  74. }
  75. return true;
  76. }
  77. /**
  78. * Check if a given request has access to read a shipping method.
  79. *
  80. * @param WP_REST_Request $request Full details about the request.
  81. * @return WP_Error|boolean
  82. */
  83. public function get_item_permissions_check( $request ) {
  84. if ( ! wc_rest_check_manager_permissions( 'shipping_methods', 'read' ) ) {
  85. return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
  86. }
  87. return true;
  88. }
  89. /**
  90. * Get shipping methods.
  91. *
  92. * @param WP_REST_Request $request Full details about the request.
  93. * @return WP_Error|WP_REST_Response
  94. */
  95. public function get_items( $request ) {
  96. $wc_shipping = WC_Shipping::instance();
  97. $response = array();
  98. foreach ( $wc_shipping->get_shipping_methods() as $id => $shipping_method ) {
  99. $method = $this->prepare_item_for_response( $shipping_method, $request );
  100. $method = $this->prepare_response_for_collection( $method );
  101. $response[] = $method;
  102. }
  103. return rest_ensure_response( $response );
  104. }
  105. /**
  106. * Get a single Shipping Method.
  107. *
  108. * @param WP_REST_Request $request Request data.
  109. * @return WP_REST_Response|WP_Error
  110. */
  111. public function get_item( $request ) {
  112. $wc_shipping = WC_Shipping::instance();
  113. $methods = $wc_shipping->get_shipping_methods();
  114. if ( empty( $methods[ $request['id'] ] ) ) {
  115. return new WP_Error( 'woocommerce_rest_shipping_method_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) );
  116. }
  117. $method = $methods[ $request['id'] ];
  118. $response = $this->prepare_item_for_response( $method, $request );
  119. return rest_ensure_response( $response );
  120. }
  121. /**
  122. * Prepare a shipping method for response.
  123. *
  124. * @param WC_Shipping_Method $method Shipping method object.
  125. * @param WP_REST_Request $request Request object.
  126. * @return WP_REST_Response $response Response data.
  127. */
  128. public function prepare_item_for_response( $method, $request ) {
  129. $data = array(
  130. 'id' => $method->id,
  131. 'title' => $method->method_title,
  132. 'description' => $method->method_description,
  133. );
  134. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  135. $data = $this->add_additional_fields_to_object( $data, $request );
  136. $data = $this->filter_response_by_context( $data, $context );
  137. // Wrap the data in a response object.
  138. $response = rest_ensure_response( $data );
  139. $response->add_links( $this->prepare_links( $method, $request ) );
  140. /**
  141. * Filter shipping methods object returned from the REST API.
  142. *
  143. * @param WP_REST_Response $response The response object.
  144. * @param WC_Shipping_Method $method Shipping method object used to create response.
  145. * @param WP_REST_Request $request Request object.
  146. */
  147. return apply_filters( 'woocommerce_rest_prepare_shipping_method', $response, $method, $request );
  148. }
  149. /**
  150. * Prepare links for the request.
  151. *
  152. * @param WC_Shipping_Method $method Shipping method object.
  153. * @param WP_REST_Request $request Request object.
  154. * @return array
  155. */
  156. protected function prepare_links( $method, $request ) {
  157. $links = array(
  158. 'self' => array(
  159. 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $method->id ) ),
  160. ),
  161. 'collection' => array(
  162. 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
  163. ),
  164. );
  165. return $links;
  166. }
  167. /**
  168. * Get the shipping method schema, conforming to JSON Schema.
  169. *
  170. * @return array
  171. */
  172. public function get_item_schema() {
  173. $schema = array(
  174. '$schema' => 'http://json-schema.org/draft-04/schema#',
  175. 'title' => 'shipping_method',
  176. 'type' => 'object',
  177. 'properties' => array(
  178. 'id' => array(
  179. 'description' => __( 'Method ID.', 'woocommerce' ),
  180. 'type' => 'string',
  181. 'context' => array( 'view' ),
  182. 'readonly' => true,
  183. ),
  184. 'title' => array(
  185. 'description' => __( 'Shipping method title.', 'woocommerce' ),
  186. 'type' => 'string',
  187. 'context' => array( 'view' ),
  188. 'readonly' => true,
  189. ),
  190. 'description' => array(
  191. 'description' => __( 'Shipping method description.', 'woocommerce' ),
  192. 'type' => 'string',
  193. 'context' => array( 'view' ),
  194. 'readonly' => true,
  195. ),
  196. ),
  197. );
  198. return $this->add_additional_fields_schema( $schema );
  199. }
  200. /**
  201. * Get any query params needed.
  202. *
  203. * @return array
  204. */
  205. public function get_collection_params() {
  206. return array(
  207. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  208. );
  209. }
  210. }