Skip to content

Commit f311a4f

Browse files
committed
Refactored ListTable to use LogServiceInterface. Refactored argument processing for Database Service.
1 parent 0305fca commit f311a4f

File tree

5 files changed

+122
-87
lines changed

5 files changed

+122
-87
lines changed

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

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
namespace WPGraphQL\Logging\Admin\View\List;
66

7-
use WPGraphQL\Logging\Logger\Database\DatabaseEntity;
8-
use WPGraphQL\Logging\Logger\Database\LogsRepository;
7+
use DateTime;
8+
use WPGraphQL\Logging\Logger\Api\LogServiceInterface;
9+
use WPGraphQL\Logging\Logger\Database\WordPressDatabaseEntity;
910
use WP_List_Table;
1011

1112
// Include the WP_List_Table class if not already loaded.
@@ -33,11 +34,11 @@ class ListTable extends WP_List_Table {
3334
/**
3435
* Constructor.
3536
*
36-
* @param \WPGraphQL\Logging\Logger\Database\LogsRepository $repository The logs repository.
37+
* @param \WPGraphQL\Logging\Logger\Api\LogServiceInterface $log_service The log service.
3738
* @param array<mixed> $args Optional. An array of arguments.
3839
*/
3940
public function __construct(
40-
public readonly LogsRepository $repository,
41+
public readonly LogServiceInterface $log_service,
4142
$args = []
4243
) {
4344
$args = wp_parse_args(
@@ -75,7 +76,7 @@ public function prepare_items(): void {
7576
$current_page = $this->get_pagenum();
7677
/** @psalm-suppress InvalidArgument */
7778
$where = $this->process_where( $_REQUEST );
78-
$total_items = $this->repository->get_log_count( $where );
79+
$total_items = $this->log_service->count_entities_by_where( $where );
7980

8081
$this->set_pagination_args(
8182
[
@@ -97,8 +98,7 @@ public function prepare_items(): void {
9798
$args['order'] = sanitize_text_field( wp_unslash( (string) $_REQUEST['order'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
9899
}
99100
$args['where'] = $where;
100-
101-
$this->items = $this->repository->get_logs( apply_filters( 'wpgraphql_logging_logs_table_query_args', $args ) );
101+
$this->items = $this->log_service->find_entities_by_where( apply_filters( 'wpgraphql_logging_logs_table_query_args', $args ) );
102102
}
103103

104104
/**
@@ -148,15 +148,15 @@ public function process_bulk_action(): void {
148148
// Remove redundant empty check since array_map always returns array.
149149
foreach ( $ids as $id ) {
150150
if ( $id > 0 ) { // Only process valid IDs.
151-
$this->repository->delete( $id );
151+
$this->log_service->delete_entity_by_id( $id );
152152
}
153153
}
154154
$deleted_count = count( array_filter( $ids, static fn( $id ) => $id > 0 ) );
155155
}
156156

157157
if ( 'delete_all' === $action ) {
158-
$count_before_delete = $this->repository->get_log_count( [] );
159-
$this->repository->delete_all();
158+
$count_before_delete = $this->log_service->count_entities_by_where( [] );
159+
$this->log_service->delete_all_entities();
160160
$deleted_count = $count_before_delete;
161161
}
162162

@@ -224,15 +224,15 @@ public function get_columns(): array {
224224
/**
225225
* Get the default column value for a log entry.
226226
*
227-
* @param mixed|\WPGraphQL\Logging\Logger\Database\DatabaseEntity $item The log entry item.
228-
* @param string $column_name The column name.
227+
* @param mixed|\WPGraphQL\Logging\Logger\Database\WordPressDatabaseEntity $item The log entry item.
228+
* @param string $column_name The column name.
229229
*
230230
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
231231
*
232232
* @return mixed The default column value or null.
233233
*/
234234
public function column_default( $item, $column_name ): mixed {
235-
if ( ! $item instanceof DatabaseEntity ) {
235+
if ( ! $item instanceof WordPressDatabaseEntity ) {
236236
return null;
237237
}
238238

@@ -277,12 +277,12 @@ public function column_default( $item, $column_name ): mixed {
277277
/**
278278
* Renders the checkbox column for a log entry.
279279
*
280-
* @param mixed|\WPGraphQL\Logging\Logger\Database\DatabaseEntity $item The log entry item.
280+
* @param mixed|\WPGraphQL\Logging\Logger\Database\WordPressDatabaseEntity $item The log entry item.
281281
*
282282
* @return string The rendered checkbox column or null.
283283
*/
284284
public function column_cb( $item ): string {
285-
if ( ! $item instanceof DatabaseEntity ) {
285+
if ( ! $item instanceof WordPressDatabaseEntity ) {
286286
return '';
287287
}
288288
return sprintf(
@@ -294,11 +294,11 @@ public function column_cb( $item ): string {
294294
/**
295295
* Renders the ID column for a log entry.
296296
*
297-
* @param \WPGraphQL\Logging\Logger\Database\DatabaseEntity $item The log entry item.
297+
* @param \WPGraphQL\Logging\Logger\Database\WordPressDatabaseEntity $item The log entry item.
298298
*
299299
* @return string The rendered ID column or null.
300300
*/
301-
public function column_id( DatabaseEntity $item ): string {
301+
public function column_id( WordPressDatabaseEntity $item ): string {
302302
$url = \WPGraphQL\Logging\Admin\ViewLogsPage::ADMIN_PAGE_SLUG;
303303
$actions = [
304304
'view' => sprintf(
@@ -327,11 +327,11 @@ public function column_id( DatabaseEntity $item ): string {
327327
/**
328328
* Renders the query column for a log entry.
329329
*
330-
* @param \WPGraphQL\Logging\Logger\Database\DatabaseEntity $item The log entry item.
330+
* @param \WPGraphQL\Logging\Logger\Database\WordPressDatabaseEntity $item The log entry item.
331331
*
332332
* @return string|null The rendered query column or null.
333333
*/
334-
public function column_query( DatabaseEntity $item ): ?string {
334+
public function column_query( WordPressDatabaseEntity $item ): ?string {
335335
$extra = $item->get_extra();
336336
return ! empty( $extra['wpgraphql_query'] ) ? esc_html( $extra['wpgraphql_query'] ) : '';
337337
}
@@ -341,7 +341,7 @@ public function column_query( DatabaseEntity $item ): ?string {
341341
*
342342
* @return string The query
343343
*/
344-
public function get_query(DatabaseEntity $item): string {
344+
public function get_query(WordPressDatabaseEntity $item): string {
345345
$query = $item->get_query();
346346
if ( ! is_string( $query ) || '' === $query ) {
347347
return '';
@@ -354,7 +354,7 @@ public function get_query(DatabaseEntity $item): string {
354354
*
355355
* @return string The event
356356
*/
357-
public function get_event(DatabaseEntity $item): string {
357+
public function get_event(WordPressDatabaseEntity $item): string {
358358

359359
$extra = $item->get_extra();
360360
return ! empty( $extra['wpgraphql_event'] ) ? esc_html( $extra['wpgraphql_event'] ) : $item->get_message();
@@ -363,11 +363,11 @@ public function get_event(DatabaseEntity $item): string {
363363
/**
364364
* Gets the event from extra.
365365
*
366-
* @param \WPGraphQL\Logging\Logger\Database\DatabaseEntity $item The log entry item.
366+
* @param \WPGraphQL\Logging\Logger\Database\WordPressDatabaseEntity $item The log entry item.
367367
*
368368
* @return int The event
369369
*/
370-
public function get_process_id(DatabaseEntity $item): int {
370+
public function get_process_id(WordPressDatabaseEntity $item): int {
371371
$extra = $item->get_extra();
372372
return ! empty( $extra['process_id'] ) ? (int) $extra['process_id'] : 0;
373373
}
@@ -377,7 +377,7 @@ public function get_process_id(DatabaseEntity $item): int {
377377
*
378378
* @return string The event
379379
*/
380-
public function get_memory_usage(DatabaseEntity $item): string {
380+
public function get_memory_usage(WordPressDatabaseEntity $item): string {
381381
$extra = $item->get_extra();
382382
return ! empty( $extra['memory_peak_usage'] ) ? esc_html( $extra['memory_peak_usage'] ) : '';
383383
}
@@ -387,7 +387,7 @@ public function get_memory_usage(DatabaseEntity $item): string {
387387
*
388388
* @return string The event
389389
*/
390-
public function get_request_headers(DatabaseEntity $item): string {
390+
public function get_request_headers(WordPressDatabaseEntity $item): string {
391391
$extra = $item->get_extra();
392392
$request_headers = $extra['request_headers'] ?? [];
393393
if ( empty( $request_headers ) || ! is_array( $request_headers ) ) {
@@ -420,7 +420,7 @@ protected function format_code(string $code): string {
420420
*
421421
* @param array<string, mixed> $request The request data.
422422
*
423-
* @return array<string> The where clauses.
423+
* @return array<string, string> The where clauses.
424424
*/
425425
protected function process_where(array $request): array {
426426
$where_clauses = [];
@@ -431,19 +431,31 @@ protected function process_where(array $request): array {
431431

432432
if ( ! empty( $request['level_filter'] ) ) {
433433
$level = sanitize_text_field( wp_unslash( (string) $request['level_filter'] ) );
434-
$where_clauses[] = "level_name = '" . $level . "'";
434+
$where_clauses[] = [
435+
'column' => 'level_name',
436+
'operator' => '=',
437+
'value' => $level,
438+
];
435439
}
436440

437441
if ( ! empty( $request['start_date'] ) ) {
438442
$start_date = sanitize_text_field( $request['start_date'] );
439-
$date = new \DateTime( $start_date );
440-
$where_clauses[] = "datetime >= '" . $date->format( 'Y-m-d H:i:s' ) . "'";
443+
$date = new DateTime( $start_date );
444+
$where_clauses[] = [
445+
'column' => 'datetime',
446+
'operator' => '>=',
447+
'value' => $date->format( 'Y-m-d H:i:s' ),
448+
];
441449
}
442450

443451
if ( ! empty( $request['end_date'] ) ) {
444452
$end_date = sanitize_text_field( $request['end_date'] );
445-
$date = new \DateTime( $end_date );
446-
$where_clauses[] = "datetime <= '" . $date->format( 'Y-m-d H:i:s' ) . "'";
453+
$date = new DateTime( $end_date );
454+
$where_clauses[] = [
455+
'column' => 'datetime',
456+
'operator' => '<=',
457+
'value' => $date->format( 'Y-m-d H:i:s' ),
458+
];
447459
}
448460

449461
// Allow developers to modify the where clauses.

plugins/wpgraphql-logging/src/Admin/View/Templates/WPGraphQLLoggerView.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* @package WPGraphQL\Logging
99
*
10-
* @var \WPGraphQL\Logging\Logger\Database\DatabaseEntity $log
10+
* @var \WPGraphQL\Logging\Logger\Database\WordPressDatabaseEntity $log
1111
*
1212
* @since 0.0.1
1313
*/

plugins/wpgraphql-logging/src/Admin/ViewLogsPage.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use WPGraphQL\Logging\Admin\View\Download\DownloadLogService;
88
use WPGraphQL\Logging\Admin\View\List\ListTable;
99
use WPGraphQL\Logging\Logger\Api\LogServiceInterface;
10-
use WPGraphQL\Logging\Logger\Database\LogsRepository;
1110
use WPGraphQL\Logging\Logger\Store\LogStoreService;
1211

1312
/**
@@ -245,7 +244,7 @@ protected function get_post_value(string $key): ?string {
245244
* Renders the list page for log entries.
246245
*/
247246
protected function render_list_page(): void {
248-
$list_table = new ListTable( new LogsRepository() ); // @phpcs:ignore SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable
247+
$list_table = new ListTable( $this->get_log_service() ); // @phpcs:ignore SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable
249248
$list_template = apply_filters(
250249
'wpgraphql_logging_list_template',
251250
__DIR__ . '/View/Templates/WPGraphQLLoggerList.php'

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,19 @@ public function find_entities_by_where(array $args = []): array {
7777
$this->where_values = [];
7878
$sql = 'SELECT * FROM %i';
7979
$this->where_values[] = sanitize_text_field( $this->get_table_name() );
80-
$sql = $this->prepare_sql( $sql, $args );
81-
82-
// @TODO
83-
// Add the order by and limit to the SQL query.
84-
// $sql .= " ORDER BY $orderby $order LIMIT %d, %d";
85-
// $values[] = $offset;
86-
// $values[] = $limit;
87-
// $query = $wpdb->prepare( $sql, $values );
88-
// @TODO - Fix this.
80+
if ( isset( $args['where'] ) && is_array( $args['where'] ) ) {
81+
$sql = $this->prepare_sql( $sql, $args['where'] );
82+
}
83+
84+
$orderby = $args['orderby'] ?? 'id';
85+
$order = $args['order'] ?? 'DESC';
86+
$limit = $args['number'] ?? 100;
87+
$offset = $args['offset'] ?? 0;
88+
89+
$sql .= " ORDER BY $orderby $order LIMIT %d, %d";
90+
$this->where_values[] = (string) $offset;
91+
$this->where_values[] = (string) $limit;
92+
8993
$results = $wpdb->get_results( $wpdb->prepare( $sql, $this->where_values ), ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
9094

9195
if ( empty( $results ) || ! is_array( $results ) ) {
@@ -177,23 +181,28 @@ protected function get_table_name(): string {
177181
/**
178182
* Prepares the SQL query.
179183
*
180-
* @param string $sql The SQL query template.
181-
* @param array<string, mixed> $where_conditions The where conditions.
184+
* @param string $sql The SQL query template.
185+
* @param array<array{column: string, operator: string, value: string}> $where_conditions The where conditions.
182186
*
183187
* @phpcs:disable SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
188+
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
184189
*
185190
* @return string The prepared SQL query.
186191
*/
187192
protected function prepare_sql(string $sql, array $where_conditions): string {
188193
$where_clauses = [];
189194
$safe_operators = $this->get_safe_operators();
190195
foreach ( $where_conditions as $column => $condition ) {
191-
if ( ! is_array( $condition ) || ! isset( $condition['value'] ) || ! isset( $condition['operator'] ) ) {
196+
if ( ! is_array( $condition ) || ! isset( $condition['column'] ) || ! isset( $condition['value'] ) || ! isset( $condition['operator'] ) ) {
192197
continue;
193198
}
194199

195-
$value = $condition['value'] ?? '';
196-
$operator = $condition['operator'] ?? '';
200+
$column = $condition['column'];
201+
if ( '' === $column ) {
202+
continue;
203+
}
204+
$value = $condition['value'];
205+
$operator = $condition['operator'];
197206
if ( ! in_array( $operator, $safe_operators, true ) ) {
198207
continue;
199208
}

0 commit comments

Comments
 (0)