diff --git a/inc/Engine/Common/PerformanceHints/Admin/Controller.php b/inc/Engine/Common/PerformanceHints/Admin/Controller.php index a0f674ced6..44eed120d9 100644 --- a/inc/Engine/Common/PerformanceHints/Admin/Controller.php +++ b/inc/Engine/Common/PerformanceHints/Admin/Controller.php @@ -205,4 +205,26 @@ private function delete_by_url( string $url ) { $factory->queries()->delete_by_url( untrailingslashit( $url ) ); } } + + /** + * Clean performance hints when changing the logo. + * + * @param mixed $value The new value of the theme modification. + * @param mixed $old_value The old value of the theme modification. + * + * @return mixed + */ + public function clean_on_logo_change( $value, $old_value ) { + if ( ! $this->is_allowed() ) { + return $value; + } + + if ( $value === $old_value ) { + return $value; + } + + $this->truncate_tables(); + + return $value; + } } diff --git a/inc/Engine/Common/PerformanceHints/Admin/Subscriber.php b/inc/Engine/Common/PerformanceHints/Admin/Subscriber.php index 2c6449c9a3..56a3cb60aa 100644 --- a/inc/Engine/Common/PerformanceHints/Admin/Subscriber.php +++ b/inc/Engine/Common/PerformanceHints/Admin/Subscriber.php @@ -79,6 +79,7 @@ public static function get_subscribed_events(): array { 'rocket_dashboard_actions' => 'display_dashboard_button', 'admin_post_rocket_clean_performance_hints' => 'clean_performance_hints', 'admin_post_rocket_clean_performance_hints_url' => 'clean_url_performance_hints', + 'pre_set_theme_mod_custom_logo' => [ 'clean_performance_hints_on_logo_change', 10, 2 ], ]; } @@ -202,4 +203,16 @@ public function clean_url_performance_hints(): void { public function clean_performance_hint_result(): void { $this->notices->clean_performance_hint_result(); } + + /** + * Clean performance hints when changing the logo. + * + * @param mixed $value The new value of the theme modification. + * @param mixed $old_value The old value of the theme modification. + * + * @return mixed + */ + public function clean_performance_hints_on_logo_change( $value, $old_value ) { + return $this->controller->clean_on_logo_change( $value, $old_value ); + } } diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Admin/Controller/CleanOnLogoChangeTest.php b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Admin/Controller/CleanOnLogoChangeTest.php new file mode 100644 index 0000000000..599a878375 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Admin/Controller/CleanOnLogoChangeTest.php @@ -0,0 +1,37 @@ + [ + 'config' => [ + 'filter' => false, + 'value' => 123, + 'old_value' => 123, + ], + 'expected' => [ + 'truncate' => false, + 'return' => 123, + ], + ], + 'testShouldNotTruncateWhenLogoNotChanged' => [ + 'config' => [ + 'filter' => true, + 'value' => 123, + 'old_value' => 123, + ], + 'expected' => [ + 'truncate' => false, + 'return' => 123, + ], + ], + 'testShouldTruncateWhenLogoChanged' => [ + 'config' => [ + 'filter' => true, + 'value' => 456, + 'old_value' => 123, + ], + 'expected' => [ + 'truncate' => true, + 'return' => 456, + ], + ], +]; diff --git a/tests/Unit/inc/Engine/Common/PerformanceHints/Admin/Controller/CleanOnLogoChangeTest.php b/tests/Unit/inc/Engine/Common/PerformanceHints/Admin/Controller/CleanOnLogoChangeTest.php new file mode 100644 index 0000000000..4233e852be --- /dev/null +++ b/tests/Unit/inc/Engine/Common/PerformanceHints/Admin/Controller/CleanOnLogoChangeTest.php @@ -0,0 +1,70 @@ +queries = $this->createMock( AboveTheFold::class ); + $this->table = $this->createMock( ATFTable::class ); + + $atf_factory = $this->createMock( ATFFactory::class ); + $atf_factory->method( 'queries' )->willReturn( $this->queries ); + $atf_factory->method( 'table' )->willReturn( $this->table ); + $this->context = $this->createMock(ContextInterface::class); + $atf_factory->method('get_context')->willReturn($this->context); + + $this->factories = [ $atf_factory ]; + } + + /** + * @dataProvider configTestData + */ + public function testShouldDoExpected( $config, $expected ) { + $controller = new Controller( ! $config['filter'] ? [] : $this->factories ); + + if ( ! empty( $config['filter'] ) ) { + $this->context->expects( $this->once() ) + ->method('is_allowed') + ->willReturn(true); + } + + if ( $expected['truncate'] ) { + $this->queries->expects( $this->once() ) + ->method( 'get_not_completed_count' ) + ->willReturn( 0 ); + + $this->table->expects( $this->once() ) + ->method( 'truncate' ); + } else { + $this->queries->expects( $this->never() ) + ->method( 'get_not_completed_count' ); + + $this->table->expects( $this->never() ) + ->method( 'truncate' ); + } + + $result = $controller->clean_on_logo_change( $config['value'], $config['old_value'] ); + + $this->assertSame( $expected['return'], $result ); + } +}