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