Skip to content

Commit

Permalink
Ensure CSS transients disabling is not reset with each version update
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Mar 18, 2022
1 parent 35dda9d commit d87b78b
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 18 deletions.
6 changes: 0 additions & 6 deletions includes/options/class-amp-options-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,6 @@ public static function validate_options( $new_options ) {
}
}

if ( array_key_exists( Option::DISABLE_CSS_TRANSIENT_CACHING, $new_options ) && true === $new_options[ Option::DISABLE_CSS_TRANSIENT_CACHING ] ) {
$options[ Option::DISABLE_CSS_TRANSIENT_CACHING ] = true;
} else {
unset( $options[ Option::DISABLE_CSS_TRANSIENT_CACHING ] );
}

/**
* Filter the options being updated, so services can handle the sanitization and validation of
* their respective options.
Expand Down
49 changes: 42 additions & 7 deletions src/BackgroundTask/MonitorCssTransientCaching.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function __construct( BackgroundTaskDeactivator $background_task_deactiva
*/
public function register() {
add_action( 'amp_plugin_update', [ $this, 'handle_plugin_update' ] );
add_filter( 'amp_options_updating', [ $this, 'sanitize_disabled_option' ], 10, 2 );
parent::register();
}

Expand Down Expand Up @@ -184,6 +185,38 @@ public function disable_css_transient_caching() {
);
}

/**
* Sanitize the option.
*
* @param array $options Existing options.
* @param array $new_options New options.
* @return array Sanitized options.
*/
public function sanitize_disabled_option( $options, $new_options ) {
$value = null;

if ( array_key_exists( Option::DISABLE_CSS_TRANSIENT_CACHING, $new_options ) ) {
$unsanitized_value = $new_options[ Option::DISABLE_CSS_TRANSIENT_CACHING ];

if ( is_bool( $unsanitized_value ) ) {
$value = (bool) $unsanitized_value;
} elseif ( is_array( $unsanitized_value ) ) {
$value = [];
foreach ( wp_array_slice_assoc( $unsanitized_value, [ self::WP_VERSION, self::GUTENBERG_VERSION ] ) as $key => $version ) {
$value[ $key ] = preg_replace( '/[^a-z0-9_\-.]/', '', $version );
}
}
}

if ( empty( $value ) ) {
unset( $options[ Option::DISABLE_CSS_TRANSIENT_CACHING ] );
} else {
$options[ Option::DISABLE_CSS_TRANSIENT_CACHING ] = $value;
}

return $options;
}

/**
* Query the number of transients containing cache stylesheets.
*
Expand Down Expand Up @@ -225,13 +258,15 @@ public function handle_plugin_update( $old_version ) {
// Reset when it was disabled prior to the versions of WP/Gutenberg being captured,
// or if the captured versions were affected at the time of disabling.
(
empty( $gutenberg_version )
||
$this->block_uniqid_transformer->is_affected_gutenberg_version( $gutenberg_version )
||
empty( $wp_version )
||
$this->block_uniqid_transformer->is_affected_wordpress_version( $wp_version )
version_compare( strtok( $old_version, '-' ), '2.2.2', '<' )
&&
(
! is_array( $disabled )
||
$this->block_uniqid_transformer->is_affected_gutenberg_version( $gutenberg_version )
||
$this->block_uniqid_transformer->is_affected_wordpress_version( $wp_version )
)
)
) {
$this->enable_css_transient_caching();
Expand Down
100 changes: 95 additions & 5 deletions tests/php/src/BackgroundTask/MonitorCssTransientCachingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function test_register() {
$monitor = $this->injector->make( MonitorCssTransientCaching::class );
$monitor->register();
$this->assertEquals( 10, has_action( 'amp_plugin_update', [ $monitor, 'handle_plugin_update' ] ) );
$this->assertEquals( 10, has_filter( 'amp_options_updating', [ $monitor, 'sanitize_disabled_option' ] ) );
}

/**
Expand Down Expand Up @@ -201,6 +202,81 @@ public function test_enable_disable_is_css_transient_caching_disabled() {
$this->assertFalse( $monitor->is_css_transient_caching_disabled() );
}

/** @return array */
public function get_data_to_test_sanitize_disabled_option() {
return [
'true' => [
true,
true,
],
'false' => [
false,
null,
],
'number' => [
123,
null,
],
'bad_array' => [
[ 1, 2, 3 ],
null,
],
'partial_good_array' => [
[
MonitorCssTransientCaching::WP_VERSION => '123-\o/',
MonitorCssTransientCaching::GUTENBERG_VERSION => '45.21<script>//!@#$%^&*()`~',
'foo' => 'bar',
],
[
MonitorCssTransientCaching::WP_VERSION => '123-o',
MonitorCssTransientCaching::GUTENBERG_VERSION => '45.21script',
],
],
'partial_all_good' => [
[
MonitorCssTransientCaching::WP_VERSION => '1.2.3',
],
[
MonitorCssTransientCaching::WP_VERSION => '1.2.3',
],
],
'fully_all_good' => [
[
MonitorCssTransientCaching::WP_VERSION => '1.2.3',
MonitorCssTransientCaching::GUTENBERG_VERSION => '4.5.6',
],
[
MonitorCssTransientCaching::WP_VERSION => '1.2.3',
MonitorCssTransientCaching::GUTENBERG_VERSION => '4.5.6',
],
],
];
}

