class-wc-rest-legacy-coupons-controller.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * REST API Legacy Coupons controller
  4. *
  5. * Handles requests to the /coupons endpoint.
  6. *
  7. * @author WooThemes
  8. * @category API
  9. * @package WooCommerce/API
  10. * @since 3.0.0
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. /**
  16. * REST API Legacy Coupons controller class.
  17. *
  18. * @package WooCommerce/API
  19. * @extends WC_REST_CRUD_Controller
  20. */
  21. class WC_REST_Legacy_Coupons_Controller extends WC_REST_CRUD_Controller {
  22. /**
  23. * Query args.
  24. *
  25. * @deprecated 3.0.0
  26. *
  27. * @param array $args Query args
  28. * @param WP_REST_Request $request Request data.
  29. * @return array
  30. */
  31. public function query_args( $args, $request ) {
  32. if ( ! empty( $request['code'] ) ) {
  33. $id = wc_get_coupon_id_by_code( $request['code'] );
  34. $args['post__in'] = array( $id );
  35. }
  36. return $args;
  37. }
  38. /**
  39. * Prepare a single coupon output for response.
  40. *
  41. * @deprecated 3.0.0
  42. *
  43. * @param WP_Post $post Post object.
  44. * @param WP_REST_Request $request Request object.
  45. * @return WP_REST_Response $data
  46. */
  47. public function prepare_item_for_response( $post, $request ) {
  48. $coupon = new WC_Coupon( (int) $post->ID );
  49. $data = $coupon->get_data();
  50. $format_decimal = array( 'amount', 'minimum_amount', 'maximum_amount' );
  51. $format_date = array( 'date_created', 'date_modified', 'date_expires' );
  52. $format_null = array( 'usage_limit', 'usage_limit_per_user', 'limit_usage_to_x_items' );
  53. // Format decimal values.
  54. foreach ( $format_decimal as $key ) {
  55. $data[ $key ] = wc_format_decimal( $data[ $key ], 2 );
  56. }
  57. // Format date values.
  58. foreach ( $format_date as $key ) {
  59. $data[ $key ] = $data[ $key ] ? wc_rest_prepare_date_response( get_gmt_from_date( date( 'Y-m-d H:i:s', $data[ $key ] ) ) ) : null;
  60. }
  61. // Format null values.
  62. foreach ( $format_null as $key ) {
  63. $data[ $key ] = $data[ $key ] ? $data[ $key ] : null;
  64. }
  65. $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  66. $data = $this->add_additional_fields_to_object( $data, $request );
  67. $data = $this->filter_response_by_context( $data, $context );
  68. $response = rest_ensure_response( $data );
  69. $response->add_links( $this->prepare_links( $post, $request ) );
  70. /**
  71. * Filter the data for a response.
  72. *
  73. * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
  74. * prepared for the response.
  75. *
  76. * @param WP_REST_Response $response The response object.
  77. * @param WP_Post $post Post object.
  78. * @param WP_REST_Request $request Request object.
  79. */
  80. return apply_filters( "woocommerce_rest_prepare_{$this->post_type}", $response, $post, $request );
  81. }
  82. /**
  83. * Prepare a single coupon for create or update.
  84. *
  85. * @deprecated 3.0.0
  86. *
  87. * @param WP_REST_Request $request Request object.
  88. * @return WP_Error|stdClass $data Post object.
  89. */
  90. protected function prepare_item_for_database( $request ) {
  91. global $wpdb;
  92. $id = isset( $request['id'] ) ? absint( $request['id'] ) : 0;
  93. $coupon = new WC_Coupon( $id );
  94. $schema = $this->get_item_schema();
  95. $data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) );
  96. // Validate required POST fields.
  97. if ( 'POST' === $request->get_method() && 0 === $coupon->get_id() ) {
  98. if ( empty( $request['code'] ) ) {
  99. return new WP_Error( 'woocommerce_rest_empty_coupon_code', sprintf( __( 'The coupon code cannot be empty.', 'woocommerce' ), 'code' ), array( 'status' => 400 ) );
  100. }
  101. }
  102. // Handle all writable props.
  103. foreach ( $data_keys as $key ) {
  104. $value = $request[ $key ];
  105. if ( ! is_null( $value ) ) {
  106. switch ( $key ) {
  107. case 'code' :
  108. $coupon_code = wc_format_coupon_code( $value );
  109. $id = $coupon->get_id() ? $coupon->get_id() : 0;
  110. $id_from_code = wc_get_coupon_id_by_code( $coupon_code, $id );
  111. if ( $id_from_code ) {
  112. return new WP_Error( 'woocommerce_rest_coupon_code_already_exists', __( 'The coupon code already exists', 'woocommerce' ), array( 'status' => 400 ) );
  113. }
  114. $coupon->set_code( $coupon_code );
  115. break;
  116. case 'meta_data' :
  117. if ( is_array( $value ) ) {
  118. foreach ( $value as $meta ) {
  119. $coupon->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
  120. }
  121. }
  122. break;
  123. case 'description' :
  124. $coupon->set_description( wp_filter_post_kses( $value ) );
  125. break;
  126. default :
  127. if ( is_callable( array( $coupon, "set_{$key}" ) ) ) {
  128. $coupon->{"set_{$key}"}( $value );
  129. }
  130. break;
  131. }
  132. }
  133. }
  134. /**
  135. * Filter the query_vars used in `get_items` for the constructed query.
  136. *
  137. * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
  138. * prepared for insertion.
  139. *
  140. * @param WC_Coupon $coupon The coupon object.
  141. * @param WP_REST_Request $request Request object.
  142. */
  143. return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $coupon, $request );
  144. }
  145. }