Skip to content

Commit

Permalink
Merge commit '6783335548d69c46f80a8e67a4807fee8b84162c' into release/…
Browse files Browse the repository at this point in the history
…v1.3.0
  • Loading branch information
jasonbahl committed Feb 7, 2024
2 parents c877325 + 6783335 commit 80b108d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
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() );
}
}

0 comments on commit 80b108d

Please sign in to comment.