Skip to content

Commit bfecdfa

Browse files
committed
feat: implement view log by id page
1 parent 691a15a commit bfecdfa

File tree

5 files changed

+95
-4
lines changed

5 files changed

+95
-4
lines changed

plugins/wpgraphql-logging/src/Admin/View/.gitkeep

Whitespace-only changes.

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,22 @@ public function column_cb( $item ): string {
168168
* @return string The rendered ID column or null.
169169
*/
170170
public function column_id( DatabaseEntity $item ): string {
171-
$url = isset( $_REQUEST['page'] ) ? esc_attr( sanitize_text_field( $_REQUEST['page'] ) ) : ''; // @phpcs:ignore WordPress.Security.NonceVerification.Recommended
171+
$url = \WPGraphQL\Logging\Admin\View_Logs_Page::ADMIN_PAGE_SLUG;
172172
$actions = [
173-
'view' => sprintf( '<a href="?page=%s&action=%s&log=%d">View</a>', $url, 'view', $item->get_id() ),
173+
'view' => sprintf(
174+
'<a href="?page=%s&action=%s&log=%d">%s</a>',
175+
esc_attr( $url ),
176+
'view',
177+
$item->get_id(),
178+
esc_html__( 'View', 'wpgraphql-logging' )
179+
),
174180
];
175-
return sprintf( '%1$s %2$s', $item->get_id(), $this->row_actions( $actions ) );
181+
182+
return sprintf(
183+
'%1$s %2$s',
184+
$item->get_id(),
185+
$this->row_actions( $actions )
186+
);
176187
}
177188

178189
/**
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Log detail view template.
4+
*
5+
* @var \WPGraphQL\Logging\Logger\Database\DatabaseEntity $log
6+
*/
7+
?>
8+
<div class="wrap">
9+
<h1><?php esc_html_e( 'Log Entry', 'wpgraphql-logging' ); ?></h1>
10+
11+
<table class="widefat striped">
12+
<tbody>
13+
<tr>
14+
<th><?php esc_html_e( 'ID', 'wpgraphql-logging' ); ?></th>
15+
<td><?php echo esc_html( $log->get_id() ); ?></td>
16+
</tr>
17+
<tr>
18+
<th><?php esc_html_e( 'Datetime', 'wpgraphql-logging' ); ?></th>
19+
<td><?php echo esc_html( $log->get_datetime() ); ?></td>
20+
</tr>
21+
<tr>
22+
<th><?php esc_html_e( 'Channel', 'wpgraphql-logging' ); ?></th>
23+
<td><?php echo esc_html( $log->get_channel() ); ?></td>
24+
</tr>
25+
<tr>
26+
<th><?php esc_html_e( 'Level', 'wpgraphql-logging' ); ?></th>
27+
<td><?php echo esc_html( $log->get_level_name() ); ?></td>
28+
</tr>
29+
<tr>
30+
<th><?php esc_html_e( 'Message', 'wpgraphql-logging' ); ?></th>
31+
<td><code><?php echo esc_html( $log->get_message() ); ?></code></td>
32+
</tr>
33+
<tr>
34+
<th><?php esc_html_e( 'Context', 'wpgraphql-logging' ); ?></th>
35+
<td><pre><?php echo esc_html( wp_json_encode( $log->get_context(), JSON_PRETTY_PRINT ) ); ?></pre></td>
36+
</tr>
37+
<tr>
38+
<th><?php esc_html_e( 'Extra', 'wpgraphql-logging' ); ?></th>
39+
<td><pre><?php echo esc_html( wp_json_encode( $log->get_extra(), JSON_PRETTY_PRINT ) ); ?></pre></td>
40+
</tr>
41+
</tbody>
42+
</table>
43+
44+
<p>
45+
<a href="<?php echo esc_url( admin_url( 'admin.php?page=' . \WPGraphQL\Logging\Admin\View_Logs_Page::ADMIN_PAGE_SLUG ) ); ?>" class="button">
46+
<?php esc_html_e( 'Back to Logs', 'wpgraphql-logging' ); ?>
47+
</a>
48+
</p>
49+
</div>

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,26 @@ protected function render_list_page(): void {
104104
* Renders the view page for a single log entry.
105105
*/
106106
protected function render_view_page(): void {
107-
// Render the view page.
107+
$log_id = isset( $_GET['log'] ) ? absint( $_GET['log'] ) : 0; // @phpcs:ignore WordPress.Security.NonceVerification.Recommended
108+
109+
if ( ! $log_id ) {
110+
echo '<div class="notice notice-error"><p>' . esc_html__( 'Invalid log ID.', 'wpgraphql-logging' ) . '</p></div>';
111+
return;
112+
}
113+
114+
$repository = new LogsRepository();
115+
$log = $repository->get_log( $log_id );
116+
117+
if ( ! $log ) {
118+
echo '<div class="notice notice-error"><p>' . esc_html__( 'Log not found.', 'wpgraphql-logging' ) . '</p></div>';
119+
return;
120+
}
121+
122+
$log_template = apply_filters(
123+
'wpgraphql_logging_view_template',
124+
__DIR__ . '/View/List/Templates/wpgraphql-logger-view.php'
125+
);
126+
127+
require_once $log_template; // @phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
108128
}
109129
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,15 @@ public function get_log_count(): int {
5858

5959
return (int) $count;
6060
}
61+
62+
/**
63+
* Get a single log entry by ID.
64+
*
65+
* @param int $id The log entry ID.
66+
*
67+
* @return ?DatabaseEntity The log entry or null if not found.
68+
*/
69+
public function get_log( int $id ): ?DatabaseEntity {
70+
return DatabaseEntity::find_by_id( $id );
71+
}
6172
}

0 commit comments

Comments
 (0)