WC_REST_Shipping_Methods_Controller

The WooCommerce WC REST Shipping Methods Controller class.

Defined (1)

The class is defined in the following location(s).

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