diff --git a/src/Admin/Settings.php b/src/Admin/Settings.php index ecb0dbde..1a406d17 100644 --- a/src/Admin/Settings.php +++ b/src/Admin/Settings.php @@ -87,6 +87,11 @@ public static function graphql_endpoint() { * @return void */ public function init() { + + // Filter the graphql_query_analyzer setting to be on if WPGraphQL Smart Cache is active + add_filter( 'graphql_setting_field_config', [ $this, 'filter_graphql_query_analyzer_enabled_field' ], 10, 3 ); + add_filter( 'graphql_get_setting_section_field_value', [ $this, 'filter_graphql_query_analyzer_enabled_value' ], 10, 5 ); + // Add to the wp-graphql admin settings page add_action( 'graphql_register_settings', @@ -300,4 +305,48 @@ function () { ); } + /** + * Filter the config for the query_analyzer_enabled setting + * + * @param array $field_config The field config for the setting + * @param string $field_name The name of the field (unfilterable in the config) + * @param string $section The slug of the section the field is registered to + * + * @return mixed + */ + public function filter_graphql_query_analyzer_enabled_field( $field_config, $field_name, $section ) { + if ( 'query_analyzer_enabled' !== $field_name || 'graphql_general_settings' !== $section ) { + return $field_config; + } + + $field_config['value'] = 'on'; + $field_config['disabled'] = true; + $field_config['default'] = 'on'; + + if ( ! \WPGraphQL::debug() ) { + $field_config['desc'] = $field_config['desc'] . ' (' . __( 'Force enabled by WPGraphQL Smart Cache to properly support cache tagging and invalidation.', 'wp-graphql-smart-cache' ) . ')'; + } + + return $field_config; + } + + /** + * Filter the value of the query_analyzer_enabled setting + * + * @param mixed $value The value of the field + * @param mixed $default_value The default value if there is no value set + * @param string $option_name The name of the option + * @param array $section_fields The setting values within the section + * @param string $section_name The name of the section the setting belongs to + * + * @return mixed|string + */ + public function filter_graphql_query_analyzer_enabled_value( $value, $default_value, string $option_name, $section_fields, $section_name ) { + if ( 'query_analyzer_enabled' !== $option_name ) { + return $value; + } + + // graphql_query_analyzer needs to be on for WPGraphQL Smart Cache to properly tag and invalidate caches + return 'on'; + } } diff --git a/tests/wpunit/AdminSettingsCacheTest.php b/tests/wpunit/AdminSettingsCacheTest.php index a9fa6e1d..9a73dd23 100644 --- a/tests/wpunit/AdminSettingsCacheTest.php +++ b/tests/wpunit/AdminSettingsCacheTest.php @@ -36,4 +36,19 @@ public function testCacheSettingsOn() { add_option( 'graphql_cache_section', [ 'cache_toggle' => 'on' ] ); $this->assertTrue( Settings::caching_enabled() ); } + + public function testQueryAnalyzerSettingIsForcedOn() { + + // disable debug mode + add_filter( 'graphql_debug_enabled', '__return_false' ); + + // assert that debug mode is off + $this->assertFalse( \WPGraphQL::debug() ); + + // update the setting to disable query analyzer + update_option( 'graphql_general_settings', [ 'query_analyzer_enabled', 'off' ] ); + + // assert that the query analyzer is still enabled, even though the setting is turned off + $this->assertTrue( \WPGraphQL\Utils\QueryAnalyzer::is_enabled() ); + } }