1 <?php
2 3 4 5 6 7 8 9
10
11 if ( ! defined( 'ABSPATH' ) ) exit;
12
13 if ( ! class_exists( 'WC_Admin_CPT' ) )
14 include( 'class-wc-admin-cpt.php' );
15
16 if ( ! class_exists( 'WC_Admin_CPT_Shop_Coupon' ) ) :
17
18 19 20
21 class WC_Admin_CPT_Shop_Coupon extends WC_Admin_CPT {
22
23 24 25
26 public function __construct() {
27 $this->type = 'shop_coupon';
28
29
30 add_filter( 'enter_title_here', array( $this, 'enter_title_here' ), 1, 2 );
31 add_action( 'edit_form_after_title', array( $this, 'coupon_description_field' ) );
32
33
34 add_filter( 'manage_edit-shop_coupon_columns', array( $this, 'edit_columns' ) );
35 add_action( 'manage_shop_coupon_posts_custom_column', array( $this, 'custom_columns' ), 2 );
36 add_filter( 'request', array( $this, 'coupons_by_type_query' ) );
37
38
39 add_action( 'restrict_manage_posts', array( $this, 'coupon_filters' ) );
40
41
42 parent::__construct();
43 }
44
45 46 47 48 49 50
51 public function enter_title_here( $text, $post ) {
52 if ( $post->post_type == 'shop_coupon' )
53 return __( 'Coupon code', 'woocommerce' );
54
55 return $text;
56 }
57
58 59 60 61
62 public function coupon_description_field( $post ) {
63 if ( $post->post_type != 'shop_coupon' )
64 return;
65 ?>
66 <textarea id="woocommerce-coupon-description" name="excerpt" cols="5" rows="2" placeholder="<?php esc_attr_e( 'Description (optional)', 'woocommerce' ); ?>"><?php echo esc_textarea( $post->post_excerpt ); ?></textarea>
67 <?php
68 }
69
70 71 72
73 public function edit_columns( $columns ) {
74 $columns = array();
75
76 $columns["cb"] = "<input type=\"checkbox\" />";
77 $columns["coupon_code"] = __( 'Code', 'woocommerce' );
78 $columns["type"] = __( 'Coupon type', 'woocommerce' );
79 $columns["amount"] = __( 'Coupon amount', 'woocommerce' );
80 $columns["description"] = __( 'Description', 'woocommerce' );
81 $columns["products"] = __( 'Product IDs', 'woocommerce' );
82 $columns["usage"] = __( 'Usage / Limit', 'woocommerce' );
83 $columns["expiry_date"] = __( 'Expiry date', 'woocommerce' );
84
85 return $columns;
86 }
87
88 89 90 91
92 public function custom_columns( $column ) {
93 global $post, $woocommerce;
94
95 switch ( $column ) {
96 case "coupon_code" :
97 $edit_link = get_edit_post_link( $post->ID );
98 $title = _draft_or_post_title();
99 $post_type_object = get_post_type_object( $post->post_type );
100 $can_edit_post = current_user_can( $post_type_object->cap->edit_post, $post->ID );
101
102 echo '<div class="code tips" data-tip="' . __( 'Edit coupon', 'woocommerce' ) . '"><a href="' . esc_attr( $edit_link ) . '"><span>' . esc_html( $title ). '</span></a></div>';
103
104 _post_states( $post );
105
106
107 $actions = array();
108
109 if ( current_user_can( $post_type_object->cap->delete_post, $post->ID ) ) {
110 if ( 'trash' == $post->post_status )
111 $actions['untrash'] = "<a title='" . esc_attr( __( 'Restore this item from the Trash', 'woocommerce' ) ) . "' href='" . wp_nonce_url( admin_url( sprintf( $post_type_object->_edit_link . '&action=untrash', $post->ID ) ), 'untrash-post_' . $post->ID ) . "'>" . __( 'Restore', 'woocommerce' ) . "</a>";
112 elseif ( EMPTY_TRASH_DAYS )
113 $actions['trash'] = "<a class='submitdelete' title='" . esc_attr( __( 'Move this item to the Trash', 'woocommerce' ) ) . "' href='" . get_delete_post_link( $post->ID ) . "'>" . __( 'Trash', 'woocommerce' ) . "</a>";
114 if ( 'trash' == $post->post_status || !EMPTY_TRASH_DAYS )
115 $actions['delete'] = "<a class='submitdelete' title='" . esc_attr( __( 'Delete this item permanently', 'woocommerce' ) ) . "' href='" . get_delete_post_link( $post->ID, '', true ) . "'>" . __( 'Delete Permanently', 'woocommerce' ) . "</a>";
116 }
117
118 $actions = apply_filters( 'post_row_actions', $actions, $post );
119
120 echo '<div class="row-actions">';
121
122 $i = 0;
123 $action_count = sizeof($actions);
124
125 foreach ( $actions as $action => $link ) {
126 ++$i;
127 ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
128 echo "<span class='$action'>$link$sep</span>";
129 }
130 echo '</div>';
131
132 break;
133 case "type" :
134 echo esc_html( wc_get_coupon_type( get_post_meta( $post->ID, 'discount_type', true ) ) );
135 break;
136 case "amount" :
137 echo esc_html( get_post_meta( $post->ID, 'coupon_amount', true ) );
138 break;
139 case "products" :
140 $product_ids = get_post_meta( $post->ID, 'product_ids', true );
141 $product_ids = $product_ids ? array_map( 'absint', explode( ',', $product_ids ) ) : array();
142 if ( sizeof( $product_ids ) > 0 )
143 echo esc_html( implode( ', ', $product_ids ) );
144 else
145 echo '–';
146 break;
147 case "usage_limit" :
148 $usage_limit = get_post_meta( $post->ID, 'usage_limit', true );
149
150 if ( $usage_limit )
151 echo esc_html( $usage_limit );
152 else
153 echo '–';
154 break;
155 case "usage" :
156 $usage_count = absint( get_post_meta( $post->ID, 'usage_count', true ) );
157 $usage_limit = esc_html( get_post_meta($post->ID, 'usage_limit', true) );
158
159 if ( $usage_limit )
160 printf( __( '%s / %s', 'woocommerce' ), $usage_count, $usage_limit );
161 else
162 printf( __( '%s / ∞', 'woocommerce' ), $usage_count );
163 break;
164 case "expiry_date" :
165 $expiry_date = get_post_meta($post->ID, 'expiry_date', true);
166
167 if ( $expiry_date )
168 echo esc_html( date_i18n( 'F j, Y', strtotime( $expiry_date ) ) );
169 else
170 echo '–';
171 break;
172 case "description" :
173 echo wp_kses_post( $post->post_excerpt );
174 break;
175 }
176 }
177
178 179 180 181 182 183
184 public function coupons_by_type_query( $vars ) {
185 global $typenow, $wp_query;
186 if ( $typenow == 'shop_coupon' && ! empty( $_GET['coupon_type'] ) ) {
187
188 $vars['meta_key'] = 'discount_type';
189 $vars['meta_value'] = wc_clean( $_GET['coupon_type'] );
190
191 }
192
193 return $vars;
194 }
195
196 197 198
199 public function coupon_filters() {
200 global $woocommerce, $typenow, $wp_query;
201
202 if ( $typenow != 'shop_coupon' )
203 return;
204
205
206 ?>
207 <select name='coupon_type' id='dropdown_shop_coupon_type'>
208 <option value=""><?php _e( 'Show all types', 'woocommerce' ); ?></option>
209 <?php
210 $types = wc_get_coupon_types();
211
212 foreach ( $types as $name => $type ) {
213 echo '<option value="' . esc_attr( $name ) . '"';
214
215 if ( isset( $_GET['coupon_type'] ) )
216 selected( $name, $_GET['coupon_type'] );
217
218 echo '>' . esc_html__( $type, 'woocommerce' ) . '</option>';
219 }
220 ?>
221 </select>
222 <?php
223
224 wc_enqueue_js( "
225 jQuery('select#dropdown_shop_coupon_type, select[name=m]').css('width', '150px').chosen();
226 " );
227 }
228 }
229
230 endif;
231
232 return new WC_Admin_CPT_Shop_Coupon();