Skip to content

Commit 9903f32

Browse files
authored
Merge pull request #409 from wpengine/chore-wpgraphql-logging-fixes-sept-2025
chore: Fix some critical errors with WPGraphQL Logging
2 parents f3e8314 + 792ea7e commit 9903f32

File tree

10 files changed

+52
-281
lines changed

10 files changed

+52
-281
lines changed

plugins/wpgraphql-logging/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ wpgraphql-logging/
8989

9090
- **Monolog-powered logging pipeline**
9191
- Default handler: stores logs in a WordPress table (`{$wpdb->prefix}wpgraphql_logging`).
92-
- Default processors: Memory usage, memory peak, web request, process ID, and `WPGraphQLQueryProcessor` (adds `wpgraphql_query`, `wpgraphql_operation_name`, `wpgraphql_variables`).
9392

9493
- **Simple developer API**
9594
- `Plugin::on()` to subscribe, `Plugin::emit()` to publish, `Plugin::transform()` to modify payloads.

plugins/wpgraphql-logging/docs/Logging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Processors add extra data to each log record. The plugin includes several defaul
4141
- **MemoryPeakUsageProcessor** - Adds peak memory usage
4242
- **WebProcessor** - Adds web request data (IP, method, URI, etc.)
4343
- **ProcessIdProcessor** - Adds the process ID
44-
- **WPGraphQLQueryProcessor** - Adds GraphQL query, variables, and operation name
44+
- **RequestHeadersProcessor** - Adds requests headers
4545

