44
55namespace 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 ;
910use 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.
0 commit comments