Skip to content

Commit 1cf293e

Browse files
committed
Refactored get_query and added this to the view and CSV download.
1 parent 5341b6d commit 1cf293e

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

plugins/wpgraphql-logging/src/Admin/View/Download/Download_Log_Service.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function generate_csv( int $log_id ): void {
5656
'Level Name',
5757
'Message',
5858
'Channel',
59+
'Query',
5960
'Context',
6061
'Extra',
6162
];
@@ -66,8 +67,9 @@ public function generate_csv( int $log_id ): void {
6667
$log->get_level(),
6768
$log->get_level_name(),
6869
$log->get_message(),
69-
wp_json_encode( $log->get_context() ),
7070
$log->get_channel(),
71+
$log->get_query(),
72+
wp_json_encode( $log->get_context() ),
7173
wp_json_encode( $log->get_extra() ),
7274
];
7375

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class List_Table extends WP_List_Table {
2828
*
2929
* @var int
3030
*/
31-
public const DEFAULT_PER_PAGE = 25;
31+
public const DEFAULT_PER_PAGE = 20;
3232

3333
/**
3434
* Constructor.
@@ -73,7 +73,6 @@ 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-
/** @psalm-suppress InvalidArgument */
7776
/** @psalm-suppress InvalidArgument */
7877
$where = $this->process_where( $_REQUEST );
7978
$total_items = $this->repository->get_log_count( $where );
@@ -276,9 +275,8 @@ public function column_query( DatabaseEntity $item ): ?string {
276275
* @return string The query
277276
*/
278277
public function get_query(DatabaseEntity $item): string {
279-
$extra = $item->get_extra();
280-
$query = ! empty( $extra['wpgraphql_query'] ) ? esc_html( $extra['wpgraphql_query'] ) : '';
281-
return '<pre style="overflow-x: auto; background: #f6f7f7; padding: 15px; border: 1px solid #ddd; border-radius: 4px; white-space: pre-wrap; word-break: break-word; max-width: 100%; max-height: 300px; overflow-y: auto; box-sizing: border-box;">' . esc_html( $query ) . '</pre>';
278+
$query = $item->get_query();
279+
return $this->format_code( $query );
282280
}
283281

284282
/**
@@ -330,7 +328,14 @@ public function get_request_headers(DatabaseEntity $item): string {
330328
if ( false === $formatted_request_headers ) {
331329
return '';
332330
}
333-
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>';
331+
return $this->format_code( $formatted_request_headers );
332+
}
333+
334+
protected function format_code(string $code): string {
335+
if ( empty( $code ) ) {
336+
return '';
337+
}
338+
return '<pre style="overflow-x: auto; background: #f4f4f4; padding: 15px; border: 1px solid #ddd; border-radius: 4px; max-height: 300px;">' . esc_html( $code ) . '</pre>';
334339
}
335340

336341
/**

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@
3939
<th><?php esc_html_e( 'Datetime', 'wpgraphql-logging' ); ?></th>
4040
<td><?php echo esc_html( (string) $log->get_datetime() ); ?></td>
4141
</tr>
42-
<tr>
43-
<th><?php esc_html_e( 'Channel', 'wpgraphql-logging' ); ?></th>
44-
<td><?php echo esc_html( (string) $log->get_channel() ); ?></td>
45-
</tr>
4642
<tr>
4743
<th><?php esc_html_e( 'Level', 'wpgraphql-logging' ); ?></th>
4844
<td><?php echo (int) $log->get_level(); ?></td>
@@ -55,6 +51,14 @@
5551
<th><?php esc_html_e( 'Message', 'wpgraphql-logging' ); ?></th>
5652
<td><?php echo esc_html( (string) $log->get_message() ); ?></td>
5753
</tr>
54+
<tr>
55+
<th><?php esc_html_e( 'Channel', 'wpgraphql-logging' ); ?></th>
56+
<td><?php echo esc_html( (string) $log->get_channel() ); ?></td>
57+
</tr>
58+
<tr>
59+
<th><?php esc_html_e( 'Query', 'wpgraphql-logging' ); ?></th>
60+
<td><pre style="overflow-x: auto; background: #f4f4f4; padding: 15px; border: 1px solid #ddd; border-radius: 4px;"><?php echo esc_html( (string) $log->get_query() ); ?></pre></td>
61+
</tr>
5862
<tr>
5963
<th><?php esc_html_e( 'Context', 'wpgraphql-logging' ); ?></th>
6064
<td><pre><?php echo esc_html( (string) wp_json_encode( $log->get_context(), JSON_PRETTY_PRINT ) ); ?></pre></td>

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,32 @@ public function get_datetime(): string {
236236
return $this->datetime;
237237
}
238238

239+
public function get_query(): ?string {
240+
241+
$extra = $this->get_extra();
242+
$context = $this->get_context();
243+
if ( empty( $context ) || ! is_array( $context ) ) {
244+
return null;
245+
}
246+
247+
$query = $context['query'];
248+
249+
$request = $context['request'] ?? null;
250+
if ( empty( $request ) || ! is_array( $request ) ) {
251+
return $query;
252+
}
253+
254+
$params = $request['params'] ?? null;
255+
if ( empty( $params ) || ! is_array( $params ) ) {
256+
return $query;
257+
}
258+
if ( isset( $params['query'] ) && is_string( $params['query'] ) ) {
259+
return $params['query'];
260+
}
261+
262+
return $query;
263+
}
264+
239265
/**
240266
* Finds multiple log entries and returns them as an array.
241267
*

0 commit comments

Comments
 (0)