/**
* @covers ::sanitize_disabled_option()
* @dataProvider get_data_to_test_sanitize_disabled_option
* @param mixed $input
* @param mixed $expected
*/
public function test_sanitize_disabled_option( $input, $expected ) {
$monitor = $this->injector->make( MonitorCssTransientCaching::class );
$options = $monitor->sanitize_disabled_option(
[],
[
Option::DISABLE_CSS_TRANSIENT_CACHING => $input,
]
);

if ( array_key_exists( Option::DISABLE_CSS_TRANSIENT_CACHING, $options ) ) {
$actual = $options[ Option::DISABLE_CSS_TRANSIENT_CACHING ];
} else {
$actual = null;
}

$this->assertEquals( $expected, $actual );
}

/**
* @covers ::query_css_transient_count()
*/
Expand Down Expand Up @@ -247,23 +323,23 @@ public function test_query_css_transient_count() {
/** @return array */
public function get_data_to_test_handle_plugin_update() {
return [
'not_disabled' => [
'not_disabled' => [
function ( MonitorCssTransientCaching $monitor ) {
$monitor->enable_css_transient_caching();
$this->assertFalse( $monitor->is_css_transient_caching_disabled() );
$monitor->handle_plugin_update( '2.2.1' );
},
false,
],
'old_reset_condition_in_range' => [
'old_reset_condition_in_range' => [
function ( MonitorCssTransientCaching $monitor ) {
AMP_Options_Manager::update_option( Option::DISABLE_CSS_TRANSIENT_CACHING, true );
$this->assertTrue( $monitor->is_css_transient_caching_disabled() );
$monitor->handle_plugin_update( '1.5.1' );
},
false,
],
'old_reset_condition_outside_range' => [
'old_reset_condition_outside_range' => [
function ( MonitorCssTransientCaching $monitor ) {
AMP_Options_Manager::update_option(
Option::DISABLE_CSS_TRANSIENT_CACHING,
Expand All @@ -277,15 +353,15 @@ function ( MonitorCssTransientCaching $monitor ) {
},
true,
],
'uniqid_before_storing_meta' => [
'uniqid_before_storing_meta' => [
function ( MonitorCssTransientCaching $monitor ) {
AMP_Options_Manager::update_option( Option::DISABLE_CSS_TRANSIENT_CACHING, true );
$this->assertTrue( $monitor->is_css_transient_caching_disabled() );
$monitor->handle_plugin_update( '2.2.1' );
},
false,
],
'uniqid_after_storing_meta' => [
'uniqid_after_storing_meta' => [
function ( MonitorCssTransientCaching $monitor ) {
AMP_Options_Manager::update_option(
Option::DISABLE_CSS_TRANSIENT_CACHING,
Expand All @@ -299,6 +375,20 @@ function ( MonitorCssTransientCaching $monitor ) {
},
true,
],
'uniqid_after_storing_meta_and_future_update' => [
function ( MonitorCssTransientCaching $monitor ) {
AMP_Options_Manager::update_option(
Option::DISABLE_CSS_TRANSIENT_CACHING,
[
MonitorCssTransientCaching::WP_VERSION => '5.9.0',
MonitorCssTransientCaching::GUTENBERG_VERSION => null,
]
);
$this->assertTrue( $monitor->is_css_transient_caching_disabled() );
$monitor->handle_plugin_update( '2.3.0' );
},
true,
],
];
}

Expand Down

0 comments on commit d87b78b

Please sign in to comment.