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