@@ -32,7 +32,7 @@ class CLWC_Customer_Loyalty_Log_Table extends WP_List_Table {
32
32
/**
33
33
* CLWC_Customer_Loyalty_Log_Table constructor.
34
34
*
35
- * @since 2.0.0
35
+ * Initializes the table's singular and plural names.
36
36
*/
37
37
public function __construct () {
38
38
parent ::__construct ( [
@@ -43,83 +43,108 @@ public function __construct() {
43
43
}
44
44
45
45
/**
46
- * Define the columns displayed in the table.
46
+ * Defines the columns displayed in the table.
47
47
*
48
- * @since 2.0.0
49
- * @return array List of columns.
48
+ * @return array List of columns with column IDs as keys and column titles as values.
50
49
*/
51
50
public function get_columns () {
52
51
return [
53
- 'name ' => esc_html__ ( 'Customer Name ' , 'customer-loyalty-for-woocommerce ' ),
54
- 'email ' => esc_html__ ( 'Email ' , 'customer-loyalty-for-woocommerce ' ),
55
- 'points ' => esc_html__ ( 'Points ' , 'customer-loyalty-for-woocommerce ' ),
52
+ 'name ' => esc_html__ ( 'Customer Name ' , 'customer-loyalty-for-woocommerce ' ),
53
+ 'email ' => esc_html__ ( 'Email ' , 'customer-loyalty-for-woocommerce ' ),
54
+ 'points ' => esc_html__ ( 'Points ' , 'customer-loyalty-for-woocommerce ' ),
55
+ 'details ' => esc_html__ ( 'Details ' , 'customer-loyalty-for-woocommerce ' ),
56
+ 'date ' => esc_html__ ( 'Date ' , 'customer-loyalty-for-woocommerce ' ),
56
57
];
57
- }
58
+ }
58
59
59
60
/**
60
- * Prepares the items for display in the table, including pagination .
61
+ * Defines which columns are sortable .
61
62
*
62
- * @since 2.0.0
63
+ * @return array Associative array with column keys as keys and boolean values indicating sortable status.
64
+ */
65
+ public function get_sortable_columns () {
66
+ return [
67
+ 'name ' => ['name ' , false ],
68
+ 'email ' => ['email ' , false ],
69
+ 'points ' => ['points ' , false ],
70
+ 'date ' => ['date ' , false ],
71
+ ];
72
+ }
73
+
74
+ /**
75
+ * Prepares items for display, including pagination and sorting.
76
+ *
77
+ * Fetches data from the database, processes pagination, and sets column headers.
63
78
*/
64
79
public function prepare_items () {
80
+ global $ wpdb ;
81
+
65
82
$ per_page = 10 ;
66
83
$ current_page = $ this ->get_pagenum ();
67
-
68
- // Fetch log entries and ensure it's an array
69
- $ all_items = $ this ->get_log_entries ();
70
- $ total_items = is_array ($ all_items ) ? count ($ all_items ) : 0 ;
71
-
72
- // Slice items for pagination
73
- $ this ->items = is_array ($ all_items ) ? array_slice ( $ all_items , ( $ current_page - 1 ) * $ per_page , $ per_page ) : [];
74
-
75
- // Pagination arguments
84
+ $ orderby = !empty ( $ _REQUEST ['orderby ' ] ) ? sanitize_sql_orderby ( $ _REQUEST ['orderby ' ] ) : 'date ' ;
85
+ $ order = !empty ( $ _REQUEST ['order ' ] ) && 'asc ' === strtolower ( $ _REQUEST ['order ' ] ) ? 'ASC ' : 'DESC ' ;
86
+
87
+ // Query to fetch entries from the custom table.
88
+ $ table_name = $ wpdb ->prefix . 'clwc_loyalty_log ' ;
89
+ $ offset = ( $ current_page - 1 ) * $ per_page ;
90
+
91
+ // Total item count for pagination.
92
+ $ total_items = $ wpdb ->get_var ( "SELECT COUNT(*) FROM $ table_name " );
93
+
94
+ // Fetch the entries with sorting and pagination
95
+ $ this ->items = $ wpdb ->get_results (
96
+ $ wpdb ->prepare (
97
+ "SELECT * FROM $ table_name ORDER BY $ orderby $ order LIMIT %d OFFSET %d " ,
98
+ $ per_page ,
99
+ $ offset
100
+ ),
101
+ ARRAY_A
102
+ );
103
+
104
+ // Set pagination arguments
76
105
$ this ->set_pagination_args ( [
77
106
'total_items ' => $ total_items ,
78
107
'per_page ' => $ per_page ,
79
108
'total_pages ' => ceil ( $ total_items / $ per_page ),
80
109
] );
81
- }
82
110
83
- /**
84
- * Retrieve log entries from the database or other source.
85
- *
86
- * @since 2.0.0
87
- * @return array List of log entries.
88
- */
89
- private function get_log_entries () {
90
- $ data = [
91
- [
'ID ' =>
1 ,
'name ' =>
'John Doe ' ,
'email ' =>
'[email protected] ' ,
'points ' =>
'+50 ' ,
'details ' =>
'Points awarded for registration ' ,
'date ' =>
'2024-11-01 ' ],
92
- [
'ID ' =>
2 ,
'name ' =>
'Jane Doe ' ,
'email ' =>
'[email protected] ' ,
'points ' =>
'-20 ' ,
'details ' =>
'Points redeemed for discount ' ,
'date ' =>
'2024-11-01 ' ],
93
- ];
94
-
95
- return is_array ($ data ) ? $ data : []; // Ensure an array is returned
111
+ // Set column headers, including sortable columns
112
+ $ columns = $ this ->get_columns ();
113
+ $ hidden = [];
114
+ $ sortable = $ this ->get_sortable_columns ();
115
+ $ this ->_column_headers = [ $ columns , $ hidden , $ sortable ];
96
116
}
97
-
98
117
99
118
/**
100
- * Render the default column values.
119
+ * Renders default column values.
101
120
*
102
- * @since 2.0.0
103
- *
104
- * @param array $item The log entry data.
105
- * @param string $column_name The name of the column.
121
+ * Provides values for columns that do not have a custom rendering method.
122
+ *
123
+ * @param array $item The log entry data for a single row .
124
+ * @param string $column_name The name of the column being rendered .
106
125
*
107
- * @return string The column content.
126
+ * @return string The content to display in the column .
108
127
*/
109
128
protected function column_default ( $ item , $ column_name ) {
110
129
switch ( $ column_name ) {
111
130
case 'name ' :
112
- return $ item ['name ' ];
113
131
case 'email ' :
114
- return $ item ['email ' ];
115
132
case 'points ' :
116
- return sprintf (
117
- '<input type="number" class="clwc-loyalty-points" data-user-id="%d" value="%d" /> ' ,
118
- esc_attr ( $ item ['ID ' ] ),
119
- esc_attr ( $ item ['points ' ] )
120
- );
133
+ case 'details ' :
134
+ case 'date ' :
135
+ return esc_html ( $ item [ $ column_name ] );
121
136
default :
122
- return '' ; // Empty for any undefined columns
137
+ return '' ; // Empty for undefined columns
123
138
}
124
139
}
140
+
141
+ /**
142
+ * Renders and displays the table.
143
+ *
144
+ * Calls `prepare_items()` and then `display()` to output the table.
145
+ */
146
+ public function display_table () {
147
+ $ this ->prepare_items ();
148
+ $ this ->display ();
149
+ }
125
150
}
0 commit comments