From fca34111b4378255d0af2cfca00adc99cb50a5dc Mon Sep 17 00:00:00 2001 From: Khadreal Date: Wed, 22 Jan 2025 18:33:14 +0100 Subject: [PATCH 1/5] Add check for donotrocketoptimise constant --:closes: #7029 --- .../Optimization/LazyRenderContent/Frontend/Controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php b/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php index e7533f587d..679db78429 100644 --- a/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php +++ b/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php @@ -136,7 +136,7 @@ public function add_hashes_when_allowed( $html ) { * @return string */ public function add_hashes( $html ) { - if ( empty( $html ) ) { + if ( empty( $html ) || rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) { return $html; } From fef9b207e5b9530dc731131eab3979dd67b3b130 Mon Sep 17 00:00:00 2001 From: Khadreal Date: Thu, 23 Jan 2025 05:58:56 +0100 Subject: [PATCH 2/5] Add test -- #7029 --- .../Frontend/Subscriber/add_hashes.php | 15 +++++++++++++++ .../Frontend/Subscriber/add_hashes.php | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php index 7ba4a5967f..fb771790ab 100644 --- a/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php +++ b/tests/Fixtures/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php @@ -80,5 +80,20 @@ 'html' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/expected_exclusions.php' ), ] ], + 'shouldNotAddHashesWhenDonotoptimize' => [ + 'config' => [ + 'donotrocketoptimize' => true, + 'row' => [ + 'url' => 'http://example.org/', + 'is_mobile' => 0, + 'below_the_fold' => json_encode([]), + 'status' => 'completed' + ], + 'html' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/original.php' ), + ], + 'expected' => [ + 'html' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/html/original.php' ), + ] + ], ] ]; diff --git a/tests/Integration/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php b/tests/Integration/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php index 699cf7caf5..c016bae208 100644 --- a/tests/Integration/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php +++ b/tests/Integration/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php @@ -9,7 +9,7 @@ * * @group PerformanceHints */ -class Test_AddHashes extends TestCase { +class Test_Add_Hashes extends TestCase { private $max_hashes; public static function set_up_before_class() { @@ -46,6 +46,7 @@ public function tear_down() { */ public function testShouldWorkAsExpected( $config, $expected ) { self::addLrc( $config['row'] ); + $this->donotrocketoptimize = $config['donotrocketoptimize'] ?? null; add_filter( 'rocket_lrc_optimization', '__return_true' ); add_filter( 'rocket_lrc_exclusions', function() use ($config) { From 2466bd4e84166a04705c015744032872379cbe34 Mon Sep 17 00:00:00 2001 From: Khadreal Date: Thu, 23 Jan 2025 06:03:21 +0100 Subject: [PATCH 3/5] Revert test name --- .../LazyRenderContent/Frontend/Subscriber/add_hashes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php b/tests/Integration/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php index c016bae208..43110d44c6 100644 --- a/tests/Integration/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php +++ b/tests/Integration/inc/Engine/Optimization/LazyRenderContent/Frontend/Subscriber/add_hashes.php @@ -9,7 +9,7 @@ * * @group PerformanceHints */ -class Test_Add_Hashes extends TestCase { +class Test_AddHashes extends TestCase { private $max_hashes; public static function set_up_before_class() { From 0306bd634b1b1a151561171776f39ba4dd550f67 Mon Sep 17 00:00:00 2001 From: Khadreal Date: Tue, 28 Jan 2025 16:33:49 +0100 Subject: [PATCH 4/5] :feat: Add tests and modify for logic for warm up process --- .../PerformanceHints/WarmUp/Controller.php | 3 ++- .../WarmUp/Subscriber/warmUpHome.php | 16 ++++++++++++++++ .../WarmUp/Subscriber/warmUpHome.php | 8 ++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/inc/Engine/Common/PerformanceHints/WarmUp/Controller.php b/inc/Engine/Common/PerformanceHints/WarmUp/Controller.php index ce2a7bc762..61671d8c1d 100644 --- a/inc/Engine/Common/PerformanceHints/WarmUp/Controller.php +++ b/inc/Engine/Common/PerformanceHints/WarmUp/Controller.php @@ -69,7 +69,8 @@ private function is_allowed(): bool { return ! ( 'local' === wp_get_environment_type() || $this->user->is_license_expired_grace_period() || - (bool) $this->options->get( 'remove_unused_css', 0 ) + (bool) $this->options->get( 'remove_unused_css', 0 ) || + rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ); } diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUpHome.php b/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUpHome.php index 1c18725374..a1614f44b1 100644 --- a/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUpHome.php +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUpHome.php @@ -7,6 +7,7 @@ 'factories' => [1], 'license_expired' => false, 'home_url' => 'http://example.com/', + 'rocket_optimise' => false, ], 'expected' => 1, ], @@ -17,6 +18,7 @@ 'factories' => [1], 'license_expired' => true, 'home_url' => 'http://example.com/', + 'rocket_optimise' => false, ], 'expected' => 0, ], @@ -27,6 +29,7 @@ 'factories' => [1], 'license_expired' => false, 'home_url' => 'http://example.com/', + 'rocket_optimise' => false, ], 'expected' => 0, ], @@ -37,6 +40,7 @@ 'license_expired' => false, 'factories' => [1], 'home_url' => 'http://example.com/', + 'rocket_optimise' => false, ], 'expected' => 0, ], @@ -47,6 +51,18 @@ 'license_expired' => false, 'factories' => [], 'home_url' => 'http://example.com/', + 'rocket_optimise' => true, + ], + 'expected' => 0, + ], + 'testShouldNotCallSendToSaasWhenDonotRocketOptimiseIsTrue' => [ + 'config' => [ + 'wp_env' => 'production', + 'remove_unused_css' => 0, + 'factories' => [1], + 'license_expired' => false, + 'home_url' => 'http://example.com/', + 'rocket_optimise' => true, ], 'expected' => 0, ], diff --git a/tests/Integration/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUpHome.php b/tests/Integration/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUpHome.php index df9703bc1c..4c71899a98 100644 --- a/tests/Integration/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUpHome.php +++ b/tests/Integration/inc/Engine/Common/PerformanceHints/WarmUp/Subscriber/warmUpHome.php @@ -35,6 +35,14 @@ public function testShouldDoExpected( $config, $expected ) { Functions\expect( 'wp_get_environment_type' )->andReturn($config['wp_env']); + Functions\when('rocket_get_constant')->alias(function ($name, $default = null) use ($config) { + if('DONOTROCKETOPTIMIZE' === $name) { + return $config['rocket_optimise']; + } + + return $default; + }); + $queue->shouldReceive('add_job_warmup') ->times($expected); From f856eed076fc2388e977b04f69ae8e3e0db1b903 Mon Sep 17 00:00:00 2001 From: Khadreal Date: Thu, 30 Jan 2025 12:34:05 +0100 Subject: [PATCH 5/5] fixed regression with wpr_lazyrendercontent get parameter --- .../Optimization/LazyRenderContent/Frontend/Controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php b/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php index 679db78429..8d8cdfaca2 100644 --- a/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php +++ b/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php @@ -136,7 +136,7 @@ public function add_hashes_when_allowed( $html ) { * @return string */ public function add_hashes( $html ) { - if ( empty( $html ) || rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) { + if ( empty( $html ) || ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) && ! isset( $_GET['wpr_lazyrendercontent'] ) ) ) {// phpcs:ignore WordPress.Security.NonceVerification.Recommended return $html; }