CouponWPListTable.php
291 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages\Membership\CouponsPage; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\Coupon\CouponEntity; |
| 6 | use ProfilePress\Core\Membership\Models\Coupon\CouponFactory; |
| 7 | use ProfilePress\Core\Membership\Models\Coupon\CouponUnit; |
| 8 | use ProfilePress\Core\Membership\Repositories\CouponRepository; |
| 9 | |
| 10 | class CouponWPListTable extends \WP_List_Table |
| 11 | { |
| 12 | public function __construct() |
| 13 | { |
| 14 | parent::__construct([ |
| 15 | 'singular' => 'ppress-coupon-code', |
| 16 | 'plural' => 'ppress-coupon-codes', |
| 17 | 'ajax' => false |
| 18 | ]); |
| 19 | } |
| 20 | |
| 21 | public function no_items() |
| 22 | { |
| 23 | _e('No coupon code found.', 'wp-user-avatar'); |
| 24 | } |
| 25 | |
| 26 | public function get_columns() |
| 27 | { |
| 28 | return [ |
| 29 | 'cb' => '<input type="checkbox" />', |
| 30 | 'coupon_code' => esc_html__('Code', 'wp-user-avatar'), |
| 31 | 'discount' => esc_html__('Discount', 'wp-user-avatar'), |
| 32 | 'redemption' => esc_html__('Redemptions', 'wp-user-avatar'), |
| 33 | 'status' => esc_html__('Status', 'wp-user-avatar'), |
| 34 | 'start_date' => esc_html__('Start Date', 'wp-user-avatar'), |
| 35 | 'end_date' => esc_html__('End Date', 'wp-user-avatar'), |
| 36 | ]; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Render the bulk edit checkbox |
| 41 | * |
| 42 | * @param CouponEntity $item |
| 43 | * |
| 44 | * @return string |
| 45 | */ |
| 46 | public function column_cb($item) |
| 47 | { |
| 48 | return sprintf('<input type="checkbox" name="coupon_id[]" value="%s" />', $item->id); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | public function column_coupon_code(CouponEntity $item) |
| 53 | { |
| 54 | $coupon_id = absint($item->id); |
| 55 | |
| 56 | $is_active = $item->is_active(); |
| 57 | |
| 58 | $edit_link = esc_url(add_query_arg(['ppress_coupon_action' => 'edit', 'id' => $coupon_id], PPRESS_MEMBERSHIP_COUPONS_SETTINGS_PAGE)); |
| 59 | $activate_link = esc_url(add_query_arg(['ppress_coupon_action' => 'activate', 'id' => $coupon_id, '_wpnonce' => wp_create_nonce('pp_coupon_activate_rule')], PPRESS_MEMBERSHIP_COUPONS_SETTINGS_PAGE)); |
| 60 | $deactivate_link = esc_url(add_query_arg(['ppress_coupon_action' => 'deactivate', 'id' => $coupon_id, '_wpnonce' => wp_create_nonce('pp_coupon_deactivate_rule')], PPRESS_MEMBERSHIP_COUPONS_SETTINGS_PAGE)); |
| 61 | $delete_link = self::delete_coupon_url($coupon_id); |
| 62 | |
| 63 | $actions = [ |
| 64 | 'edit' => sprintf('<a href="%s">%s</a>', $edit_link, esc_html__('Edit', 'wp-user-avatar')), |
| 65 | ]; |
| 66 | |
| 67 | if (true === $is_active) { |
| 68 | $actions['deactivate'] = sprintf('<a href="%s">%s</a>', $deactivate_link, esc_html__('Deactivate', 'wp-user-avatar')); |
| 69 | } |
| 70 | |
| 71 | if (false === $is_active) { |
| 72 | $actions['activate'] = sprintf('<a href="%s">%s</a>', $activate_link, esc_html__('Activate', 'wp-user-avatar')); |
| 73 | } |
| 74 | |
| 75 | $actions['delete'] = sprintf('<a class="pp-confirm-delete" href="%s">%s</a>', $delete_link, esc_html__('Delete', 'wp-user-avatar')); |
| 76 | |
| 77 | $a = '<a href="' . $edit_link . '">' . esc_html($item->code) . '</a>'; |
| 78 | |
| 79 | $coupon_type = $item->is_recurring() ? esc_html__('Recurring', 'wp-user-avatar') : esc_html__('First Payment', 'wp-user-avatar'); |
| 80 | |
| 81 | $a .= ' <span class="post-state"> — ' . $coupon_type . '</span>'; |
| 82 | |
| 83 | return '<strong>' . $a . '</strong>' . $this->row_actions($actions); |
| 84 | } |
| 85 | |
| 86 | public function column_discount(CouponEntity $item) |
| 87 | { |
| 88 | $unit = $item->unit; |
| 89 | |
| 90 | if (CouponUnit::FLAT == $unit) { |
| 91 | return ppress_display_amount($item->amount); |
| 92 | } |
| 93 | |
| 94 | return $item->amount . '%'; |
| 95 | } |
| 96 | |
| 97 | public function column_start_date(CouponEntity $item) |
| 98 | { |
| 99 | $date = $item->start_date; |
| 100 | |
| 101 | if (empty($date)) return '—'; |
| 102 | |
| 103 | return gmdate(get_option('date_format'), strtotime($date . ' UTC')); |
| 104 | } |
| 105 | |
| 106 | public function column_end_date(CouponEntity $item) |
| 107 | { |
| 108 | $date = $item->end_date; |
| 109 | |
| 110 | if (empty($date)) return '—'; |
| 111 | |
| 112 | return gmdate(get_option('date_format'), strtotime($date . ' UTC')); |
| 113 | } |
| 114 | |
| 115 | public function column_redemption(CouponEntity $item) |
| 116 | { |
| 117 | $usage_limit = absint($item->usage_limit); |
| 118 | |
| 119 | if (empty($usage_limit) || $usage_limit === 0) { |
| 120 | $usage_limit = esc_html__('Unlimited', 'wp-user-avatar'); |
| 121 | } |
| 122 | |
| 123 | return sprintf('<a href="%s">%s</a>', |
| 124 | add_query_arg(['coupon_code' => $item->code], PPRESS_MEMBERSHIP_ORDERS_SETTINGS_PAGE), |
| 125 | $item->get_usage_count() . ' / ' . $usage_limit |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | public function column_status(CouponEntity $item) |
| 130 | { |
| 131 | $status = sprintf('<span class="dashicons-before dashicons-yes">%s</span>', esc_html__('Active', 'wp-user-avatar')); |
| 132 | if ( ! $item->is_active()) { |
| 133 | $status = sprintf('<span class="dashicons-before dashicons-no-alt">%s</span>', esc_html__('Inactive', 'wp-user-avatar')); |
| 134 | } |
| 135 | |
| 136 | if ($item->is_expired()) { |
| 137 | $status = sprintf('<span class="dashicons-before dashicons-no-alt">%s</span>', esc_html__('Expired', 'wp-user-avatar')); |
| 138 | } |
| 139 | |
| 140 | return $status; |
| 141 | } |
| 142 | |
| 143 | public static function delete_coupon_url($coupon_id) |
| 144 | { |
| 145 | $nonce_delete = wp_create_nonce('pp_coupon_delete_rule'); |
| 146 | |
| 147 | return add_query_arg(['action' => 'delete', 'id' => $coupon_id, '_wpnonce' => $nonce_delete], PPRESS_MEMBERSHIP_COUPONS_SETTINGS_PAGE); |
| 148 | } |
| 149 | |
| 150 | public function get_coupons($per_page, $current_page = 1) |
| 151 | { |
| 152 | return CouponRepository::init()->retrieveAll($per_page, $current_page); |
| 153 | } |
| 154 | |
| 155 | public function record_count() |
| 156 | { |
| 157 | return CouponRepository::init()->record_count(); |
| 158 | } |
| 159 | |
| 160 | public function prepare_items() |
| 161 | { |
| 162 | $this->_column_headers = $this->get_column_info(); |
| 163 | |
| 164 | $this->process_bulk_action(); |
| 165 | |
| 166 | $per_page = $this->get_items_per_page('coupons_per_page', 10); |
| 167 | $current_page = $this->get_pagenum(); |
| 168 | $total_items = $this->record_count(); |
| 169 | |
| 170 | $this->set_pagination_args([ |
| 171 | 'total_items' => $total_items, //WE have to calculate the total number of items |
| 172 | 'per_page' => $per_page //WE have to determine how many items to show on a page |
| 173 | ]); |
| 174 | |
| 175 | $this->items = $this->get_coupons($per_page, $current_page); |
| 176 | } |
| 177 | |
| 178 | public function current_action() |
| 179 | { |
| 180 | if (isset($_REQUEST['filter_action']) && ! empty($_REQUEST['filter_action'])) { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | if (isset($_REQUEST['action']) && -1 != $_REQUEST['action']) { |
| 185 | return $_REQUEST['action']; |
| 186 | } |
| 187 | |
| 188 | if (isset($_REQUEST['ppress_coupon_action']) && -1 != $_REQUEST['ppress_coupon_action']) { |
| 189 | return $_REQUEST['ppress_coupon_action']; |
| 190 | } |
| 191 | |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | public function get_bulk_actions() |
| 196 | { |
| 197 | $actions = [ |
| 198 | 'bulk-delete' => esc_html__('Delete', 'wp-user-avatar'), |
| 199 | 'bulk-activate' => esc_html__('Activate', 'wp-user-avatar'), |
| 200 | 'bulk-deactivate' => esc_html__('Deactivate', 'wp-user-avatar') |
| 201 | ]; |
| 202 | |
| 203 | return $actions; |
| 204 | } |
| 205 | |
| 206 | public function process_bulk_action() |
| 207 | { |
| 208 | $coupon_id = absint(ppress_var($_GET, 'id', 0)); |
| 209 | |
| 210 | $couponObj = CouponFactory::fromId($coupon_id); |
| 211 | |
| 212 | if ('deactivate' === $this->current_action()) { |
| 213 | |
| 214 | check_admin_referer('pp_coupon_deactivate_rule'); |
| 215 | |
| 216 | if ( ! current_user_can('manage_options')) return; |
| 217 | |
| 218 | $couponObj->deactivate(); |
| 219 | } |
| 220 | |
| 221 | if ('activate' === $this->current_action()) { |
| 222 | |
| 223 | check_admin_referer('pp_coupon_activate_rule'); |
| 224 | |
| 225 | if ( ! current_user_can('manage_options')) return; |
| 226 | |
| 227 | $couponObj->activate(); |
| 228 | } |
| 229 | |
| 230 | if ('delete' === $this->current_action()) { |
| 231 | |
| 232 | check_admin_referer('pp_coupon_delete_rule'); |
| 233 | |
| 234 | if ( ! current_user_can('manage_options')) return; |
| 235 | |
| 236 | CouponRepository::init()->delete($coupon_id); |
| 237 | } |
| 238 | |
| 239 | if ('bulk-delete' === $this->current_action()) { |
| 240 | |
| 241 | check_admin_referer('bulk-' . $this->_args['plural']); |
| 242 | |
| 243 | if ( ! current_user_can('manage_options')) return; |
| 244 | |
| 245 | $coupon_ids = array_map('absint', $_POST['coupon_id']); |
| 246 | |
| 247 | foreach ($coupon_ids as $coupon_id) { |
| 248 | CouponRepository::init()->delete($coupon_id); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if ('bulk-activate' === $this->current_action()) { |
| 253 | |
| 254 | check_admin_referer('bulk-' . $this->_args['plural']); |
| 255 | |
| 256 | if ( ! current_user_can('manage_options')) return; |
| 257 | |
| 258 | $coupon_ids = array_map('absint', $_POST['coupon_id']); |
| 259 | |
| 260 | foreach ($coupon_ids as $coupon_id) { |
| 261 | CouponFactory::fromId($coupon_id)->activate(); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | if ('bulk-deactivate' === $this->current_action()) { |
| 266 | |
| 267 | check_admin_referer('bulk-' . $this->_args['plural']); |
| 268 | |
| 269 | if ( ! current_user_can('manage_options')) return; |
| 270 | |
| 271 | $coupon_ids = array_map('absint', $_POST['coupon_id']); |
| 272 | |
| 273 | foreach ($coupon_ids as $coupon_id) { |
| 274 | CouponFactory::fromId($coupon_id)->deactivate(); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if ($this->current_action() !== false) { |
| 279 | ppress_do_admin_redirect(PPRESS_MEMBERSHIP_COUPONS_SETTINGS_PAGE); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * @return array List of CSS classes for the table tag. |
| 285 | */ |
| 286 | public function get_table_classes() |
| 287 | { |
| 288 | return array('widefat', 'fixed', 'striped', 'coupon', 'ppview'); |
| 289 | } |
| 290 | } |
| 291 |