-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathclass-pmpro-orders-list-table.php
More file actions
1299 lines (1142 loc) · 42.1 KB
/
class-pmpro-orders-list-table.php
File metadata and controls
1299 lines (1142 loc) · 42.1 KB
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
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class PMPro_Orders_List_Table extends WP_List_Table {
/**
* The text domain of this plugin.
*
* @since 2.11
*
* @access private
* @var string $plugin_text_domain The text domain of this plugin.
*/
protected $plugin_text_domain;
/**
* Call the parent constructor to override the defaults $args
*
* @param string $plugin_text_domain Text domain of the plugin.
*
* @since 2.11
*/
public function __construct() {
$this->plugin_text_domain = 'paid-memberships-pro';
parent::__construct(
array(
'plural' => 'orders',
// Plural value used for labels and the objects being listed.
'singular' => 'order',
// Singular label for an object being listed, e.g. 'post'.
'ajax' => false,
// If true, the parent class will call the _js_vars() method in the footer
)
);
}
/**
* Sets up screen options for the orders list table.
*
* @since 3.0
*/
public static function hook_screen_options() {
$list_table = new PMPro_Orders_List_Table();
add_screen_option(
'per_page',
array(
'default' => 20,
'label' => __( 'Orders per page', 'paid-memberships-pro' ),
'option' => 'pmpro_orders_per_page',
)
);
add_filter(
'screen_settings',
array(
$list_table,
'screen_controls',
),
10,
2
);
add_filter(
'set-screen-option',
array(
$list_table,
'set_screen_option',
),
10,
3
);
set_screen_options();
}
/**
* Sets the screen options.
*
* @param string $dummy Unused.
* @param string $option Screen option name.
* @param string $value Screen option value.
* @return string
*/
public function set_screen_option( $dummy, $option, $value ) {
if ( 'pmpro_orders_per_page' === $option ) {
return $value;
} else {
return $dummy;
}
}
/**
* Prepares the list of items for displaying.
*
* Query, filter data, handle sorting, and pagination, and any other data-manipulation required prior to rendering
*
* @since 2.11
*/
public function prepare_items() {
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable);
$items_per_page = $this->get_items_per_page( 'pmpro_orders_per_page' );
/**
* Filter to set the default number of items to show per page
* on the Orders page in the admin.
*
* @since 1.8.4.5
*
* @param int $limit The number of items to show per page.
*/
$items_per_page = apply_filters( 'pmpro_orders_per_page', $items_per_page );
$this->items = $this->sql_table_data( false );
$total_items = $this->sql_table_data( true );
$this->set_pagination_args(
array(
'total_items' => $total_items,
'per_page' => $items_per_page,
'total_pages' => ceil( $total_items / $items_per_page ),
)
);
}
/**
* Get a list of columns.
*
* The format is: 'internal-name' => 'Title'
*
* @since 2.11
*
* @return array
*/
public function get_columns() {
$columns = array(
'order_id' => __( 'ID', 'paid-memberships-pro' ),
//We cant use 'code' as is because it formats the column as code
'order_code' => __( 'Code', 'paid-memberships-pro' ),
'user' => __( 'User', 'paid-memberships-pro' ),
'level' => __( 'Level', 'paid-memberships-pro' ),
'total' => __( 'Total', 'paid-memberships-pro' ),
'billing' => __( 'Billing', 'paid-memberships-pro' ),
'gateway' => __( 'Gateway', 'paid-memberships-pro' ),
'transaction_ids' => __( 'Transaction IDs', 'paid-memberships-pro' ),
'order_status' => __( 'Status', 'paid-memberships-pro' ),
'date' => __( 'Date', 'paid-memberships-pro' ),
);
// Re-implementing old hook, will be deprecated.
ob_start();
do_action( 'pmpro_orders_extra_cols_header' );
$extra_cols = ob_get_clean();
preg_match_all( '/<th>(.*?)<\/th>/s', $extra_cols, $matches );
$custom_field_num = 0;
foreach ( $matches[1] as $match ) {
$columns[ 'custom_field_' . $custom_field_num ] = $match;
$custom_field_num++;
}
// Shortcut for editing columns in default discount code list location.
$current_screen = get_current_screen();
if ( ! empty( $current_screen ) && strpos( $current_screen->id, "pmpro-orders" ) !== false ) {
$columns = apply_filters( 'pmpro_manage_orderslist_columns', $columns );
}
return $columns;
}
/**
* Define which columns are hidden
*
* @return Array
*/
public function get_hidden_columns() {
$user = wp_get_current_user();
if ( ! $user ) {
return array();
}
// Check whether the current user has changed screen options or not.
$hidden = get_user_meta( $user->ID, 'manage' . $this->screen->id . 'columnshidden', true );
// If user meta is not found, add the default hidden columns.
// Right now, we don't have any default hidden columns.
if ( ! $hidden ) {
$hidden = array();
update_user_meta( $user->ID, 'manage' . $this->screen->id . 'columnshidden', $hidden );
}
return $hidden;
}
/**
* Get a list of sortable columns. The format is:
* 'internal-name' => 'orderby'
* or
* 'internal-name' => array( 'orderby', true )
*
* The second format will make the initial sorting order be descending
*
* @since 2.11
*
* @return array
*/
protected function get_sortable_columns() {
/**
* actual sorting still needs to be done by prepare_items.
* specify which columns should have the sort icon.
*
* key => value
* column name_in_list_table => columnname in the db
*/
$sortable_columns = array(
'order_id' => array( 'id', true ),
'level' => array( 'name', false ),
'total' => array( 'total', false ),
'order_status' => array( 'status_label', false ),
'date' => array( 'timestamp', false ),
);
return $sortable_columns;
}
/**
* Text displayed when no user data is available
*
* @since 2.11
*
* @return void
*/
public function no_items() {
esc_html_e( 'No orders found.', 'paid-memberships-pro' );
}
/**
* Get the table data
*
* @return Array|integer if $count parameter = true
*/
private function sql_table_data( $count = false, $limit = 15 ) {
global $wpdb;
$now = current_time( 'timestamp' );
$s = isset( $_REQUEST['s'] ) ? trim( sanitize_text_field( $_REQUEST['s'] ) ) : '';
$l = isset( $_REQUEST['l'] ) ? intval( $_REQUEST['l'] ) : false;
$discount_code = isset( $_REQUEST['discount-code'] ) ? intval( $_REQUEST['discount-code'] ) : false;
$start_month = isset( $_REQUEST['start-month'] ) ? intval( $_REQUEST['start-month'] ) : '1';
$start_day = isset( $_REQUEST['start-day'] ) ? intval( $_REQUEST['start-day'] ) : '1';
$start_year = isset( $_REQUEST['start-year'] ) ? intval( $_REQUEST['start-year'] ) : date( 'Y', $now );
$end_month = isset( $_REQUEST['end-month'] ) ? intval( $_REQUEST['end-month'] ) : date( 'n', $now );
$end_day = isset( $_REQUEST['end-day'] ) ? intval( $_REQUEST['end-day'] ) : date( 'j', $now );
$end_year = isset( $_REQUEST['end-year'] ) ? intval( $_REQUEST['end-year'] ) : date( 'Y', $now );
$predefined_date = isset( $_REQUEST['predefined-date'] ) ? sanitize_text_field( $_REQUEST['predefined-date'] ) : 'This Month';
$status = isset( $_REQUEST['status'] ) ? sanitize_text_field( $_REQUEST['status'] ) : '';
$filter = isset( $_REQUEST['filter'] ) ? sanitize_text_field( $_REQUEST['filter'] ) : 'all';
$pn = isset( $_REQUEST['paged'] ) ? intval( $_REQUEST['paged'] ) : 1;
$items_per_page = $this->get_items_per_page( 'pmpro_orders_per_page' );
/**
* Filter to set the default number of items to show per page
* on the Orders page in the admin.
*
* @since 1.8.4.5
*
* @param int $limit The number of items to show per page.
*/
$limit = apply_filters( 'pmpro_orders_per_page', $items_per_page );
$end = $pn * $limit;
$start = $end - $limit;
// filters
if ( empty( $filter ) || $filter === 'all' ) {
$condition = '1=1';
$filter = 'all';
} elseif ( $filter == 'within-a-date-range' ) {
$start_date = $start_year . '-' . $start_month . '-' . $start_day;
$end_date = $end_year . '-' . $end_month . '-' . $end_day;
// add times to dates and localize
$start_date = get_gmt_from_date( $start_date . ' 00:00:00' );
$end_date = get_gmt_from_date( $end_date . ' 23:59:59' );
$condition = "o.timestamp BETWEEN '" . esc_sql( $start_date ) . "' AND '" . esc_sql( $end_date ) . "'";
} elseif ( $filter == 'predefined-date-range' ) {
if ( $predefined_date == 'Last Month' ) {
$start_date = date( 'Y-m-d', strtotime( 'first day of last month', $now ) );
$end_date = date( 'Y-m-d', strtotime( 'last day of last month', $now ) );
} elseif ( $predefined_date == 'This Month' ) {
$start_date = date( 'Y-m-d', strtotime( 'first day of this month', $now ) );
$end_date = date( 'Y-m-d', strtotime( 'last day of this month', $now ) );
} elseif ( $predefined_date == 'This Year' ) {
$year = date( 'Y', $now );
$start_date = date( 'Y-m-d', strtotime( "first day of January $year", $now ) );
$end_date = date( 'Y-m-d', strtotime( "last day of December $year", $now ) );
} elseif ( $predefined_date == 'Last Year' ) {
$year = date( 'Y', $now ) - 1;
$start_date = date( 'Y-m-d', strtotime( "first day of January $year", $now ) );
$end_date = date( 'Y-m-d', strtotime( "last day of December $year", $now ) );
}
// add times to dates and localize
$start_date = get_gmt_from_date( $start_date . ' 00:00:00' );
$end_date = get_gmt_from_date( $end_date . ' 23:59:59' );
$condition = "o.timestamp BETWEEN '" . esc_sql( $start_date ) . "' AND '" . esc_sql( $end_date ) . "'";
} elseif ( $filter == 'within-a-level' ) {
$condition = 'o.membership_id = ' . esc_sql( $l );
} elseif ( $filter == 'with-discount-code' ) {
$condition = 'dc.code_id = ' . esc_sql( $discount_code );
} elseif ( $filter == 'within-a-status' ) {
$condition = "o.status = '" . esc_sql( $status ) . "' ";
} elseif ( $filter == 'only-paid' ) {
$condition = "o.total > 0";
} elseif( $filter == 'only-free' ) {
$condition = "o.total = 0";
} else {
$condition = "";
}
$condition = apply_filters( 'pmpro_admin_orders_query_condition', $condition, $filter );
$orderby = '';
if( ! empty( $_REQUEST['order'] ) && ! empty( $_REQUEST['orderby'] ) && ! $count ) {
if( isset( $_REQUEST['orderby'] ) ) {
$orderby = $this->sanitize_orderby( sanitize_text_field( $_REQUEST['orderby'] ) );
} else {
$orderby = 'id';
}
if( isset( $_REQUEST['order'] ) && $_REQUEST['order'] == 'asc' ) {
$order = 'ASC';
} else {
$order = 'DESC';
}
if( $orderby == 'total' ) {
$orderby = 'total + 0'; //This pads the number and allows it to sort correctly
}
$order_query = "ORDER BY $orderby $order";
} else {
$order_query = 'ORDER BY id DESC';
}
$paid_string = __( 'Paid', 'paid-memberships-pro' );
$cancelled_string = __( 'Cancelled', 'paid-memberships-pro' );
$refunded_string = __( 'Refunded', 'paid-memberships-pro' );
$token_string = __( 'Token', 'paid-memberships-pro' );
$review_string = __( 'Review', 'paid-memberships-pro' );
$pending_string = __( 'Pending', 'paid-memberships-pro' );
$error_string = __( 'Error', 'paid-memberships-pro' );
if( $count ) {
$calculation_function = 'COUNT(*), ';
} else {
$calculation_function = 'SQL_CALC_FOUND_ROWS';
}
$sqlQuery = "SELECT $calculation_function o.id, CASE WHEN o.status = 'success' THEN 'Paid' WHEN o.status = 'cancelled' THEN '$paid_string' WHEN o.status = 'refunded' THEN '$refunded_string' WHEN o.status = 'token' THEN '$token_string' WHEN o.status = 'review' THEN '$review_string' WHEN o.status = 'pending' THEN '$pending_string' WHEN o.status = 'error' THEN '$error_string' ELSE '$cancelled_string' END as `status_label` FROM $wpdb->pmpro_membership_orders o LEFT JOIN $wpdb->pmpro_membership_levels ml ON o.membership_id = ml.id LEFT JOIN $wpdb->users u ON o.user_id = u.ID ";
if ( $s ) {
$join_with_usermeta = apply_filters( 'pmpro_orders_search_usermeta', false );
if ( $join_with_usermeta ) {
$sqlQuery .= "LEFT JOIN $wpdb->usermeta um ON o.user_id = um.user_id ";
}
if ( $filter === 'with-discount-code' ) {
$sqlQuery .= "LEFT JOIN $wpdb->pmpro_discount_codes_uses dc ON o.id = dc.order_id ";
}
$sqlQuery .= 'WHERE (1=2 ';
$fields = array(
'o.id',
'o.code',
'o.billing_name',
'o.billing_street',
'o.billing_city',
'o.billing_state',
'o.billing_zip',
'o.billing_phone',
'o.payment_type',
'o.cardtype',
'o.accountnumber',
'o.status',
'o.gateway',
'o.gateway_environment',
'o.payment_transaction_id',
'o.subscription_transaction_id',
'u.user_login',
'u.user_email',
'u.display_name',
'ml.name',
);
if ( $join_with_usermeta ) {
$fields[] = 'um.meta_value';
}
$fields = apply_filters( 'pmpro_orders_search_fields', $fields );
foreach ( $fields as $field ) {
$sqlQuery .= ' OR ' . esc_sql( $field ) . " LIKE '%" . esc_sql( $s ) . "%' ";
}
$sqlQuery .= ') ';
//Not escaping here because we escape the values in the condition statement
$sqlQuery .= 'AND ' . $condition . ' ';
if( ! $count ) {
$sqlQuery .= 'GROUP BY o.id ORDER BY o.id DESC, o.timestamp DESC ';
}
} else {
if ( $filter === 'with-discount-code' ) {
$sqlQuery .= "LEFT JOIN $wpdb->pmpro_discount_codes_uses dc ON o.id = dc.order_id ";
}
//Not escaping here because we escape the values in the condition statement
$sqlQuery .= "WHERE " . $condition . ' ' . $order_query . ' ';
}
if( $count ) {
return $wpdb->get_var( $sqlQuery );
} else {
$sqlQuery .= "LIMIT " . esc_sql( $start ) . "," . esc_sql( $limit );
$order_ids = $wpdb->get_col( $sqlQuery );
$order_data = array();
foreach ( $order_ids as $order_id ) {
$order = new MemberOrder();
$order->nogateway = true;
$order->getMemberOrderByID( $order_id );
$order->getUser();
$order_data[] = $order;
}
return $order_data;
}
}
/**
* Add extra markup in the toolbars before or after the list
*
* @param string $which, helps you decide if you add the markup after (bottom) or before (top) the list array( '' => 'Select a Level' )
*/
function extra_tablenav( $which ) {
if ( $which == 'top' ) {
global $wpdb, $pmpro_msg, $pmpro_msgt;
$now = current_time( 'timestamp' );
if ( isset( $_REQUEST['l'] ) ) {
$l = intval( $_REQUEST['l'] );
} else {
$l = false;
}
if ( isset( $_REQUEST['discount-code'] ) ) {
$discount_code = intval( $_REQUEST['discount-code'] );
} else {
$discount_code = false;
}
if ( isset( $_REQUEST['start-month'] ) ) {
$start_month = intval( $_REQUEST['start-month'] );
} else {
$start_month = '1';
}
if ( isset( $_REQUEST['start-day'] ) ) {
$start_day = intval( $_REQUEST['start-day'] );
} else {
$start_day = '1';
}
if ( isset( $_REQUEST['start-year'] ) ) {
$start_year = intval( $_REQUEST['start-year'] );
} else {
$start_year = date( 'Y', $now );
}
if ( isset( $_REQUEST['end-month'] ) ) {
$end_month = intval( $_REQUEST['end-month'] );
} else {
$end_month = date( 'n', $now );
}
if ( isset( $_REQUEST['end-day'] ) ) {
$end_day = intval( $_REQUEST['end-day'] );
} else {
$end_day = date( 'j', $now );
}
if ( isset( $_REQUEST['end-year'] ) ) {
$end_year = intval( $_REQUEST['end-year'] );
} else {
$end_year = date( 'Y', $now );
}
if ( isset( $_REQUEST['predefined-date'] ) ) {
$predefined_date = sanitize_text_field( $_REQUEST['predefined-date'] );
} else {
$predefined_date = 'This Month';
}
if ( isset( $_REQUEST['status'] ) ) {
$status = sanitize_text_field( $_REQUEST['status'] );
} else {
$status = '';
}
if ( isset( $_REQUEST['filter'] ) ) {
$filter = sanitize_text_field( $_REQUEST['filter'] );
} else {
$filter = 'all';
}
// filters
if ( empty( $filter ) || $filter === 'all' ) {
$filter = 'all';
}
// The code that goes before the table is here
if ( ! empty( $pmpro_msg ) ) { ?>
<div id="message" class="
<?php
if ( $pmpro_msgt == 'success' ) {
echo 'updated fade';
} else {
echo 'error';
}
?>
"><p><?php echo esc_html( $pmpro_msg ); ?></p></div>
<?php } ?>
<div class="tablenav top">
<?php esc_html_e( 'Show', 'paid-memberships-pro' ); ?>
<select id="filter" name="filter">
<option value="all" <?php selected( $filter, 'all' ); ?>><?php esc_html_e( 'All', 'paid-memberships-pro' ); ?></option>
<option
value="within-a-date-range" <?php selected( $filter, 'within-a-date-range' ); ?>><?php esc_html_e( 'Within a Date Range', 'paid-memberships-pro' ); ?></option>
<option
value="predefined-date-range" <?php selected( $filter, 'predefined-date-range' ); ?>><?php esc_html_e( 'Predefined Date Range', 'paid-memberships-pro' ); ?></option>
<option
value="within-a-level" <?php selected( $filter, 'within-a-level' ); ?>><?php esc_html_e( 'Within a Level', 'paid-memberships-pro' ); ?></option>
<option
value="with-discount-code" <?php selected( $filter, 'with-discount-code' ); ?>><?php esc_html_e( 'With a Discount Code', 'paid-memberships-pro' ); ?></option>
<option
value="within-a-status" <?php selected( $filter, 'within-a-status' ); ?>><?php esc_html_e( 'Within a Status', 'paid-memberships-pro' ); ?></option>
<option
value="only-paid" <?php selected( $filter, 'only-paid' ); ?>><?php esc_html_e( 'Only Paid Orders', 'paid-memberships-pro' ); ?></option>
<option
value="only-free" <?php selected( $filter, 'only-free' ); ?>><?php esc_html_e( 'Only Free Orders', 'paid-memberships-pro' ); ?></option>
<?php $custom_filters = apply_filters( 'pmpro_admin_orders_filters', array() ); ?>
<?php foreach( $custom_filters as $value => $name ) { ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $filter, $value ); ?>><?php echo esc_html( $name ); ?></option>
<?php } ?>
</select>
<span id="from"><?php esc_html_e( 'From', 'paid-memberships-pro' ); ?></span>
<select id="start-month" name="start-month">
<?php for ( $i = 1; $i < 13; $i ++ ) { ?>
<option
value="<?php echo esc_attr( $i ); ?>" <?php selected( $start_month, $i ); ?>><?php echo esc_html( date_i18n( 'F', mktime( 0, 0, 0, $i, 2 ) ) ); ?></option>
<?php } ?>
</select>
<input id='start-day' name="start-day" type="text" size="2"
value="<?php echo esc_attr( $start_day ); ?>"/>
<input id='start-year' name="start-year" type="text" size="4"
value="<?php echo esc_attr( $start_year ); ?>"/>
<span id="to"><?php esc_html_e( 'To', 'paid-memberships-pro' ); ?></span>
<select id="end-month" name="end-month">
<?php for ( $i = 1; $i < 13; $i ++ ) { ?>
<option
value="<?php echo esc_attr( $i ); ?>" <?php selected( $end_month, $i ); ?>><?php echo esc_html( date_i18n( 'F', mktime( 0, 0, 0, $i, 2 ) ) ); ?></option>
<?php } ?>
</select>
<input id='end-day' name="end-day" type="text" size="2" value="<?php echo esc_attr( $end_day ); ?>"/>
<input id='end-year' name="end-year" type="text" size="4" value="<?php echo esc_attr( $end_year ); ?>"/>
<span id="filterby"><?php esc_html_e( 'filter by ', 'paid-memberships-pro' ); ?></span>
<select id="predefined-date" name="predefined-date">
<option
value="<?php echo 'This Month'; ?>" <?php selected( $predefined_date, 'This Month' ); ?>><?php esc_html_e( 'This Month', 'paid-memberships-pro' ); ?></option>
<option
value="<?php echo 'Last Month'; ?>" <?php selected( $predefined_date, 'Last Month' ); ?>><?php esc_html_e( 'Last Month', 'paid-memberships-pro' ); ?></option>
<option
value="<?php echo 'This Year'; ?>" <?php selected( $predefined_date, 'This Year' ); ?>><?php esc_html_e( 'This Year', 'paid-memberships-pro' ); ?></option>
<option
value="<?php echo 'Last Year'; ?>" <?php selected( $predefined_date, 'Last Year' ); ?>><?php esc_html_e( 'Last Year', 'paid-memberships-pro' ); ?></option>
</select>
<?php
// Note: only orders belonging to current levels can be filtered. There is no option for orders belonging to deleted levels
$levels = pmpro_sort_levels_by_order( pmpro_getAllLevels( true, true ) );
?>
<select id="l" name="l">
<?php foreach ( $levels as $level ) { ?>
<option
value="<?php echo esc_attr( $level->id ); ?>" <?php selected( $l, $level->id ); ?>><?php echo esc_html( $level->name ); ?></option>
<?php } ?>
</select>
<?php
$sqlQuery = "SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->pmpro_discount_codes ";
$sqlQuery .= "ORDER BY id DESC ";
$codes = $wpdb->get_results($sqlQuery, OBJECT);
if ( ! empty( $codes ) ) { ?>
<select id="discount-code" name="discount-code">
<?php foreach ( $codes as $code ) { ?>
<option
value="<?php echo esc_attr( $code->id ); ?>" <?php selected( $discount_code, $code->id ); ?>><?php echo esc_html( $code->code ); ?></option>
<?php } ?>
</select>
<?php } ?>
<?php
$statuses = pmpro_getOrderStatuses();
?>
<select id="status" name="status">
<?php foreach ( $statuses as $the_status ) { ?>
<option
value="<?php echo esc_attr( $the_status ); ?>" <?php selected( $the_status, $status ); ?>><?php echo esc_html( $the_status ); ?></option>
<?php } ?>
</select>
<input type="hidden" name="page" value="pmpro-orders"/>
<input id="submit" class="button" type="submit" value="<?php esc_attr_e( 'Filter', 'paid-memberships-pro' ); ?>"/>
<script>
//update month/year when period dropdown is changed
jQuery(document).ready(function () {
jQuery('#filter').on('change',function () {
pmpro_ShowMonthOrYear();
});
});
function pmpro_ShowMonthOrYear() {
var filter = jQuery('#filter').val();
if (filter == 'all') {
jQuery('#start-month').hide();
jQuery('#start-day').hide();
jQuery('#start-year').hide();
jQuery('#end-month').hide();
jQuery('#end-day').hide();
jQuery('#end-year').hide();
jQuery('#predefined-date').hide();
jQuery('#status').hide();
jQuery('#l').hide();
jQuery('#discount-code').hide();
jQuery('#from').hide();
jQuery('#to').hide();
jQuery('#submit').show();
jQuery('#filterby').hide();
}
else if (filter == 'within-a-date-range') {
jQuery('#start-month').show();
jQuery('#start-day').show();
jQuery('#start-year').show();
jQuery('#end-month').show();
jQuery('#end-day').show();
jQuery('#end-year').show();
jQuery('#predefined-date').hide();
jQuery('#status').hide();
jQuery('#l').hide();
jQuery('#discount-code').hide();
jQuery('#submit').show();
jQuery('#from').show();
jQuery('#to').show();
jQuery('#filterby').hide();
}
else if (filter == 'predefined-date-range') {
jQuery('#start-month').hide();
jQuery('#start-day').hide();
jQuery('#start-year').hide();
jQuery('#end-month').hide();
jQuery('#end-day').hide();
jQuery('#end-year').hide();
jQuery('#predefined-date').show();
jQuery('#status').hide();
jQuery('#l').hide();
jQuery('#discount-code').hide();
jQuery('#submit').show();
jQuery('#from').hide();
jQuery('#to').hide();
jQuery('#filterby').show();
}
else if (filter == 'within-a-level') {
jQuery('#start-month').hide();
jQuery('#start-day').hide();
jQuery('#start-year').hide();
jQuery('#end-month').hide();
jQuery('#end-day').hide();
jQuery('#end-year').hide();
jQuery('#predefined-date').hide();
jQuery('#status').hide();
jQuery('#l').show();
jQuery('#discount-code').hide();
jQuery('#submit').show();
jQuery('#from').hide();
jQuery('#to').hide();
jQuery('#filterby').show();
}
else if (filter == 'with-discount-code') {
jQuery('#start-month').hide();
jQuery('#start-day').hide();
jQuery('#start-year').hide();
jQuery('#end-month').hide();
jQuery('#end-day').hide();
jQuery('#end-year').hide();
jQuery('#predefined-date').hide();
jQuery('#status').hide();
jQuery('#l').hide();
jQuery('#discount-code').show();
jQuery('#submit').show();
jQuery('#from').hide();
jQuery('#to').hide();
jQuery('#filterby').show();
}
else if (filter == 'within-a-status') {
jQuery('#start-month').hide();
jQuery('#start-day').hide();
jQuery('#start-year').hide();
jQuery('#end-month').hide();
jQuery('#end-day').hide();
jQuery('#end-year').hide();
jQuery('#predefined-date').hide();
jQuery('#status').show();
jQuery('#l').hide();
jQuery('#discount-code').hide();
jQuery('#submit').show();
jQuery('#from').hide();
jQuery('#to').hide();
jQuery('#filterby').show();
}
else if(filter == 'only-paid' || filter == 'only-free' ) {
jQuery('#start-month').hide();
jQuery('#start-day').hide();
jQuery('#start-year').hide();
jQuery('#end-month').hide();
jQuery('#end-day').hide();
jQuery('#end-year').hide();
jQuery('#predefined-date').hide();
jQuery('#status').hide();
jQuery('#l').hide();
jQuery('#discount-code').hide();
jQuery('#submit').show();
jQuery('#from').hide();
jQuery('#to').hide();
jQuery('#filterby').hide();
} else {
jQuery('#start-month').hide();
jQuery('#start-day').hide();
jQuery('#start-year').hide();
jQuery('#end-month').hide();
jQuery('#end-day').hide();
jQuery('#end-year').hide();
jQuery('#predefined-date').hide();
jQuery('#status').hide();
jQuery('#l').hide();
jQuery('#discount-code').hide();
jQuery('#submit').show();
jQuery('#from').hide();
jQuery('#to').hide();
jQuery('#filterby').hide();
}
}
pmpro_ShowMonthOrYear();
</script>
<?php
}
}
/**
* Sanitize the orderby value.
* Only allow fields we want to order by.
* Make sure we append the correct table prefix.
* Make sure there is no other SQL in the value.
* @param string $orderby The column to order by.
* @return string The sanitized value.
*/
function sanitize_orderby( $orderby ) {
$allowed_orderbys = array(
'id' => 'id',
'name' => 'membership_id',
'total' => 'total',
'status_label' => 'status',
'timestamp' => 'timestamp',
);
if ( ! empty( $allowed_orderbys[$orderby] ) ) {
$orderby = $allowed_orderbys[$orderby];
} else {
$orderby = false;
}
return $orderby;
}
/**
* Render a column when no column specific method exists.
*
* @param array $item
* @param string $column_name
*
* @return mixed
*/
public function column_default( $item, $column_name ) {
$column_item = (array) apply_filters( 'pmpro_order_list_item', $item );
if ( isset( $column_item[ $column_name ] ) ) {
// If the user is adding content via the "pmpro_order_list_item" filter.
echo( esc_html( $column_item[ $column_name ] ) );
} elseif ( 0 === strpos( $column_name, 'custom_field_' ) ) {
// If the user is adding content via the "pmpro_orders_extra_cols_body" hook.
// Re-implementing old hook, will be deprecated.
ob_start();
do_action( 'pmpro_orders_extra_cols_body', $item );
$extra_cols = ob_get_clean();
preg_match_all( '/<td>(.*?)<\/td>/s', $extra_cols, $matches );
$custom_field_num_arr = explode( 'custom_field_', $column_name );
$custom_field_num = $custom_field_num_arr[1];
if ( is_numeric( $custom_field_num ) && isset( $matches[1][ intval( $custom_field_num ) ] ) ) {
// If the escaping here breaks your old column body, use the new filters.
echo( wp_kses_post( $matches[1][ intval( $custom_field_num ) ] ) );
}
} else {
// The preferred ways of doing things.
do_action( 'pmpro_manage_orderlist_custom_column', $column_name, $item->id );
}
}
/**
* Renders the columns order ID value
*
* @param object $item
*
* @return string
*/
public function column_order_id( $item ) {
return $item->id;
}
/**
* Renders the columns order code value
*
* @param object $item
*
* @return string
*/
public function column_order_code( $item ) {
?>
<strong><a href="admin.php?page=pmpro-orders&order=<?php echo esc_attr( $item->id ); ?>"><?php echo esc_html( $item->code ); ?></a></strong>
<button title="<?php echo esc_attr__('Copy code to the clipboard', 'paid-memberships-pro' ) ?>" type="button"
class="pmpro_copy_order_id pmpro_copy_to_clipboard button-link edit-filters" style="display:block">
<span class="dashicons dashicons-clipboard" aria-hidden="true"></span>
</button>
<div class="row-actions">
<?php
$delete_text = esc_html(
sprintf(
// translators: %s is the Order Code.
__( 'Deleting orders is permanent and can affect active users. Are you sure you want to delete order %s?', 'paid-memberships-pro' ),
str_replace( "'", '', $item->code )
)
);
$delete_nonce_url = wp_nonce_url(
add_query_arg(
[
'page' => 'pmpro-orders',
'action' => 'delete_order',
'delete' => $item->id,
'order' => isset( $_REQUEST['order'] ) ? intval( $_REQUEST['order'] ) : null,
'orderby' => isset( $_REQUEST['orderby'] ) ? sanitize_text_field( $_REQUEST['orderby'] ) : null,
's' => isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : null,
'filter' => isset( $_REQUEST['filter'] ) ? sanitize_text_field( $_REQUEST['filter'] ) : null,
'start-month' => isset( $_REQUEST['start-month'] ) ? intval( $_REQUEST['start-month'] ) : null,
'start-day' => isset( $_REQUEST['start-day'] ) ? intval( $_REQUEST['start-day'] ) : null,
'start-year' => isset( $_REQUEST['start-year'] ) ? intval( $_REQUEST['start-year'] ) : null,
'end-month' => isset( $_REQUEST['end-month'] ) ? intval( $_REQUEST['end-month'] ) : null,
'end-day' => isset( $_REQUEST['end-day'] ) ? intval( $_REQUEST['end-day'] ) : null,
'end-year' => isset( $_REQUEST['end-year'] ) ? intval( $_REQUEST['end-year'] ) : null,
'predefined-date' => isset( $_REQUEST['predefined-date'] ) ? sanitize_text_field( $_REQUEST['predefined-date'] ) : null,
'l' => isset( $_REQUEST['l'] ) ? sanitize_text_field( $_REQUEST['l'] ) : null,
'status' => isset( $_REQUEST['status'] ) ? sanitize_text_field( $_REQUEST['status'] ) : null,
'discount-code' => isset( $_REQUEST['discount-code'] ) ? sanitize_text_field( $_REQUEST['discount-code'] ) : null,
],
admin_url( 'admin.php' )
),
'delete_order',
'pmpro_orders_nonce'
);
$refund_text = esc_html(
sprintf(
// translators: %s is the Order Code.
__( 'Refund order %s at the payment gateway. This action is permanent. The user and admin will receive an email confirmation after the refund is processed. Are you sure you want to refund this order?', 'paid-memberships-pro' ),
str_replace( "'", '', $item->code )
)
);
$refund_nonce_url = wp_nonce_url(
add_query_arg(
[
'page' => 'pmpro-orders',
'action' => 'refund_order',
'refund' => $item->id,
],
admin_url( 'admin.php' )
),
'refund_order',
'pmpro_orders_nonce'
);
$actions = [
'id' => sprintf(
// translators: %s is the Order ID.
__( 'ID: %s', 'paid-memberships-pro' ),
esc_attr( $item->id )
),
'edit' => sprintf(
'<a title="%1$s" href="%2$s">%3$s</a>',
esc_attr__( 'Edit', 'paid-memberships-pro' ),
esc_url(
add_query_arg(
[
'page' => 'pmpro-orders',
'order' => $item->id,
],
admin_url( 'admin.php' )
)
),
esc_html__( 'Edit', 'paid-memberships-pro' )
),
'copy' => sprintf(
'<a title="%1$s" href="%2$s">%3$s</a>',
esc_attr__( 'Copy', 'paid-memberships-pro' ),
esc_url(
add_query_arg(
[
'page' => 'pmpro-orders',
'order' => - 1,
'copy' => $item->id,
],
admin_url( 'admin.php' )
)
),
esc_html__( 'Copy', 'paid-memberships-pro' )
),
'delete' => sprintf(
'<a title="%1$s" href="%2$s">%3$s</a>',
esc_attr__( 'Delete', 'paid-memberships-pro' ),
'javascript:pmpro_askfirst(\'' . esc_js( $delete_text ) . '\', \'' . esc_js( $delete_nonce_url ) . '\'); void(0);',
esc_html__( 'Delete', 'paid-memberships-pro' )
),
'print' => sprintf(
'<a title="%1$s" href="%2$s" target="_blank" rel="noopener noreferrer">%3$s</a>',
esc_attr__( 'Print', 'paid-memberships-pro' ),
esc_url(