Skip to content

Commit aafecfd

Browse files
authored
Merge pull request #476 from wpengine/chore-logging-plugin-default-configuration-on-activation
chore: Set default configuration for the logging plugin on activation
2 parents 5247aa3 + 8156902 commit aafecfd

File tree

5 files changed

+158
-31
lines changed

5 files changed

+158
-31
lines changed

.changeset/nice-clocks-shave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@wpengine/wpgraphql-logging-wordpress-plugin": patch
3+
---
4+
5+
chore: Add default configuration on plugin activation if no configuration already exists.

plugins/wpgraphql-logging/src/Admin/Settings/ConfigurationHelper.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ public function clear_cache(): void {
139139
* Get the option key for the settings.
140140
*/
141141
public function get_option_key(): string {
142-
return (string) apply_filters( 'wpgraphql_logging_settings_key', WPGRAPHQL_LOGGING_SETTINGS_KEY );
142+
return WPGRAPHQL_LOGGING_SETTINGS_KEY;
143143
}
144144

145145
/**
146146
* Get the settings group for caching.
147147
*/
148148
public function get_settings_group(): string {
149-
return (string) apply_filters( 'wpgraphql_logging_settings_group_settings_group', WPGRAPHQL_LOGGING_SETTINGS_GROUP );
149+
return WPGRAPHQL_LOGGING_SETTINGS_GROUP;
150150
}
151151

152152
/**
@@ -185,21 +185,26 @@ protected function load_config(): void {
185185

186186
$cache_duration = (int) apply_filters( 'wpgraphql_logging_config_cache_duration', self::CACHE_DURATION );
187187

188+
// Try to get from wp_cache first (in-memory cache).
188189
$cached_config = wp_cache_get( $option_key, self::CACHE_GROUP );
189190
if ( is_array( $cached_config ) ) {
190191
$this->config = $cached_config;
191192
return;
192193
}
193194

195+
// Try to get from the WordPress object cache (could be Redis, Memcached, etc.).
194196
$cached_config = wp_cache_get( $option_key, $this->get_settings_group() );
195197
if ( is_array( $cached_config ) ) {
196198
$this->config = $cached_config;
199+
// Store in our custom cache group for faster access next time.
197200
wp_cache_set( $option_key, $cached_config, self::CACHE_GROUP, $cache_duration );
198201
return;
199202
}
200203

204+
// Load from database.
201205
$this->config = $this->get_option_value( $option_key, [] );
202206

207+
// Cache the result in both cache groups.
203208
wp_cache_set( $option_key, $this->config, self::CACHE_GROUP, $cache_duration );
204209
wp_cache_set( $option_key, $this->config, $this->get_settings_group(), $cache_duration );
205210
}

plugins/wpgraphql-logging/src/Plugin.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
namespace WPGraphQL\Logging;
66

77
use WPGraphQL\Logging\Admin\Settings\ConfigurationHelper;
8+
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
9+
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\DataManagementTab;
810
use WPGraphQL\Logging\Admin\SettingsPage;
911
use WPGraphQL\Logging\Admin\ViewLogsPage;
1012
use WPGraphQL\Logging\Events\EventManager;
13+
use WPGraphQL\Logging\Events\Events;
1114
use WPGraphQL\Logging\Events\QueryEventLifecycle;
1215
use WPGraphQL\Logging\Logger\Api\LogServiceInterface;
1316
use WPGraphQL\Logging\Logger\Scheduler\DataDeletionScheduler;
@@ -117,6 +120,44 @@ public static function get_log_service(): LogServiceInterface {
117120
public static function activate(): void {
118121
$log_service = self::get_log_service();
119122
$log_service->activate();
123+
self::set_default_configuration();
124+
}
125+
126+
/**
127+
* Set the default configuration for the plugin on activation.
128+
*/
129+
public static function set_default_configuration(): void {
130+
$configuration = ConfigurationHelper::get_instance();
131+
$option_key = $configuration->get_option_key();
132+
$option_value = get_option( $option_key, [] );
133+
if ( ! empty( $option_value ) ) {
134+
return;
135+
}
136+
137+
$option_value = [
138+
BasicConfigurationTab::get_name() => [
139+
BasicConfigurationTab::ENABLED => true,
140+
BasicConfigurationTab::EXCLUDE_QUERY => '__schema,GetSeedNode', // Exclude introspection and GetSeedNode queries.
141+
BasicConfigurationTab::DATA_SAMPLING => '10',
142+
BasicConfigurationTab::EVENT_LOG_SELECTION => [
143+
Events::PRE_REQUEST,
144+
Events::BEFORE_GRAPHQL_EXECUTION,
145+
Events::BEFORE_RESPONSE_RETURNED,
146+
Events::REQUEST_DATA,
147+
Events::REQUEST_RESULTS,
148+
Events::RESPONSE_HEADERS_TO_SEND,
149+
],
150+
BasicConfigurationTab::LOG_RESPONSE => false,
151+
],
152+
DataManagementTab::get_name() => [
153+
DataManagementTab::DATA_DELETION_ENABLED => true,
154+
DataManagementTab::DATA_RETENTION_DAYS => 7,
155+
DataManagementTab::DATA_SANITIZATION_ENABLED => true,
156+
DataManagementTab::DATA_SANITIZATION_METHOD => 'recommended',
157+
],
158+
];
159+
160+
update_option( $option_key, $option_value );
120161
}
121162

122163
/**

plugins/wpgraphql-logging/tests/wpunit/Core/PluginTest.php

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
use lucatume\WPBrowser\TestCase\WPTestCase;
99
use ReflectionClass;
1010
use WPGraphQL\Logging\Events\EventManager;
11-
11+
use WPGraphQL\Logging\Logger\Database\WordPressDatabaseEntity;
12+
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
13+
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\DataManagementTab;
14+
use WPGraphQL\Logging\Events\Events;
15+
use WPGraphQL\Logging\Admin\Settings\ConfigurationHelper;
1216

1317
/**
1418
* Test for the Plugin
@@ -55,6 +59,76 @@ public function test_clone_method_throws_error() {
5559
$this->assertInstanceOf( Plugin::class, $clone );
5660
}
5761

62+
public function test_plugin_activate() {
63+
64+
// Delet configuration option
65+
$configuration = ConfigurationHelper::get_instance();
66+
$option_key = $configuration->get_option_key();
67+
delete_option( $option_key );
68+
69+
// Verify that the configuration option has been deleted
70+
$option_value = get_option( $option_key );
71+
$this->assertEmpty( $option_value );
72+
73+
74+
$plugin = Plugin::init();
75+
$plugin::activate();
76+
77+
// Verify that the datatbase has been created
78+
global $wpdb;
79+
$table_name = WordPressDatabaseEntity::get_table_name();
80+
$table_exists = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) );
81+
$this->assertNotEmpty( $table_exists );
82+
$this->assertEquals( $table_exists, $table_name );
83+
84+
// Verify that the default configuration has been set
85+
$option_value = $configuration->get_option_value( WPGRAPHQL_LOGGING_SETTINGS_KEY );
86+
$this->assertNotEmpty( $option_value );
87+
$default_configuration = [
88+
BasicConfigurationTab::get_name() => [
89+
BasicConfigurationTab::ENABLED => true,
90+
BasicConfigurationTab::EXCLUDE_QUERY => '__schema,GetSeedNode', // Exclude introspection and GetSeedNode queries.
91+
BasicConfigurationTab::DATA_SAMPLING => '10',
92+
BasicConfigurationTab::EVENT_LOG_SELECTION => [
93+
Events::PRE_REQUEST,
94+
Events::BEFORE_GRAPHQL_EXECUTION,
95+
Events::BEFORE_RESPONSE_RETURNED,
96+
Events::REQUEST_DATA,
97+
Events::REQUEST_RESULTS,
98+
Events::RESPONSE_HEADERS_TO_SEND,
99+
],
100+
BasicConfigurationTab::LOG_RESPONSE => false,
101+
],
102+
DataManagementTab::get_name() => [
103+
DataManagementTab::DATA_DELETION_ENABLED => true,
104+
DataManagementTab::DATA_RETENTION_DAYS => 7,
105+
DataManagementTab::DATA_SANITIZATION_ENABLED => true,
106+
DataManagementTab::DATA_SANITIZATION_METHOD => 'recommended',
107+
],
108+
];
109+
$this->assertEquals( $option_value, $default_configuration );
110+
}
111+
112+
public function test_plugin_activate_when_configuration_already_exists() {
113+
$configuration = ConfigurationHelper::get_instance();
114+
$option_key = $configuration->get_option_key();
115+
$default_configuration = [
116+
BasicConfigurationTab::get_name() => [
117+
BasicConfigurationTab::ENABLED => true,
118+
],
119+
];
120+
update_option( $option_key, $default_configuration );
121+
122+
123+
$plugin = Plugin::init();
124+
$plugin::activate();
125+
126+
// Verify that the default configuration has not been set
127+
$configuration = ConfigurationHelper::get_instance();
128+
$option_value = $configuration->get_option_value( WPGRAPHQL_LOGGING_SETTINGS_KEY );
129+
$this->assertEquals( $option_value, $default_configuration );
130+
}
131+
58132
public function test_wakeup_method_throws_error() {
59133
$reflection = new ReflectionClass( Plugin::class );
60134
$plugin = $reflection->newInstanceWithoutConstructor();

plugins/wpgraphql-logging/wpgraphql-logging.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,6 @@
4343
return;
4444
}
4545

46-
if ( file_exists( __DIR__ . '/activation.php' ) ) {
47-
require_once __DIR__ . '/activation.php';
48-
// @phpstan-ignore-next-line
49-
register_activation_hook( __FILE__, 'wpgraphql_logging_activation_callback' );
50-
}
51-
52-
if ( file_exists( __DIR__ . '/deactivation.php' ) ) {
53-
require_once __DIR__ . '/deactivation.php';
54-
// @phpstan-ignore-next-line
55-
register_deactivation_hook( __FILE__, 'wpgraphql_logging_deactivation_callback' );
56-
}
57-
58-
59-
// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
60-
// phpcs:enable SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
61-
62-
if ( ! function_exists( 'wpgraphql_logging_init' ) ) {
63-
/**
64-
* Initializes plugin.
65-
*/
66-
function wpgraphql_logging_init(): void {
67-
wpgraphql_logging_constants();
68-
wpgraphql_logging_plugin_init();
69-
wpgraphql_logging_plugin_admin_notice_correct_build();
70-
wpgraphql_logging_plugin_admin_notice_min_php_version();
71-
}
72-
}
73-
7446
if ( ! function_exists( 'wpgraphql_logging_constants' ) ) {
7547
/**
7648
* Define plugin constants.
@@ -99,6 +71,36 @@ function wpgraphql_logging_constants(): void {
9971
}
10072
}
10173

74+
// Define constants early - needed for activation/deactivation hooks.
75+
wpgraphql_logging_constants();
76+
77+
if ( file_exists( __DIR__ . '/activation.php' ) ) {
78+
require_once __DIR__ . '/activation.php';
79+
// @phpstan-ignore-next-line
80+
register_activation_hook( __FILE__, 'wpgraphql_logging_activation_callback' );
81+
}
82+
83+
if ( file_exists( __DIR__ . '/deactivation.php' ) ) {
84+
require_once __DIR__ . '/deactivation.php';
85+
// @phpstan-ignore-next-line
86+
register_deactivation_hook( __FILE__, 'wpgraphql_logging_deactivation_callback' );
87+
}
88+
89+
90+
// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
91+
// phpcs:enable SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
92+
93+
if ( ! function_exists( 'wpgraphql_logging_init' ) ) {
94+
/**
95+
* Initializes plugin.
96+
*/
97+
function wpgraphql_logging_init(): void {
98+
wpgraphql_logging_plugin_init();
99+
wpgraphql_logging_plugin_admin_notice_correct_build();
100+
wpgraphql_logging_plugin_admin_notice_min_php_version();
101+
}
102+
}
103+
102104
if ( ! function_exists( 'wpgraphql_logging_plugin_init' ) ) {
103105
/**
104106
* Initialize the WPGraphQL Logging plugin.

0 commit comments

Comments
 (0)