Skip to content

Commit 8c80c1e

Browse files
committed
Fixed count when filters are applied.
1 parent 37da3b8 commit 8c80c1e

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

plugins/wpgraphql-logging/src/Admin/View/List/List_Table.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ public function prepare_items(): void {
7373

7474
$per_page = $this->get_items_per_page( 'logs_per_page', self::DEFAULT_PER_PAGE );
7575
$current_page = $this->get_pagenum();
76-
$total_items = $this->repository->get_log_count();
76+
/** @psalm-suppress InvalidArgument */
77+
$where = $this->process_where( $_REQUEST );
78+
$total_items = $this->repository->get_log_count( $where );
7779

7880
$this->set_pagination_args(
7981
[
@@ -94,9 +96,9 @@ public function prepare_items(): void {
9496
if ( array_key_exists( 'order', $_REQUEST ) ) {
9597
$args['order'] = sanitize_text_field( wp_unslash( (string) $_REQUEST['order'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
9698
}
97-
/** @psalm-suppress InvalidArgument */
98-
$args['where'] = $this->process_where( $_REQUEST );
99-
$this->items = $this->repository->get_logs( apply_filters( 'wpgraphql_logging_logs_table_query_args', $args ) );
99+
$args['where'] = $where;
100+
101+
$this->items = $this->repository->get_logs( apply_filters( 'wpgraphql_logging_logs_table_query_args', $args ) );
100102
}
101103

102104
/**

plugins/wpgraphql-logging/src/Admin/View/List/Templates/wpgraphql-logger-filters.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
$wpgraphql_logging_current_end_date = '';
2828

2929
// Verify nonce before processing form data.
30-
if ( ! empty( $request['wpgraphql_logging_nonce'] ) && false === wp_verify_nonce( $request['wpgraphql_logging_nonce'], 'wpgraphql_logging_filter' ) ) {
30+
if ( isset( $_REQUEST['wpgraphql_logging_nonce'] ) && (bool) wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['wpgraphql_logging_nonce'] ) ), 'wpgraphql_logging_filter' ) ) {
3131
$wpgraphql_logging_current_level = isset( $_REQUEST['level_filter'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['level_filter'] ) ) : '';
3232
$wpgraphql_logging_current_start_date = isset( $_REQUEST['start_date'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['start_date'] ) ) : '';
3333
$wpgraphql_logging_current_end_date = isset( $_REQUEST['end_date'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['end_date'] ) ) : '';
@@ -52,5 +52,7 @@
5252
</option>
5353
<?php endforeach; ?>
5454
</select>
55-
<?php submit_button( __( 'Filter', 'wpgraphql-logging' ), 'secondary', 'filter_action', false ); ?>
55+
56+
<?php submit_button( __( 'Filter', 'wpgraphql-logging' ), 'secondary', '', false ); ?>
57+
5658
</div>

plugins/wpgraphql-logging/src/Logger/Database/LogsRepository.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,32 @@ public function get_logs(array $args = []): array {
5353
/**
5454
* Get the total number of log entries.
5555
*
56+
* @param array<string> $where_clauses Array of where clauses to filter the count.
57+
*
58+
* @phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
59+
*
5660
* @return int The total number of log entries.
5761
*/
58-
public function get_log_count(): int {
59-
$cache_key = 'wpgraphql_logs_count';
60-
$count = wp_cache_get( $cache_key );
62+
public function get_log_count(array $where_clauses): int {
63+
global $wpdb;
64+
$table_name = DatabaseEntity::get_table_name();
6165

62-
if ( is_int( $count ) ) {
63-
return $count;
66+
if (empty($where_clauses)) {
67+
return (int) $wpdb->get_var( $wpdb->prepare( // @phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
68+
'SELECT COUNT(*) FROM %i',
69+
$table_name
70+
) );
6471
}
6572

66-
global $wpdb;
67-
$table_name = DatabaseEntity::get_table_name();
68-
$count = $wpdb->get_var( $wpdb->prepare( // @phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
69-
'SELECT COUNT(*) FROM %i',
70-
$table_name
71-
) );
72-
wp_cache_set( $cache_key, $count, '', 300 );
73+
$where = '';
74+
foreach ( $where_clauses as $clause ) {
75+
if ( '' !== $where ) {
76+
$where .= ' AND ';
77+
}
78+
$where .= (string) $clause;
79+
}
7380

74-
return (int) $count;
81+
return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table_name} WHERE {$where}" ); // @phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
7582
}
7683

7784
/**

0 commit comments

Comments
 (0)