Skip to content

Commit 90d64a9

Browse files
committed
Added sorting to the admin grid.
1 parent 603efab commit 90d64a9

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

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

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public function __construct(
5353

5454
/**
5555
* Prepare items for display.
56+
*
57+
* @phpcs:disable WordPress.Security.NonceVerification.Recommended
58+
*
59+
* @psalm-suppress PossiblyInvalidCast
5660
*/
5761
public function prepare_items(): void {
5862
$this->process_bulk_action();
@@ -67,7 +71,6 @@ public function prepare_items(): void {
6771
]
6872
);
6973

70-
// @TODO
7174
$per_page = $this->get_items_per_page( 'logs_per_page', self::DEFAULT_PER_PAGE );
7275
$current_page = $this->get_pagenum();
7376
$total_items = $this->repository->get_log_count();
@@ -79,12 +82,21 @@ public function prepare_items(): void {
7982
]
8083
);
8184

82-
$this->items = $this->repository->get_logs(
83-
[
84-
'number' => $per_page,
85-
'offset' => ( $current_page - 1 ) * $per_page,
86-
]
87-
);
85+
$args = [
86+
'number' => $per_page,
87+
'offset' => ( $current_page - 1 ) * $per_page,
88+
];
89+
90+
if ( array_key_exists( 'orderby', $_REQUEST ) ) {
91+
$args['orderby'] = sanitize_text_field( wp_unslash( (string) $_REQUEST['orderby'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
92+
}
93+
94+
if ( array_key_exists( 'order', $_REQUEST ) ) {
95+
$args['order'] = sanitize_text_field( wp_unslash( (string) $_REQUEST['order'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
96+
}
97+
98+
99+
$this->items = $this->repository->get_logs( apply_filters( 'wpgraphql_logging_logs_table_query_args', $args ) );
88100
}
89101

90102
/**
@@ -308,6 +320,20 @@ public function get_request_headers(DatabaseEntity $item): string {
308320
if ( false === $formatted_request_headers ) {
309321
return '';
310322
}
311-
return '<pre style="overflow-x: auto; background: #f4f4f4; padding: 15px; border: 1px solid #ddd; border-radius: 4px;">' . esc_html( $formatted_request_headers ) . '</pre>';
323+
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>';
324+
}
325+
326+
/**
327+
* Get a list of sortable columns.
328+
*
329+
* @return array<string, array{0: string, 1: bool}> The sortable columns.
330+
*/
331+
protected function get_sortable_columns(): array {
332+
return [
333+
'id' => [ 'id', false ],
334+
'date' => [ 'datetime', true ],
335+
'level' => [ 'level', false ],
336+
'level_name' => [ 'level_name', false ],
337+
];
312338
}
313339
}

0 commit comments

Comments
 (0)