From da6f96c1677aa29a6be082077dd21654d94561d1 Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Mon, 26 Jun 2023 20:27:56 +0600 Subject: [PATCH 01/18] Place Ad Blocking Recovery tags. --- .../Core/Tags/Guards/WP_Query_404_Guard.php | 34 ++++++ includes/Modules/AdSense.php | 21 ++++ .../Ad_Blocking_Recovery_Tag_Guard.php | 37 ++++++ .../AdSense/Ad_Blocking_Recovery_Web_Tag.php | 114 ++++++++++++++++++ includes/Modules/AdSense/Tag_Guard.php | 6 +- 5 files changed, 207 insertions(+), 5 deletions(-) create mode 100644 includes/Core/Tags/Guards/WP_Query_404_Guard.php create mode 100644 includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php create mode 100644 includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php diff --git a/includes/Core/Tags/Guards/WP_Query_404_Guard.php b/includes/Core/Tags/Guards/WP_Query_404_Guard.php new file mode 100644 index 00000000000..a4b6c90c0c0 --- /dev/null +++ b/includes/Core/Tags/Guards/WP_Query_404_Guard.php @@ -0,0 +1,34 @@ +is_tag_blocked() ) { $tag->use_guard( new Tag_Verify_Guard( $this->context->input() ) ); + $tag->use_guard( new WP_Query_404_Guard( $this->context->input() ) ); $tag->use_guard( new Tag_Guard( $module_settings ) ); $tag->use_guard( new Auto_Ad_Guard( $module_settings ) ); $tag->use_guard( new Tag_Environment_Type_Guard() ); @@ -797,6 +801,23 @@ private function register_tag() { $tag->register(); } } + + if ( Feature_Flags::enabled( 'adBlockerDetection' ) && ! $this->context->is_amp() ) { + $ad_blocking_recovery_web_tag = new Ad_Blocking_Recovery_Web_Tag( $settings['accountID'], self::MODULE_SLUG ); + + if ( $ad_blocking_recovery_web_tag->is_tag_blocked() ) { + return; + } + + $ad_blocking_recovery_web_tag->use_guard( new Tag_Verify_Guard( $this->context->input() ) ); + $ad_blocking_recovery_web_tag->use_guard( new WP_Query_404_Guard( $this->context->input() ) ); + $ad_blocking_recovery_web_tag->use_guard( new Ad_Blocking_Recovery_Tag_Guard( $module_settings ) ); + $ad_blocking_recovery_web_tag->use_guard( new Tag_Environment_Type_Guard() ); + + if ( $ad_blocking_recovery_web_tag->can_register() ) { + $ad_blocking_recovery_web_tag->register(); + } + } } /** diff --git a/includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php b/includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php new file mode 100644 index 00000000000..9429a25d5de --- /dev/null +++ b/includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php @@ -0,0 +1,37 @@ +settings->get(); + + return ! empty( $settings['adBlockingRecoverySetupStatus'] ) && $settings['useAdBlockerDetectionSnippet']; + } + +} diff --git a/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php b/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php new file mode 100644 index 00000000000..170f2e46cbc --- /dev/null +++ b/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php @@ -0,0 +1,114 @@ +get_method_proxy_once( 'render' ) ); + + add_filter( + 'wp_resource_hints', + $this->get_dns_prefetch_hints_callback( '//fundingchoicesmessages.google.com' ), + 10, + 2 + ); + + $this->do_init_tag_action(); + } + + /** + * Outputs the AdSense script tag. + * + * @since n.e.x.t + */ + protected function render() { + + $ep_kses_allowed_html_tags = array( + 'script' => array( + 'async' => true, + 'src' => true, + 'nonce' => true, + ), + ); + + $tags = $this->ad_blocking_recovery_tag->get(); + + if ( empty( $tags['tag'] ) || empty( $option['error_protection_code'] ) ) { + return; + } + + printf( "\n\n", esc_html__( 'Google AdSense snippet added by Site Kit', 'google-site-kit' ) ); + wp_kses( $tags['tag'], $ep_kses_allowed_html_tags ); + if ( $this->use_error_protection_snippet ) { + wp_kses( $tags['error_protection_code'], $ep_kses_allowed_html_tags ); + } + printf( "\n\n", esc_html__( 'End Google AdSense snippet added by Site Kit', 'google-site-kit' ) ); + } + + /** + * Sets the Ad_Blocking_Recovery_Tag instance. + * + * @since n.e.x.t + * + * @param Ad_Blocking_Recovery_Tag $ad_blocking_recovery_tag Ad_Blocking_Recovery_Tag instance. + */ + public function set_ad_blocking_recovery_tag( Ad_Blocking_Recovery_Tag $ad_blocking_recovery_tag ) { + $this->ad_blocking_recovery_tag = $ad_blocking_recovery_tag; + } + + /** + * Sets Use Error Protection Snippet. + * + * @since n.e.x.t + * + * @param bool $use_error_protection_snippet Whether to use the Ad Blocking Recovery Error Protection Snippet. + */ + public function set_use_error_snippet( $use_error_protection_snippet ) { + $this->use_error_protection_snippet = $use_error_protection_snippet; + } + +} diff --git a/includes/Modules/AdSense/Tag_Guard.php b/includes/Modules/AdSense/Tag_Guard.php index 477e26bcb21..338fc668283 100644 --- a/includes/Modules/AdSense/Tag_Guard.php +++ b/includes/Modules/AdSense/Tag_Guard.php @@ -26,15 +26,11 @@ class Tag_Guard extends Module_Tag_Guard { * * @since 1.24.0 * @since 1.30.0 Update to return FALSE on 404 pages deliberately. + * @since n.e.x.t Update to remove the check for 404 pages. * * @return bool|WP_Error TRUE if guarded tag can be activated, otherwise FALSE or an error. */ public function can_activate() { - // Do not allow AdSense tags on 404 pages. - if ( is_404() ) { - return false; - } - $settings = $this->settings->get(); // For web stories, the tag must only be rendered if a story-specific ad unit is provided. From b407deadecc7f80ebef79efcd5ed0a81ec209b98 Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Tue, 27 Jun 2023 17:38:01 +0600 Subject: [PATCH 02/18] Remove failing test. --- .../Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php | 1 - includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php | 1 - .../phpunit/integration/Modules/AdSense/Tag_GuardTest.php | 8 -------- 3 files changed, 10 deletions(-) diff --git a/includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php b/includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php index 9429a25d5de..a2cd4ea2e5e 100644 --- a/includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php +++ b/includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php @@ -33,5 +33,4 @@ public function can_activate() { return ! empty( $settings['adBlockingRecoverySetupStatus'] ) && $settings['useAdBlockerDetectionSnippet']; } - } diff --git a/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php b/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php index 170f2e46cbc..2a3c5815ea0 100644 --- a/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php +++ b/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php @@ -110,5 +110,4 @@ public function set_ad_blocking_recovery_tag( Ad_Blocking_Recovery_Tag $ad_block public function set_use_error_snippet( $use_error_protection_snippet ) { $this->use_error_protection_snippet = $use_error_protection_snippet; } - } diff --git a/tests/phpunit/integration/Modules/AdSense/Tag_GuardTest.php b/tests/phpunit/integration/Modules/AdSense/Tag_GuardTest.php index f9eaa0156db..6102db9fb15 100644 --- a/tests/phpunit/integration/Modules/AdSense/Tag_GuardTest.php +++ b/tests/phpunit/integration/Modules/AdSense/Tag_GuardTest.php @@ -64,12 +64,4 @@ public function test_cant_activate_when_clientid_is_invalid() { $this->settings->merge( array( 'clientID' => '' ) ); $this->assertFalse( $this->guard->can_activate(), 'Should return FALSE when clientID is empty.' ); } - - public function test_cant_activate_on_404() { - $this->go_to( '/?p=123456789' ); - - $this->assertQueryTrue( 'is_404' ); - $this->assertFalse( $this->guard->can_activate(), 'Should return FALSE when the current page doesnt exist (is_404).' ); - } - } From 4157afaef2507d16d3e2387dbab2e634a56ac64d Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Tue, 27 Jun 2023 18:03:01 +0600 Subject: [PATCH 03/18] Update `render` method. --- .../AdSense/Ad_Blocking_Recovery_Web_Tag.php | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php b/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php index 2a3c5815ea0..40436571b2f 100644 --- a/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php +++ b/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php @@ -66,27 +66,20 @@ public function register() { * @since n.e.x.t */ protected function render() { - - $ep_kses_allowed_html_tags = array( - 'script' => array( - 'async' => true, - 'src' => true, - 'nonce' => true, - ), - ); - $tags = $this->ad_blocking_recovery_tag->get(); if ( empty( $tags['tag'] ) || empty( $option['error_protection_code'] ) ) { return; } - printf( "\n\n", esc_html__( 'Google AdSense snippet added by Site Kit', 'google-site-kit' ) ); - wp_kses( $tags['tag'], $ep_kses_allowed_html_tags ); + printf( "\n\n", esc_html__( 'Google AdSense Ad Blocking Recovery snippet added by Site Kit', 'google-site-kit' ) ); + echo $tags['tag']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + printf( "\n\n", esc_html__( 'End Google AdSense Ad Blocking Recovery snippet added by Site Kit', 'google-site-kit' ) ); if ( $this->use_error_protection_snippet ) { - wp_kses( $tags['error_protection_code'], $ep_kses_allowed_html_tags ); + printf( "\n\n", esc_html__( 'Google AdSense Ad Blocking Recovery Error Protection snippet added by Site Kit', 'google-site-kit' ) ); + echo $tags['error_protection_code']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + printf( "\n\n", esc_html__( 'End Google AdSense Ad Blocking Recovery Error Protection snippet added by Site Kit', 'google-site-kit' ) ); } - printf( "\n\n", esc_html__( 'End Google AdSense snippet added by Site Kit', 'google-site-kit' ) ); } /** From bfdaf247e5102cc6fa0410bb7cdb3851dfe8673b Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Tue, 27 Jun 2023 23:24:55 +0600 Subject: [PATCH 04/18] Add test for `WP_Query_404_Guard`. --- .../Tags/Guards/WP_Query_404_GuardTest.php | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/phpunit/integration/Core/Tags/Guards/WP_Query_404_GuardTest.php diff --git a/tests/phpunit/integration/Core/Tags/Guards/WP_Query_404_GuardTest.php b/tests/phpunit/integration/Core/Tags/Guards/WP_Query_404_GuardTest.php new file mode 100644 index 00000000000..27b91e03619 --- /dev/null +++ b/tests/phpunit/integration/Core/Tags/Guards/WP_Query_404_GuardTest.php @@ -0,0 +1,47 @@ +guard = new WP_Query_404_Guard(); + } + + public function test_can_activate() { + $this->go_to( '/' ); + + $this->assertQueryTrue( 'is_home', 'is_front_page' ); + $this->assertTrue( $this->guard->can_activate(), 'Should return TRUE when the current page exists (is_home).' ); + } + + public function test_cant_activate_on_404() { + $this->go_to( '/?p=123456789' ); + + $this->assertQueryTrue( 'is_404' ); + $this->assertFalse( $this->guard->can_activate(), 'Should return FALSE when the current page doesnt exist (is_404).' ); + } + +} From e788bde667113b3281bb84808fdbfef34821e7a0 Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Tue, 27 Jun 2023 23:43:31 +0600 Subject: [PATCH 05/18] Add test for `Ad_Blocking_Recovery_Tag_Guard`. --- .../Ad_Blocking_Recovery_Tag_GuardTest.php | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Tag_GuardTest.php diff --git a/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Tag_GuardTest.php b/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Tag_GuardTest.php new file mode 100644 index 00000000000..5bb643e0e50 --- /dev/null +++ b/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Tag_GuardTest.php @@ -0,0 +1,68 @@ + 'tag-placed', + 'useAdBlockerDetectionSnippet' => true, + ) + ); + + $this->settings = new Settings( new Options( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ) ); + $this->guard = new Ad_Blocking_Recovery_Tag_Guard( $this->settings ); + } + + public function test_can_activate() { + $this->assertTrue( $this->guard->can_activate() ); + } + + public function test_cant_activate_when_adBlockingRecovery_is_not_set_up() { + $this->settings->merge( array( 'adBlockingRecoverySetupStatus' => '' ) ); + $this->assertFalse( $this->guard->can_activate(), 'Should return FALSE when adBlockingRecoverySetupStatus is empty.' ); + } + + public function test_cant_activate_when_useAdBlockerDetectionSnippet_is_falsy() { + $this->settings->merge( array( 'useAdBlockerDetectionSnippet' => false ) ); + $this->assertFalse( $this->guard->can_activate(), 'Should return FALSE when useAdBlockerDetectionSnippet is falsy.' ); + } +} From d195382d0a650fbc943ebbe77b6a38274585cf6c Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Wed, 28 Jun 2023 00:01:41 +0600 Subject: [PATCH 06/18] Set values in the ABR tag. --- includes/Modules/AdSense.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/includes/Modules/AdSense.php b/includes/Modules/AdSense.php index ec6691443cb..b2e42f8e497 100644 --- a/includes/Modules/AdSense.php +++ b/includes/Modules/AdSense.php @@ -804,6 +804,8 @@ private function register_tag() { if ( Feature_Flags::enabled( 'adBlockerDetection' ) && ! $this->context->is_amp() ) { $ad_blocking_recovery_web_tag = new Ad_Blocking_Recovery_Web_Tag( $settings['accountID'], self::MODULE_SLUG ); + $this->web_tag->set_ad_blocking_recovery_tag( $this->ad_blocking_recovery_tag ); + $this->web_tag->set_use_error_snippet( $this->get_settings()->get()['useAdBlockerDetectionErrorSnippet'] ); if ( $ad_blocking_recovery_web_tag->is_tag_blocked() ) { return; From 8b3bdf81eb8f9d08fd6898212cfb4293985eeb49 Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Wed, 28 Jun 2023 00:26:02 +0600 Subject: [PATCH 07/18] Fix typo. --- includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php b/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php index 40436571b2f..d654cdb3f36 100644 --- a/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php +++ b/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php @@ -68,7 +68,7 @@ public function register() { protected function render() { $tags = $this->ad_blocking_recovery_tag->get(); - if ( empty( $tags['tag'] ) || empty( $option['error_protection_code'] ) ) { + if ( empty( $tags['tag'] ) || empty( $tags['error_protection_code'] ) ) { return; } From ccc3da34ebc2aef7fd8563eb8ba6e2b29b6aa2fa Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Wed, 28 Jun 2023 00:31:50 +0600 Subject: [PATCH 08/18] Add test for `Ad_Blocking_Recovery_Web_Tag`. --- .../Ad_Blocking_Recovery_Web_TagTest.php | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Web_TagTest.php diff --git a/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Web_TagTest.php b/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Web_TagTest.php new file mode 100644 index 00000000000..6793f9147f1 --- /dev/null +++ b/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Web_TagTest.php @@ -0,0 +1,101 @@ +tag = new Ad_Blocking_Recovery_Tag( new Options( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ) ); + $this->web_tag = new Ad_Blocking_Recovery_Web_Tag( 'test-id', AdSense::MODULE_SLUG ); + $this->web_tag->set_ad_blocking_recovery_tag( $this->tag ); + $this->web_tag->set_use_error_snippet( true ); + } + + public function test_renders_nothing_when_not_available() { + remove_all_actions( 'wp_head' ); + + $this->web_tag->register(); + + $output = $this->capture_action( 'wp_head' ); + + $this->assertEmpty( $output ); + } + + public function test_renders_tags() { + remove_all_actions( 'wp_head' ); + + $this->tag->set( + array( + 'tag' => 'test-tag', + 'error_protection_code' => 'test-error-protection-code', + ) + ); + + $this->web_tag->register(); + + $output = $this->capture_action( 'wp_head' ); + + $this->assertStringContainsString( 'Google AdSense Ad Blocking Recovery snippet added by Site Kit', $output ); + $this->assertStringContainsString( 'Google AdSense Ad Blocking Recovery Error Protection snippet added by Site Kit', $output ); + $this->assertStringContainsString( 'test-tag', $output ); + $this->assertStringContainsString( 'test-error-protection-code', $output ); + + } + + public function test_does_nt_render_error_protection_tag_when_disabled() { + remove_all_actions( 'wp_head' ); + + $this->tag->set( + array( + 'tag' => 'test-tag', + 'error_protection_code' => 'test-error-protection-code', + ) + ); + $this->web_tag->set_use_error_snippet( false ); + + $this->web_tag->register(); + + $output = $this->capture_action( 'wp_head' ); + + $this->assertStringContainsString( 'Google AdSense Ad Blocking Recovery snippet added by Site Kit', $output ); + $this->assertStringNotContainsString( 'Google AdSense Ad Blocking Recovery Error Protection snippet added by Site Kit', $output ); + $this->assertStringContainsString( 'test-tag', $output ); + $this->assertStringNotContainsString( 'test-error-protection-code', $output ); + + } +} From c345e31dc9602b20bda00242def7585ceaefc6de Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Wed, 28 Jun 2023 02:37:19 +0600 Subject: [PATCH 09/18] Remove test group. --- .../integration/Core/Tags/Guards/WP_Query_404_GuardTest.php | 3 --- .../Modules/AdSense/Ad_Blocking_Recovery_Tag_GuardTest.php | 1 - .../Modules/AdSense/Ad_Blocking_Recovery_Web_TagTest.php | 1 - 3 files changed, 5 deletions(-) diff --git a/tests/phpunit/integration/Core/Tags/Guards/WP_Query_404_GuardTest.php b/tests/phpunit/integration/Core/Tags/Guards/WP_Query_404_GuardTest.php index 27b91e03619..200125125df 100644 --- a/tests/phpunit/integration/Core/Tags/Guards/WP_Query_404_GuardTest.php +++ b/tests/phpunit/integration/Core/Tags/Guards/WP_Query_404_GuardTest.php @@ -13,9 +13,6 @@ use Google\Site_Kit\Core\Tags\Guards\WP_Query_404_Guard; use Google\Site_Kit\Tests\TestCase; -/** - * @group test - */ class WP_Query_404_GuardTest extends TestCase { /** diff --git a/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Tag_GuardTest.php b/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Tag_GuardTest.php index 5bb643e0e50..5b07a359348 100644 --- a/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Tag_GuardTest.php +++ b/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Tag_GuardTest.php @@ -19,7 +19,6 @@ /** * @group Modules * @group AdSense - * @group test */ class Ad_Blocking_Recovery_Tag_GuardTest extends TestCase { diff --git a/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Web_TagTest.php b/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Web_TagTest.php index 6793f9147f1..501c5f29b2d 100644 --- a/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Web_TagTest.php +++ b/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Web_TagTest.php @@ -20,7 +20,6 @@ /** * @group Modules * @group AdSense - * @group test */ class Ad_Blocking_Recovery_Web_TagTest extends TestCase { From ce2313b75c4451eed5cc6b76478c7f719ae16274 Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Wed, 28 Jun 2023 12:44:19 +0600 Subject: [PATCH 10/18] Remove unused param. --- includes/Modules/AdSense.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Modules/AdSense.php b/includes/Modules/AdSense.php index b2e42f8e497..320af94767e 100644 --- a/includes/Modules/AdSense.php +++ b/includes/Modules/AdSense.php @@ -792,7 +792,7 @@ private function register_tag() { if ( ! $tag->is_tag_blocked() ) { $tag->use_guard( new Tag_Verify_Guard( $this->context->input() ) ); - $tag->use_guard( new WP_Query_404_Guard( $this->context->input() ) ); + $tag->use_guard( new WP_Query_404_Guard() ); $tag->use_guard( new Tag_Guard( $module_settings ) ); $tag->use_guard( new Auto_Ad_Guard( $module_settings ) ); $tag->use_guard( new Tag_Environment_Type_Guard() ); @@ -812,7 +812,7 @@ private function register_tag() { } $ad_blocking_recovery_web_tag->use_guard( new Tag_Verify_Guard( $this->context->input() ) ); - $ad_blocking_recovery_web_tag->use_guard( new WP_Query_404_Guard( $this->context->input() ) ); + $ad_blocking_recovery_web_tag->use_guard( new WP_Query_404_Guard() ); $ad_blocking_recovery_web_tag->use_guard( new Ad_Blocking_Recovery_Tag_Guard( $module_settings ) ); $ad_blocking_recovery_web_tag->use_guard( new Tag_Environment_Type_Guard() ); From 8071bcd07eef0dff52e945089d30ae34de4f7d64 Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Wed, 28 Jun 2023 13:17:07 +0600 Subject: [PATCH 11/18] Use setters for correct thing. --- includes/Modules/AdSense.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Modules/AdSense.php b/includes/Modules/AdSense.php index 320af94767e..1ba4ebfb90a 100644 --- a/includes/Modules/AdSense.php +++ b/includes/Modules/AdSense.php @@ -804,8 +804,8 @@ private function register_tag() { if ( Feature_Flags::enabled( 'adBlockerDetection' ) && ! $this->context->is_amp() ) { $ad_blocking_recovery_web_tag = new Ad_Blocking_Recovery_Web_Tag( $settings['accountID'], self::MODULE_SLUG ); - $this->web_tag->set_ad_blocking_recovery_tag( $this->ad_blocking_recovery_tag ); - $this->web_tag->set_use_error_snippet( $this->get_settings()->get()['useAdBlockerDetectionErrorSnippet'] ); + $ad_blocking_recovery_web_tag->set_ad_blocking_recovery_tag( $this->ad_blocking_recovery_tag ); + $ad_blocking_recovery_web_tag->set_use_error_snippet( $settings['useAdBlockerDetectionErrorSnippet'] ); if ( $ad_blocking_recovery_web_tag->is_tag_blocked() ) { return; From b098923ee10f1c5054d140080762fff8cc3cd670 Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Wed, 28 Jun 2023 13:17:34 +0600 Subject: [PATCH 12/18] Fix phpdocs of `Ad_Blocking_Recovery_Tag_Guard`. --- includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php b/includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php index a2cd4ea2e5e..43a699fd8d0 100644 --- a/includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php +++ b/includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php @@ -13,9 +13,9 @@ use Google\Site_Kit\Core\Modules\Tags\Module_Tag_Guard; /** - * Class for the AdSense tag guard. + * Class for the AdSense Ad Blocking Recovery tag guard. * - * @since 1.24.0 + * @since n.e.x.t * @access private * @ignore */ From bcd195fe1addfd089fdaadcfcbea97692ded3bfe Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Wed, 28 Jun 2023 13:18:04 +0600 Subject: [PATCH 13/18] Update `Ad_Blocking_Recovery_Tag_GuardTest`. --- .../Ad_Blocking_Recovery_Tag_GuardTest.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Tag_GuardTest.php b/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Tag_GuardTest.php index 5b07a359348..afa12fbde32 100644 --- a/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Tag_GuardTest.php +++ b/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Tag_GuardTest.php @@ -55,13 +55,18 @@ public function test_can_activate() { $this->assertTrue( $this->guard->can_activate() ); } - public function test_cant_activate_when_adBlockingRecovery_is_not_set_up() { - $this->settings->merge( array( 'adBlockingRecoverySetupStatus' => '' ) ); - $this->assertFalse( $this->guard->can_activate(), 'Should return FALSE when adBlockingRecoverySetupStatus is empty.' ); + /** + * @dataProvider data_can_not_activate + */ + public function test_can_not_activate( $settings ) { + $this->settings->merge( $settings ); + $this->assertFalse( $this->guard->can_activate() ); } - public function test_cant_activate_when_useAdBlockerDetectionSnippet_is_falsy() { - $this->settings->merge( array( 'useAdBlockerDetectionSnippet' => false ) ); - $this->assertFalse( $this->guard->can_activate(), 'Should return FALSE when useAdBlockerDetectionSnippet is falsy.' ); + public function data_can_not_activate() { + return array( + 'when adBlockingRecoverySetupStatus is empty' => array( array( 'adBlockingRecoverySetupStatus' => '' ) ), + 'when useAdBlockerDetectionSnippet is falsy' => array( array( 'useAdBlockerDetectionSnippet' => false ) ), + ); } } From 826d6a4f6207cc6207c4156e44132008ec3678f5 Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Wed, 28 Jun 2023 13:22:13 +0600 Subject: [PATCH 14/18] Update phpdoc for `AdSense\Tag_Guard::can_activate` to be more accurate. --- includes/Modules/AdSense/Tag_Guard.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Modules/AdSense/Tag_Guard.php b/includes/Modules/AdSense/Tag_Guard.php index 338fc668283..5b5c1f03ae1 100644 --- a/includes/Modules/AdSense/Tag_Guard.php +++ b/includes/Modules/AdSense/Tag_Guard.php @@ -26,7 +26,7 @@ class Tag_Guard extends Module_Tag_Guard { * * @since 1.24.0 * @since 1.30.0 Update to return FALSE on 404 pages deliberately. - * @since n.e.x.t Update to remove the check for 404 pages. + * @since n.e.x.t Extract the check for 404 pages to dedicated Guard. * * @return bool|WP_Error TRUE if guarded tag can be activated, otherwise FALSE or an error. */ From 5ba46f6246a9f92e0002edb1f6b5cb6eee1186e2 Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Wed, 28 Jun 2023 14:03:34 +0600 Subject: [PATCH 15/18] Return early if tag is blocked. --- includes/Modules/AdSense.php | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/includes/Modules/AdSense.php b/includes/Modules/AdSense.php index 1ba4ebfb90a..a3fa38276d5 100644 --- a/includes/Modules/AdSense.php +++ b/includes/Modules/AdSense.php @@ -790,16 +790,18 @@ private function register_tag() { $tag = new Web_Tag( $settings['clientID'], self::MODULE_SLUG ); } - if ( ! $tag->is_tag_blocked() ) { - $tag->use_guard( new Tag_Verify_Guard( $this->context->input() ) ); - $tag->use_guard( new WP_Query_404_Guard() ); - $tag->use_guard( new Tag_Guard( $module_settings ) ); - $tag->use_guard( new Auto_Ad_Guard( $module_settings ) ); - $tag->use_guard( new Tag_Environment_Type_Guard() ); - - if ( $tag->can_register() ) { - $tag->register(); - } + if ( $tag->is_tag_blocked() ) { + return; + } + + $tag->use_guard( new Tag_Verify_Guard( $this->context->input() ) ); + $tag->use_guard( new WP_Query_404_Guard() ); + $tag->use_guard( new Tag_Guard( $module_settings ) ); + $tag->use_guard( new Auto_Ad_Guard( $module_settings ) ); + $tag->use_guard( new Tag_Environment_Type_Guard() ); + + if ( $tag->can_register() ) { + $tag->register(); } if ( Feature_Flags::enabled( 'adBlockerDetection' ) && ! $this->context->is_amp() ) { @@ -807,10 +809,6 @@ private function register_tag() { $ad_blocking_recovery_web_tag->set_ad_blocking_recovery_tag( $this->ad_blocking_recovery_tag ); $ad_blocking_recovery_web_tag->set_use_error_snippet( $settings['useAdBlockerDetectionErrorSnippet'] ); - if ( $ad_blocking_recovery_web_tag->is_tag_blocked() ) { - return; - } - $ad_blocking_recovery_web_tag->use_guard( new Tag_Verify_Guard( $this->context->input() ) ); $ad_blocking_recovery_web_tag->use_guard( new WP_Query_404_Guard() ); $ad_blocking_recovery_web_tag->use_guard( new Ad_Blocking_Recovery_Tag_Guard( $module_settings ) ); From 0c72a7d77c3d20b8aa704886a3fd70dc11782836 Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Wed, 28 Jun 2023 14:22:36 +0600 Subject: [PATCH 16/18] Refactor `Ad_Blocking_Recovery_Web_Tag` to be based on `Tag` class. --- includes/Modules/AdSense.php | 4 +- .../AdSense/Ad_Blocking_Recovery_Web_Tag.php | 43 +++++++----------- .../Ad_Blocking_Recovery_Web_TagTest.php | 45 +++++++------------ 3 files changed, 32 insertions(+), 60 deletions(-) diff --git a/includes/Modules/AdSense.php b/includes/Modules/AdSense.php index a3fa38276d5..d067c81b5e1 100644 --- a/includes/Modules/AdSense.php +++ b/includes/Modules/AdSense.php @@ -805,9 +805,7 @@ private function register_tag() { } if ( Feature_Flags::enabled( 'adBlockerDetection' ) && ! $this->context->is_amp() ) { - $ad_blocking_recovery_web_tag = new Ad_Blocking_Recovery_Web_Tag( $settings['accountID'], self::MODULE_SLUG ); - $ad_blocking_recovery_web_tag->set_ad_blocking_recovery_tag( $this->ad_blocking_recovery_tag ); - $ad_blocking_recovery_web_tag->set_use_error_snippet( $settings['useAdBlockerDetectionErrorSnippet'] ); + $ad_blocking_recovery_web_tag = new Ad_Blocking_Recovery_Web_Tag( $this->ad_blocking_recovery_tag, $settings['useAdBlockerDetectionErrorSnippet'] ); $ad_blocking_recovery_web_tag->use_guard( new Tag_Verify_Guard( $this->context->input() ) ); $ad_blocking_recovery_web_tag->use_guard( new WP_Query_404_Guard() ); diff --git a/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php b/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php index d654cdb3f36..e0531d7e7c5 100644 --- a/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php +++ b/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php @@ -10,7 +10,7 @@ namespace Google\Site_Kit\Modules\AdSense; -use Google\Site_Kit\Core\Modules\Tags\Module_Web_Tag; +use Google\Site_Kit\Core\Tags\Tag; use Google\Site_Kit\Core\Util\Method_Proxy_Trait; use Google\Site_Kit\Core\Tags\Tag_With_DNS_Prefetch_Trait; @@ -21,9 +21,10 @@ * @access private * @ignore */ -class Ad_Blocking_Recovery_Web_Tag extends Module_Web_Tag { +class Ad_Blocking_Recovery_Web_Tag extends Tag { - use Method_Proxy_Trait, Tag_With_DNS_Prefetch_Trait; + use Method_Proxy_Trait; + use Tag_With_DNS_Prefetch_Trait; /** * Ad_Blocking_Recovery_Tag instance. @@ -41,6 +42,18 @@ class Ad_Blocking_Recovery_Web_Tag extends Module_Web_Tag { */ protected $use_error_protection_snippet; + /** + * Constructor. + * + * @since n.e.x.t + * + * @param Ad_Blocking_Recovery_Tag $ad_blocking_recovery_tag Ad_Blocking_Recovery_Tag instance. + * @param bool $use_error_protection_snippet Use Error Protection Snippet. + */ + public function __construct( Ad_Blocking_Recovery_Tag $ad_blocking_recovery_tag, $use_error_protection_snippet ) { + $this->ad_blocking_recovery_tag = $ad_blocking_recovery_tag; + $this->use_error_protection_snippet = $use_error_protection_snippet; + } /** * Registers tag hooks. @@ -56,8 +69,6 @@ public function register() { 10, 2 ); - - $this->do_init_tag_action(); } /** @@ -81,26 +92,4 @@ protected function render() { printf( "\n\n", esc_html__( 'End Google AdSense Ad Blocking Recovery Error Protection snippet added by Site Kit', 'google-site-kit' ) ); } } - - /** - * Sets the Ad_Blocking_Recovery_Tag instance. - * - * @since n.e.x.t - * - * @param Ad_Blocking_Recovery_Tag $ad_blocking_recovery_tag Ad_Blocking_Recovery_Tag instance. - */ - public function set_ad_blocking_recovery_tag( Ad_Blocking_Recovery_Tag $ad_blocking_recovery_tag ) { - $this->ad_blocking_recovery_tag = $ad_blocking_recovery_tag; - } - - /** - * Sets Use Error Protection Snippet. - * - * @since n.e.x.t - * - * @param bool $use_error_protection_snippet Whether to use the Ad Blocking Recovery Error Protection Snippet. - */ - public function set_use_error_snippet( $use_error_protection_snippet ) { - $this->use_error_protection_snippet = $use_error_protection_snippet; - } } diff --git a/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Web_TagTest.php b/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Web_TagTest.php index 501c5f29b2d..9db826e9b4a 100644 --- a/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Web_TagTest.php +++ b/tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Web_TagTest.php @@ -12,7 +12,6 @@ use Google\Site_Kit\Context; use Google\Site_Kit\Core\Storage\Options; -use Google\Site_Kit\Modules\AdSense; use Google\Site_Kit\Modules\AdSense\Ad_Blocking_Recovery_Tag; use Google\Site_Kit\Modules\AdSense\Ad_Blocking_Recovery_Web_Tag; use Google\Site_Kit\Tests\TestCase; @@ -23,32 +22,13 @@ */ class Ad_Blocking_Recovery_Web_TagTest extends TestCase { - /** - * Ad_Blocking_Recovery_Tag object. - * @var Ad_Blocking_Recovery_Tag - */ - private $tag; - - /** - * Ad_Blocking_Recovery_Web_Tag object. - * - * @var Ad_Blocking_Recovery_Web_Tag - */ - private $web_tag; - - public function set_up() { - parent::set_up(); - - $this->tag = new Ad_Blocking_Recovery_Tag( new Options( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ) ); - $this->web_tag = new Ad_Blocking_Recovery_Web_Tag( 'test-id', AdSense::MODULE_SLUG ); - $this->web_tag->set_ad_blocking_recovery_tag( $this->tag ); - $this->web_tag->set_use_error_snippet( true ); - } + public function test_renders_nothing_when_tag_is_not_available() { + $tag = new Ad_Blocking_Recovery_Tag( new Options( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ) ); + $ad_blocking_recovery_web_tag = new Ad_Blocking_Recovery_Web_Tag( $tag, true ); - public function test_renders_nothing_when_not_available() { remove_all_actions( 'wp_head' ); - $this->web_tag->register(); + $ad_blocking_recovery_web_tag->register(); $output = $this->capture_action( 'wp_head' ); @@ -56,16 +36,19 @@ public function test_renders_nothing_when_not_available() { } public function test_renders_tags() { + $tag = new Ad_Blocking_Recovery_Tag( new Options( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ) ); + $ad_blocking_recovery_web_tag = new Ad_Blocking_Recovery_Web_Tag( $tag, true ); + remove_all_actions( 'wp_head' ); - $this->tag->set( + $tag->set( array( 'tag' => 'test-tag', 'error_protection_code' => 'test-error-protection-code', ) ); - $this->web_tag->register(); + $ad_blocking_recovery_web_tag->register(); $output = $this->capture_action( 'wp_head' ); @@ -76,18 +59,20 @@ public function test_renders_tags() { } - public function test_does_nt_render_error_protection_tag_when_disabled() { + public function test_does_not_render_error_protection_tag_when_disabled() { + $tag = new Ad_Blocking_Recovery_Tag( new Options( new Context( GOOGLESITEKIT_PLUGIN_MAIN_FILE ) ) ); + $ad_blocking_recovery_web_tag = new Ad_Blocking_Recovery_Web_Tag( $tag, false ); + remove_all_actions( 'wp_head' ); - $this->tag->set( + $tag->set( array( 'tag' => 'test-tag', 'error_protection_code' => 'test-error-protection-code', ) ); - $this->web_tag->set_use_error_snippet( false ); - $this->web_tag->register(); + $ad_blocking_recovery_web_tag->register(); $output = $this->capture_action( 'wp_head' ); From 968965b26316aea9f32cd37f0169cfc2b4ac65ce Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Wed, 28 Jun 2023 14:27:54 +0600 Subject: [PATCH 17/18] Update class description. --- includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php b/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php index e0531d7e7c5..3416e09fc7a 100644 --- a/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php +++ b/includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php @@ -15,7 +15,7 @@ use Google\Site_Kit\Core\Tags\Tag_With_DNS_Prefetch_Trait; /** - * Class for Web tag. + * Class for Ad Blocking Recovery tag. * * @since n.e.x.t * @access private From 136eb7725d3802f088e6e9e036539e80ec72b280 Mon Sep 17 00:00:00 2001 From: Arafat Zahan Date: Wed, 28 Jun 2023 14:43:50 +0600 Subject: [PATCH 18/18] Update WP_Query_404_Guard. --- .../Core/Tags/Guards/WP_Query_404_Guard.php | 2 +- .../Tags/Guards/WP_Query_404_GuardTest.php | 20 ++++++------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/includes/Core/Tags/Guards/WP_Query_404_Guard.php b/includes/Core/Tags/Guards/WP_Query_404_Guard.php index a4b6c90c0c0..28db6cca75c 100644 --- a/includes/Core/Tags/Guards/WP_Query_404_Guard.php +++ b/includes/Core/Tags/Guards/WP_Query_404_Guard.php @@ -13,7 +13,7 @@ use Google\Site_Kit\Core\Guards\Guard_Interface; /** - * Base class for a module tag guard. + * Class for WP_Query 404 guard. * * @since n.e.x.t * @access private diff --git a/tests/phpunit/integration/Core/Tags/Guards/WP_Query_404_GuardTest.php b/tests/phpunit/integration/Core/Tags/Guards/WP_Query_404_GuardTest.php index 200125125df..d6e4e692a66 100644 --- a/tests/phpunit/integration/Core/Tags/Guards/WP_Query_404_GuardTest.php +++ b/tests/phpunit/integration/Core/Tags/Guards/WP_Query_404_GuardTest.php @@ -15,30 +15,22 @@ class WP_Query_404_GuardTest extends TestCase { - /** - * Tag_Guard object. - * - * @var WP_Query_404_Guard - */ - private $guard; - - public function set_up() { - parent::set_up(); - $this->guard = new WP_Query_404_Guard(); - } - public function test_can_activate() { + $guard = new WP_Query_404_Guard(); + $this->go_to( '/' ); $this->assertQueryTrue( 'is_home', 'is_front_page' ); - $this->assertTrue( $this->guard->can_activate(), 'Should return TRUE when the current page exists (is_home).' ); + $this->assertTrue( $guard->can_activate(), 'Should return TRUE when the current page exists (is_home).' ); } public function test_cant_activate_on_404() { + $guard = new WP_Query_404_Guard(); + $this->go_to( '/?p=123456789' ); $this->assertQueryTrue( 'is_404' ); - $this->assertFalse( $this->guard->can_activate(), 'Should return FALSE when the current page doesnt exist (is_404).' ); + $this->assertFalse( $guard->can_activate(), 'Should return FALSE when the current page doesnt exist (is_404).' ); } }