Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: force enable query analyzer #270

Merged
merged 4 commits into from
Feb 7, 2024
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
49 changes: 49 additions & 0 deletions src/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -300,4 +305,48 @@ function () {
);
}

/**
* Filter the config for the query_analyzer_enabled setting
*
* @param array<string,mixed> $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'] . ' (<strong>' . __( 'Force enabled by WPGraphQL Smart Cache to properly support cache tagging and invalidation.', 'wp-graphql-smart-cache' ) . '</strong>)';
}

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<string,mixed> $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';
}
}
15 changes: 15 additions & 0 deletions tests/wpunit/AdminSettingsCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}
jasonbahl marked this conversation as resolved.
Show resolved Hide resolved
}
Loading