Skip to content

Commit

Permalink
Merge pull request #7212 from google/enhance/7186-place-abr-tags
Browse files Browse the repository at this point in the history
  • Loading branch information
aaemnnosttv authored Jun 28, 2023
2 parents 63d8884 + 136eb77 commit 712004e
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 20 deletions.
34 changes: 34 additions & 0 deletions includes/Core/Tags/Guards/WP_Query_404_Guard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Class Google\Site_Kit\Core\Tags\Guards\WP_Query_404_Guard
*
* @package Google\Site_Kit\Core\Tags\Guards
* @copyright 2023 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

namespace Google\Site_Kit\Core\Tags\Guards;

use Google\Site_Kit\Core\Guards\Guard_Interface;

/**
* Class for WP_Query 404 guard.
*
* @since n.e.x.t
* @access private
* @ignore
*/
class WP_Query_404_Guard implements Guard_Interface {
/**
* Determines whether the guarded tag can be activated or not.
*
* @since n.e.x.t
*
* @return bool TRUE if guarded tag can be activated, otherwise FALSE or an error.
*/
public function can_activate() {
return ! is_404();
}

}
33 changes: 26 additions & 7 deletions includes/Modules/AdSense.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
use Google\Site_Kit\Core\Storage\Encrypted_Options;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Core\Storage\User_Options;
use Google\Site_Kit\Core\Tags\Guards\WP_Query_404_Guard;
use Google\Site_Kit\Modules\AdSense\Ad_Blocking_Recovery_Tag_Guard;
use Google\Site_Kit\Modules\AdSense\Ad_Blocking_Recovery_Web_Tag;
use WP_Error;
use WP_REST_Response;

Expand Down Expand Up @@ -787,14 +790,30 @@ 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 Tag_Guard( $module_settings ) );
$tag->use_guard( new Auto_Ad_Guard( $module_settings ) );
$tag->use_guard( new Tag_Environment_Type_Guard() );
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() ) {
$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() );
$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 ( $tag->can_register() ) {
$tag->register();
if ( $ad_blocking_recovery_web_tag->can_register() ) {
$ad_blocking_recovery_web_tag->register();
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Class Google\Site_Kit\Modules\AdSense\Ad_Blocking_Recovery_Tag_Guard
*
* @package Google\Site_Kit\Modules\AdSense
* @copyright 2023 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

namespace Google\Site_Kit\Modules\AdSense;

use Google\Site_Kit\Core\Modules\Tags\Module_Tag_Guard;

/**
* Class for the AdSense Ad Blocking Recovery tag guard.
*
* @since n.e.x.t
* @access private
* @ignore
*/
class Ad_Blocking_Recovery_Tag_Guard extends Module_Tag_Guard {

/**
* Determines whether the guarded tag can be activated or not.
*
* @since n.e.x.t
*
* @return bool TRUE if guarded tag can be activated, otherwise FALSE or an error.
*/
public function can_activate() {
$settings = $this->settings->get();

return ! empty( $settings['adBlockingRecoverySetupStatus'] ) && $settings['useAdBlockerDetectionSnippet'];
}
}
95 changes: 95 additions & 0 deletions includes/Modules/AdSense/Ad_Blocking_Recovery_Web_Tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Class Google\Site_Kit\Modules\AdSense\Ad_Blocking_Recovery_Web_Tag
*
* @package Google\Site_Kit\Modules\AdSense
* @copyright 2023 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

namespace Google\Site_Kit\Modules\AdSense;

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;

/**
* Class for Ad Blocking Recovery tag.
*
* @since n.e.x.t
* @access private
* @ignore
*/
class Ad_Blocking_Recovery_Web_Tag extends Tag {

use Method_Proxy_Trait;
use Tag_With_DNS_Prefetch_Trait;

/**
* Ad_Blocking_Recovery_Tag instance.
*
* @since n.e.x.t
* @var Ad_Blocking_Recovery_Tag
*/
protected $ad_blocking_recovery_tag;

/**
* Use Error Protection Snippet.
*
* @since n.e.x.t
* @var bool
*/
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.
*
* @since n.e.x.t
*/
public function register() {
add_action( 'wp_head', $this->get_method_proxy_once( 'render' ) );

add_filter(
'wp_resource_hints',
$this->get_dns_prefetch_hints_callback( '//fundingchoicesmessages.google.com' ),
10,
2
);
}

/**
* Outputs the AdSense script tag.
*
* @since n.e.x.t
*/
protected function render() {
$tags = $this->ad_blocking_recovery_tag->get();

if ( empty( $tags['tag'] ) || empty( $tags['error_protection_code'] ) ) {
return;
}

printf( "\n<!-- %s -->\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<!-- %s -->\n", esc_html__( 'End Google AdSense Ad Blocking Recovery snippet added by Site Kit', 'google-site-kit' ) );
if ( $this->use_error_protection_snippet ) {
printf( "\n<!-- %s -->\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<!-- %s -->\n", esc_html__( 'End Google AdSense Ad Blocking Recovery Error Protection snippet added by Site Kit', 'google-site-kit' ) );
}
}
}
6 changes: 1 addition & 5 deletions includes/Modules/AdSense/Tag_Guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.
*/
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* WP_Query_404_GuardTest
*
* @package Google\Site_Kit\Tests\Core\Tags\Guards
* @copyright 2023 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

namespace Google\Site_Kit\Tests\Core\Tags\Guards;

use Google\Site_Kit\Core\Tags\Guards\WP_Query_404_Guard;
use Google\Site_Kit\Tests\TestCase;

class WP_Query_404_GuardTest extends TestCase {

public function test_can_activate() {
$guard = new WP_Query_404_Guard();

$this->go_to( '/' );

$this->assertQueryTrue( 'is_home', 'is_front_page' );
$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( $guard->can_activate(), 'Should return FALSE when the current page doesnt exist (is_404).' );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Class Google\Site_Kit\Tests\Modules\AdSense\Ad_Blocking_Recovery_Tag_GuardTest
*
* @package Google\Site_Kit\Tests\Modules\AdSense
* @copyright 2023 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/

namespace Google\Site_Kit\Tests\Modules\AdSense;

use Google\Site_Kit\Context;
use Google\Site_Kit\Core\Storage\Options;
use Google\Site_Kit\Modules\AdSense\Settings;
use Google\Site_Kit\Modules\AdSense\Ad_Blocking_Recovery_Tag_Guard;
use Google\Site_Kit\Tests\TestCase;

/**
* @group Modules
* @group AdSense
*/
class Ad_Blocking_Recovery_Tag_GuardTest extends TestCase {

/**
* Settings object.
*
* @var Settings
*/
private $settings;

/**
* Ad_Blocking_Recovery_Tag_Guard object.
*
* @var Ad_Blocking_Recovery_Tag_Guard
*/
private $guard;

public function set_up() {
parent::set_up();

update_option(
Settings::OPTION,
array(
'adBlockingRecoverySetupStatus' => '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() );
}

/**
* @dataProvider data_can_not_activate
*/
public function test_can_not_activate( $settings ) {
$this->settings->merge( $settings );
$this->assertFalse( $this->guard->can_activate() );
}

public function data_can_not_activate() {
return array(
'when adBlockingRecoverySetupStatus is empty' => array( array( 'adBlockingRecoverySetupStatus' => '' ) ),
'when useAdBlockerDetectionSnippet is falsy' => array( array( 'useAdBlockerDetectionSnippet' => false ) ),
);
}
}
Loading

0 comments on commit 712004e

Please sign in to comment.