Skip to content

Commit 69c15b0

Browse files
committed
feat: connect logging settings with QueryEventLifecycle
1 parent 8dfeeef commit 69c15b0

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

plugins/wpgraphql-logging/bin/local/setup-docker-env.sh

100644100755
File mode changed.

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

Whitespace-only changes.

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use WPGraphQL\Logging\Logger\LoggerService;
1010
use WPGraphQL\Request;
1111
use WPGraphQL\WPSchema;
12+
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\Basic_Configuration_Tab;
1213

1314
/**
1415
* WPGraphQL Query Event Lifecycle.
@@ -39,6 +40,8 @@ class QueryEventLifecycle {
3940
*/
4041
protected function __construct( LoggerService $logger ) {
4142
$this->logger = $logger;
43+
$full_config = get_option( WPGRAPHQL_LOGGING_SETTINGS_KEY, [] );
44+
$this->config = $full_config['basic_configuration'] ?? [];
4245
}
4346

4447
/**
@@ -54,6 +57,40 @@ public static function init(): QueryEventLifecycle {
5457
return self::$instance;
5558
}
5659

60+
/**
61+
* Checks if logging is enabled based on user settings.
62+
*/
63+
protected function is_logging_enabled(): bool {
64+
// Check the main "Enabled" checkbox first.
65+
$is_enabled = $this->config[ Basic_Configuration_Tab::ENABLED ] ?? false;
66+
if ( ! $is_enabled ) {
67+
return false;
68+
}
69+
70+
// Check if the current user is an admin if that option is enabled.
71+
$log_for_admin = $this->config[ Basic_Configuration_Tab::ADMIN_USER_LOGGING ] ?? false;
72+
if ( $log_for_admin && ! current_user_can( 'manage_options' ) ) {
73+
return false;
74+
}
75+
76+
// Check for IP restrictions.
77+
$ip_restrictions = $this->config[ Basic_Configuration_Tab::IP_RESTRICTIONS ] ?? '';
78+
if ( ! empty( $ip_restrictions ) ) {
79+
$allowed_ips = array_map( 'trim', explode( ',', $ip_restrictions ) );
80+
if ( ! in_array( $_SERVER['REMOTE_ADDR'], $allowed_ips, true ) ) {
81+
return false;
82+
}
83+
}
84+
85+
// Check the data sampling rate.
86+
$sampling_rate = (int) ( $this->config[ Basic_Configuration_Tab::DATA_SAMPLING ] ?? 100 );
87+
if ( mt_rand( 0, 100 ) >= $sampling_rate ) {
88+
return false;
89+
}
90+
91+
return true;
92+
}
93+
5794
/**
5895
* Initial Incoming Request.
5996
*
@@ -65,6 +102,10 @@ public static function init(): QueryEventLifecycle {
65102
*/
66103
public function log_pre_request( string $query, ?string $operation_name, ?array $variables ): void {
67104
try {
105+
if ( ! $this->is_logging_enabled() ) {
106+
return;
107+
}
108+
68109
$context = [
69110
'query' => $query,
70111
'variables' => $variables,
@@ -102,6 +143,10 @@ public function log_pre_request( string $query, ?string $operation_name, ?array
102143
*/
103144
public function log_graphql_before_execute(Request $request ): void {
104145
try {
146+
if ( ! $this->is_logging_enabled() ) {
147+
return;
148+
}
149+
105150
/** @var \GraphQL\Server\OperationParams $params */
106151
$params = $request->params;
107152
$context = [
@@ -149,6 +194,9 @@ public function log_graphql_before_execute(Request $request ): void {
149194
*/
150195
public function log_before_response_returned(array|ExecutionResult $filtered_response, array|ExecutionResult $response, WPSchema $schema, ?string $operation, string $query, ?array $variables, Request $request, ?string $query_id): void {
151196
try {
197+
if ( ! $this->is_logging_enabled() ) {
198+
return;
199+
}
152200
$context = [
153201
'response' => $response,
154202
'schema' => $schema,

0 commit comments

Comments
 (0)