Skip to content

Commit d291424

Browse files
committed
Set default config on activation.
Adds logic to set a default configuration when the plugin is activated, ensuring initial settings are present. Refactors ConfigurationHelper to remove unnecessary filters. Updates tests to verify default configuration is set on activation. Also ensures plugin constants are defined before activation/deactivation hooks are registered.
1 parent 5247aa3 commit d291424

File tree

4 files changed

+123
-31
lines changed

4 files changed

+123
-31
lines changed

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: 45 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,46 @@ public function test_clone_method_throws_error() {
5559
$this->assertInstanceOf( Plugin::class, $clone );
5660
}
5761

62+
public function test_plugin_activate() {
63+
$plugin = Plugin::init();
64+
$plugin::activate();
65+
66+
// Verify that the datatbase has been created
67+
global $wpdb;
68+
$table_name = WordPressDatabaseEntity::get_table_name();
69+
$table_exists = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) );
70+
$this->assertNotEmpty( $table_exists );
71+
$this->assertEquals( $table_exists, $table_name );
72+
73+
// Verify that the default configuration has been set
74+
$configuration = ConfigurationHelper::get_instance();
75+
$option_value = $configuration->get_option_value( WPGRAPHQL_LOGGING_SETTINGS_KEY );
76+
$this->assertNotEmpty( $option_value );
77+
$default_configuration = [
78+
BasicConfigurationTab::get_name() => [
79+
BasicConfigurationTab::ENABLED => true,
80+
BasicConfigurationTab::EXCLUDE_QUERY => '__schema,GetSeedNode', // Exclude introspection and GetSeedNode queries.
81+
BasicConfigurationTab::DATA_SAMPLING => '10',
82+
BasicConfigurationTab::EVENT_LOG_SELECTION => [
83+
Events::PRE_REQUEST,
84+
Events::BEFORE_GRAPHQL_EXECUTION,
85+
Events::BEFORE_RESPONSE_RETURNED,
86+
Events::REQUEST_DATA,
87+
Events::REQUEST_RESULTS,
88+
Events::RESPONSE_HEADERS_TO_SEND,
89+
],
90+
BasicConfigurationTab::LOG_RESPONSE => false,
91+
],
92+
DataManagementTab::get_name() => [
93+
DataManagementTab::DATA_DELETION_ENABLED => true,
94+
DataManagementTab::DATA_RETENTION_DAYS => 7,
95+
DataManagementTab::DATA_SANITIZATION_ENABLED => true,
96+
DataManagementTab::DATA_SANITIZATION_METHOD => 'recommended',
97+
],
98+
];
99+
$this->assertEquals( $option_value, $default_configuration );
100+
}
101+
58102
public function test_wakeup_method_throws_error() {
59103
$reflection = new ReflectionClass( Plugin::class );
60104
$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)