diff --git a/plugins/performance-lab/includes/site-health/audit-autoloaded-options/hooks.php b/plugins/performance-lab/includes/site-health/audit-autoloaded-options/hooks.php index 95f9036752..d1acf58e2e 100644 --- a/plugins/performance-lab/includes/site-health/audit-autoloaded-options/hooks.php +++ b/plugins/performance-lab/includes/site-health/audit-autoloaded-options/hooks.php @@ -129,3 +129,25 @@ function perflab_aao_extend_core_check( string $description ): string { return $description . perflab_aao_get_autoloaded_options_table() . perflab_aao_get_disabled_autoloaded_options_table(); } add_filter( 'site_status_autoloaded_options_limit_description', 'perflab_aao_extend_core_check' ); + +/** + * Filters the list of disabled options to exclude options that are autoloaded. + * + * This filter modifies the 'option_perflab_aao_disabled_options' to ensure + * that autoloaded options are not included in the disabled options list. + * + * @since n.e.x.t + * + * @param string[]|mixed $disabled_options Array of disabled options. + * @return string[] Filtered array of disabled options excluding autoloaded options. + */ +function perflab_filter_option_perflab_aao_disabled_options( $disabled_options ): array { + $autoload_option_names = wp_list_pluck( perflab_aao_query_autoloaded_options(), 'option_name' ); + return array_filter( + (array) $disabled_options, + static function ( $option ) use ( $autoload_option_names ): bool { + return ! in_array( $option, $autoload_option_names, true ); + } + ); +} +add_filter( 'option_perflab_aao_disabled_options', 'perflab_filter_option_perflab_aao_disabled_options' ); diff --git a/plugins/performance-lab/tests/includes/site-health/audit-autoloaded-options/test-audit-autoloaded-options.php b/plugins/performance-lab/tests/includes/site-health/audit-autoloaded-options/test-audit-autoloaded-options.php index 99c69f84f8..8c5a557a15 100644 --- a/plugins/performance-lab/tests/includes/site-health/audit-autoloaded-options/test-audit-autoloaded-options.php +++ b/plugins/performance-lab/tests/includes/site-health/audit-autoloaded-options/test-audit-autoloaded-options.php @@ -121,12 +121,7 @@ public function test_perflab_aao_autoloaded_options_disable_revert_functionality wp_set_current_user( $user_id ); // Mock wp_redirect to avoid actual redirection. - add_filter( - 'wp_redirect', - static function () { - return false; - } - ); + add_filter( 'wp_redirect', '__return_false' ); // Add an autoload option with small size length value for testing. $test_option_string = 'test'; @@ -177,14 +172,48 @@ static function () { // Test that the reverted autoloaded option is displayed in the autoloaded options perflab_aao_get_autoloaded_options_table(). $table_html = perflab_aao_get_autoloaded_options_table(); $this->assertStringContainsString( self::AUTOLOADED_OPTION_KEY, $table_html ); + } - // Remove the mock filter. - remove_filter( - 'wp_redirect', - static function () { - return false; - } - ); + /** + * Test that the list of disabled options excludes options that are autoloaded. + * + * @covers ::perflab_filter_option_perflab_aao_disabled_options + */ + public function test_perflab_aao_autoloaded_options_auto_enable_functionality(): void { + + $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); + wp_set_current_user( $user_id ); + + // Mock wp_redirect to avoid actual redirection. + add_filter( 'wp_redirect', '__return_false' ); + + // Add an autoload option with bigger size length value for testing. + self::set_autoloaded_option( self::WARNING_AUTOLOADED_SIZE_LIMIT_IN_BYTES ); + + $table_html = perflab_aao_get_autoloaded_options_table(); + $this->assertStringContainsString( self::AUTOLOADED_OPTION_KEY, $table_html ); + + // Check disable autoloaded option functionality. + $_REQUEST['_wpnonce'] = wp_create_nonce( 'perflab_aao_update_autoload' ); + $_GET['action'] = 'perflab_aao_update_autoload'; + $_GET['option_name'] = self::AUTOLOADED_OPTION_KEY; + $_GET['autoload'] = 'false'; + + perflab_aao_handle_update_autoload(); + + // Test that the autoloaded option is not displayed in the perflab_aao_get_autoloaded_options_table() after disabling. + $table_html = perflab_aao_get_autoloaded_options_table(); + $this->assertStringNotContainsString( self::AUTOLOADED_OPTION_KEY, $table_html ); + + // The option already exists, so update it. + update_option( self::AUTOLOADED_OPTION_KEY, wp_generate_password( self::WARNING_AUTOLOADED_SIZE_LIMIT_IN_BYTES ), 'yes' ); + + $table_html = perflab_aao_get_autoloaded_options_table(); + $this->assertStringContainsString( self::AUTOLOADED_OPTION_KEY, $table_html ); + + // Test that the disabled autoloaded option is displayed in the disabled options perflab_aao_get_disabled_autoloaded_options_table(). + $table_html = perflab_aao_get_disabled_autoloaded_options_table(); + $this->assertStringNotContainsString( self::AUTOLOADED_OPTION_KEY, $table_html ); } /**