/wp-content/plugins/woocommerce/includes/api/v1/class-wc-api-coupons.php

https://gitlab.com/webkod3r/tripolis · PHP · 246 lines · 105 code · 50 blank · 91 comment · 8 complexity · 25db4d1e0f0d8e05164934acabdb4ce6 MD5 · raw file

  1. <?php
  2. /**
  3. * WooCommerce API Coupons Class
  4. *
  5. * Handles requests to the /coupons endpoint
  6. *
  7. * @author WooThemes
  8. * @category API
  9. * @package WooCommerce/API
  10. * @since 2.1
  11. * @version 2.1
  12. */
  13. if ( ! defined( 'ABSPATH' ) ) {
  14. exit; // Exit if accessed directly
  15. }
  16. class WC_API_Coupons extends WC_API_Resource {
  17. /** @var string $base the route base */
  18. protected $base = '/coupons';
  19. /**
  20. * Register the routes for this class
  21. *
  22. * GET /coupons
  23. * GET /coupons/count
  24. * GET /coupons/<id>
  25. *
  26. * @since 2.1
  27. * @param array $routes
  28. * @return array
  29. */
  30. public function register_routes( $routes ) {
  31. # GET /coupons
  32. $routes[ $this->base ] = array(
  33. array( array( $this, 'get_coupons' ), WC_API_Server::READABLE ),
  34. );
  35. # GET /coupons/count
  36. $routes[ $this->base . '/count'] = array(
  37. array( array( $this, 'get_coupons_count' ), WC_API_Server::READABLE ),
  38. );
  39. # GET /coupons/<id>
  40. $routes[ $this->base . '/(?P<id>\d+)' ] = array(
  41. array( array( $this, 'get_coupon' ), WC_API_Server::READABLE ),
  42. );
  43. # GET /coupons/code/<code>, note that coupon codes can contain spaces, dashes and underscores
  44. $routes[ $this->base . '/code/(?P<code>\w[\w\s\-]*)' ] = array(
  45. array( array( $this, 'get_coupon_by_code' ), WC_API_Server::READABLE ),
  46. );
  47. return $routes;
  48. }
  49. /**
  50. * Get all coupons
  51. *
  52. * @since 2.1
  53. * @param string $fields
  54. * @param array $filter
  55. * @param int $page
  56. * @return array
  57. */
  58. public function get_coupons( $fields = null, $filter = array(), $page = 1 ) {
  59. $filter['page'] = $page;
  60. $query = $this->query_coupons( $filter );
  61. $coupons = array();
  62. foreach( $query->posts as $coupon_id ) {
  63. if ( ! $this->is_readable( $coupon_id ) )
  64. continue;
  65. $coupons[] = current( $this->get_coupon( $coupon_id, $fields ) );
  66. }
  67. $this->server->add_pagination_headers( $query );
  68. return array( 'coupons' => $coupons );
  69. }
  70. /**
  71. * Get the coupon for the given ID
  72. *
  73. * @since 2.1
  74. * @param int $id the coupon ID
  75. * @param string $fields fields to include in response
  76. * @return array|WP_Error
  77. */
  78. public function get_coupon( $id, $fields = null ) {
  79. global $wpdb;
  80. $id = $this->validate_request( $id, 'shop_coupon', 'read' );
  81. if ( is_wp_error( $id ) )
  82. return $id;
  83. // get the coupon code
  84. $code = $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM $wpdb->posts WHERE id = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $id ) );
  85. if ( is_null( $code ) )
  86. return new WP_Error( 'woocommerce_api_invalid_coupon_id', __( 'Invalid coupon ID', 'woocommerce' ), array( 'status' => 404 ) );
  87. $coupon = new WC_Coupon( $code );
  88. $coupon_post = get_post( $coupon->id );
  89. $coupon_data = array(
  90. 'id' => $coupon->id,
  91. 'code' => $coupon->code,
  92. 'type' => $coupon->type,
  93. 'created_at' => $this->server->format_datetime( $coupon_post->post_date_gmt ),
  94. 'updated_at' => $this->server->format_datetime( $coupon_post->post_modified_gmt ),
  95. 'amount' => wc_format_decimal( $coupon->amount, 2 ),
  96. 'individual_use' => ( 'yes' === $coupon->individual_use ),
  97. 'product_ids' => array_map( 'absint', (array) $coupon->product_ids ),
  98. 'exclude_product_ids' => array_map( 'absint', (array) $coupon->exclude_product_ids ),
  99. 'usage_limit' => ( ! empty( $coupon->usage_limit ) ) ? $coupon->usage_limit : null,
  100. 'usage_limit_per_user' => ( ! empty( $coupon->usage_limit_per_user ) ) ? $coupon->usage_limit_per_user : null,
  101. 'limit_usage_to_x_items' => (int) $coupon->limit_usage_to_x_items,
  102. 'usage_count' => (int) $coupon->usage_count,
  103. 'expiry_date' => $this->server->format_datetime( $coupon->expiry_date ),
  104. 'enable_free_shipping' => $coupon->enable_free_shipping(),
  105. 'product_category_ids' => array_map( 'absint', (array) $coupon->product_categories ),
  106. 'exclude_product_category_ids' => array_map( 'absint', (array) $coupon->exclude_product_categories ),
  107. 'exclude_sale_items' => $coupon->exclude_sale_items(),
  108. 'minimum_amount' => wc_format_decimal( $coupon->minimum_amount, 2 ),
  109. 'customer_emails' => $coupon->customer_email,
  110. );
  111. return array( 'coupon' => apply_filters( 'woocommerce_api_coupon_response', $coupon_data, $coupon, $fields, $this->server ) );
  112. }
  113. /**
  114. * Get the total number of coupons
  115. *
  116. * @since 2.1
  117. * @param array $filter
  118. * @return array
  119. */
  120. public function get_coupons_count( $filter = array() ) {
  121. $query = $this->query_coupons( $filter );
  122. if ( ! current_user_can( 'read_private_shop_coupons' ) )
  123. return new WP_Error( 'woocommerce_api_user_cannot_read_coupons_count', __( 'You do not have permission to read the coupons count', 'woocommerce' ), array( 'status' => 401 ) );
  124. return array( 'count' => (int) $query->found_posts );
  125. }
  126. /**
  127. * Get the coupon for the given code
  128. *
  129. * @since 2.1
  130. * @param string $code the coupon code
  131. * @param string $fields fields to include in response
  132. * @return int|WP_Error
  133. */
  134. public function get_coupon_by_code( $code, $fields = null ) {
  135. global $wpdb;
  136. $id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 1;", $code ) );
  137. if ( is_null( $id ) )
  138. return new WP_Error( 'woocommerce_api_invalid_coupon_code', __( 'Invalid coupon code', 'woocommerce' ), array( 'status' => 404 ) );
  139. return $this->get_coupon( $id, $fields );
  140. }
  141. /**
  142. * Create a coupon
  143. *
  144. * @TODO implement in 2.2
  145. * @param array $data
  146. * @return array
  147. */
  148. public function create_coupon( $data ) {
  149. return array();
  150. }
  151. /**
  152. * Edit a coupon
  153. *
  154. * @TODO implement in 2.2
  155. * @param int $id the coupon ID
  156. * @param array $data
  157. * @return array
  158. */
  159. public function edit_coupon( $id, $data ) {
  160. $id = $this->validate_request( $id, 'shop_coupon', 'edit' );
  161. if ( is_wp_error( $id ) )
  162. return $id;
  163. return $this->get_coupon( $id );
  164. }
  165. /**
  166. * Delete a coupon
  167. *
  168. * @TODO enable along with PUT/POST in 2.2
  169. * @param int $id the coupon ID
  170. * @param bool $force true to permanently delete coupon, false to move to trash
  171. * @return array
  172. */
  173. public function delete_coupon( $id, $force = false ) {
  174. $id = $this->validate_request( $id, 'shop_coupon', 'delete' );
  175. if ( is_wp_error( $id ) )
  176. return $id;
  177. return $this->delete( $id, 'shop_coupon', ( 'true' === $force ) );
  178. }
  179. /**
  180. * Helper method to get coupon post objects
  181. *
  182. * @since 2.1
  183. * @param array $args request arguments for filtering query
  184. * @return WP_Query
  185. */
  186. private function query_coupons( $args ) {
  187. // set base query arguments
  188. $query_args = array(
  189. 'fields' => 'ids',
  190. 'post_type' => 'shop_coupon',
  191. 'post_status' => 'publish',
  192. );
  193. $query_args = $this->merge_query_args( $query_args, $args );
  194. return new WP_Query( $query_args );
  195. }
  196. }