Skip to content

Commit 1558ff7

Browse files
committed
fix: correct filtering and pagination in Log Display Screen
The Log Display Screen was applying filters after pagination, which caused incorrect page counts and could show empty pages when filters were active. Additionally, the types dropdown had a typo that checked the wrong REQUEST parameter. This reorders the logic to apply month and type filters before pagination, ensuring the total_items count and page calculations reflect the filtered dataset. The min/max dates for the month dropdown are still populated from the complete unfiltered dataset so all available months remain visible in the filter. Fixes #120
1 parent f8cde80 commit 1558ff7

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

includes/class-syndication-logger-viewer.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ public function get_bulk_actions() {
105105
}
106106

107107
/**
108+
* Prepares the list of items for displaying.
108109
*
110+
* Fetches log data, applies filters, sorts, and paginates.
111+
* Filters are applied BEFORE pagination so that pagination
112+
* reflects the filtered result count accurately.
109113
*/
110114
public function prepare_items() {
111115
$columns = $this->get_columns();
@@ -138,45 +142,50 @@ public function prepare_items() {
138142
foreach( $log_data as $site_id => $log_items ) {
139143
$this->prepared_data = array_merge( $this->prepared_data, $log_items );
140144
}
141-
usort( $this->prepared_data, array( $this, 'usort_reorder' ) );
142145

143-
$per_page = $this->get_items_per_page( 'per_page' );
144-
$current_page = $this->get_pagenum();
145-
$total_items = count( $this->prepared_data );
146-
147-
$this->found_data = array_slice( $this->prepared_data,( ( $current_page-1 )* $per_page ), $per_page );
146+
// Populate min/max dates from ALL data (before filtering) for the date dropdown.
147+
if ( $this->prepared_data ) {
148+
$items_sorted_by_time = $this->prepared_data;
148149

149-
150-
// Populate min/max dates.
151-
if ( $this->found_data ) {
152-
$items_sorted_by_time = $this->found_data;
153-
154-
usort( $items_sorted_by_time, function ( $a, $b ) {
150+
usort( $items_sorted_by_time, function ( $a, $b ) {
155151
return strtotime( $a['time'] ) - strtotime( $b['time'] );
156152
} );
157153

158154
$this->_max_date = strtotime( end( $items_sorted_by_time )['time'] );
159155
$this->_min_date = strtotime( reset( $items_sorted_by_time )['time'] );
160156
}
161157

158+
// Apply filters BEFORE pagination so counts are accurate.
159+
$filtered_data = $this->prepared_data;
162160

163-
// Filter by month
161+
// Filter by month.
164162
$requested_month = isset( $_REQUEST['month'] ) ? esc_attr( $_REQUEST['month'] ) : null;
165163
if ( $requested_month ) {
166-
$this->found_data = array_filter( $this->found_data, function ( $item ) use ( $requested_month ) {
164+
$filtered_data = array_filter( $filtered_data, function ( $item ) use ( $requested_month ) {
167165
return date( 'Y-m', strtotime( $item['time'] ) ) === $requested_month;
168166
} );
169167
}
170168

171-
172-
// Filter by type
169+
// Filter by type.
173170
$requested_type = isset( $_REQUEST['type'] ) ? esc_attr( $_REQUEST['type'] ) : null;
174171
if ( $requested_type ) {
175-
$this->found_data = array_filter( $this->found_data, function ( $item ) use ( $requested_type ) {
172+
$filtered_data = array_filter( $filtered_data, function ( $item ) use ( $requested_type ) {
176173
return $requested_type === $item['msg_type'];
177174
} );
178175
}
179176

177+
// Re-index array after filtering.
178+
$filtered_data = array_values( $filtered_data );
179+
180+
// Sort the filtered data.
181+
usort( $filtered_data, array( $this, 'usort_reorder' ) );
182+
183+
// Paginate the filtered and sorted data.
184+
$per_page = $this->get_items_per_page( 'per_page' );
185+
$current_page = $this->get_pagenum();
186+
$total_items = count( $filtered_data );
187+
188+
$this->found_data = array_slice( $filtered_data, ( ( $current_page - 1 ) * $per_page ), $per_page );
180189

181190
$this->set_pagination_args( array(
182191
'total_items' => $total_items,
@@ -258,7 +267,7 @@ protected function _create_months_dropdown() {
258267
}
259268

260269
protected function _create_types_dropdown() {
261-
$requested_type = isset( $_REQUEST['month'] ) ? esc_attr( $_REQUEST['type'] ) : null;
270+
$requested_type = isset( $_REQUEST['type'] ) ? esc_attr( $_REQUEST['type'] ) : null;
262271
?>
263272
<label class="screen-reader-text" for="filter-by-type">Filter by type</label>
264273
<select name="type" id="filter-by-type">

0 commit comments

Comments
 (0)