WC_REST_Legacy_Coupons_Controller

REST API Legacy Coupons controller class.

Defined (1)

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

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