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
5 changes: 5 additions & 0 deletions .changeset/nice-clocks-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wpengine/wpgraphql-logging-wordpress-plugin": patch
---

chore: Add default configuration on plugin activation if no configuration already exists.
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ public function clear_cache(): void {
* Get the option key for the settings.
*/
public function get_option_key(): string {
return (string) apply_filters( 'wpgraphql_logging_settings_key', WPGRAPHQL_LOGGING_SETTINGS_KEY );
return WPGRAPHQL_LOGGING_SETTINGS_KEY;
}

/**
* Get the settings group for caching.
*/
public function get_settings_group(): string {
return (string) apply_filters( 'wpgraphql_logging_settings_group_settings_group', WPGRAPHQL_LOGGING_SETTINGS_GROUP );
return WPGRAPHQL_LOGGING_SETTINGS_GROUP;
}

/**
Expand Down Expand Up @@ -185,21 +185,26 @@ protected function load_config(): void {

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

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

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

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

// Cache the result in both cache groups.
wp_cache_set( $option_key, $this->config, self::CACHE_GROUP, $cache_duration );
wp_cache_set( $option_key, $this->config, $this->get_settings_group(), $cache_duration );
}
Expand Down
41 changes: 41 additions & 0 deletions plugins/wpgraphql-logging/src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
namespace WPGraphQL\Logging;

use WPGraphQL\Logging\Admin\Settings\ConfigurationHelper;
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\DataManagementTab;
use WPGraphQL\Logging\Admin\SettingsPage;
use WPGraphQL\Logging\Admin\ViewLogsPage;
use WPGraphQL\Logging\Events\EventManager;
use WPGraphQL\Logging\Events\Events;
use WPGraphQL\Logging\Events\QueryEventLifecycle;
use WPGraphQL\Logging\Logger\Api\LogServiceInterface;
use WPGraphQL\Logging\Logger\Scheduler\DataDeletionScheduler;
Expand Down Expand Up @@ -117,6 +120,44 @@ public static function get_log_service(): LogServiceInterface {
public static function activate(): void {
$log_service = self::get_log_service();
$log_service->activate();
self::set_default_configuration();
}

/**
* Set the default configuration for the plugin on activation.
*/
public static function set_default_configuration(): void {
$configuration = ConfigurationHelper::get_instance();
$option_key = $configuration->get_option_key();
$option_value = get_option( $option_key, [] );
if ( ! empty( $option_value ) ) {
return;
}

$option_value = [
BasicConfigurationTab::get_name() => [
BasicConfigurationTab::ENABLED => true,
BasicConfigurationTab::EXCLUDE_QUERY => '__schema,GetSeedNode', // Exclude introspection and GetSeedNode queries.
BasicConfigurationTab::DATA_SAMPLING => '10',
BasicConfigurationTab::EVENT_LOG_SELECTION => [
Events::PRE_REQUEST,
Events::BEFORE_GRAPHQL_EXECUTION,
Events::BEFORE_RESPONSE_RETURNED,
Events::REQUEST_DATA,
Events::REQUEST_RESULTS,
Events::RESPONSE_HEADERS_TO_SEND,
],
BasicConfigurationTab::LOG_RESPONSE => false,
],
DataManagementTab::get_name() => [
DataManagementTab::DATA_DELETION_ENABLED => true,
DataManagementTab::DATA_RETENTION_DAYS => 7,
DataManagementTab::DATA_SANITIZATION_ENABLED => true,
DataManagementTab::DATA_SANITIZATION_METHOD => 'recommended',
],
];

update_option( $option_key, $option_value );
}

/**
Expand Down
76 changes: 75 additions & 1 deletion plugins/wpgraphql-logging/tests/wpunit/Core/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
use lucatume\WPBrowser\TestCase\WPTestCase;
use ReflectionClass;
use WPGraphQL\Logging\Events\EventManager;

use WPGraphQL\Logging\Logger\Database\WordPressDatabaseEntity;
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\DataManagementTab;
use WPGraphQL\Logging\Events\Events;
use WPGraphQL\Logging\Admin\Settings\ConfigurationHelper;

/**
* Test for the Plugin
Expand Down Expand Up @@ -55,6 +59,76 @@ public function test_clone_method_throws_error() {
$this->assertInstanceOf( Plugin::class, $clone );
}

public function test_plugin_activate() {

// Delet configuration option
$configuration = ConfigurationHelper::get_instance();
$option_key = $configuration->get_option_key();
delete_option( $option_key );

// Verify that the configuration option has been deleted
$option_value = get_option( $option_key );
$this->assertEmpty( $option_value );


$plugin = Plugin::init();
$plugin::activate();

// Verify that the datatbase has been created
global $wpdb;
$table_name = WordPressDatabaseEntity::get_table_name();
$table_exists = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) );
$this->assertNotEmpty( $table_exists );
$this->assertEquals( $table_exists, $table_name );

// Verify that the default configuration has been set
$option_value = $configuration->get_option_value( WPGRAPHQL_LOGGING_SETTINGS_KEY );
$this->assertNotEmpty( $option_value );
$default_configuration = [
BasicConfigurationTab::get_name() => [
BasicConfigurationTab::ENABLED => true,
BasicConfigurationTab::EXCLUDE_QUERY => '__schema,GetSeedNode', // Exclude introspection and GetSeedNode queries.
BasicConfigurationTab::DATA_SAMPLING => '10',
BasicConfigurationTab::EVENT_LOG_SELECTION => [
Events::PRE_REQUEST,
Events::BEFORE_GRAPHQL_EXECUTION,
Events::BEFORE_RESPONSE_RETURNED,
Events::REQUEST_DATA,
Events::REQUEST_RESULTS,
Events::RESPONSE_HEADERS_TO_SEND,
],
BasicConfigurationTab::LOG_RESPONSE => false,
],
DataManagementTab::get_name() => [
DataManagementTab::DATA_DELETION_ENABLED => true,
DataManagementTab::DATA_RETENTION_DAYS => 7,
DataManagementTab::DATA_SANITIZATION_ENABLED => true,
DataManagementTab::DATA_SANITIZATION_METHOD => 'recommended',
],
];
$this->assertEquals( $option_value, $default_configuration );
}

public function test_plugin_activate_when_configuration_already_exists() {
$configuration = ConfigurationHelper::get_instance();
$option_key = $configuration->get_option_key();
$default_configuration = [
BasicConfigurationTab::get_name() => [
BasicConfigurationTab::ENABLED => true,
],
];
update_option( $option_key, $default_configuration );


$plugin = Plugin::init();
$plugin::activate();

// Verify that the default configuration has not been set
$configuration = ConfigurationHelper::get_instance();
$option_value = $configuration->get_option_value( WPGRAPHQL_LOGGING_SETTINGS_KEY );
$this->assertEquals( $option_value, $default_configuration );
}

public function test_wakeup_method_throws_error() {
$reflection = new ReflectionClass( Plugin::class );
$plugin = $reflection->newInstanceWithoutConstructor();
Expand Down
58 changes: 30 additions & 28 deletions plugins/wpgraphql-logging/wpgraphql-logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,6 @@
return;
}

if ( file_exists( __DIR__ . '/activation.php' ) ) {
require_once __DIR__ . '/activation.php';
// @phpstan-ignore-next-line
register_activation_hook( __FILE__, 'wpgraphql_logging_activation_callback' );
}

if ( file_exists( __DIR__ . '/deactivation.php' ) ) {
require_once __DIR__ . '/deactivation.php';
// @phpstan-ignore-next-line
register_deactivation_hook( __FILE__, 'wpgraphql_logging_deactivation_callback' );
}


// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
// phpcs:enable SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh

if ( ! function_exists( 'wpgraphql_logging_init' ) ) {
/**
* Initializes plugin.
*/
function wpgraphql_logging_init(): void {
wpgraphql_logging_constants();
wpgraphql_logging_plugin_init();
wpgraphql_logging_plugin_admin_notice_correct_build();
wpgraphql_logging_plugin_admin_notice_min_php_version();
}
}

