class-wc-rest-shipping-zone-methods-controller.php 17 KB

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