/
ListTable.php
1440 lines (1237 loc) · 44.4 KB
/
ListTable.php
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Automattic\WooCommerce\Internal\Admin\Orders;
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;
use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
use WC_Order;
use WP_List_Table;
use WP_Screen;
/**
* Admin list table for orders as managed by the OrdersTableDataStore.
*/
class ListTable extends WP_List_Table {
/**
* Order type.
*
* @var string
*/
private $order_type;
/**
* Request vars.
*
* @var array
*/
private $request = array();
/**
* Contains the arguments to be used in the order query.
*
* @var array
*/
private $order_query_args = array();
/**
* Tracks if a filter (ie, date or customer filter) has been applied.
*
* @var bool
*/
private $has_filter = false;
/**
* Page controller instance for this request.
*
* @var PageController
*/
private $page_controller;
/**
* Tracks whether we're currently inside the trash.
*
* @var boolean
*/
private $is_trash = false;
/**
* Caches order counts by status.
*
* @var array
*/
private $status_count_cache = null;
/**
* Sets up the admin list table for orders (specifically, for orders managed by the OrdersTableDataStore).
*
* @see WC_Admin_List_Table_Orders for the corresponding class used in relation to the traditional WP Post store.
*/
public function __construct() {
parent::__construct(
array(
'singular' => 'order',
'plural' => 'orders',
'ajax' => false,
)
);
}
/**
* Init method, invoked by DI container.
*
* @internal This method is not intended to be used directly (except for testing).
* @param PageController $page_controller Page controller instance for this request.
*/
final public function init( PageController $page_controller ) {
$this->page_controller = $page_controller;
}
/**
* Performs setup work required before rendering the table.
*
* @param array $args Args to initialize this list table.
*
* @return void
*/
public function setup( $args = array() ): void {
$this->order_type = $args['order_type'] ?? 'shop_order';
add_action( 'admin_notices', array( $this, 'bulk_action_notices' ) );
add_filter( "manage_{$this->screen->id}_columns", array( $this, 'get_columns' ), 0 );
add_filter( 'set_screen_option_edit_' . $this->order_type . '_per_page', array( $this, 'set_items_per_page' ), 10, 3 );
add_filter( 'default_hidden_columns', array( $this, 'default_hidden_columns' ), 10, 2 );
add_action( 'admin_footer', array( $this, 'enqueue_scripts' ) );
$this->items_per_page();
set_screen_options();
add_action( 'manage_' . wc_get_page_screen_id( $this->order_type ) . '_custom_column', array( $this, 'render_column' ), 10, 2 );
}
/**
* Render individual column.
*
* @param string $column_id Column ID to render.
* @param WC_Order $order Order object.
*/
public function render_column( $column_id, $order ) {
if ( ! $order ) {
return;
}
if ( is_callable( array( $this, 'render_' . $column_id . '_column' ) ) ) {
call_user_func( array( $this, 'render_' . $column_id . '_column' ), $order );
}
}
/**
* Handles output for the default column.
*
* @param \WC_Order $order Current WooCommerce order object.
* @param string $column_name Identifier for the custom column.
*/
public function column_default( $order, $column_name ) {
/**
* Fires for each custom column for a specific order type. This hook takes precedence over the generic
* action `manage_{$this->screen->id}_custom_column`.
*
* @param string $column_name Identifier for the custom column.
* @param \WC_Order $order Current WooCommerce order object.
*
* @since 7.3.0
*/
do_action( 'woocommerce_' . $this->order_type . '_list_table_custom_column', $column_name, $order );
/**
* Fires for each custom column in the Custom Order Table in the administrative screen.
*
* @param string $column_name Identifier for the custom column.
* @param \WC_Order $order Current WooCommerce order object.
*
* @since 7.0.0
*/
do_action( "manage_{$this->screen->id}_custom_column", $column_name, $order );
}
/**
* Sets up an items-per-page control.
*/
private function items_per_page(): void {
add_screen_option(
'per_page',
array(
'default' => 20,
'option' => 'edit_' . $this->order_type . '_per_page',
)
);
}
/**
* Saves the items-per-page setting.
*
* @param mixed $default The default value.
* @param string $option The option being configured.
* @param int $value The submitted option value.
*
* @return mixed
*/
public function set_items_per_page( $default, string $option, int $value ) {
return 'edit_' . $this->order_type . '_per_page' === $option ? absint( $value ) : $default;
}
/**
* Render the table.
*
* @return void
*/
public function display() {
$post_type = get_post_type_object( $this->order_type );
$title = esc_html( $post_type->labels->name );
$add_new = esc_html( $post_type->labels->add_new );
$new_page_link = $this->page_controller->get_new_page_url( $this->order_type );
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo wp_kses_post(
"
<div class='wrap'>
<h1 class='wp-heading-inline'>{$title}</h1>
<a href='" . esc_url( $new_page_link ) . "' class='page-title-action'>{$add_new}</a>
<hr class='wp-header-end'>"
);
if ( $this->should_render_blank_state() ) {
$this->render_blank_state();
return;
}
$this->views();
echo '<form id="wc-orders-filter" method="get" action="' . esc_url( get_admin_url( null, 'admin.php' ) ) . '">';
$this->print_hidden_form_fields();
$this->search_box( esc_html__( 'Search orders', 'woocommerce' ), 'orders-search-input' );
parent::display();
echo '</form> </div>';
}
/**
* Renders advice in the event that no orders exist yet.
*
* @return void
*/
public function render_blank_state(): void {
?>
<div class="woocommerce-BlankState">
<h2 class="woocommerce-BlankState-message">
<?php esc_html_e( 'When you receive a new order, it will appear here.', 'woocommerce' ); ?>
</h2>
<div class="woocommerce-BlankState-buttons">
<a class="woocommerce-BlankState-cta button-primary button" target="_blank" href="https://docs.woocommerce.com/document/managing-orders/?utm_source=blankslate&utm_medium=product&utm_content=ordersdoc&utm_campaign=woocommerceplugin"><?php esc_html_e( 'Learn more about orders', 'woocommerce' ); ?></a>
</div>
<?php
/**
* Renders after the 'blank state' message for the order list table has rendered.
*
* @since 6.6.1
*/
do_action( 'wc_marketplace_suggestions_orders_empty_state' ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingSinceComment
?>
</div>
<?php
}
/**
* Retrieves the list of bulk actions available for this table.
*
* @return array
*/
protected function get_bulk_actions() {
$selected_status = $this->order_query_args['status'] ?? false;
if ( array( 'trash' ) === $selected_status ) {
$actions = array(
'untrash' => __( 'Restore', 'woocommerce' ),
'delete' => __( 'Delete permanently', 'woocommerce' ),
);
} else {
$actions = array(
'mark_processing' => __( 'Change status to processing', 'woocommerce' ),
'mark_on-hold' => __( 'Change status to on-hold', 'woocommerce' ),
'mark_completed' => __( 'Change status to completed', 'woocommerce' ),
'mark_cancelled' => __( 'Change status to cancelled', 'woocommerce' ),
'trash' => __( 'Move to Trash', 'woocommerce' ),
);
}
if ( wc_string_to_bool( get_option( 'woocommerce_allow_bulk_remove_personal_data', 'no' ) ) ) {
$actions['remove_personal_data'] = __( 'Remove personal data', 'woocommerce' );
}
return $actions;
}
/**
* Prepares the list of items for displaying.
*/
public function prepare_items() {
$limit = $this->get_items_per_page( 'edit_' . $this->order_type . '_per_page' );
$this->order_query_args = array(
'limit' => $limit,
'page' => $this->get_pagenum(),
'paginate' => true,
'type' => $this->order_type,
);
foreach ( array( 'status', 's', 'm', '_customer_user' ) as $query_var ) {
$this->request[ $query_var ] = sanitize_text_field( wp_unslash( $_REQUEST[ $query_var ] ?? '' ) );
}
/**
* Allows 3rd parties to filter the initial request vars before defaults and other logic is applied.
*
* @param array $request Request to be passed to `wc_get_orders()`.
*
* @since 7.3.0
*/
$this->request = apply_filters( 'woocommerce_' . $this->order_type . '_list_table_request', $this->request );
$this->set_status_args();
$this->set_order_args();
$this->set_date_args();
$this->set_customer_args();
$this->set_search_args();
/**
* Provides an opportunity to modify the query arguments used in the (Custom Order Table-powered) order list
* table.
*
* @since 6.9.0
*
* @param array $query_args Arguments to be passed to `wc_get_orders()`.
*/
$order_query_args = (array) apply_filters( 'woocommerce_order_list_table_prepare_items_query_args', $this->order_query_args );
/**
* Same as `woocommerce_order_list_table_prepare_items_query_args` but for a specific order type.
*
* @param array $query_args Arguments to be passed to `wc_get_orders()`.
*
* @since 7.3.0
*/
$order_query_args = apply_filters( 'woocommerce_' . $this->order_type . '_list_table_prepare_items_query_args', $this->order_query_args );
// We must ensure the 'paginate' argument is set.
$order_query_args['paginate'] = true;
$orders = wc_get_orders( $order_query_args );
$this->items = $orders->orders;
$max_num_pages = $orders->max_num_pages;
// Check in case the user has attempted to page beyond the available range of orders.
if ( 0 === $max_num_pages && $this->order_query_args['page'] > 1 ) {
$count_query_args = $order_query_args;
$count_query_args['page'] = 1;
$count_query_args['limit'] = 1;
$order_count = wc_get_orders( $count_query_args );
$max_num_pages = (int) ceil( $order_count->total / $order_query_args['limit'] );
}
$this->set_pagination_args(
array(
'total_items' => $orders->total ?? 0,
'per_page' => $limit,
'total_pages' => $max_num_pages,
)
);
// Are we inside the trash?
$this->is_trash = 'trash' === $this->request['status'];
}
/**
* Updates the WC Order Query arguments as needed to support orderable columns.
*/
private function set_order_args() {
$sortable = $this->get_sortable_columns();
$field = sanitize_text_field( wp_unslash( $_GET['orderby'] ?? '' ) );
$direction = strtoupper( sanitize_text_field( wp_unslash( $_GET['order'] ?? '' ) ) );
if ( ! in_array( $field, $sortable, true ) ) {
return;
}
$this->order_query_args['orderby'] = $field;
$this->order_query_args['order'] = in_array( $direction, array( 'ASC', 'DESC' ), true ) ? $direction : 'ASC';
}
/**
* Implements date (month-based) filtering.
*/
private function set_date_args() {
$year_month = sanitize_text_field( wp_unslash( $_GET['m'] ?? '' ) );
if ( empty( $year_month ) || ! preg_match( '/^[0-9]{6}$/', $year_month ) ) {
return;
}
$year = (int) substr( $year_month, 0, 4 );
$month = (int) substr( $year_month, 4, 2 );
if ( $month < 0 || $month > 12 ) {
return;
}
$last_day_of_month = date_create( "$year-$month" )->format( 'Y-m-t' );
$this->order_query_args['date_created'] = "$year-$month-01..." . $last_day_of_month;
$this->has_filter = true;
}
/**
* Implements filtering of orders by customer.
*/
private function set_customer_args() {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$customer = (int) wp_unslash( $_GET['_customer_user'] ?? '' );
if ( $customer < 1 ) {
return;
}
$this->order_query_args['customer'] = $customer;
$this->has_filter = true;
}
/**
* Implements filtering of orders by status.
*/
private function set_status_args() {
$status = array_filter( array_map( 'trim', (array) $this->request['status'] ) );
if ( empty( $status ) || in_array( 'all', $status, true ) ) {
/**
* Allows 3rd parties to set the default list of statuses for a given order type.
*
* @param string[] $statuses Statuses.
*
* @since 7.3.0
*/
$status = apply_filters(
'woocommerce_' . $this->order_type . '_list_table_default_statuses',
array_intersect(
array_keys( wc_get_order_statuses() ),
get_post_stati( array( 'show_in_admin_all_list' => true ), 'names' )
)
);
} else {
$this->has_filter = true;
}
$this->order_query_args['status'] = $status;
}
/**
* Implements order search.
*/
private function set_search_args(): void {
$search_term = trim( sanitize_text_field( $this->request['s'] ) );
if ( ! empty( $search_term ) ) {
$this->order_query_args['s'] = $search_term;
$this->has_filter = true;
}
}
/**
* Get the list of views for this table (all orders, completed orders, etc, each with a count of the number of
* corresponding orders).
*
* @return array
*/
public function get_views() {
$view_counts = array();
$view_links = array();
$statuses = $this->get_visible_statuses();
$current = ! empty( $this->request['status'] ) ? sanitize_text_field( $this->request['status'] ) : 'all';
$all_count = 0;
foreach ( array_keys( $statuses ) as $slug ) {
$total_in_status = $this->count_orders_by_status( $slug );
if ( $total_in_status > 0 ) {
$view_counts[ $slug ] = $total_in_status;
}
if ( ( get_post_status_object( $slug ) )->show_in_admin_all_list ) {
$all_count += $total_in_status;
}
}
$view_links['all'] = $this->get_view_link( 'all', __( 'All', 'woocommerce' ), $all_count, '' === $current || 'all' === $current );
foreach ( $view_counts as $slug => $count ) {
$view_links[ $slug ] = $this->get_view_link( $slug, $statuses[ $slug ], $count, $slug === $current );
}
return $view_links;
}
/**
* Count orders by status.
*
* @param string|string[] $status The order status we are interested in.
*
* @return int
*/
private function count_orders_by_status( $status ): int {
global $wpdb;
// Compute all counts and cache if necessary.
if ( is_null( $this->status_count_cache ) ) {
$orders_table = OrdersTableDataStore::get_orders_table_name();
$res = $wpdb->get_results(
$wpdb->prepare(
"SELECT status, COUNT(*) AS cnt FROM {$orders_table} WHERE type = %s GROUP BY status", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$this->order_type
),
ARRAY_A
);
$this->status_count_cache =
$res
? array_combine( array_column( $res, 'status' ), array_map( 'absint', array_column( $res, 'cnt' ) ) )
: array();
}
$status = (array) $status;
$count = array_sum( array_intersect_key( $this->status_count_cache, array_flip( $status ) ) );
/**
* Allows 3rd parties to modify the count of orders by status.
*
* @param int $count Number of orders for the given status.
* @param string[] $status List of order statuses in the count.
* @since 7.3.0
*/
return apply_filters(
'woocommerce_' . $this->order_type . '_list_table_order_count',
$count,
$status
);
}
/**
* Checks whether the blank state should be rendered or not. This depends on whether there are others with a visible
* status.
*
* @return boolean TRUE when the blank state should be rendered, FALSE otherwise.
*/
private function should_render_blank_state(): bool {
return ( ! $this->has_filter ) && 0 === $this->count_orders_by_status( array_keys( $this->get_visible_statuses() ) );
}
/**
* Returns a list of slug and labels for order statuses that should be visible in the status list.
*
* @return array slug => label array of order statuses.
*/
private function get_visible_statuses(): array {
return array_intersect_key(
array_merge(
wc_get_order_statuses(),
array(
'trash' => ( get_post_status_object( 'trash' ) )->label,
'draft' => ( get_post_status_object( 'draft' ) )->label,
)
),
array_flip( get_post_stati( array( 'show_in_admin_status_list' => true ) ) )
);
}
/**
* Form a link to use in the list of table views.
*
* @param string $slug Slug used to identify the view (usually the order status slug).
* @param string $name Human-readable name of the view (usually the order status label).
* @param int $count Number of items in this view.
* @param bool $current If this is the current view.
*
* @return string
*/
private function get_view_link( string $slug, string $name, int $count, bool $current ): string {
$base_url = get_admin_url( null, 'admin.php?page=wc-orders' . ( 'shop_order' === $this->order_type ? '' : '--' . $this->order_type ) );
$url = esc_url( add_query_arg( 'status', $slug, $base_url ) );
$name = esc_html( $name );
$count = absint( $count );
$class = $current ? 'class="current"' : '';
return "<a href='$url' $class>$name <span class='count'>($count)</span></a>";
}
/**
* Extra controls to be displayed between bulk actions and pagination.
*
* @param string $which Either 'top' or 'bottom'.
*/
protected function extra_tablenav( $which ) {
echo '<div class="alignleft actions">';
if ( 'top' === $which ) {
ob_start();
$this->months_filter();
$this->customers_filter();
/**
* Fires before the "Filter" button on the list table for orders and other order types.
*
* @since 7.3.0
*
* @param string $order_type The order type.
* @param string $which The location of the extra table nav: 'top' or 'bottom'.
*/
do_action( 'woocommerce_order_list_table_restrict_manage_orders', $this->order_type, $which );
$output = ob_get_clean();
if ( ! empty( $output ) ) {
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
submit_button( __( 'Filter', 'woocommerce' ), '', 'filter_action', false, array( 'id' => 'order-query-submit' ) );
}
}
if ( $this->is_trash && $this->has_items() && current_user_can( 'edit_others_shop_orders' ) ) {
submit_button( __( 'Empty Trash', 'woocommerce' ), 'apply', 'delete_all', false );
}
/**
* Fires immediately following the closing "actions" div in the tablenav for the order
* list table.
*
* @since 7.3.0
*
* @param string $order_type The order type.
* @param string $which The location of the extra table nav: 'top' or 'bottom'.
*/
do_action( 'woocommerce_order_list_table_extra_tablenav', $this->order_type, $which );
echo '</div>';
}
/**
* Render the months filter dropdown.
*
* @return void
*/
private function months_filter() {
// XXX: [review] we may prefer to move this logic outside of the ListTable class.
global $wp_locale;
global $wpdb;
$orders_table = esc_sql( OrdersTableDataStore::get_orders_table_name() );
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$order_dates = $wpdb->get_results(
"
SELECT DISTINCT YEAR( date_created_gmt ) AS year,
MONTH( date_created_gmt ) AS month
FROM $orders_table
WHERE status NOT IN (
'trash'
)
ORDER BY year DESC, month DESC;
"
);
$m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
echo '<select name="m" id="filter-by-date">';
echo '<option ' . selected( $m, 0, false ) . ' value="0">' . esc_html__( 'All dates', 'woocommerce' ) . '</option>';
foreach ( $order_dates as $date ) {
$month = zeroise( $date->month, 2 );
$month_year_text = sprintf(
/* translators: 1: Month name, 2: 4-digit year. */
esc_html_x( '%1$s %2$d', 'order dates dropdown', 'woocommerce' ),
$wp_locale->get_month( $month ),
$date->year
);
printf(
'<option %1$s value="%2$s">%3$s</option>\n',
selected( $m, $date->year . $month, false ),
esc_attr( $date->year . $month ),
esc_html( $month_year_text )
);
}
echo '</select>';
}
/**
* Render the customer filter dropdown.
*
* @return void
*/
public function customers_filter() {
$user_string = '';
$user_id = '';
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( ! empty( $_GET['_customer_user'] ) ) {
$user_id = absint( $_GET['_customer_user'] );
$user = get_user_by( 'id', $user_id );
$user_string = sprintf(
/* translators: 1: user display name 2: user ID 3: user email */
esc_html__( '%1$s (#%2$s – %3$s)', 'woocommerce' ),
$user->display_name,
absint( $user->ID ),
$user->user_email
);
}
// Note: use of htmlspecialchars (below) is to prevent XSS when rendered by selectWoo.
?>
<select class="wc-customer-search" name="_customer_user" data-placeholder="<?php esc_attr_e( 'Filter by registered customer', 'woocommerce' ); ?>" data-allow_clear="true">
<option value="<?php echo esc_attr( $user_id ); ?>" selected="selected"><?php echo htmlspecialchars( wp_kses_post( $user_string ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></option>
</select>
<?php
}
/**
* Get list columns.
*
* @return array
*/
public function get_columns() {
/**
* Filters the list of columns.
*
* @param array $columns List of sortable columns.
*
* @since 7.3.0
*/
return apply_filters(
'woocommerce_' . $this->order_type . '_list_table_columns',
array(
'cb' => '<input type="checkbox" />',
'order_number' => esc_html__( 'Order', 'woocommerce' ),
'order_date' => esc_html__( 'Date', 'woocommerce' ),
'order_status' => esc_html__( 'Status', 'woocommerce' ),
'billing_address' => esc_html__( 'Billing', 'woocommerce' ),
'shipping_address' => esc_html__( 'Ship to', 'woocommerce' ),
'order_total' => esc_html__( 'Total', 'woocommerce' ),
'wc_actions' => esc_html__( 'Actions', 'woocommerce' ),
)
);
}
/**
* Defines the default sortable columns.
*
* @return string[]
*/
public function get_sortable_columns() {
/**
* Filters the list of sortable columns.
*
* @param array $sortable_columns List of sortable columns.
*
* @since 7.3.0
*/
return apply_filters(
'woocommerce_' . $this->order_type . '_list_table_sortable_columns',
array(
'order_number' => 'ID',
'order_date' => 'date',
'order_total' => 'order_total',
)
);
}
/**
* Specify the columns we wish to hide by default.
*
* @param array $hidden Columns set to be hidden.
* @param WP_Screen $screen Screen object.
*
* @return array
*/
public function default_hidden_columns( array $hidden, WP_Screen $screen ) {
if ( isset( $screen->id ) && wc_get_page_screen_id( 'shop-order' ) === $screen->id ) {
$hidden = array_merge(
$hidden,
array(
'billing_address',
'shipping_address',
'wc_actions',
)
);
}
return $hidden;
}
/**
* Checklist column, used for selecting items for processing by a bulk action.
*
* @param WC_Order $item The order object for the current row.
*
* @return string
*/
public function column_cb( $item ) {
return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', esc_attr( $this->_args['singular'] ), esc_attr( $item->get_id() ) );
}
/**
* Renders the order number, customer name and provides a preview link.
*
* @param WC_Order $order The order object for the current row.
*
* @return void
*/
public function render_order_number_column( WC_Order $order ): void {
$buyer = '';
if ( $order->get_billing_first_name() || $order->get_billing_last_name() ) {
/* translators: 1: first name 2: last name */
$buyer = trim( sprintf( _x( '%1$s %2$s', 'full name', 'woocommerce' ), $order->get_billing_first_name(), $order->get_billing_last_name() ) );
} elseif ( $order->get_billing_company() ) {
$buyer = trim( $order->get_billing_company() );
} elseif ( $order->get_customer_id() ) {
$user = get_user_by( 'id', $order->get_customer_id() );
$buyer = ucwords( $user->display_name );
}
/**
* Filter buyer name in list table orders.
*
* @since 3.7.0
*
* @param string $buyer Buyer name.
* @param WC_Order $order Order data.
*/
$buyer = apply_filters( 'woocommerce_admin_order_buyer_name', $buyer, $order );
if ( $order->get_status() === 'trash' ) {
echo '<strong>#' . esc_attr( $order->get_order_number() ) . ' ' . esc_html( $buyer ) . '</strong>';
} else {
echo '<a href="#" class="order-preview" data-order-id="' . absint( $order->get_id() ) . '" title="' . esc_attr( __( 'Preview', 'woocommerce' ) ) . '">' . esc_html( __( 'Preview', 'woocommerce' ) ) . '</a>';
echo '<a href="' . esc_url( $this->get_order_edit_link( $order ) ) . '" class="order-view"><strong>#' . esc_attr( $order->get_order_number() ) . ' ' . esc_html( $buyer ) . '</strong></a>';
}
}
/**
* Get the edit link for an order.
*
* @param WC_Order $order Order object.
*
* @return string Edit link for the order.
*/
private function get_order_edit_link( WC_Order $order ) : string {
return $this->page_controller->get_edit_url( $order->get_id() );
}
/**
* Renders the order date.
*
* @param WC_Order $order The order object for the current row.
*
* @return void
*/
public function render_order_date_column( WC_Order $order ): void {
$order_timestamp = $order->get_date_created() ? $order->get_date_created()->getTimestamp() : '';
if ( ! $order_timestamp ) {
echo '–';
return;
}
// Check if the order was created within the last 24 hours, and not in the future.
if ( $order_timestamp > strtotime( '-1 day', time() ) && $order_timestamp <= time() ) {
$show_date = sprintf(
/* translators: %s: human-readable time difference */
_x( '%s ago', '%s = human-readable time difference', 'woocommerce' ),
human_time_diff( $order->get_date_created()->getTimestamp(), time() )
);
} else {
$show_date = $order->get_date_created()->date_i18n( apply_filters( 'woocommerce_admin_order_date_format', __( 'M j, Y', 'woocommerce' ) ) ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
}
printf(
'<time datetime="%1$s" title="%2$s">%3$s</time>',
esc_attr( $order->get_date_created()->date( 'c' ) ),
esc_html( $order->get_date_created()->date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ) ),
esc_html( $show_date )
);
}
/**
* Renders the order status.
*
* @param WC_Order $order The order object for the current row.
*
* @return void
*/
public function render_order_status_column( WC_Order $order ): void {
$tooltip = '';
remove_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
$comment_count = get_comment_count( $order->get_id() );
add_filter( 'comments_clauses', array( 'WC_Comments', 'exclude_order_comments' ), 10, 1 );
$approved_comments_count = absint( $comment_count['approved'] );
if ( $approved_comments_count ) {
$latest_notes = wc_get_order_notes(
array(
'order_id' => $order->get_id(),
'limit' => 1,
'orderby' => 'date_created_gmt',
)
);
$latest_note = current( $latest_notes );
if ( isset( $latest_note->content ) && 1 === $approved_comments_count ) {
$tooltip = wc_sanitize_tooltip( $latest_note->content );
} elseif ( isset( $latest_note->content ) ) {
/* translators: %d: notes count */
$tooltip = wc_sanitize_tooltip( $latest_note->content . '<br/><small style="display:block">' . sprintf( _n( 'Plus %d other note', 'Plus %d other notes', ( $approved_comments_count - 1 ), 'woocommerce' ), $approved_comments_count - 1 ) . '</small>' );
} else {
/* translators: %d: notes count */
$tooltip = wc_sanitize_tooltip( sprintf( _n( '%d note', '%d notes', $approved_comments_count, 'woocommerce' ), $approved_comments_count ) );
}
}
// Gracefully handle legacy statuses.
if ( in_array( $order->get_status(), array( 'trash', 'draft' ), true ) ) {
$status_name = ( get_post_status_object( $order->get_status() ) )->label;
} else {
$status_name = wc_get_order_status_name( $order->get_status() );
}
if ( $tooltip ) {
printf( '<mark class="order-status %s tips" data-tip="%s"><span>%s</span></mark>', esc_attr( sanitize_html_class( 'status-' . $order->get_status() ) ), wp_kses_post( $tooltip ), esc_html( $status_name ) );
} else {
printf( '<mark class="order-status %s"><span>%s</span></mark>', esc_attr( sanitize_html_class( 'status-' . $order->get_status() ) ), esc_html( $status_name ) );
}
}
/**
* Renders order billing information.
*
* @param WC_Order $order The order object for the current row.
*
* @return void
*/
public function render_billing_address_column( WC_Order $order ): void {
$address = $order->get_formatted_billing_address();
if ( $address ) {
echo esc_html( preg_replace( '#<br\s*/?>#i', ', ', $address ) );
if ( $order->get_payment_method() ) {
/* translators: %s: payment method */
echo '<span class="description">' . sprintf( esc_html__( 'via %s', 'woocommerce' ), esc_html( $order->get_payment_method_title() ) ) . '</span>';
}
} else {
echo '–';
}
}
/**
* Renders order shipping information.
*
* @param WC_Order $order The order object for the current row.
*
* @return void
*/
public function render_shipping_address_column( WC_Order $order ): void {
$address = $order->get_formatted_shipping_address();
if ( $address ) {
echo '<a target="_blank" href="' . esc_url( $order->get_shipping_address_map_url() ) . '">' . esc_html( preg_replace( '#<br\s*/?>#i', ', ', $address ) ) . '</a>';
if ( $order->get_shipping_method() ) {
/* translators: %s: shipping method */
echo '<span class="description">' . sprintf( esc_html__( 'via %s', 'woocommerce' ), esc_html( $order->get_shipping_method() ) ) . '</span>';
}
} else {
echo '–';
}
}
/**
* Renders the order total.
*
* @param WC_Order $order The order object for the current row.
*
* @return void
*/
public function render_order_total_column( WC_Order $order ): void {
if ( $order->get_payment_method_title() ) {
/* translators: %s: method */
echo '<span class="tips" data-tip="' . esc_attr( sprintf( __( 'via %s', 'woocommerce' ), $order->get_payment_method_title() ) ) . '">' . wp_kses_post( $order->get_formatted_order_total() ) . '</span>';
} else {
echo wp_kses_post( $order->get_formatted_order_total() );
}
}
/**
* Renders order actions.
*
* @param WC_Order $order The order object for the current row.
*
* @return void
*/
public function render_wc_actions_column( WC_Order $order ): void {
echo '<p>';
/**
* Fires before the order action buttons (within the actions column for the order list table)