Skip to content

Commit 5baf690

Browse files
committed
👌 IMPROVE: Updated the Log and Loyalty tables
1 parent a2a9f87 commit 5baf690

File tree

2 files changed

+122
-106
lines changed

2 files changed

+122
-106
lines changed

admin/class-customer-loyalty-table.php

+49-58
Original file line numberDiff line numberDiff line change
@@ -26,57 +26,54 @@
2626
*/
2727
class CLWC_Customer_Loyalty_Table extends WP_List_Table {
2828

29-
/**
30-
* CLWC_Customer_Loyalty_Table constructor.
31-
*/
3229
public function __construct() {
3330
parent::__construct( [
3431
'singular' => 'clwc_customer',
3532
'plural' => 'clwc_customers',
3633
'ajax' => false,
3734
] );
3835
}
39-
/**
40-
* Define table columns.
41-
*
42-
* @since 2.0.0
43-
* @return array List of columns.
44-
*/
36+
4537
public function get_columns() {
4638
return [
4739
'name' => esc_html__( 'Customer Name', 'customer-loyalty-for-woocommerce' ),
4840
'email' => esc_html__( 'Email', 'customer-loyalty-for-woocommerce' ),
4941
'points' => esc_html__( 'Points', 'customer-loyalty-for-woocommerce' ),
5042
];
5143
}
44+
45+
public function get_sortable_columns() {
46+
return [
47+
'name' => ['name', true],
48+
'email' => ['email', false],
49+
'points' => ['points', false],
50+
];
51+
}
52+
53+
public function prepare_items() {
54+
$per_page = 10;
55+
$current_page = $this->get_pagenum();
56+
$search = isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : '';
57+
58+
$all_items = $this->get_customers_with_points( $search );
5259

53-
public function single_row( $item ) {
54-
echo '<tr>';
55-
foreach ( $this->get_columns() as $column_name => $column_display_name ) {
56-
echo '<td>';
57-
// Use column-specific methods or column_default
58-
if ( method_exists( $this, 'column_' . $column_name ) ) {
59-
echo call_user_func( [ $this, 'column_' . $column_name ], $item );
60+
// Sorting
61+
$orderby = !empty( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : 'name';
62+
$order = !empty( $_REQUEST['order'] ) && $_REQUEST['order'] === 'desc' ? 'desc' : 'asc';
63+
64+
usort( $all_items, function ( $a, $b ) use ( $orderby, $order ) {
65+
if ( $orderby === 'points' ) {
66+
// Sort points as integers
67+
$result = (int) $a['points'] <=> (int) $b['points'];
6068
} else {
61-
echo $this->column_default( $item, $column_name );
69+
// Sort name and email as strings, stripping HTML from name
70+
$valA = $orderby === 'name' ? strip_tags( $a['name'] ) : $a[$orderby];
71+
$valB = $orderby === 'name' ? strip_tags( $b['name'] ) : $b[$orderby];
72+
$result = strcmp( $valA, $valB );
6273
}
63-
echo '</td>';
64-
}
65-
echo '</tr>';
66-
}
74+
return ( $order === 'asc' ) ? $result : -$result;
75+
});
6776

68-
/**
69-
* Prepare items for display in the table, including pagination.
70-
*
71-
* @since 2.0.0
72-
* @return void
73-
*/
74-
public function prepare_items() {
75-
$per_page = 10;
76-
$current_page = $this->get_pagenum();
77-
78-
// Retrieve data and set items
79-
$all_items = $this->get_customers_with_points();
8077
$this->items = array_slice( $all_items, ( $current_page - 1 ) * $per_page, $per_page );
8178

8279
$total_items = count( $all_items );
@@ -86,26 +83,24 @@ public function prepare_items() {
8683
'total_pages' => ceil( $total_items / $per_page ),
8784
] );
8885

89-
// Column headers required for display
90-
$columns = $this->get_columns();
91-
$hidden = [];
92-
$sortable = [];
86+
$columns = $this->get_columns();
87+
$hidden = [];
88+
$sortable = $this->get_sortable_columns();
9389
$this->_column_headers = [ $columns, $hidden, $sortable ];
9490
}
9591

92+
private function get_customers_with_points( $search = '' ) {
93+
$args = [
94+
'role' => 'customer',
95+
'search' => $search ? '*' . esc_attr( $search ) . '*' : '',
96+
'search_columns' => ['display_name', 'user_email']
97+
];
9698

97-
/**
98-
* Retrieve customers and their loyalty points.
99-
*
100-
* @since 2.0.0
101-
* @return array List of customers with loyalty points.
102-
*/
103-
private function get_customers_with_points() {
104-
$users = get_users( [ 'role' => 'customer' ] ); // Using a simple role query
99+
$users = get_users( $args );
105100
$data = [];
106-
101+
107102
foreach ( $users as $user ) {
108-
$points = (int) get_user_meta( $user->ID, 'clwc_loyalty_points', true ); // Default to 0 if not set
103+
$points = (int) get_user_meta( $user->ID, 'clwc_loyalty_points', true );
109104
$data[] = [
110105
'ID' => $user->ID,
111106
'name' => sprintf(
@@ -117,20 +112,10 @@ private function get_customers_with_points() {
117112
'points' => $points,
118113
];
119114
}
120-
121-
error_log( print_r( $data, true ) );
122115

123116
return $data;
124-
}
117+
}
125118

126-
/**
127-
* Render default column values.
128-
*
129-
* @since 2.0.0
130-
* @param array $item The customer data.
131-
* @param string $column_name The name of the column.
132-
* @return string The column content.
133-
*/
134119
protected function column_default( $item, $column_name ) {
135120
switch ( $column_name ) {
136121
case 'name':
@@ -146,4 +131,10 @@ protected function column_default( $item, $column_name ) {
146131
return 'N/A';
147132
}
148133
}
134+
135+
public function extra_tablenav( $which ) {
136+
if ( 'top' === $which ) {
137+
$this->search_box( __( 'Search Customers', 'customer-loyalty-for-woocommerce' ), 'clwc_search' );
138+
}
139+
}
149140
}

admin/class-loyalty-log-table.php

+73-48
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CLWC_Customer_Loyalty_Log_Table extends WP_List_Table {
3232
/**
3333
* CLWC_Customer_Loyalty_Log_Table constructor.
3434
*
35-
* @since 2.0.0
35+
* Initializes the table's singular and plural names.
3636
*/
3737
public function __construct() {
3838
parent::__construct( [
@@ -43,83 +43,108 @@ public function __construct() {
4343
}
4444

4545
/**
46-
* Define the columns displayed in the table.
46+
* Defines the columns displayed in the table.
4747
*
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.
5049
*/
5150
public function get_columns() {
5251
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' ),
5657
];
57-
}
58+
}
5859

5960
/**
60-
* Prepares the items for display in the table, including pagination.
61+
* Defines which columns are sortable.
6162
*
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.
6378
*/
6479
public function prepare_items() {
80+
global $wpdb;
81+
6582
$per_page = 10;
6683
$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
76105
$this->set_pagination_args( [
77106
'total_items' => $total_items,
78107
'per_page' => $per_page,
79108
'total_pages' => ceil( $total_items / $per_page ),
80109
] );
81-
}
82110

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 ];
96116
}
97-
98117

99118
/**
100-
* Render the default column values.
119+
* Renders default column values.
101120
*
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.
106125
*
107-
* @return string The column content.
126+
* @return string The content to display in the column.
108127
*/
109128
protected function column_default( $item, $column_name ) {
110129
switch ( $column_name ) {
111130
case 'name':
112-
return $item['name'];
113131
case 'email':
114-
return $item['email'];
115132
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 ] );
121136
default:
122-
return ''; // Empty for any undefined columns
137+
return ''; // Empty for undefined columns
123138
}
124139
}
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+
}
125150
}

0 commit comments

Comments
 (0)