Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion plugins/wpgraphql-logging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ wpgraphql-logging/

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

- **Simple developer API**
- `Plugin::on()` to subscribe, `Plugin::emit()` to publish, `Plugin::transform()` to modify payloads.
Expand Down
2 changes: 1 addition & 1 deletion plugins/wpgraphql-logging/docs/Logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Processors add extra data to each log record. The plugin includes several defaul
- **MemoryPeakUsageProcessor** - Adds peak memory usage
- **WebProcessor** - Adds web request data (IP, method, URI, etc.)
- **ProcessIdProcessor** - Adds the process ID
- **WPGraphQLQueryProcessor** - Adds GraphQL query, variables, and operation name
- **RequestHeadersProcessor** - Adds requests headers

## Default Components

Expand Down
68 changes: 35 additions & 33 deletions plugins/wpgraphql-logging/src/Admin/View/List/List_Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function process_bulk_action(): void {

$nonce_action = 'bulk-' . $this->_args['plural'];
$nonce_value = $_REQUEST['_wpnonce'] ?? '';

$nonce = is_string( $nonce_value ) ? $nonce_value : '';

$nonce_result = wp_verify_nonce( $nonce, $nonce_action );
Expand Down Expand Up @@ -156,42 +156,44 @@ public function process_bulk_action(): void {
$deleted_count = $count_before_delete;
}

if ( $deleted_count > 0 ) {
$preserved_filters = [];
$filter_keys = [ 'level_filter', 'start_date', 'end_date' ];
if ( $deleted_count <= 0 ) {
return;
}

foreach ( $filter_keys as $key ) {
$value = $_REQUEST[ $key ] ?? null;
if ( ! empty( $value ) && is_string( $value ) ) {
$preserved_filters[ $key ] = sanitize_text_field( wp_unslash( $value ) );
}
}
$preserved_filters = [];
$filter_keys = [ 'level_filter', 'start_date', 'end_date' ];

$redirect_url = remove_query_arg( [ 'action', 'action2', 'log', '_wpnonce' ] );
$redirect_url = add_query_arg(
array_merge(
[ 'deleted_count' => $deleted_count ],
$preserved_filters
),
$redirect_url
);

if ( ! headers_sent() ) {
wp_safe_redirect( esc_url_raw( $redirect_url ) );
exit;
} else {
echo '<meta http-equiv="refresh" content="0;url=' . esc_url( $redirect_url ) . '">';
printf(
'<div class="notice notice-success"><p>%s <a href="%s">%s</a></p></div>',
esc_html__( 'Logs deleted successfully.', 'wpgraphql-logging' ),
esc_url( $redirect_url ),
esc_html__( 'Return to Logs', 'wpgraphql-logging' )
);
exit;
foreach ( $filter_keys as $key ) {
$value = $_REQUEST[ $key ] ?? null;
if ( ! empty( $value ) && is_string( $value ) ) {
$preserved_filters[ $key ] = sanitize_text_field( wp_unslash( $value ) );
}
}

$redirect_url = remove_query_arg( [ 'action', 'action2', 'log', '_wpnonce' ] );
$redirect_url = add_query_arg(
array_merge(
[ 'deleted_count' => $deleted_count ],
$preserved_filters
),
$redirect_url
);

if ( ! headers_sent() ) {
wp_safe_redirect( esc_url_raw( $redirect_url ) );
exit;
}

echo '<meta http-equiv="refresh" content="0;url=' . esc_url( $redirect_url ) . '">';
printf(
'<div class="notice notice-success"><p>%s <a href="%s">%s</a></p></div>',
esc_html__( 'Logs deleted successfully.', 'wpgraphql-logging' ),
esc_url( $redirect_url ),
esc_html__( 'Return to Logs', 'wpgraphql-logging' )
);
exit;
}

/**
* Get the columns for the logs table.
*
Expand Down Expand Up @@ -475,7 +477,7 @@ protected function display_tablenav( $which ): void {
<div class="alignleft actions bulkactions">
<?php $this->bulk_actions( $which_position ); ?>
</div>

<?php
$this->extra_tablenav( $which );
$this->pagination( $which_position );
Expand Down
10 changes: 5 additions & 5 deletions plugins/wpgraphql-logging/src/Events/QueryActionLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public function __construct( LoggerService $logger, array $config ) {
*
* This method hooks into the `do_graphql_request` action.
*
* @param string $query
* @param string|null $query
* @param string|null $operation_name
* @param array<string, mixed>|null $variables
*/
public function log_pre_request( string $query, ?string $operation_name, ?array $variables ): void {
public function log_pre_request( ?string $query, ?string $operation_name, ?array $variables ): void {
try {
if ( ! $this->is_logging_enabled( $this->config, $query ) ) {
return;
Expand Down Expand Up @@ -114,7 +114,7 @@ public function log_graphql_before_execute( Request $request ): void {
if ( ! in_array( Events::BEFORE_GRAPHQL_EXECUTION, $selected_events, true ) ) {
return;
}

$payload = EventManager::transform( Events::BEFORE_GRAPHQL_EXECUTION, [
'context' => $context,
'level' => Level::Info,
Expand All @@ -135,7 +135,7 @@ public function log_graphql_before_execute( Request $request ): void {
* @param array<mixed>|\GraphQL\Executor\ExecutionResult $response
* @param \WPGraphQL\WPSchema $schema
* @param string|null $operation
* @param string $query
* @param string|null $query
* @param array<string, mixed>|null $variables
* @param \WPGraphQL\Request $request
* @param string|null $query_id
Expand All @@ -145,7 +145,7 @@ public function log_before_response_returned(
array|ExecutionResult $response,
WPSchema $schema,
?string $operation,
string $query,
?string $query,
?array $variables,
Request $request,
?string $query_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ public function delete(int $id): bool {
public function delete_all(): void {
global $wpdb;
$table_name = DatabaseEntity::get_table_name();
$wpdb->query( $wpdb->prepare( "DELETE FROM %i", $table_name ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
$wpdb->query( $wpdb->prepare( 'DELETE FROM %i', $table_name ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
}
}
2 changes: 0 additions & 2 deletions plugins/wpgraphql-logging/src/Logger/LoggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Monolog\Processor\WebProcessor;
use WPGraphQL\Logging\Logger\Handlers\WordPressDatabaseHandler;
use WPGraphQL\Logging\Logger\Processors\RequestHeadersProcessor;
use WPGraphQL\Logging\Logger\Processors\WPGraphQLQueryProcessor;

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

Expand Down
9 changes: 9 additions & 0 deletions plugins/wpgraphql-logging/src/Logger/LoggingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ trait LoggingHelper {
* phpcs:disable Generic.Metrics.CyclomaticComplexity, SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
*/
protected function is_logging_enabled( array $config, ?string $query_string = null ): bool {
if ( null === $query_string ) {
return false;
}

$is_enabled = true;
// Check the main "Enabled" checkbox.
if ( ! (bool) ( $config[ Basic_Configuration_Tab::ENABLED ] ?? false ) ) {
$is_enabled = false;
}

// Do not log the seedQuery for Faust.js
if ( $is_enabled && ( 'query GetSeedNode' === trim( $query_string ) ) ) {
$is_enabled = false;
}

// Check if the current user is an admin if that option is enabled.
if ( $is_enabled && ( (bool) ( $config[ Basic_Configuration_Tab::ADMIN_USER_LOGGING ] ?? false ) ) ) {
if ( ! current_user_can( 'manage_options' ) ) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use WPGraphQL\Logging\Logger\Handlers\WordPressDatabaseHandler;

/**
* Class WPGraphQLQueryProcessorTest
* Class WordPressDatabaseHandlerTest
*
* Tests for the WordPressDatabaseHandler class.
*
Expand Down
Loading
Loading