Skip to content

Commit 37da3b8

Browse files
committed
Added filters for date and level
Allows users to filter by date and level.
1 parent 90d64a9 commit 37da3b8

File tree

6 files changed

+146
-15
lines changed

6 files changed

+146
-15
lines changed

plugins/wpgraphql-logging/psalm.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,11 @@
4242
<file name="src/Admin/View/List/Templates/wpgraphql-logger-view.php"/>
4343
</errorLevel>
4444
</RedundantCast>
45+
46+
<PossiblyInvalidArgument>
47+
<errorLevel type="suppress">
48+
<file name="src/Admin/View/List/Templates/wpgraphql-logger-filters.php"/>
49+
</errorLevel>
50+
</PossiblyInvalidArgument>
4551
</issueHandlers>
4652
</psalm>

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

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ public function prepare_items(): void {
9494
if ( array_key_exists( 'order', $_REQUEST ) ) {
9595
$args['order'] = sanitize_text_field( wp_unslash( (string) $_REQUEST['order'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
9696
}
97-
98-
99-
$this->items = $this->repository->get_logs( apply_filters( 'wpgraphql_logging_logs_table_query_args', $args ) );
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 ) );
100100
}
101101

102102
/**
@@ -323,6 +323,41 @@ public function get_request_headers(DatabaseEntity $item): string {
323323
return '<pre style="overflow-x: auto; background: #f4f4f4; padding: 15px; border: 1px solid #ddd; border-radius: 4px; max-height: 300px;">' . esc_html( $formatted_request_headers ) . '</pre>';
324324
}
325325

326+
/**
327+
* Process the where clauses for filtering.
328+
*
329+
* @param array<string, mixed> $request The request data.
330+
*
331+
* @return array<string> The where clauses.
332+
*/
333+
protected function process_where(array $request): array {
334+
$where_clauses = [];
335+
336+
if ( ! empty( $request['wpgraphql_logging_nonce'] ) && false === wp_verify_nonce( $request['wpgraphql_logging_nonce'], 'wpgraphql_logging_filter' ) ) {
337+
return [];
338+
}
339+
340+
if ( ! empty( $request['level_filter'] ) ) {
341+
$level = sanitize_text_field( wp_unslash( (string) $request['level_filter'] ) );
342+
$where_clauses[] = "level_name = '" . $level . "'";
343+
}
344+
345+
if ( ! empty( $request['start_date'] ) ) {
346+
$start_date = sanitize_text_field( $request['start_date'] );
347+
$date = new \DateTime( $start_date );
348+
$where_clauses[] = "datetime >= '" . $date->format( 'Y-m-d H:i:s' ) . "'";
349+
}
350+
351+
if ( ! empty( $request['end_date'] ) ) {
352+
$end_date = sanitize_text_field( $request['end_date'] );
353+
$date = new \DateTime( $end_date );
354+
$where_clauses[] = "datetime <= '" . $date->format( 'Y-m-d H:i:s' ) . "'";
355+
}
356+
357+
// Allow developers to modify the where clauses.
358+
return apply_filters( 'wpgraphql_logging_logs_table_where_clauses', $where_clauses, $request );
359+
}
360+
326361
/**
327362
* Get a list of sortable columns.
328363
*
@@ -336,4 +371,19 @@ protected function get_sortable_columns(): array {
336371
'level_name' => [ 'level_name', false ],
337372
];
338373
}
374+
375+
/**
376+
* Render extra table navigation controls.
377+
*
378+
* @param string $which The location of the nav ('top' or 'bottom').
379+
*/
380+
protected function extra_tablenav( $which ): void {
381+
382+
// Only display above the table.
383+
if ( 'top' !== $which ) {
384+
return;
385+
}
386+
$template = apply_filters( 'wpgraphql_logging_filters_template', __DIR__ . '/Templates/wpgraphql-logger-filters.php' );
387+
require_once $template; // @phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
388+
}
339389
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Loggers list view template using WP_List_Table.
7+
*
8+
* @package WPGraphQL\Logger\Admin\View\List\Templates
9+
*
10+
* @var \WPGraphQL\Logging\Admin\View\List\List_Table $list_table List table instance.
11+
*
12+
* @since 0.0.1
13+
*/
14+
15+
if ( ! defined( 'ABSPATH' ) ) {
16+
exit;
17+
}
18+
?>
19+
<div class="alignleft actions">
20+
<?php
21+
// Nonce for security.
22+
wp_nonce_field( 'wpgraphql_logging_filter', 'wpgraphql_logging_nonce' );
23+
24+
// Get current filter values.
25+
$wpgraphql_logging_current_level = '';
26+
$wpgraphql_logging_current_start_date = '';
27+
$wpgraphql_logging_current_end_date = '';
28+
29+
// Verify nonce before processing form data.
30+
if ( ! empty( $request['wpgraphql_logging_nonce'] ) && false === wp_verify_nonce( $request['wpgraphql_logging_nonce'], 'wpgraphql_logging_filter' ) ) {
31+
$wpgraphql_logging_current_level = isset( $_REQUEST['level_filter'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['level_filter'] ) ) : '';
32+
$wpgraphql_logging_current_start_date = isset( $_REQUEST['start_date'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['start_date'] ) ) : '';
33+
$wpgraphql_logging_current_end_date = isset( $_REQUEST['end_date'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['end_date'] ) ) : '';
34+
}
35+
36+
/**
37+
* Log levels for filtering.
38+
* Based on Monolog levels.
39+
*
40+
* @see https://datatracker.ietf.org/doc/html/rfc5424
41+
*/
42+
$wpgraphql_logging_log_levels = [ 'debug', 'info', 'notice', 'warning', 'error', 'critical', 'alert', 'emergency' ];
43+
?>
44+
<input type="text" name="start_date" placeholder="Start Date (YYYY-MM-DD)" value="<?php echo esc_attr( $wpgraphql_logging_current_start_date ); ?>" />
45+
<input type="text" name="end_date" placeholder="End Date (YYYY-MM-DD)" value="<?php echo esc_attr( $wpgraphql_logging_current_end_date ); ?>" />
46+
47+
<select name="level_filter">
48+
<option value="">All Levels</option>
49+
<?php foreach ( $wpgraphql_logging_log_levels as $wpgraphql_logging_log_level ) : ?>
50+
<option value="<?php echo esc_attr( $wpgraphql_logging_log_level ); ?>" <?php selected( $wpgraphql_logging_current_level, $wpgraphql_logging_log_level ); ?>>
51+
<?php echo esc_html( ucfirst( $wpgraphql_logging_log_level ) ); ?>
52+
</option>
53+
<?php endforeach; ?>
54+
</select>
55+
<?php submit_button( __( 'Filter', 'wpgraphql-logging' ), 'secondary', 'filter_action', false ); ?>
56+
</div>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55
/**
6-
* Webhooks list view template using WP_List_Table.
6+
* Logs list view template using WP_List_Table.
77
*
88
* @package WPGraphQL\Logger\Admin\View\List\Templates
99
*

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -239,22 +239,34 @@ public function get_datetime(): string {
239239
/**
240240
* Finds multiple log entries and returns them as an array.
241241
*
242-
* @param int $limit The maximum number of log entries to return.
243-
* @param int $offset The offset for pagination.
244-
* @param string $orderby The column to order by.
245-
* @param string $order The order direction (ASC or DESC).
242+
* @param int $limit The maximum number of log entries to return.
243+
* @param int $offset The offset for pagination.
244+
* @param array<string, mixed> $where_clauses Optional. Additional WHERE conditions.
245+
* @param string $orderby The column to order by.
246+
* @param string $order The order direction (ASC or DESC).
246247
*
247248
* @return array<\WPGraphQL\Logging\Logger\Database\DatabaseEntity> An array of DatabaseEntity instances, or an empty array if none found.
248249
*/
249-
public static function find_logs(int $limit, int $offset, string $orderby = 'id', string $order = 'DESC'): array {
250+
public static function find_logs(int $limit, int $offset, array $where_clauses = [], string $orderby = 'id', string $order = 'DESC'): array {
250251
global $wpdb;
251252
$table_name = self::get_table_name();
252-
$order = esc_sql( strtoupper( $order ) );
253-
$orderby = esc_sql( $orderby );
253+
$order = sanitize_text_field( strtoupper( $order ) );
254+
$orderby = sanitize_text_field( $orderby );
255+
256+
$where = '';
257+
foreach ( $where_clauses as $clause ) {
258+
if ( '' !== $where ) {
259+
$where .= ' AND ';
260+
}
261+
$where .= (string) $clause;
262+
}
263+
if ( '' !== $where ) {
264+
$where = 'WHERE ' . $where;
265+
}
254266

255267
/** @psalm-suppress PossiblyInvalidCast */
256268
$query = $wpdb->prepare(
257-
"SELECT * FROM {$table_name} ORDER BY {$orderby} {$order} LIMIT %d, %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
269+
"SELECT * FROM {$table_name} {$where} ORDER BY {$orderby} {$order} LIMIT %d, %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
258270
$offset,
259271
$limit
260272
);

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,28 @@ public function get_logs(array $args = []): array {
2626
'offset' => 0,
2727
'orderby' => 'id',
2828
'order' => 'DESC',
29+
'where' => [],
2930
];
3031
$args = wp_parse_args( $args, $defaults );
3132

32-
$orderby = esc_sql( $args['orderby'] );
33+
$orderby = $args['orderby'];
3334
if ( ! is_string( $orderby ) || '' === $orderby ) {
3435
$orderby = $defaults['orderby'];
3536
}
36-
$order = esc_sql( $args['order'] );
37+
$order = $args['order'];
3738
if ( ! is_string( $order ) || '' === $order ) {
3839
$order = $defaults['order'];
3940
}
41+
$where = $args['where'];
42+
if ( ! is_array( $where ) ) {
43+
$where = $defaults['where'];
44+
}
45+
4046
$limit = absint( $args['number'] );
4147
$offset = absint( $args['offset'] );
4248

43-
return DatabaseEntity::find_logs( $limit, $offset, $orderby, $order );
49+
50+
return DatabaseEntity::find_logs( $limit, $offset, $where, $orderby, $order );
4451
}
4552

4653
/**

0 commit comments

Comments
 (0)