if ( ! function_exists( 'wpgraphql_logging_constants' ) ) {
/**
* Define plugin constants.
Expand Down Expand Up @@ -99,6 +71,36 @@ function wpgraphql_logging_constants(): void {
}
}

// Define constants early - needed for activation/deactivation hooks.
wpgraphql_logging_constants();

if ( file_exists( __DIR__ . '/activation.php' ) ) {
require_once __DIR__ . '/activation.php';
// @phpstan-ignore-next-line
register_activation_hook( __FILE__, 'wpgraphql_logging_activation_callback' );
}

if ( file_exists( __DIR__ . '/deactivation.php' ) ) {
require_once __DIR__ . '/deactivation.php';
// @phpstan-ignore-next-line
register_deactivation_hook( __FILE__, 'wpgraphql_logging_deactivation_callback' );
}


// phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
// phpcs:enable SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh

if ( ! function_exists( 'wpgraphql_logging_init' ) ) {
/**
* Initializes plugin.
*/
function wpgraphql_logging_init(): void {
wpgraphql_logging_plugin_init();
wpgraphql_logging_plugin_admin_notice_correct_build();
wpgraphql_logging_plugin_admin_notice_min_php_version();
}
}

if ( ! function_exists( 'wpgraphql_logging_plugin_init' ) ) {
/**
* Initialize the WPGraphQL Logging plugin.
Expand Down
Loading