Skip to content

Commit 99ae298

Browse files
committed
Small refactor of plugin class. Added integration tests for configuration helper.
1 parent 50fe55b commit 99ae298

File tree

4 files changed

+29
-16
lines changed

4 files changed

+29
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public function get_option_value( string $option_key, mixed $default_value = nul
165165
* Hook into WordPress to clear cache when settings are updated.
166166
* This should be called during plugin initialization.
167167
*/
168-
public static function init_cache_hooks(): void {
168+
public static function register_cache_hooks(): void {
169169
$instance = self::get_instance();
170170
$option_key = $instance->get_option_key();
171171

plugins/wpgraphql-logging/src/Admin/ViewLogsPage.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,6 @@ public function register_settings_page(): void {
9292
'dashicons-list-view',
9393
25
9494
);
95-
add_submenu_page(
96-
'graphiql-ide',
97-
esc_html__( 'GraphQL Logs', 'wpgraphql-logging' ),
98-
esc_html__( 'GraphQL Logs', 'wpgraphql-logging' ),
99-
'manage_options',
100-
self::ADMIN_PAGE_SLUG,
101-
[ $this, 'render_admin_page' ]
102-
);
10395

10496
// Updates the list table when filters are applied.
10597
add_action( 'load-' . $this->page_hook, [ $this, 'process_page_actions_before_rendering' ], 10, 0 );

plugins/wpgraphql-logging/src/Plugin.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,17 @@ public static function init(): self {
5454
}
5555

5656
/**
57-
* Initialize the plugin admin, frontend & api functionality.
57+
* Initialize various components of the plugin.
5858
*/
5959
public function setup(): void {
60-
ConfigurationHelper::init_cache_hooks();
61-
SettingsPage::init();
62-
ViewLogsPage::init();
63-
QueryEventLifecycle::init();
64-
DataDeletionScheduler::init();
60+
QueryEventLifecycle::init(); // Event lifecycle for capturing logs.
61+
DataDeletionScheduler::init(); // Data deletion scheduler.
62+
63+
if ( is_admin() ) {
64+
ConfigurationHelper::register_cache_hooks(); // Register cache hooks.
65+
SettingsPage::init(); // Settings page.
66+
ViewLogsPage::init(); // View logs page.
67+
}
6568

6669
do_action( 'wpgraphql_logging_plugin_setup', self::$instance );
6770
}
@@ -122,7 +125,6 @@ public static function activate(): void {
122125
* @since 0.0.1
123126
*/
124127
public static function deactivate(): void {
125-
126128
DataDeletionScheduler::clear_scheduled_deletion();
127129
$log_service = self::get_log_service();
128130
$log_service->deactivate();

plugins/wpgraphql-logging/tests/wpunit/Admin/Settings/ConfigurationHelperTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,23 @@ public function test_get_is_enabled(): void {
117117
$this->assertFalse($instance->is_enabled('data_management', 'enabled'));
118118
}
119119

120+
public function test_register_cache_hooks(): void {
121+
$instance = ConfigurationHelper::get_instance();
122+
123+
124+
// Set initial configuration
125+
$configuration = ['enabled' => true];
126+
update_option($instance->get_option_key(), $configuration);
127+
128+
// Register cache hooks.
129+
ConfigurationHelper::register_cache_hooks();
130+
131+
$this->assertEquals(10, has_action("update_option_{$instance->get_option_key()}", [$instance, 'clear_cache']));
132+
$this->assertEquals(10, has_action("add_option_{$instance->get_option_key()}", [$instance, 'clear_cache']));
133+
$this->assertEquals(10, has_action("delete_option_{$instance->get_option_key()}", [$instance, 'clear_cache']));
134+
135+
// Test that the cache is cleared when the option is updated.
136+
update_option($instance->get_option_key(), ['enabled' => false]);
137+
$this->assertEquals(['enabled' => false], $instance->get_config());
138+
}
120139
}

0 commit comments

Comments
 (0)