OrderWPListTable.php
466 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Admin\SettingsPages\Membership\OrdersPage; |
| 4 | |
| 5 | use ProfilePress\Core\Admin\SettingsPages\Membership\CustomersPage\CustomerWPListTable; |
| 6 | use ProfilePress\Core\Membership\Emails\NewOrderReceipt; |
| 7 | use ProfilePress\Core\Membership\Models\Customer\CustomerFactory; |
| 8 | use ProfilePress\Core\Membership\Models\Order\OrderEntity; |
| 9 | use ProfilePress\Core\Membership\Models\Order\OrderFactory; |
| 10 | use ProfilePress\Core\Membership\Models\Order\OrderMode; |
| 11 | use ProfilePress\Core\Membership\Models\Order\OrderStatus; |
| 12 | use ProfilePress\Core\Membership\Models\Order\OrderType; |
| 13 | use ProfilePress\Core\Membership\Models\Plan\PlanFactory; |
| 14 | use ProfilePress\Core\Membership\PaymentMethods\PaymentMethods; |
| 15 | use ProfilePress\Core\Membership\Repositories\OrderRepository; |
| 16 | use ProfilePress\Core\Membership\Services\OrderService; |
| 17 | |
| 18 | class OrderWPListTable extends \WP_List_Table |
| 19 | { |
| 20 | private $views_count = []; |
| 21 | |
| 22 | public function __construct() |
| 23 | { |
| 24 | parent::__construct([ |
| 25 | 'singular' => 'ppress-order', |
| 26 | 'plural' => 'ppress-orders', |
| 27 | 'ajax' => false |
| 28 | ]); |
| 29 | |
| 30 | $order_statuses = array_keys(OrderStatus::get_all()); |
| 31 | $order_statuses[] = 'all'; |
| 32 | |
| 33 | foreach ($order_statuses as $id) { |
| 34 | |
| 35 | if ('all' == $id) { |
| 36 | $this->views_count[$id] = OrderRepository::init()->record_count(); |
| 37 | } else { |
| 38 | $this->views_count[$id] = OrderRepository::init()->get_count_by_status($id); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public function no_items() |
| 44 | { |
| 45 | _e('No orders found.', 'wp-user-avatar'); |
| 46 | } |
| 47 | |
| 48 | public function get_columns() |
| 49 | { |
| 50 | $columns = [ |
| 51 | 'cb' => '<input type="checkbox" />', |
| 52 | 'order' => esc_html__('Order', 'wp-user-avatar'), |
| 53 | 'plan' => esc_html__('Plan', 'wp-user-avatar'), |
| 54 | 'order_total' => esc_html__('Total', 'wp-user-avatar'), |
| 55 | 'status' => esc_html__('Status', 'wp-user-avatar'), |
| 56 | 'payment_method' => esc_html__('Payment Method', 'wp-user-avatar'), |
| 57 | 'date_created' => esc_html__('Date', 'wp-user-avatar') |
| 58 | ]; |
| 59 | |
| 60 | return $columns; |
| 61 | } |
| 62 | |
| 63 | public function get_views() |
| 64 | { |
| 65 | $views = []; |
| 66 | |
| 67 | $args = ['all' => esc_html__('All', 'wp-user-avatar')] + OrderStatus::get_all(); |
| 68 | |
| 69 | foreach ($args as $id => $status) { |
| 70 | |
| 71 | $url = $id == 'all' ? PPRESS_MEMBERSHIP_ORDERS_SETTINGS_PAGE : esc_url(add_query_arg(['status' => $id])); |
| 72 | |
| 73 | $views[$id] = sprintf( |
| 74 | '<a href="%s"%s>%s <span class="count">(%s)</span></a>', |
| 75 | $url, |
| 76 | ppressGET_var('status', 'all') == $id ? ' class="current"' : '', |
| 77 | $status, |
| 78 | $this->views_count[$id] |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | return $views; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Render the bulk edit checkbox |
| 87 | * |
| 88 | * @param OrderEntity $order |
| 89 | * |
| 90 | * @return string |
| 91 | */ |
| 92 | public function column_cb($order) |
| 93 | { |
| 94 | return sprintf('<input type="checkbox" name="order_id[]" value="%s" />', $order->id); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | public function column_order(OrderEntity $order) |
| 99 | { |
| 100 | $order_id = absint($order->id); |
| 101 | |
| 102 | $edit_link = esc_url(self::view_edit_order_url($order_id)); |
| 103 | $view_customer_link = esc_url(CustomerWPListTable::view_customer_url($order->customer_id)); |
| 104 | $delete_link = self::delete_order_url($order_id); |
| 105 | |
| 106 | $actions = [ |
| 107 | 'edit' => sprintf('<a href="%s">%s</a>', $edit_link, esc_html__('Edit', 'wp-user-avatar')), |
| 108 | 'customer' => sprintf('<a href="%s">%s</a>', $view_customer_link, esc_html__('View Customer', 'wp-user-avatar')), |
| 109 | ]; |
| 110 | |
| 111 | $actions['delete'] = sprintf('<a class="pp-confirm-delete" href="%s">%s</a>', $delete_link, esc_html__('Delete', 'wp-user-avatar')); |
| 112 | |
| 113 | $customer_name = CustomerFactory::fromId($order->customer_id)->get_name(); |
| 114 | |
| 115 | if ( ! empty($customer_name)) { |
| 116 | $title = sprintf(__('#%1$s - %2$s', 'wp-user-avatar'), $order->get_id(), $customer_name); |
| 117 | } else { |
| 118 | $title = sprintf(__('#%1$s - No Customer Assigned', 'wp-user-avatar'), $order->get_id()); |
| 119 | } |
| 120 | |
| 121 | $title = sprintf('<a class="row-title" href="%s">%s</a>', $edit_link, $title); |
| 122 | |
| 123 | $title .= ' <strong><span class="post-state"> — ' . OrderType::get_label($order->order_type) . '</span></strong>'; |
| 124 | |
| 125 | return $title . $this->row_actions($actions); |
| 126 | } |
| 127 | |
| 128 | public function column_plan(OrderEntity $order) |
| 129 | { |
| 130 | return PlanFactory::fromId($order->plan_id)->get_name(); |
| 131 | } |
| 132 | |
| 133 | public function column_payment_method(OrderEntity $order) |
| 134 | { |
| 135 | if (empty($order->payment_method)) return '—'; |
| 136 | |
| 137 | return PaymentMethods::get_instance()->get_by_id($order->payment_method)->get_method_title(); |
| 138 | } |
| 139 | |
| 140 | public function column_order_total(OrderEntity $order) |
| 141 | { |
| 142 | return ppress_display_amount($order->get_total()); |
| 143 | } |
| 144 | |
| 145 | public function column_date_created(OrderEntity $order) |
| 146 | { |
| 147 | $date = $order->date_created; |
| 148 | |
| 149 | if (empty($date)) return '—'; |
| 150 | |
| 151 | return ppress_format_date_time($order->date_created); |
| 152 | } |
| 153 | |
| 154 | public function column_status(OrderEntity $order) |
| 155 | { |
| 156 | return self::get_order_status_badge($order->status); |
| 157 | } |
| 158 | |
| 159 | public static function delete_order_url($order_id) |
| 160 | { |
| 161 | $nonce_delete = wp_create_nonce('pp_order_delete_rule'); |
| 162 | |
| 163 | return add_query_arg(['action' => 'delete', 'id' => $order_id, '_wpnonce' => $nonce_delete], PPRESS_MEMBERSHIP_ORDERS_SETTINGS_PAGE); |
| 164 | } |
| 165 | |
| 166 | public static function view_edit_order_url($order_id) |
| 167 | { |
| 168 | return add_query_arg(['ppress_order_action' => 'edit', 'id' => $order_id], PPRESS_MEMBERSHIP_ORDERS_SETTINGS_PAGE); |
| 169 | } |
| 170 | |
| 171 | public function record_count() |
| 172 | { |
| 173 | $status = ppressGET_var('status', 'all'); |
| 174 | |
| 175 | if ($status != 'all') { |
| 176 | return $this->views_count[$status]; |
| 177 | } |
| 178 | |
| 179 | return OrderRepository::init()->record_count(); |
| 180 | } |
| 181 | |
| 182 | public function prepare_items() |
| 183 | { |
| 184 | $this->_column_headers = $this->get_column_info(); |
| 185 | |
| 186 | $this->process_bulk_action(); |
| 187 | |
| 188 | $per_page = $this->get_items_per_page('orders_per_page', 10); |
| 189 | $current_page = $this->get_pagenum(); |
| 190 | $offset = ($current_page - 1) * $per_page; |
| 191 | |
| 192 | $search = ! empty($_GET['s']) ? sanitize_text_field($_GET['s']) : ''; |
| 193 | |
| 194 | $start_date = ppressGET_var('start_date'); |
| 195 | $end_date = ppressGET_var('end_date'); |
| 196 | $status = ppressGET_var('status'); |
| 197 | $mode = ppressGET_var('mode'); |
| 198 | if ('all' == $mode) $mode = false; |
| 199 | |
| 200 | $payment_method = ppressGET_var('gateway'); |
| 201 | if ('all' == $payment_method) $payment_method = false; |
| 202 | |
| 203 | $query_args = [ |
| 204 | 'number' => $per_page, |
| 205 | 'offset' => $offset, |
| 206 | 'search' => $search, |
| 207 | 'status' => [$status], |
| 208 | 'mode' => $mode, |
| 209 | 'payment_method' => $payment_method, |
| 210 | 'start_date' => $start_date, |
| 211 | 'end_date' => $end_date |
| 212 | ]; |
| 213 | |
| 214 | if (ppressGET_var('by_ci')) { |
| 215 | $query_args['customer_id'] = absint($_GET['by_ci']); |
| 216 | } |
| 217 | |
| 218 | if (ppressGET_var('coupon_code')) { |
| 219 | $query_args['coupon_code'] = sanitize_text_field($_GET['coupon_code']); |
| 220 | } |
| 221 | |
| 222 | $this->items = OrderRepository::init()->retrieveBy($query_args); |
| 223 | |
| 224 | $total_items = OrderRepository::init()->retrieveBy($query_args, true); |
| 225 | |
| 226 | $this->set_pagination_args([ |
| 227 | 'total_items' => $total_items, |
| 228 | 'per_page' => $per_page |
| 229 | ]); |
| 230 | } |
| 231 | |
| 232 | public function current_action() |
| 233 | { |
| 234 | if (isset($_REQUEST['filter_action']) && ! empty($_REQUEST['filter_action'])) { |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | if (isset($_REQUEST['action']) && -1 != $_REQUEST['action']) { |
| 239 | return $_REQUEST['action']; |
| 240 | } |
| 241 | |
| 242 | if (isset($_REQUEST['ppress_order_action']) && -1 != $_REQUEST['ppress_order_action']) { |
| 243 | return $_REQUEST['ppress_order_action']; |
| 244 | } |
| 245 | |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | public function get_bulk_actions() |
| 250 | { |
| 251 | $actions = [ |
| 252 | 'bulk-delete' => esc_html__('Delete', 'wp-user-avatar'), |
| 253 | 'resend-receipt' => esc_html__('Resend Order Receipt', 'wp-user-avatar') |
| 254 | ]; |
| 255 | |
| 256 | return $actions; |
| 257 | } |
| 258 | |
| 259 | public function process_bulk_action() |
| 260 | { |
| 261 | $order_id = absint(ppress_var($_GET, 'id', 0)); |
| 262 | |
| 263 | // Bail if user is not an admin or without admin privileges. |
| 264 | if ( ! current_user_can('manage_options')) return; |
| 265 | |
| 266 | if ('delete' === $this->current_action()) { |
| 267 | |
| 268 | check_admin_referer('pp_order_delete_rule'); |
| 269 | |
| 270 | if ( ! current_user_can('manage_options')) return; |
| 271 | |
| 272 | OrderService::init()->delete_order($order_id); |
| 273 | } |
| 274 | |
| 275 | if ('bulk-delete' === $this->current_action()) { |
| 276 | |
| 277 | check_admin_referer('bulk-' . $this->_args['plural']); |
| 278 | |
| 279 | if ( ! current_user_can('manage_options')) return; |
| 280 | |
| 281 | $order_ids = array_map('absint', $_GET['order_id']); |
| 282 | |
| 283 | foreach ($order_ids as $order_id) { |
| 284 | OrderService::init()->delete_order($order_id); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if ('resend-receipt' === $this->current_action()) { |
| 289 | |
| 290 | check_admin_referer('bulk-' . $this->_args['plural']); |
| 291 | |
| 292 | if ( ! current_user_can('manage_options')) return; |
| 293 | |
| 294 | $order_ids = array_map('absint', $_GET['order_id']); |
| 295 | |
| 296 | foreach ($order_ids as $order_id) { |
| 297 | NewOrderReceipt::init()->dispatch_email(OrderFactory::fromId($order_id)); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if ($this->current_action() !== false) { |
| 302 | wp_safe_redirect(PPRESS_MEMBERSHIP_ORDERS_SETTINGS_PAGE); |
| 303 | exit; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | public function filter_bar() |
| 308 | { |
| 309 | $start_date = isset($_GET['start_date']) ? sanitize_text_field($_GET['start_date']) : null; |
| 310 | $end_date = isset($_GET['end_date']) ? sanitize_text_field($_GET['end_date']) : null; |
| 311 | $gateway = isset($_GET['gateway']) ? sanitize_text_field($_GET['gateway']) : 'all'; |
| 312 | $mode = isset($_GET['mode']) ? sanitize_text_field($_GET['mode']) : 'all'; |
| 313 | $customer = isset($_GET['by_ci']) ? absint($_GET['by_ci']) : 'all'; |
| 314 | |
| 315 | $status = ppressGET_var('status'); |
| 316 | $clear_url = PPRESS_MEMBERSHIP_ORDERS_SETTINGS_PAGE; |
| 317 | |
| 318 | $payment_methods = apply_filters('ppress_orders_table_payment_methods', PaymentMethods::get_instance()->get_all()); |
| 319 | $order_modes = OrderMode::get_all(); |
| 320 | |
| 321 | echo '<div class="wp-filter" id="ppress-filters">'; |
| 322 | echo '<div class="filter-items">'; |
| 323 | |
| 324 | if ( ! empty($order_modes)) : ?> |
| 325 | |
| 326 | <span id="ppress-mode-filter"> |
| 327 | <select name="mode"> |
| 328 | <option value="all"><?= esc_html__('All Modes', 'wp-user-avatar') ?></option> |
| 329 | <?php foreach ($order_modes as $id => $label) : ?> |
| 330 | <option value="<?= $id ?>" <?php selected($id, $mode) ?>><?= $label ?></option> |
| 331 | <?php endforeach; ?> |
| 332 | </select> |
| 333 | </span> |
| 334 | |
| 335 | <?php endif; ?> |
| 336 | |
| 337 | <span id="ppress-date-filters" class="ppress-from-to-wrapper"> |
| 338 | <span class="ppress-start-date-wrap"> |
| 339 | <input type="text" name="start_date" id="start-date" placeholder="<?= _x('From', 'date filter', 'wp-user-avatar') ?>" value="<?= $start_date ?>" class="ppress_datepicker"> |
| 340 | </span> |
| 341 | <span id="ppress-end-date-wrap"> |
| 342 | <input type="text" name="end_date" id="end-date" value="<?= $end_date ?>" placeholder="<?= _x('To', 'date filter', 'wp-user-avatar') ?>" class="ppress_datepicker"> |
| 343 | </span> |
| 344 | </span> |
| 345 | |
| 346 | <?php if ( ! empty($payment_methods)) : ?> |
| 347 | <span id="ppress-gateway-filter"> |
| 348 | <select name="gateway"> |
| 349 | <option value="all"><?= esc_html__('All Payment Methods', 'wp-user-avatar') ?></option> |
| 350 | <?php foreach ($payment_methods as $id => $method) : ?> |
| 351 | <option value="<?= $id ?>" <?php selected($id, $gateway) ?>><?= $method->method_title ?></option> |
| 352 | <?php endforeach; ?> |
| 353 | </select> |
| 354 | </span> |
| 355 | |
| 356 | <?php endif; ?> |
| 357 | |
| 358 | <span id="ppress-customer-filter"> |
| 359 | <select name="by_ci" class="ppress-select2-field customer_user" style="min-width:180px"> |
| 360 | <option value="all"><?= esc_html__('All Customers', 'wp-user-avatar') ?></option> |
| 361 | <?php if ( ! empty($customer) && 'all' != $customer) : ?> |
| 362 | <option value="<?= $customer ?>" selected><?= CustomerFactory::fromId($customer)->get_name() ?></option> |
| 363 | <?php endif; ?> |
| 364 | </select> |
| 365 | </span> |
| 366 | |
| 367 | <span id="ppress-after-core-filters"> |
| 368 | <input type="submit" class="button button-secondary" value="<?php esc_html_e('Filter', 'wp-user-avatar'); ?>"/> |
| 369 | |
| 370 | <?php if ( ! empty($_GET['s']) || ! empty($start_date) || ! empty($end_date) || ('all' !== $gateway) || $mode !== 'all' || $customer !== 'all') : ?> |
| 371 | <a href="<?php echo esc_url($clear_url); ?>" class="button-secondary"> |
| 372 | <?php esc_html_e('Clear', 'wp-user-avatar'); ?> |
| 373 | </a> |
| 374 | <?php endif; ?> |
| 375 | </span> |
| 376 | |
| 377 | <?php if ( ! empty($status)) : ?> |
| 378 | <input type="hidden" name="status" value="<?php echo esc_attr($status); ?>"/> |
| 379 | <?php endif; |
| 380 | echo '</div>'; |
| 381 | $this->search_box(__('Search Orders', 'wp-user-avatar'), 'ppress_order_search'); |
| 382 | echo '</div>'; |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Returns markup for an Order status badge. |
| 387 | * |
| 388 | * @param string $order_status Order status ID. |
| 389 | * |
| 390 | * @return string |
| 391 | * |
| 392 | */ |
| 393 | public static function get_order_status_badge($order_status) |
| 394 | { |
| 395 | switch ($order_status) { |
| 396 | case OrderStatus::REFUNDED : |
| 397 | $icon = '<span class="ppress-admin-status-badge__icon dashicons-before dashicons-undo"></span>'; |
| 398 | break; |
| 399 | case OrderStatus::FAILED : |
| 400 | $icon = '<span class="ppress-admin-status-badge__icon dashicons-before dashicons-no-alt"></span>'; |
| 401 | break; |
| 402 | case OrderStatus::COMPLETED : |
| 403 | $icon = '<span class="ppress-admin-status-badge__icon dashicons-before dashicons-yes"></span>'; |
| 404 | break; |
| 405 | default: |
| 406 | $icon = ''; |
| 407 | } |
| 408 | |
| 409 | $icon = apply_filters('ppress_get_order_status_badge_icon', $icon, $order_status); |
| 410 | |
| 411 | ob_start(); |
| 412 | ?> |
| 413 | |
| 414 | <span class="ppress-admin-status-badge ppress-admin-status-badge--<?php echo esc_attr($order_status); ?>"> |
| 415 | |
| 416 | <span class="ppress-admin-status-badge__text"> |
| 417 | <?php echo OrderStatus::get_label($order_status); ?> |
| 418 | </span> |
| 419 | <span class="ppress-admin-status-badge__icon"> |
| 420 | <?php |
| 421 | echo wp_kses( |
| 422 | $icon, |
| 423 | array( |
| 424 | 'span' => array( |
| 425 | 'class' => true, |
| 426 | ), |
| 427 | 'svg' => array( |
| 428 | 'class' => true, |
| 429 | 'xmlns' => true, |
| 430 | 'width' => true, |
| 431 | 'height' => true, |
| 432 | 'viewbox' => true, |
| 433 | 'aria-hidden' => true, |
| 434 | 'role' => true, |
| 435 | 'focusable' => true, |
| 436 | ), |
| 437 | 'path' => array( |
| 438 | 'fill' => true, |
| 439 | 'fill-rule' => true, |
| 440 | 'd' => true, |
| 441 | 'transform' => true, |
| 442 | ), |
| 443 | 'polygon' => array( |
| 444 | 'fill' => true, |
| 445 | 'fill-rule' => true, |
| 446 | 'points' => true, |
| 447 | 'transform' => true, |
| 448 | 'focusable' => true, |
| 449 | ), |
| 450 | ) |
| 451 | ); |
| 452 | ?> |
| 453 | </span> |
| 454 | </span> |
| 455 | <?php return ob_get_clean(); |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * @return array List of CSS classes for the table tag. |
| 460 | */ |
| 461 | public function get_table_classes() |
| 462 | { |
| 463 | return array('widefat', 'fixed', 'striped', 'order', 'ppview'); |
| 464 | } |
| 465 | } |
| 466 |