4646
## Default Components
4747

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

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function process_bulk_action(): void {
128128

129129
$nonce_action = 'bulk-' . $this->_args['plural'];
130130
$nonce_value = $_REQUEST['_wpnonce'] ?? '';
131-
131+
132132
$nonce = is_string( $nonce_value ) ? $nonce_value : '';
133133

134134
$nonce_result = wp_verify_nonce( $nonce, $nonce_action );
@@ -156,42 +156,44 @@ public function process_bulk_action(): void {
156156
$deleted_count = $count_before_delete;
157157
}
158158

159-
if ( $deleted_count > 0 ) {
160-
$preserved_filters = [];
161-
$filter_keys = [ 'level_filter', 'start_date', 'end_date' ];
159+
if ( $deleted_count <= 0 ) {
160+
return;
161+
}
162162

163-
foreach ( $filter_keys as $key ) {
164-
$value = $_REQUEST[ $key ] ?? null;
165-
if ( ! empty( $value ) && is_string( $value ) ) {
166-
$preserved_filters[ $key ] = sanitize_text_field( wp_unslash( $value ) );
167-
}
168-
}
163+
$preserved_filters = [];
164+
$filter_keys = [ 'level_filter', 'start_date', 'end_date' ];
169165

170-
$redirect_url = remove_query_arg( [ 'action', 'action2', 'log', '_wpnonce' ] );
171-
$redirect_url = add_query_arg(
172-
array_merge(
173-
[ 'deleted_count' => $deleted_count ],
174-
$preserved_filters
175-
),
176-
$redirect_url
177-
);
178-
179-
if ( ! headers_sent() ) {
180-
wp_safe_redirect( esc_url_raw( $redirect_url ) );
181-
exit;
182-
} else {
183-
echo '<meta http-equiv="refresh" content="0;url=' . esc_url( $redirect_url ) . '">';
184-
printf(
185-
'<div class="notice notice-success"><p>%s <a href="%s">%s</a></p></div>',
186-
esc_html__( 'Logs deleted successfully.', 'wpgraphql-logging' ),
187-
esc_url( $redirect_url ),
188-
esc_html__( 'Return to Logs', 'wpgraphql-logging' )
189-
);
190-
exit;
166+
foreach ( $filter_keys as $key ) {
167+
$value = $_REQUEST[ $key ] ?? null;
168+
if ( ! empty( $value ) && is_string( $value ) ) {
169+
$preserved_filters[ $key ] = sanitize_text_field( wp_unslash( $value ) );
191170
}
192171
}
172+
173+
$redirect_url = remove_query_arg( [ 'action', 'action2', 'log', '_wpnonce' ] );
174+
$redirect_url = add_query_arg(
175+
array_merge(
176+
[ 'deleted_count' => $deleted_count ],
177+
$preserved_filters
178+
),
179+
$redirect_url
180+
);
181+
182+
if ( ! headers_sent() ) {
183+
wp_safe_redirect( esc_url_raw( $redirect_url ) );
184+
exit;
185+
}
186+
187+
echo '<meta http-equiv="refresh" content="0;url=' . esc_url( $redirect_url ) . '">';
188+
printf(
189+
'<div class="notice notice-success"><p>%s <a href="%s">%s</a></p></div>',
190+
esc_html__( 'Logs deleted successfully.', 'wpgraphql-logging' ),
191+
esc_url( $redirect_url ),
192+
esc_html__( 'Return to Logs', 'wpgraphql-logging' )
193+
);
194+
exit;
193195
}
194-
196+
195197
/**
196198
* Get the columns for the logs table.
197199
*
@@ -475,7 +477,7 @@ protected function display_tablenav( $which ): void {
475477
<div class="alignleft actions bulkactions">
476478
<?php $this->bulk_actions( $which_position ); ?>
477479
</div>
478-
480+
479481
<?php
480482
$this->extra_tablenav( $which );
481483
$this->pagination( $which_position );

plugins/wpgraphql-logging/src/Events/QueryActionLogger.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public function __construct( LoggerService $logger, array $config ) {
5555
*
5656
* This method hooks into the `do_graphql_request` action.
5757
*
58-
* @param string $query
58+
* @param string|null $query
5959
* @param string|null $operation_name
6060
* @param array<string, mixed>|null $variables
6161
*/
62-
public function log_pre_request( string $query, ?string $operation_name, ?array $variables ): void {
62+
public function log_pre_request( ?string $query, ?string $operation_name, ?array $variables ): void {
6363
try {
6464
if ( ! $this->is_logging_enabled( $this->config, $query ) ) {
6565
return;
@@ -114,7 +114,7 @@ public function log_graphql_before_execute( Request $request ): void {
114114
if ( ! in_array( Events::BEFORE_GRAPHQL_EXECUTION, $selected_events, true ) ) {
115115
return;
116116
}
117-
117+
118118
$payload = EventManager::transform( Events::BEFORE_GRAPHQL_EXECUTION, [
119119
'context' => $context,
120120
'level' => Level::Info,
@@ -135,7 +135,7 @@ public function log_graphql_before_execute( Request $request ): void {
135135
* @param array<mixed>|\GraphQL\Executor\ExecutionResult $response
136136
* @param \WPGraphQL\WPSchema $schema
137137
* @param string|null $operation
138-
* @param string $query
138+
* @param string|null $query
139139
* @param array<string, mixed>|null $variables
140140
* @param \WPGraphQL\Request $request
141141
* @param string|null $query_id
@@ -145,7 +145,7 @@ public function log_before_response_returned(
145145
array|ExecutionResult $response,
146146
WPSchema $schema,
147147
?string $operation,
148-
string $query,
148+
?string $query,
149149
?array $variables,
150150
Request $request,
151151
?string $query_id

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,6 @@ public function delete(int $id): bool {
115115
public function delete_all(): void {
116116
global $wpdb;
117117
$table_name = DatabaseEntity::get_table_name();
118-
$wpdb->query( $wpdb->prepare( "DELETE FROM %i", $table_name ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
118+
$wpdb->query( $wpdb->prepare( 'DELETE FROM %i', $table_name ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
119119
}
120120
}

plugins/wpgraphql-logging/src/Logger/LoggerService.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Monolog\Processor\WebProcessor;
1414
use WPGraphQL\Logging\Logger\Handlers\WordPressDatabaseHandler;
1515
use WPGraphQL\Logging\Logger\Processors\RequestHeadersProcessor;
16-
use WPGraphQL\Logging\Logger\Processors\WPGraphQLQueryProcessor;
1716

1817
/**
1918
* LoggerService class for managing the Monolog logger instance.
@@ -225,7 +224,6 @@ public static function get_default_processors(): array {
225224
new MemoryPeakUsageProcessor(), // Logs memory peak data.
226225
new WebProcessor(), // Logs web request data. e.g. IP address, request method, URI, etc.
227226
new ProcessIdProcessor(), // Logs the process ID.
228-
new WPGraphQLQueryProcessor(), // Custom processor to capture GraphQL request data.
229227
new RequestHeadersProcessor(), // Custom processor to capture request headers.
230228
];
231229

plugins/wpgraphql-logging/src/Logger/LoggingHelper.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,21 @@ trait LoggingHelper {
1818
* phpcs:disable Generic.Metrics.CyclomaticComplexity, SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
1919
*/
2020
protected function is_logging_enabled( array $config, ?string $query_string = null ): bool {
21+
if ( null === $query_string ) {
22+
return false;
23+
}
24+
2125
$is_enabled = true;
2226
// Check the main "Enabled" checkbox.
2327
if ( ! (bool) ( $config[ Basic_Configuration_Tab::ENABLED ] ?? false ) ) {
2428
$is_enabled = false;
2529
}
2630

31+
// Do not log the seedQuery for Faust.js
32+
if ( $is_enabled && ( 'query GetSeedNode' === trim( $query_string ) ) ) {
33+
$is_enabled = false;
34+
}
35+
2736
// Check if the current user is an admin if that option is enabled.
2837
if ( $is_enabled && ( (bool) ( $config[ Basic_Configuration_Tab::ADMIN_USER_LOGGING ] ?? false ) ) ) {
2938
if ( ! current_user_can( 'manage_options' ) ) {

plugins/wpgraphql-logging/src/Logger/Processors/WPGraphQLQueryProcessor.php

Lines changed: 0 additions & 102 deletions
This file was deleted.

plugins/wpgraphql-logging/tests/wpunit/Logger/Handlers/WordPressDatabaseHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use WPGraphQL\Logging\Logger\Handlers\WordPressDatabaseHandler;
1313

1414
/**
15-
* Class WPGraphQLQueryProcessorTest
15+
* Class WordPressDatabaseHandlerTest
1616
*
1717
* Tests for the WordPressDatabaseHandler class.
1818
*

0 commit comments

Comments
 (0)