/includes/api/class-wc-rest-shipping-zone-methods-controller.php

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