From 52c362daad5d499d0a215e0128431927ea18839d Mon Sep 17 00:00:00 2001 From: Khadreal Date: Thu, 19 Dec 2024 13:25:54 +0100 Subject: [PATCH 1/7] :construction: Add termly compatibility subscriber class :closes: #6863 --- inc/ThirdParty/Plugins/Cookie/Termly.php | 56 ++++++++++++++++++++++++ inc/ThirdParty/ServiceProvider.php | 3 ++ 2 files changed, 59 insertions(+) create mode 100644 inc/ThirdParty/Plugins/Cookie/Termly.php diff --git a/inc/ThirdParty/Plugins/Cookie/Termly.php b/inc/ThirdParty/Plugins/Cookie/Termly.php new file mode 100644 index 0000000000..d436f67de6 --- /dev/null +++ b/inc/ThirdParty/Plugins/Cookie/Termly.php @@ -0,0 +1,56 @@ + 'exclude_defer_js', + 'rocket_delay_js_exclusions' => 'exclude_defer_js', + ]; + } + + /** + * Check if Termly auto blocker is on. + * + * @return bool + */ + private function should_exclude(): bool { + $auto_block = get_option( 'termly_display_auto_blocker', 'off' ); + if ( 'on' !== $auto_block ) { + return false; + } + + return true; + } + + /** + * Excludes Termly JS files from delay JS + * + * @param array $exclude_delay_js Array of JS to be excluded. + * + * @return array + */ + public function exclude_defer_js( array $exclude_delay_js ): array { + $exclude_delay_js[] = 'app.termly.io/resource-blocker/(.*)'; + + return $exclude_delay_js; + } +} diff --git a/inc/ThirdParty/ServiceProvider.php b/inc/ThirdParty/ServiceProvider.php index 25cdc783e6..3b763f1e9a 100644 --- a/inc/ThirdParty/ServiceProvider.php +++ b/inc/ThirdParty/ServiceProvider.php @@ -11,6 +11,7 @@ use WP_Rocket\Subscriber\Third_Party\Plugins\SyntaxHighlighter_Subscriber; use WP_Rocket\ThirdParty\Plugins\Ads\Adthrive; use WP_Rocket\ThirdParty\Plugins\ConvertPlug; +use WP_Rocket\ThirdParty\Plugins\Cookie\Termly; use WP_Rocket\ThirdParty\Plugins\Ecommerce\BigCommerce; use WP_Rocket\ThirdParty\Plugins\Ecommerce\WooCommerceSubscriber; use WP_Rocket\ThirdParty\Plugins\I18n\TranslatePress; @@ -98,6 +99,7 @@ class ServiceProvider extends AbstractServiceProvider { 'wpgeotargeting', 'weglot', 'contactform7', + 'termly_subscriber', ]; /** @@ -227,5 +229,6 @@ public function register(): void { $this->getContainer()->addShared( 'translatepress', TranslatePress::class ); $this->getContainer()->addShared( 'wpgeotargeting', WPGeotargeting::class ); $this->getContainer()->addShared( 'contactform7', ContactForm7::class ); + $this->getContainer()->addShared( 'termly_subscriber', Termly::class ); } } From 0a1722d2f18287529656ba3713abcb4fd64f2e64 Mon Sep 17 00:00:00 2001 From: Khadreal Date: Thu, 19 Dec 2024 15:14:11 +0100 Subject: [PATCH 2/7] Add integration test :sparkles: --- inc/ThirdParty/Plugins/Cookie/Termly.php | 8 +++-- .../Plugins/Cookie/excludeDelayJs.php | 13 ++++++++ .../Plugins/Cookie/excludeDelayJs.php | 31 +++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 tests/Fixtures/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php create mode 100644 tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php diff --git a/inc/ThirdParty/Plugins/Cookie/Termly.php b/inc/ThirdParty/Plugins/Cookie/Termly.php index d436f67de6..79e1ca783a 100644 --- a/inc/ThirdParty/Plugins/Cookie/Termly.php +++ b/inc/ThirdParty/Plugins/Cookie/Termly.php @@ -15,9 +15,7 @@ class Termly implements Subscriber_Interface { * @return array */ public static function get_subscribed_events() { - $termly_version = defined( 'TERMLY_VERSION' ); - - if ( ! $termly_version ) { + if ( ! defined( 'TERMLY_VERSION' ) ) { return []; } @@ -49,6 +47,10 @@ private function should_exclude(): bool { * @return array */ public function exclude_defer_js( array $exclude_delay_js ): array { + if ( ! $this->should_exclude() ) { + return $exclude_delay_js; + } + $exclude_delay_js[] = 'app.termly.io/resource-blocker/(.*)'; return $exclude_delay_js; diff --git a/tests/Fixtures/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php b/tests/Fixtures/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php new file mode 100644 index 0000000000..3ff6f30526 --- /dev/null +++ b/tests/Fixtures/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php @@ -0,0 +1,13 @@ + [ + 'config' => [ + 'excluded' => [], + 'termly_display_auto_blocker' => 'on' + ], + 'excluded' => [ + 'app.termly.io/resource-blocker/(.*)', + ] + ] +]; diff --git a/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php b/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php new file mode 100644 index 0000000000..f283cad7a9 --- /dev/null +++ b/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php @@ -0,0 +1,31 @@ +once() + ->andReturn( $config['termly_display_auto_blocker'] ); + + $this->assertSame( $expected, $termly->exclude_defer_js( $config['excluded'] ) ); + } +} From ea8d595cb5a7d879b65504b613cb02809ec5b208 Mon Sep 17 00:00:00 2001 From: Khadreal Date: Fri, 20 Dec 2024 13:33:14 +0100 Subject: [PATCH 3/7] Add more test and modify logic --- inc/ThirdParty/Plugins/Cookie/Termly.php | 25 ++------ .../inc/ThirdParty/Plugins/Cookie/deferJs.php | 61 +++++++++++++++++++ .../inc/ThirdParty/Plugins/Cookie/deferJs.php | 61 +++++++++++++++++++ .../Plugins/Cookie/excludeDelayJs.php | 2 +- 4 files changed, 129 insertions(+), 20 deletions(-) create mode 100644 tests/Fixtures/inc/ThirdParty/Plugins/Cookie/deferJs.php create mode 100644 tests/Integration/inc/ThirdParty/Plugins/Cookie/deferJs.php diff --git a/inc/ThirdParty/Plugins/Cookie/Termly.php b/inc/ThirdParty/Plugins/Cookie/Termly.php index 79e1ca783a..809b0dbbcd 100644 --- a/inc/ThirdParty/Plugins/Cookie/Termly.php +++ b/inc/ThirdParty/Plugins/Cookie/Termly.php @@ -20,34 +20,21 @@ public static function get_subscribed_events() { } return [ - 'rocket_exclude_defer_js' => 'exclude_defer_js', - 'rocket_delay_js_exclusions' => 'exclude_defer_js', + 'rocket_exclude_defer_js' => 'exclude_termly_defer_and_delay_js', + 'rocket_delay_js_exclusions' => 'exclude_termly_defer_and_delay_js', ]; } /** - * Check if Termly auto blocker is on. - * - * @return bool - */ - private function should_exclude(): bool { - $auto_block = get_option( 'termly_display_auto_blocker', 'off' ); - if ( 'on' !== $auto_block ) { - return false; - } - - return true; - } - - /** - * Excludes Termly JS files from delay JS + * Defer and delay Termly Resources * * @param array $exclude_delay_js Array of JS to be excluded. * * @return array */ - public function exclude_defer_js( array $exclude_delay_js ): array { - if ( ! $this->should_exclude() ) { + public function exclude_termly_defer_and_delay_js( array $exclude_delay_js ): array { + $auto_block = get_option( 'termly_display_auto_blocker', 'off' ); + if ( 'on' !== $auto_block ) { return $exclude_delay_js; } diff --git a/tests/Fixtures/inc/ThirdParty/Plugins/Cookie/deferJs.php b/tests/Fixtures/inc/ThirdParty/Plugins/Cookie/deferJs.php new file mode 100644 index 0000000000..9aeb7873e1 --- /dev/null +++ b/tests/Fixtures/inc/ThirdParty/Plugins/Cookie/deferJs.php @@ -0,0 +1,61 @@ + + + +HTML +; + +$expected = << + + +HTML +; + +$expected_exclusion = << + + +HTML +; + +$exclusions_list = (object) [ + 'defer_js_external_exclusions' => [ + 'gist.github.com', + '/api/scripts/lb_cs.js', + ], +]; + +return [ + 'testShouldReturnUpdatedHTML' => [ + 'config' => [ + 'donotrocketoptimize' => false, + 'post_meta' => false, + 'options' => [ + 'defer_all_js' => 1, + 'exclude_defer_js' => [], + ], + 'exclusions' => $exclusions_list, + ], + 'html' => $html, + 'expected' => $expected, + ], + + 'testShouldEvaluateRegexPatternInOptions' => [ + 'config' => [ + 'donotrocketoptimize' => false, + 'post_meta' => false, + 'options' => [ + 'defer_all_js' => 1, + 'exclude_defer_js' => [ + 'app.termly.io/resource-blocker/(.*)', + ], + ], + 'exclusions' => $exclusions_list, + ], + 'html' => $html, + 'expected' => $expected_exclusion, + ], +]; diff --git a/tests/Integration/inc/ThirdParty/Plugins/Cookie/deferJs.php b/tests/Integration/inc/ThirdParty/Plugins/Cookie/deferJs.php new file mode 100644 index 0000000000..283d19fdff --- /dev/null +++ b/tests/Integration/inc/ThirdParty/Plugins/Cookie/deferJs.php @@ -0,0 +1,61 @@ +exclude_defer_js = $config['options']['exclude_defer_js']; + $this->defer_js = $config['options']['defer_all_js']; + + add_filter( 'pre_get_rocket_option_defer_all_js', [ $this, 'set_defer_js' ] ); + add_filter( 'pre_get_rocket_option_exclude_defer_js', [ $this, 'set_exclude_defer_js' ] ); + + set_transient( 'wpr_dynamic_lists', $config['exclusions'], HOUR_IN_SECONDS ); + + $this->assertSame( + $expected, + apply_filters( 'rocket_buffer', $html ) + ); + } + + public function set_defer_js() { + return $this->defer_js; + } + + public function set_exclude_defer_js() { + return $this->exclude_defer_js; + } +} diff --git a/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php b/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php index f283cad7a9..dde5df4d70 100644 --- a/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php +++ b/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php @@ -26,6 +26,6 @@ public function testShouldReturnExpected( $config, $expected ) { ->once() ->andReturn( $config['termly_display_auto_blocker'] ); - $this->assertSame( $expected, $termly->exclude_defer_js( $config['excluded'] ) ); + $this->assertSame( $expected, $termly->exclude_termly_defer_and_delay_js( $config['excluded'] ) ); } } From 73fa805f7e17c7ffff92bb4a2cfffb30e804980e Mon Sep 17 00:00:00 2001 From: Khadreal Date: Fri, 20 Dec 2024 13:44:55 +0100 Subject: [PATCH 4/7] Fixed lint error --- tests/Integration/inc/ThirdParty/Plugins/Cookie/deferJs.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Integration/inc/ThirdParty/Plugins/Cookie/deferJs.php b/tests/Integration/inc/ThirdParty/Plugins/Cookie/deferJs.php index 283d19fdff..5969aa10a6 100644 --- a/tests/Integration/inc/ThirdParty/Plugins/Cookie/deferJs.php +++ b/tests/Integration/inc/ThirdParty/Plugins/Cookie/deferJs.php @@ -10,7 +10,7 @@ * @group Termly */ class Test_deferJs extends TestCase { - use ContentTrait; + private $defer_js; private $exclude_defer_js; @@ -47,7 +47,7 @@ public function testShouldReturnExpected( $config, $html, $expected ) { $this->assertSame( $expected, - apply_filters( 'rocket_buffer', $html ) + wpm_apply_filters_typed('string', 'rocket_buffer', $html ) ); } From 3a86bd5962015ef4d72824001f53ea2a847764a3 Mon Sep 17 00:00:00 2001 From: Khadreal Date: Fri, 20 Dec 2024 14:42:42 +0100 Subject: [PATCH 5/7] Improve test coverage --- .../Plugins/Cookie/excludeDelayJs.php | 9 ++++- .../Plugins/Cookie/excludeDelayJs.php | 36 +++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/tests/Fixtures/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php b/tests/Fixtures/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php index 3ff6f30526..638aa72089 100644 --- a/tests/Fixtures/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php +++ b/tests/Fixtures/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php @@ -1,12 +1,19 @@ [ + 'config' => [ + 'excluded' => [], + 'termly_display_auto_blocker' => 'off' + ], + 'expected' => [] + ], 'shouldExcludeTermlyResources' => [ 'config' => [ 'excluded' => [], 'termly_display_auto_blocker' => 'on' ], - 'excluded' => [ + 'expected' => [ 'app.termly.io/resource-blocker/(.*)', ] ] diff --git a/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php b/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php index dde5df4d70..95b6041c04 100644 --- a/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php +++ b/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php @@ -14,18 +14,48 @@ */ class Test_ExcludeDelayJs extends TestCase { + private $event; + private $subscriber; + protected $path_to_test_data = '/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php'; + public function set_up() { + parent::set_up(); + + if ( ! defined( 'TERMLY_VERSION' ) ) { + define( 'TERMLY_VERSION', '1.0' ); + } + + $container = wpm_apply_filters_typed( 'string', 'rocket_container', '' ); + + $this->event = $container->get( 'event_manager' ); + } + + public function tear_down() { + $this->event->remove_subscriber( $this->subscriber ); + + parent::tear_down(); + } + /** * @dataProvider configTestData */ public function testShouldReturnExpected( $config, $expected ) { - $termly = new Termly(); + $this->subscriber = new Termly(); + $this->event->add_subscriber( $this->subscriber ); Functions\expect( 'get_option' ) - ->once() + ->twice() ->andReturn( $config['termly_display_auto_blocker'] ); - $this->assertSame( $expected, $termly->exclude_termly_defer_and_delay_js( $config['excluded'] ) ); + $this->assertSame( + $expected, + wpm_apply_filters_typed( 'array', 'rocket_delay_js_exclusions', $config['excluded'] ) + ); + + $this->assertSame( + $expected, + wpm_apply_filters_typed( 'array', 'rocket_exclude_defer_js', $config['excluded'] ) + ); } } From 3dbd2aa0e4a22b9eb7079ecd650aaf85bb31b704 Mon Sep 17 00:00:00 2001 From: Khadreal Date: Fri, 20 Dec 2024 14:50:58 +0100 Subject: [PATCH 6/7] Fix filter type --- .../inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php b/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php index 95b6041c04..a10bbe9559 100644 --- a/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php +++ b/tests/Integration/inc/ThirdParty/Plugins/Cookie/excludeDelayJs.php @@ -26,7 +26,7 @@ public function set_up() { define( 'TERMLY_VERSION', '1.0' ); } - $container = wpm_apply_filters_typed( 'string', 'rocket_container', '' ); + $container = wpm_apply_filters_typed('object', 'rocket_container', '' ); $this->event = $container->get( 'event_manager' ); } From 7290425c592a0eada048d51de17dbd5e51ce4c30 Mon Sep 17 00:00:00 2001 From: Khadreal Date: Tue, 24 Dec 2024 14:45:33 +0100 Subject: [PATCH 7/7] Add subscriber to common subscriber --- inc/Plugin.php | 1 + 1 file changed, 1 insertion(+) diff --git a/inc/Plugin.php b/inc/Plugin.php index c9733eec93..99de360f09 100644 --- a/inc/Plugin.php +++ b/inc/Plugin.php @@ -401,6 +401,7 @@ private function init_common_subscribers() { 'performance_hints_admin_subscriber', 'lrc_frontend_subscriber', 'taxonomy_subscriber', + 'termly_subscriber', ]; $host_type = HostResolver::get_host_service();