class-wc-admin-list-table-coupons.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * List tables: coupons.
  4. *
  5. * @package WooCommerce/Admin
  6. * @version 3.3.0
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit;
  10. }
  11. if ( class_exists( 'WC_Admin_List_Table_Coupons', false ) ) {
  12. return;
  13. }
  14. if ( ! class_exists( 'WC_Admin_List_Table', false ) ) {
  15. include_once 'abstract-class-wc-admin-list-table.php';
  16. }
  17. /**
  18. * WC_Admin_List_Table_Coupons Class.
  19. */
  20. class WC_Admin_List_Table_Coupons extends WC_Admin_List_Table {
  21. /**
  22. * Post type.
  23. *
  24. * @var string
  25. */
  26. protected $list_table_type = 'shop_coupon';
  27. /**
  28. * Constructor.
  29. */
  30. public function __construct() {
  31. parent::__construct();
  32. add_filter( 'disable_months_dropdown', '__return_true' );
  33. }
  34. /**
  35. * Render blank state.
  36. */
  37. protected function render_blank_state() {
  38. echo '<div class="woocommerce-BlankState">';
  39. echo '<h2 class="woocommerce-BlankState-message">' . esc_html__( 'Coupons are a great way to offer discounts and rewards to your customers. They will appear here once created.', 'woocommerce' ) . '</h2>';
  40. echo '<a class="woocommerce-BlankState-cta button-primary button" href="' . esc_url( admin_url( 'post-new.php?post_type=shop_coupon' ) ) . '">' . esc_html__( 'Create your first coupon', 'woocommerce' ) . '</a>';
  41. echo '<a class="woocommerce-BlankState-cta button" target="_blank" href="https://docs.woocommerce.com/document/coupon-management/?utm_source=blankslate&utm_medium=product&utm_content=couponsdoc&utm_campaign=woocommerceplugin">' . esc_html__( 'Learn more about coupons', 'woocommerce' ) . '</a>';
  42. echo '</div>';
  43. }
  44. /**
  45. * Define primary column.
  46. *
  47. * @return string
  48. */
  49. protected function get_primary_column() {
  50. return 'coupon_code';
  51. }
  52. /**
  53. * Get row actions to show in the list table.
  54. *
  55. * @param array $actions Array of actions.
  56. * @param WP_Post $post Current post object.
  57. * @return array
  58. */
  59. protected function get_row_actions( $actions, $post ) {
  60. unset( $actions['inline hide-if-no-js'] );
  61. return $actions;
  62. }
  63. /**
  64. * Define which columns to show on this screen.
  65. *
  66. * @param array $columns Existing columns.
  67. * @return array
  68. */
  69. public function define_columns( $columns ) {
  70. $show_columns = array();
  71. $show_columns['cb'] = $columns['cb'];
  72. $show_columns['coupon_code'] = __( 'Code', 'woocommerce' );
  73. $show_columns['type'] = __( 'Coupon type', 'woocommerce' );
  74. $show_columns['amount'] = __( 'Coupon amount', 'woocommerce' );
  75. $show_columns['description'] = __( 'Description', 'woocommerce' );
  76. $show_columns['products'] = __( 'Product IDs', 'woocommerce' );
  77. $show_columns['usage'] = __( 'Usage / Limit', 'woocommerce' );
  78. $show_columns['expiry_date'] = __( 'Expiry date', 'woocommerce' );
  79. return $show_columns;
  80. }
  81. /**
  82. * Pre-fetch any data for the row each column has access to it. the_coupon global is there for bw compat.
  83. *
  84. * @param int $post_id Post ID being shown.
  85. */
  86. protected function prepare_row_data( $post_id ) {
  87. global $the_coupon;
  88. if ( empty( $this->object ) || $this->object->get_id() !== $post_id ) {
  89. $this->object = new WC_Coupon( $post_id );
  90. $the_coupon = $this->object;
  91. }
  92. }
  93. /**
  94. * Render columm: coupon_code.
  95. */
  96. protected function render_coupon_code_column() {
  97. global $post;
  98. $edit_link = get_edit_post_link( $this->object->get_id() );
  99. $title = $this->object->get_code();
  100. echo '<strong><a class="row-title" href="' . esc_url( $edit_link ) . '">' . esc_html( $title ) . '</a>';
  101. _post_states( $post );
  102. echo '</strong>';
  103. }
  104. /**
  105. * Render columm: type.
  106. */
  107. protected function render_type_column() {
  108. echo esc_html( wc_get_coupon_type( $this->object->get_discount_type() ) );
  109. }
  110. /**
  111. * Render columm: amount.
  112. */
  113. protected function render_amount_column() {
  114. echo esc_html( wc_format_localized_price( $this->object->get_amount() ) );
  115. }
  116. /**
  117. * Render columm: products.
  118. */
  119. protected function render_products_column() {
  120. $product_ids = $this->object->get_product_ids();
  121. if ( count( $product_ids ) > 0 ) {
  122. echo esc_html( implode( ', ', $product_ids ) );
  123. } else {
  124. echo '&ndash;';
  125. }
  126. }
  127. /**
  128. * Render columm: usage_limit.
  129. */
  130. protected function render_usage_limit_column() {
  131. $usage_limit = $this->object->get_usage_limit();
  132. if ( $usage_limit ) {
  133. echo esc_html( $usage_limit );
  134. } else {
  135. echo '&ndash;';
  136. }
  137. }
  138. /**
  139. * Render columm: usage.
  140. */
  141. protected function render_usage_column() {
  142. $usage_count = $this->object->get_usage_count();
  143. $usage_limit = $this->object->get_usage_limit();
  144. printf(
  145. /* translators: 1: count 2: limit */
  146. __( '%1$s / %2$s', 'woocommerce' ),
  147. esc_html( $usage_count ),
  148. $usage_limit ? esc_html( $usage_limit ) : '&infin;'
  149. );
  150. }
  151. /**
  152. * Render columm: expiry_date.
  153. */
  154. protected function render_expiry_date_column() {
  155. $expiry_date = $this->object->get_date_expires();
  156. if ( $expiry_date ) {
  157. echo esc_html( $expiry_date->date_i18n( 'F j, Y' ) );
  158. } else {
  159. echo '&ndash;';
  160. }
  161. }
  162. /**
  163. * Render columm: description.
  164. */
  165. protected function render_description_column() {
  166. echo wp_kses_post( $this->object->get_description() ? $this->object->get_description() : '&ndash;' );
  167. }
  168. /**
  169. * Render any custom filters and search inputs for the list table.
  170. */
  171. protected function render_filters() {
  172. ?>
  173. <select name="coupon_type" id="dropdown_shop_coupon_type">
  174. <option value=""><?php esc_html_e( 'Show all types', 'woocommerce' ); ?></option>
  175. <?php
  176. $types = wc_get_coupon_types();
  177. foreach ( $types as $name => $type ) {
  178. echo '<option value="' . esc_attr( $name ) . '"';
  179. if ( isset( $_GET['coupon_type'] ) ) { // WPCS: input var ok.
  180. selected( $name, wc_clean( wp_unslash( $_GET['coupon_type'] ) ) ); // WPCS: input var ok, sanitization ok.
  181. }
  182. echo '>' . esc_html( $type ) . '</option>';
  183. }
  184. ?>
  185. </select>
  186. <?php
  187. }
  188. /**
  189. * Handle any custom filters.
  190. *
  191. * @param array $query_vars Query vars.
  192. * @return array
  193. */
  194. protected function query_filters( $query_vars ) {
  195. if ( ! empty( $_GET['coupon_type'] ) ) { // WPCS: input var ok, sanitization ok.
  196. $query_vars['meta_key'] = 'discount_type'; // phpcs:ignore WordPress.VIP.SlowDBQuery.slow_db_query_meta_key
  197. $query_vars['meta_value'] = wc_clean( wp_unslash( $_GET['coupon_type'] ) ); // phpcs:ignore WordPress.VIP.SlowDBQuery.slow_db_query_meta_value, WordPress.VIP.SuperGlobalInputUsage.AccessDetected
  198. }
  199. return $query_vars;
  200. }
  201. }