-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7212 from google/enhance/7186-place-abr-tags
- Loading branch information
Showing
9 changed files
with
385 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
includes/Modules/AdSense/Ad_Blocking_Recovery_Tag_Guard.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ) ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
tests/phpunit/integration/Core/Tags/Guards/WP_Query_404_GuardTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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).' ); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
tests/phpunit/integration/Modules/AdSense/Ad_Blocking_Recovery_Tag_GuardTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ), | ||
); | ||
} | ||
} |
Oops, something went wrong.