WC_REST_Shipping_Zone_Methods_Controller

REST API Shipping Zone Methods class.

Defined (1)

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

/includes/api/class-wc-rest-shipping-zone-methods-controller.php  
  1. class WC_REST_Shipping_Zone_Methods_Controller extends WC_REST_Shipping_Zones_Controller_Base { 
  2.  
  3. /** 
  4. * Register the routes for Shipping Zone Methods. 
  5. */ 
  6. public function register_routes() { 
  7. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<zone_id>[\d]+)/methods', array( 
  8. 'args' => array( 
  9. 'zone_id' => array( 
  10. 'description' => __( 'Unique ID for the zone.', 'woocommerce' ),  
  11. 'type' => 'integer',  
  12. ),  
  13. ),  
  14. array( 
  15. 'methods' => WP_REST_Server::READABLE,  
  16. 'callback' => array( $this, 'get_items' ),  
  17. 'permission_callback' => array( $this, 'get_items_permissions_check' ),  
  18. ),  
  19. array( 
  20. 'methods' => WP_REST_Server::CREATABLE,  
  21. 'callback' => array( $this, 'create_item' ),  
  22. 'permission_callback' => array( $this, 'create_item_permissions_check' ),  
  23. 'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array( 
  24. 'method_id' => array( 
  25. 'required' => true,  
  26. 'readonly' => false,  
  27. 'description' => __( 'Shipping method ID.', 'woocommerce' ),  
  28. ),  
  29. ) ),  
  30. ),  
  31. 'schema' => array( $this, 'get_public_item_schema' ),  
  32. ) ); 
  33.  
  34. register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<zone_id>[\d]+)/methods/(?P<instance_id>[\d]+)', array( 
  35. 'args' => array( 
  36. 'zone_id' => array( 
  37. 'description' => __( 'Unique ID for the zone.', 'woocommerce' ),  
  38. 'type' => 'integer',  
  39. ),  
  40. 'instance_id' => array( 
  41. 'description' => __( 'Unique ID for the instance.', 'woocommerce' ),  
  42. 'type' => 'integer',  
  43. ),  
  44. ),  
  45. array( 
  46. 'methods' => WP_REST_Server::READABLE,  
  47. 'callback' => array( $this, 'get_item' ),  
  48. 'permission_callback' => array( $this, 'get_items_permissions_check' ),  
  49. ),  
  50. array( 
  51. 'methods' => WP_REST_Server::EDITABLE,  
  52. 'callback' => array( $this, 'update_item' ),  
  53. 'permission_callback' => array( $this, 'update_items_permissions_check' ),  
  54. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),  
  55. ),  
  56. array( 
  57. 'methods' => WP_REST_Server::DELETABLE,  
  58. 'callback' => array( $this, 'delete_item' ),  
  59. 'permission_callback' => array( $this, 'delete_items_permissions_check' ),  
  60. 'args' => array( 
  61. 'force' => array( 
  62. 'default' => false,  
  63. 'type' => 'boolean',  
  64. 'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ),  
  65. ),  
  66. ),  
  67. ),  
  68. 'schema' => array( $this, 'get_public_item_schema' ),  
  69. ) ); 
  70.  
  71. /** 
  72. * Get a single Shipping Zone Method. 
  73. * @param WP_REST_Request $request 
  74. * @return WP_REST_Response|WP_Error 
  75. */ 
  76. public function get_item( $request ) { 
  77. $zone = $this->get_zone( $request['zone_id'] ); 
  78.  
  79. if ( is_wp_error( $zone ) ) { 
  80. return $zone; 
  81.  
  82. $instance_id = (int) $request['instance_id']; 
  83. $methods = $zone->get_shipping_methods(); 
  84. $method = false; 
  85.  
  86. foreach ( $methods as $method_obj ) { 
  87. if ( $instance_id === $method_obj->instance_id ) { 
  88. $method = $method_obj; 
  89. break; 
  90.  
  91. if ( false === $method ) { 
  92. return new WP_Error( 'woocommerce_rest_shipping_zone_method_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) ); 
  93.  
  94. $data = $this->prepare_item_for_response( $method, $request ); 
  95.  
  96. return rest_ensure_response( $data ); 
  97.  
  98. /** 
  99. * Get all Shipping Zone Methods. 
  100. * @param WP_REST_Request $request 
  101. * @return WP_REST_Response|WP_Error 
  102. */ 
  103. public function get_items( $request ) { 
  104. $zone = $this->get_zone( $request['zone_id'] ); 
  105.  
  106. if ( is_wp_error( $zone ) ) { 
  107. return $zone; 
  108.  
  109. $methods = $zone->get_shipping_methods(); 
  110. $data = array(); 
  111.  
  112. foreach ( $methods as $method_obj ) { 
  113. $method = $this->prepare_item_for_response( $method_obj, $request ); 
  114. $data[] = $method; 
  115.  
  116. return rest_ensure_response( $data ); 
  117.  
  118. /** 
  119. * Create a new shipping zone method instance. 
  120. * @param WP_REST_Request $request Full details about the request. 
  121. * @return WP_REST_Request|WP_Error 
  122. */ 
  123. public function create_item( $request ) { 
  124. global $wpdb; 
  125.  
  126. $method_id = $request['method_id']; 
  127. $zone = $this->get_zone( $request['zone_id'] ); 
  128. if ( is_wp_error( $zone ) ) { 
  129. return $zone; 
  130.  
  131. $instance_id = $zone->add_shipping_method( $method_id ); 
  132. $methods = $zone->get_shipping_methods(); 
  133. $method = false; 
  134. foreach ( $methods as $method_obj ) { 
  135. if ( $instance_id === $method_obj->instance_id ) { 
  136. $method = $method_obj; 
  137. break; 
  138.  
  139. if ( false === $method ) { 
  140. return new WP_Error( 'woocommerce_rest_shipping_zone_not_created', __( 'Resource cannot be created.', 'woocommerce' ), array( 'status' => 500 ) ); 
  141.  
  142. $method = $this->update_fields( $instance_id, $method, $request ); 
  143. if ( is_wp_error( $method ) ) { 
  144. return $method; 
  145.  
  146. $data = $this->prepare_item_for_response( $method, $request ); 
  147. return rest_ensure_response( $data ); 
  148.  
  149. /** 
  150. * Delete a shipping method instance. 
  151. * @param WP_REST_Request $request Full details about the request 
  152. * @return WP_Error|boolean 
  153. */ 
  154. public function delete_item( $request ) { 
  155. global $wpdb; 
  156.  
  157. $zone = $this->get_zone( $request['zone_id'] ); 
  158. if ( is_wp_error( $zone ) ) { 
  159. return $zone; 
  160.  
  161. $instance_id = (int) $request['instance_id']; 
  162. $force = $request['force']; 
  163.  
  164. $methods = $zone->get_shipping_methods(); 
  165. $method = false; 
  166.  
  167. foreach ( $methods as $method_obj ) { 
  168. if ( $instance_id === $method_obj->instance_id ) { 
  169. $method = $method_obj; 
  170. break; 
  171.  
  172. if ( false === $method ) { 
  173. return new WP_Error( 'woocommerce_rest_shipping_zone_method_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) ); 
  174.  
  175. $method = $this->update_fields( $instance_id, $method, $request ); 
  176. if ( is_wp_error( $method ) ) { 
  177. return $method; 
  178.  
  179. $request->set_param( 'context', 'view' ); 
  180. $response = $this->prepare_item_for_response( $method, $request ); 
  181.  
  182. // Actually delete 
  183. if ( $force ) { 
  184. $zone->delete_shipping_method( $instance_id ); 
  185. } else { 
  186. return new WP_Error( 'rest_trash_not_supported', __( 'Shipping methods do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) ); 
  187.  
  188. /** 
  189. * Fires after a product review is deleted via the REST API. 
  190. * @param object $method 
  191. * @param WP_REST_Response $response The response data. 
  192. * @param WP_REST_Request $request The request sent to the API. 
  193. */ 
  194. do_action( 'rest_delete_product_review', $method, $response, $request ); 
  195.  
  196. return $response; 
  197.  
  198. /** 
  199. * Update A Single Shipping Zone Method. 
  200. * @param WP_REST_Request $request 
  201. * @return WP_REST_Response|WP_Error 
  202. */ 
  203. public function update_item( $request ) { 
  204. global $wpdb; 
  205.  
  206. $zone = $this->get_zone( $request['zone_id'] ); 
  207. if ( is_wp_error( $zone ) ) { 
  208. return $zone; 
  209.  
  210. $instance_id = (int) $request['instance_id']; 
  211. $methods = $zone->get_shipping_methods(); 
  212. $method = false; 
  213.  
  214. foreach ( $methods as $method_obj ) { 
  215. if ( $instance_id === $method_obj->instance_id ) { 
  216. $method = $method_obj; 
  217. break; 
  218.  
  219. if ( false === $method ) { 
  220. return new WP_Error( 'woocommerce_rest_shipping_zone_method_invalid', __( 'Resource does not exist.', 'woocommerce' ), array( 'status' => 404 ) ); 
  221.  
  222. $method = $this->update_fields( $instance_id, $method, $request ); 
  223. if ( is_wp_error( $method ) ) { 
  224. return $method; 
  225.  
  226. $data = $this->prepare_item_for_response( $method, $request ); 
  227. return rest_ensure_response( $data ); 
  228.  
  229. /** 
  230. * Updates settings, order, and enabled status on create. 
  231. * @param $instance_id integer 
  232. * @param $method 
  233. * @param WP_REST_Request $request 
  234. * @return $method 
  235. */ 
  236. public function update_fields( $instance_id, $method, $request ) { 
  237. global $wpdb; 
  238.  
  239. // Update settings if present 
  240. if ( isset( $request['settings'] ) ) { 
  241. $method->init_instance_settings(); 
  242. $instance_settings = $method->instance_settings; 
  243. $errors_found = false; 
  244. foreach ( $method->get_instance_form_fields() as $key => $field ) { 
  245. if ( isset( $request['settings'][ $key ] ) ) { 
  246. if ( is_callable( array( $this, 'validate_setting_' . $field['type'] . '_field' ) ) ) { 
  247. $value = $this->{'validate_setting_' . $field['type'] . '_field'}( $request['settings'][ $key ], $field ); 
  248. } else { 
  249. $value = $this->validate_setting_text_field( $request['settings'][ $key ], $field ); 
  250. if ( is_wp_error( $value ) ) { 
  251. $errors_found = true; 
  252. break; 
  253. $instance_settings[ $key ] = $value; 
  254.  
  255. if ( $errors_found ) { 
  256. return new WP_Error( 'rest_setting_value_invalid', __( 'An invalid setting value was passed.', 'woocommerce' ), array( 'status' => 400 ) ); 
  257.  
  258. update_option( $method->get_instance_option_key(), apply_filters( 'woocommerce_shipping_' . $method->id . '_instance_settings_values', $instance_settings, $method ) ); 
  259.  
  260. // Update order 
  261. if ( isset( $request['order'] ) ) { 
  262. $wpdb->update( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'method_order' => absint( $request['order'] ) ), array( 'instance_id' => absint( $instance_id ) ) ); 
  263. $method->method_order = absint( $request['order'] ); 
  264.  
  265. // Update if this method is enabled or not. 
  266. if ( isset( $request['enabled'] ) ) { 
  267. if ( $wpdb->update( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'is_enabled' => $request['enabled'] ), array( 'instance_id' => absint( $instance_id ) ) ) ) { 
  268. do_action( 'woocommerce_shipping_zone_method_status_toggled', $instance_id, $method->id, $request['zone_id'], $request['enabled'] ); 
  269. $method->enabled = ( true === $request['enabled'] ? 'yes' : 'no' ); 
  270.  
  271. return $method; 
  272.  
  273. /** 
  274. * Prepare the Shipping Zone Method for the REST response. 
  275. * @param array $item Shipping Zone Method. 
  276. * @param WP_REST_Request $request Request object. 
  277. * @return WP_REST_Response $response 
  278. */ 
  279. public function prepare_item_for_response( $item, $request ) { 
  280. $method = array( 
  281. 'id' => $item->instance_id,  
  282. 'instance_id' => $item->instance_id,  
  283. 'title' => $item->instance_settings['title'],  
  284. 'order' => $item->method_order,  
  285. 'enabled' => ( 'yes' === $item->enabled ),  
  286. 'method_id' => $item->id,  
  287. 'method_title' => $item->method_title,  
  288. 'method_description' => $item->method_description,  
  289. 'settings' => $this->get_settings( $item ),  
  290. ); 
  291.  
  292. $context = empty( $request['context'] ) ? 'view' : $request['context']; 
  293. $data = $this->add_additional_fields_to_object( $method, $request ); 
  294. $data = $this->filter_response_by_context( $data, $context ); 
  295.  
  296. // Wrap the data in a response object. 
  297. $response = rest_ensure_response( $data ); 
  298.  
  299. $response->add_links( $this->prepare_links( $request['zone_id'], $item->instance_id ) ); 
  300.  
  301. $response = $this->prepare_response_for_collection( $response ); 
  302.  
  303. return $response; 
  304.  
  305. /** 
  306. * Return settings associated with this shipping zone method instance. 
  307. */ 
  308. public function get_settings( $item ) { 
  309. $item->init_instance_settings(); 
  310. $settings = array(); 
  311. foreach ( $item->get_instance_form_fields() as $id => $field ) { 
  312. $data = array( 
  313. 'id' => $id,  
  314. 'label' => $field['title'],  
  315. 'description' => empty( $field['description'] ) ? '' : $field['description'],  
  316. 'type' => $field['type'],  
  317. 'value' => $item->instance_settings[ $id ],  
  318. 'default' => empty( $field['default'] ) ? '' : $field['default'],  
  319. 'tip' => empty( $field['description'] ) ? '' : $field['description'],  
  320. 'placeholder' => empty( $field['placeholder'] ) ? '' : $field['placeholder'],  
  321. ); 
  322. if ( ! empty( $field['options'] ) ) { 
  323. $data['options'] = $field['options']; 
  324. $settings[ $id ] = $data; 
  325. return $settings; 
  326.  
  327. /** 
  328. * Prepare links for the request. 
  329. * @param int $zone_id Given Shipping Zone ID. 
  330. * @param int $instance_id Given Shipping Zone Method Instance ID. 
  331. * @return array Links for the given Shipping Zone Method. 
  332. */ 
  333. protected function prepare_links( $zone_id, $instance_id ) { 
  334. $base = '/' . $this->namespace . '/' . $this->rest_base . '/' . $zone_id; 
  335. $links = array( 
  336. 'self' => array( 
  337. 'href' => rest_url( $base . '/methods/' . $instance_id ),  
  338. ),  
  339. 'collection' => array( 
  340. 'href' => rest_url( $base . '/methods' ),  
  341. ),  
  342. 'describes' => array( 
  343. 'href' => rest_url( $base ),  
  344. ),  
  345. ); 
  346.  
  347. return $links; 
  348.  
  349. /** 
  350. * Get the Shipping Zone Methods schema, conforming to JSON Schema 
  351. * @return array 
  352. */ 
  353. public function get_item_schema() { 
  354. $schema = array( 
  355. '$schema' => 'http://json-schema.org/draft-04/schema#',  
  356. 'title' => 'shipping_zone_method',  
  357. 'type' => 'object',  
  358. 'properties' => array( 
  359. 'id' => array( 
  360. 'description' => __( 'Shipping method instance ID.', 'woocommerce' ),  
  361. 'type' => 'integer',  
  362. 'context' => array( 'view', 'edit' ),  
  363. 'readonly' => true,  
  364. ),  
  365. 'instance_id' => array( 
  366. 'description' => __( 'Shipping method instance ID.', 'woocommerce' ),  
  367. 'type' => 'integer',  
  368. 'context' => array( 'view', 'edit' ),  
  369. 'readonly' => true,  
  370. ),  
  371. 'title' => array( 
  372. 'description' => __( 'Shipping method customer facing title.', 'woocommerce' ),  
  373. 'type' => 'string',  
  374. 'context' => array( 'view', 'edit' ),  
  375. 'readonly' => true,  
  376. ),  
  377. 'order' => array( 
  378. 'description' => __( 'Shipping method sort order.', 'woocommerce' ),  
  379. 'type' => 'integer',  
  380. 'context' => array( 'view', 'edit' ),  
  381. ),  
  382. 'enabled' => array( 
  383. 'description' => __( 'Shipping method enabled status.', 'woocommerce' ),  
  384. 'type' => 'boolean',  
  385. 'context' => array( 'view', 'edit' ),  
  386. ),  
  387. 'method_id' => array( 
  388. 'description' => __( 'Shipping method ID.', 'woocommerce' ),  
  389. 'type' => 'string',  
  390. 'context' => array( 'view', 'edit' ),  
  391. 'readonly' => true,  
  392. ),  
  393. 'method_title' => array( 
  394. 'description' => __( 'Shipping method title.', 'woocommerce' ),  
  395. 'type' => 'string',  
  396. 'context' => array( 'view', 'edit' ),  
  397. 'readonly' => true,  
  398. ),  
  399. 'method_description' => array( 
  400. 'description' => __( 'Shipping method description.', 'woocommerce' ),  
  401. 'type' => 'string',  
  402. 'context' => array( 'view', 'edit' ),  
  403. 'readonly' => true,  
  404. ),  
  405. 'settings' => array( 
  406. 'description' => __( 'Shipping method settings.', 'woocommerce' ),  
  407. 'type' => 'object',  
  408. 'context' => array( 'view', 'edit' ),  
  409. 'properties' => array( 
  410. 'id' => array( 
  411. 'description' => __( 'A unique identifier for the setting.', 'woocommerce' ),  
  412. 'type' => 'string',  
  413. 'context' => array( 'view', 'edit' ),  
  414. 'readonly' => true,  
  415. ),  
  416. 'label' => array( 
  417. 'description' => __( 'A human readable label for the setting used in interfaces.', 'woocommerce' ),  
  418. 'type' => 'string',  
  419. 'context' => array( 'view', 'edit' ),  
  420. 'readonly' => true,  
  421. ),  
  422. 'description' => array( 
  423. 'description' => __( 'A human readable description for the setting used in interfaces.', 'woocommerce' ),  
  424. 'type' => 'string',  
  425. 'context' => array( 'view', 'edit' ),  
  426. 'readonly' => true,  
  427. ),  
  428. 'type' => array( 
  429. 'description' => __( 'Type of setting.', 'woocommerce' ),  
  430. 'type' => 'string',  
  431. 'context' => array( 'view', 'edit' ),  
  432. 'enum' => array( 'text', 'email', 'number', 'color', 'password', 'textarea', 'select', 'multiselect', 'radio', 'image_width', 'checkbox' ),  
  433. 'readonly' => true,  
  434. ),  
  435. 'value' => array( 
  436. 'description' => __( 'Setting value.', 'woocommerce' ),  
  437. 'type' => 'string',  
  438. 'context' => array( 'view', 'edit' ),  
  439. ),  
  440. 'default' => array( 
  441. 'description' => __( 'Default value for the setting.', 'woocommerce' ),  
  442. 'type' => 'string',  
  443. 'context' => array( 'view', 'edit' ),  
  444. 'readonly' => true,  
  445. ),  
  446. 'tip' => array( 
  447. 'description' => __( 'Additional help text shown to the user about the setting.', 'woocommerce' ),  
  448. 'type' => 'string',  
  449. 'context' => array( 'view', 'edit' ),  
  450. 'readonly' => true,  
  451. ),  
  452. 'placeholder' => array( 
  453. 'description' => __( 'Placeholder text to be displayed in text inputs.', 'woocommerce' ),  
  454. 'type' => 'string',  
  455. 'context' => array( 'view', 'edit' ),  
  456. 'readonly' => true,  
  457. ),  
  458. ),  
  459. ),  
  460. ),  
  461. ); 
  462.  
  463. return $this->add_additional_fields_schema( $schema );