Skip to content

Commit

Permalink
Merge pull request #1374 from WordPress/fix/1340-auto-update-options
Browse files Browse the repository at this point in the history
Fix disabled options reappear in Site Health after external update
  • Loading branch information
mukeshpanchal27 authored Jul 26, 2024
2 parents 1216ead + ed61b7e commit 8212900
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 );
}

/**
Expand Down

0 comments on commit 8212900

Please sign in to comment.