update
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\AdSense\AMP_Tag
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\AdSense
|
||||
* @copyright 2021 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_AMP_Tag;
|
||||
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;
|
||||
|
||||
/**
|
||||
* Class for AMP tag.
|
||||
*
|
||||
* @since 1.24.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class AMP_Tag extends Module_AMP_Tag {
|
||||
|
||||
use Method_Proxy_Trait;
|
||||
|
||||
/**
|
||||
* Internal flag for whether the AdSense tag has been printed.
|
||||
*
|
||||
* @since 1.24.0
|
||||
* @var bool
|
||||
*/
|
||||
private $adsense_tag_printed = false;
|
||||
|
||||
/**
|
||||
* Web Story Ad Slot ID.
|
||||
*
|
||||
* @since 1.27.0
|
||||
* @var string
|
||||
*/
|
||||
private $story_ad_slot_id = '';
|
||||
|
||||
/**
|
||||
* Registers tag hooks.
|
||||
*
|
||||
* @since 1.24.0
|
||||
*/
|
||||
public function register() {
|
||||
if ( is_singular( 'web-story' ) ) {
|
||||
// If Web Stories are enabled, render the auto ads code.
|
||||
add_action( 'web_stories_print_analytics', $this->get_method_proxy( 'render_story_auto_ads' ) );
|
||||
} else {
|
||||
// For AMP Native and Transitional (if `wp_body_open` supported).
|
||||
add_action( 'wp_body_open', $this->get_method_proxy( 'render' ), -9999 );
|
||||
// For AMP Native and Transitional (as fallback).
|
||||
add_filter( 'the_content', $this->get_method_proxy( 'amp_content_add_auto_ads' ) );
|
||||
// For AMP Reader (if `amp_post_template_body_open` supported).
|
||||
add_action( 'amp_post_template_body_open', $this->get_method_proxy( 'render' ), -9999 );
|
||||
// For AMP Reader (as fallback).
|
||||
add_action( 'amp_post_template_footer', $this->get_method_proxy( 'render' ), -9999 );
|
||||
|
||||
// Load amp-auto-ads component for AMP Reader.
|
||||
$this->enqueue_amp_reader_component_script( 'amp-auto-ads', 'https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js' );
|
||||
}
|
||||
|
||||
$this->do_init_tag_action();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the attributes for amp-story-auto-ads and amp-auto-ads tags.
|
||||
*
|
||||
* @since 1.39.0
|
||||
*
|
||||
* @param string $type Whether it's for web stories. Can be `web-story` or ``.
|
||||
* @return array Filtered $options.
|
||||
*/
|
||||
private function get_auto_ads_attributes( $type = '' ) {
|
||||
$options = array(
|
||||
'ad-client' => $this->tag_id,
|
||||
);
|
||||
|
||||
if ( 'web-story' === $type && ! empty( $this->story_ad_slot_id ) ) {
|
||||
$options['ad-slot'] = $this->story_ad_slot_id;
|
||||
}
|
||||
|
||||
$filtered_options = 'web-story' === $type
|
||||
? apply_filters( 'googlesitekit_amp_story_auto_ads_attributes', $options, $this->tag_id, $this->story_ad_slot_id )
|
||||
: apply_filters( 'googlesitekit_amp_auto_ads_attributes', $options, $this->tag_id, $this->story_ad_slot_id );
|
||||
|
||||
if ( is_array( $filtered_options ) && ! empty( $filtered_options ) ) {
|
||||
$options = $filtered_options;
|
||||
$options['ad-client'] = $this->tag_id;
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the <amp-auto-ads> tag.
|
||||
*
|
||||
* @since 1.24.0
|
||||
*/
|
||||
protected function render() {
|
||||
if ( $this->adsense_tag_printed ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->adsense_tag_printed = true;
|
||||
|
||||
$attributes = '';
|
||||
foreach ( $this->get_auto_ads_attributes() as $amp_auto_ads_opt_key => $amp_auto_ads_opt_value ) {
|
||||
$attributes .= sprintf( ' data-%s="%s"', esc_attr( $amp_auto_ads_opt_key ), esc_attr( $amp_auto_ads_opt_value ) );
|
||||
}
|
||||
|
||||
printf( "\n<!-- %s -->\n", esc_html__( 'Google AdSense AMP snippet added by Site Kit', 'google-site-kit' ) );
|
||||
|
||||
printf(
|
||||
'<amp-auto-ads type="adsense" %s%s></amp-auto-ads>',
|
||||
$attributes, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
$this->get_tag_blocked_on_consent_attribute() // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
);
|
||||
|
||||
printf( "\n<!-- %s -->\n", esc_html__( 'End Google AdSense AMP snippet added by Site Kit', 'google-site-kit' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the AMP auto ads tag if opted in.
|
||||
*
|
||||
* @since 1.24.0
|
||||
*
|
||||
* @param string $content The page content.
|
||||
* @return string Filtered $content.
|
||||
*/
|
||||
private function amp_content_add_auto_ads( $content ) {
|
||||
// Only run for the primary application of the `the_content` filter.
|
||||
if ( $this->adsense_tag_printed || ! in_the_loop() ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
$this->adsense_tag_printed = true;
|
||||
|
||||
$snippet_comment_begin = sprintf( "\n<!-- %s -->\n", esc_html__( 'Google AdSense AMP snippet added by Site Kit', 'google-site-kit' ) );
|
||||
$snippet_comment_end = sprintf( "\n<!-- %s -->\n", esc_html__( 'End Google AdSense AMP snippet added by Site Kit', 'google-site-kit' ) );
|
||||
|
||||
$tag = sprintf(
|
||||
'<amp-auto-ads type="adsense" data-ad-client="%s"%s></amp-auto-ads>',
|
||||
esc_attr( $this->tag_id ),
|
||||
$this->get_tag_blocked_on_consent_attribute()
|
||||
);
|
||||
|
||||
return $snippet_comment_begin . $tag . $snippet_comment_end . $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Web Story Ad Slot ID
|
||||
*
|
||||
* @since 1.27.0
|
||||
*
|
||||
* @param string $ad_slot_id The Ad Slot ID.
|
||||
*/
|
||||
public function set_story_ad_slot_id( $ad_slot_id ) {
|
||||
$this->story_ad_slot_id = $ad_slot_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the AMP Web Story auto ads code if enabled.
|
||||
*
|
||||
* @since 1.27.0
|
||||
*/
|
||||
private function render_story_auto_ads() {
|
||||
$config = array(
|
||||
'ad-attributes' => array(
|
||||
'type' => 'adsense',
|
||||
),
|
||||
);
|
||||
|
||||
$attributes = array();
|
||||
foreach ( $this->get_auto_ads_attributes( 'web-story' ) as $key => $value ) {
|
||||
$attributes[ 'data-' . $key ] = $value;
|
||||
}
|
||||
|
||||
$config['ad-attributes'] = array_merge( $config['ad-attributes'], $attributes );
|
||||
|
||||
printf( "\n<!-- %s -->\n", esc_html__( 'Google AdSense AMP snippet added by Site Kit', 'google-site-kit' ) );
|
||||
|
||||
printf( '<amp-story-auto-ads><script type="application/json">%s</script></amp-story-auto-ads>', wp_json_encode( $config ) );
|
||||
|
||||
printf( "\n<!-- %s -->\n", esc_html__( 'End Google AdSense AMP snippet added by Site Kit', 'google-site-kit' ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\AdSense\Ad_Blocking_Recovery_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\Storage\Setting;
|
||||
|
||||
/**
|
||||
* Class for AdSense Ad blocking recovery Tag.
|
||||
*
|
||||
* @since 1.104.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Ad_Blocking_Recovery_Tag extends Setting {
|
||||
|
||||
const OPTION = 'googlesitekit_adsense_ad_blocking_recovery_tag';
|
||||
|
||||
/**
|
||||
* Gets ad blocking recovery tag.
|
||||
*
|
||||
* @since 1.104.0
|
||||
*
|
||||
* @return array Array with tag and error protection code.
|
||||
*/
|
||||
public function get() {
|
||||
$option = parent::get();
|
||||
|
||||
if ( ! $this->is_valid_tag_object( $option ) ) {
|
||||
return $this->get_default();
|
||||
}
|
||||
|
||||
return $option;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets ad blocking recovery tag.
|
||||
*
|
||||
* @since 1.104.0
|
||||
*
|
||||
* @param array $value Array with tag and error protection code.
|
||||
*
|
||||
* @return bool True on success, false on failure.
|
||||
*/
|
||||
public function set( $value ) {
|
||||
if ( ! $this->is_valid_tag_object( $value ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parent::set( $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the expected value type.
|
||||
*
|
||||
* @since 1.104.0
|
||||
*
|
||||
* @return string The type name.
|
||||
*/
|
||||
protected function get_type() {
|
||||
return 'object';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default value.
|
||||
*
|
||||
* @since 1.104.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_default() {
|
||||
return array(
|
||||
'tag' => '',
|
||||
'error_protection_code' => '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether the given value is a valid tag object.
|
||||
*
|
||||
* @since 1.104.0
|
||||
*
|
||||
* @param mixed $tag Tag object.
|
||||
*
|
||||
* @return bool TRUE if valid, otherwise FALSE.
|
||||
*/
|
||||
private function is_valid_tag_object( $tag ) {
|
||||
return is_array( $tag ) && isset( $tag['tag'] ) && isset( $tag['error_protection_code'] ) && is_string( $tag['tag'] ) && is_string( $tag['error_protection_code'] );
|
||||
}
|
||||
}
|
||||
@@ -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 1.105.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Ad_Blocking_Recovery_Tag_Guard extends Module_Tag_Guard {
|
||||
|
||||
/**
|
||||
* Determines whether the guarded tag can be activated or not.
|
||||
*
|
||||
* @since 1.105.0
|
||||
*
|
||||
* @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['useAdBlockingRecoverySnippet'];
|
||||
}
|
||||
}
|
||||
@@ -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 1.105.0
|
||||
* @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 1.105.0
|
||||
* @var Ad_Blocking_Recovery_Tag
|
||||
*/
|
||||
protected $ad_blocking_recovery_tag;
|
||||
|
||||
/**
|
||||
* Use Error Protection Snippet.
|
||||
*
|
||||
* @since 1.105.0
|
||||
* @var bool
|
||||
*/
|
||||
protected $use_error_protection_snippet;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.105.0
|
||||
*
|
||||
* @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 1.105.0
|
||||
*/
|
||||
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 1.105.0
|
||||
*/
|
||||
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' ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\AdSense\Auto_Ad_Guard
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\Analytics
|
||||
* @copyright 2021 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;
|
||||
|
||||
/**
|
||||
* Tag guard class for the AdSense module that blocks the tag placement if it is disabled for a certain user group.
|
||||
*
|
||||
* @since 1.39.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Auto_Ad_Guard extends Module_Tag_Guard {
|
||||
|
||||
/**
|
||||
* Determines whether the guarded tag can be activated or not.
|
||||
*
|
||||
* @since 1.39.0
|
||||
*
|
||||
* @return bool TRUE if guarded tag can be activated, otherwise FALSE.
|
||||
*/
|
||||
public function can_activate() {
|
||||
$settings = $this->settings->get();
|
||||
if ( ! isset( $settings['autoAdsDisabled'] ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
( in_array( 'loggedinUsers', $settings['autoAdsDisabled'], true ) && is_user_logged_in() ) ||
|
||||
( in_array( 'contentCreators', $settings['autoAdsDisabled'], true ) && current_user_can( 'edit_posts' ) )
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\AdSense\Settings
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\AdSense
|
||||
* @copyright 2021 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\Module_Settings;
|
||||
use Google\Site_Kit\Core\Storage\Setting_With_Legacy_Keys_Trait;
|
||||
use Google\Site_Kit\Core\Storage\Setting_With_Owned_Keys_Interface;
|
||||
use Google\Site_Kit\Core\Storage\Setting_With_Owned_Keys_Trait;
|
||||
use Google\Site_Kit\Core\Storage\Setting_With_ViewOnly_Keys_Interface;
|
||||
|
||||
/**
|
||||
* Class for AdSense settings.
|
||||
*
|
||||
* @since 1.2.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Settings extends Module_Settings implements Setting_With_Owned_Keys_Interface, Setting_With_ViewOnly_Keys_Interface {
|
||||
use Setting_With_Legacy_Keys_Trait, Setting_With_Owned_Keys_Trait;
|
||||
|
||||
const OPTION = 'googlesitekit_adsense_settings';
|
||||
|
||||
/**
|
||||
* Various ad blocking recovery setup statuses.
|
||||
*/
|
||||
const AD_BLOCKING_RECOVERY_SETUP_STATUS_TAG_PLACED = 'tag-placed';
|
||||
const AD_BLOCKING_RECOVERY_SETUP_STATUS_SETUP_CONFIRMED = 'setup-confirmed';
|
||||
|
||||
/**
|
||||
* Legacy account statuses to be migrated on-the-fly.
|
||||
*
|
||||
* @since 1.9.0
|
||||
* @var array
|
||||
*/
|
||||
protected $legacy_account_statuses = array(
|
||||
'account-connected' => array(
|
||||
'accountStatus' => 'approved',
|
||||
'siteStatus' => 'added',
|
||||
),
|
||||
'account-connected-nonmatching' => array(
|
||||
'accountStatus' => 'approved',
|
||||
'siteStatus' => 'added',
|
||||
),
|
||||
'account-connected-no-data' => array(
|
||||
'accountStatus' => 'approved',
|
||||
'siteStatus' => 'added',
|
||||
),
|
||||
'account-pending-review' => array(
|
||||
'accountStatus' => 'approved',
|
||||
'siteStatus' => 'none',
|
||||
),
|
||||
'account-required-action' => array(
|
||||
'accountStatus' => 'no-client',
|
||||
),
|
||||
'disapproved-account-afc' => array(
|
||||
'accountStatus' => 'no-client',
|
||||
),
|
||||
'ads-display-pending' => array(
|
||||
'accountStatus' => 'pending',
|
||||
),
|
||||
'disapproved-account' => array(
|
||||
'accountStatus' => 'disapproved',
|
||||
),
|
||||
'no-account' => array(
|
||||
'accountStatus' => 'none',
|
||||
),
|
||||
'no-account-tag-found' => array(
|
||||
'accountStatus' => 'none',
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* Registers the setting in WordPress.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public function register() {
|
||||
parent::register();
|
||||
|
||||
$this->register_legacy_keys_migration(
|
||||
array(
|
||||
'account_id' => 'accountID',
|
||||
'accountId' => 'accountID',
|
||||
'account_status' => 'accountStatus',
|
||||
'adsenseTagEnabled' => 'useSnippet',
|
||||
'client_id' => 'clientID',
|
||||
'clientId' => 'clientID',
|
||||
'setup_complete' => 'setupComplete',
|
||||
)
|
||||
);
|
||||
|
||||
$this->register_owned_keys();
|
||||
|
||||
add_filter(
|
||||
'option_' . self::OPTION,
|
||||
function ( $option ) {
|
||||
/**
|
||||
* Filters the AdSense account ID to use.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $account_id Empty by default, will fall back to the option value if not set.
|
||||
*/
|
||||
$account_id = apply_filters( 'googlesitekit_adsense_account_id', '' );
|
||||
|
||||
if ( $account_id ) {
|
||||
$option['accountID'] = $account_id;
|
||||
}
|
||||
|
||||
// Migrate legacy account statuses (now split into account status and site status).
|
||||
if ( ! empty( $option['accountStatus'] ) && isset( $this->legacy_account_statuses[ $option['accountStatus'] ] ) ) {
|
||||
foreach ( $this->legacy_account_statuses[ $option['accountStatus'] ] as $key => $value ) {
|
||||
$option[ $key ] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// Migration of legacy setting.
|
||||
if ( ! empty( $option['setupComplete'] ) ) {
|
||||
$option['accountSetupComplete'] = $option['setupComplete'];
|
||||
$option['siteSetupComplete'] = $option['setupComplete'];
|
||||
}
|
||||
unset( $option['setupComplete'] );
|
||||
|
||||
return $option;
|
||||
}
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'pre_update_option_' . self::OPTION,
|
||||
function ( $value, $old_value ) {
|
||||
if ( isset( $old_value['setupCompletedTimestamp'] ) ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if ( ! empty( $old_value['accountStatus'] ) && ! empty( $old_value['siteStatus'] ) && 'ready' === $old_value['accountStatus'] && 'ready' === $old_value['siteStatus'] ) {
|
||||
$value['setupCompletedTimestamp'] = strtotime( '-1 month' );
|
||||
} elseif ( ! empty( $value['accountStatus'] ) && ! empty( $value['siteStatus'] ) && 'ready' === $value['accountStatus'] && 'ready' === $value['siteStatus'] ) {
|
||||
$value['setupCompletedTimestamp'] = time();
|
||||
}
|
||||
|
||||
return $value;
|
||||
},
|
||||
10,
|
||||
2
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns keys for owned settings.
|
||||
*
|
||||
* @since 1.16.0
|
||||
*
|
||||
* @return array An array of keys for owned settings.
|
||||
*/
|
||||
public function get_owned_keys() {
|
||||
return array(
|
||||
'accountID',
|
||||
'clientID',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns keys for view-only settings.
|
||||
*
|
||||
* @since 1.122.0
|
||||
*
|
||||
* @return array An array of keys for view-only settings.
|
||||
*/
|
||||
public function get_view_only_keys() {
|
||||
return array( 'accountID' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default value.
|
||||
*
|
||||
* @since 1.2.0
|
||||
* @since 1.102.0 Added settings for the Ad Blocking Recovery feature.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_default() {
|
||||
return array(
|
||||
'ownerID' => 0,
|
||||
'accountID' => '',
|
||||
'autoAdsDisabled' => array(),
|
||||
'clientID' => '',
|
||||
'accountStatus' => '',
|
||||
'siteStatus' => '',
|
||||
'accountSetupComplete' => false,
|
||||
'siteSetupComplete' => false,
|
||||
'useSnippet' => true,
|
||||
'webStoriesAdUnit' => '',
|
||||
'setupCompletedTimestamp' => null,
|
||||
'useAdBlockingRecoverySnippet' => false,
|
||||
'useAdBlockingRecoveryErrorSnippet' => false,
|
||||
'adBlockingRecoverySetupStatus' => '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the callback for sanitizing the setting's value before saving.
|
||||
*
|
||||
* @since 1.6.0
|
||||
*
|
||||
* @return callable|null
|
||||
*/
|
||||
protected function get_sanitize_callback() {
|
||||
return function( $option ) {
|
||||
if ( is_array( $option ) ) {
|
||||
if ( isset( $option['accountSetupComplete'] ) ) {
|
||||
$option['accountSetupComplete'] = (bool) $option['accountSetupComplete'];
|
||||
}
|
||||
if ( isset( $option['siteStatusComplete'] ) ) {
|
||||
$option['siteStatusComplete'] = (bool) $option['siteStatusComplete'];
|
||||
}
|
||||
if ( isset( $option['useSnippet'] ) ) {
|
||||
$option['useSnippet'] = (bool) $option['useSnippet'];
|
||||
}
|
||||
if ( isset( $option['autoAdsDisabled'] ) ) {
|
||||
$option['autoAdsDisabled'] = (array) $option['autoAdsDisabled'];
|
||||
}
|
||||
|
||||
if ( isset( $option['useAdBlockingRecoverySnippet'] ) ) {
|
||||
$option['useAdBlockingRecoverySnippet'] = (bool) $option['useAdBlockingRecoverySnippet'];
|
||||
}
|
||||
if ( isset( $option['useAdBlockingRecoveryErrorSnippet'] ) ) {
|
||||
$option['useAdBlockingRecoveryErrorSnippet'] = (bool) $option['useAdBlockingRecoveryErrorSnippet'];
|
||||
}
|
||||
if (
|
||||
isset( $option['adBlockingRecoverySetupStatus'] ) &&
|
||||
! in_array(
|
||||
$option['adBlockingRecoverySetupStatus'],
|
||||
array(
|
||||
'',
|
||||
self::AD_BLOCKING_RECOVERY_SETUP_STATUS_TAG_PLACED,
|
||||
self::AD_BLOCKING_RECOVERY_SETUP_STATUS_SETUP_CONFIRMED,
|
||||
),
|
||||
true
|
||||
)
|
||||
) {
|
||||
$option['adBlockingRecoverySetupStatus'] = $this->get()['adBlockingRecoverySetupStatus'];
|
||||
}
|
||||
}
|
||||
return $option;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\AdSense\Tag_Guard
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\AdSense
|
||||
* @copyright 2021 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 tag guard.
|
||||
*
|
||||
* @since 1.24.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Tag_Guard extends Module_Tag_Guard {
|
||||
|
||||
/**
|
||||
* Determines whether the guarded tag can be activated or not.
|
||||
*
|
||||
* @since 1.24.0
|
||||
* @since 1.30.0 Update to return FALSE on 404 pages deliberately.
|
||||
* @since 1.105.0 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() {
|
||||
$settings = $this->settings->get();
|
||||
|
||||
// For web stories, the tag must only be rendered if a story-specific ad unit is provided.
|
||||
if ( is_singular( 'web-story' ) && empty( $settings['webStoriesAdUnit'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ! empty( $settings['useSnippet'] ) && ! empty( $settings['clientID'] );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Modules\AdSense\Tag_Matchers
|
||||
*
|
||||
* @package Google\Site_Kit\Core\Modules\AdSense
|
||||
* @copyright 2024 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\Modules\AdSense;
|
||||
|
||||
use Google\Site_Kit\Core\Modules\Tags\Module_Tag_Matchers;
|
||||
use Google\Site_Kit\Core\Tags\Tag_Matchers_Interface;
|
||||
|
||||
/**
|
||||
* Class for Tag matchers.
|
||||
*
|
||||
* @since 1.119.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Tag_Matchers extends Module_Tag_Matchers implements Tag_Matchers_Interface {
|
||||
|
||||
/**
|
||||
* Holds array of regex tag matchers.
|
||||
*
|
||||
* @since 1.119.0
|
||||
*
|
||||
* @return array Array of regex matchers.
|
||||
*/
|
||||
public function regex_matchers() {
|
||||
return array(
|
||||
// Detect google_ad_client.
|
||||
"/google_ad_client: ?[\"|'](.*?)[\"|']/",
|
||||
|
||||
// Detect old style auto-ads tags.
|
||||
'/<(?:script|amp-auto-ads) [^>]*data-ad-client="([^"]+)"/',
|
||||
|
||||
// Detect new style auto-ads tags.
|
||||
'/<(?:script|amp-auto-ads)[^>]*src="[^"]*\\?client=(ca-pub-[^"]+)"[^>]*>/',
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\AdSense\Web_Tag
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\AdSense
|
||||
* @copyright 2021 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_Web_Tag;
|
||||
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;
|
||||
use Google\Site_Kit\Core\Tags\Tag_With_DNS_Prefetch_Trait;
|
||||
use Google\Site_Kit\Core\Util\BC_Functions;
|
||||
|
||||
/**
|
||||
* Class for Web tag.
|
||||
*
|
||||
* @since 1.24.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Web_Tag extends Module_Web_Tag {
|
||||
|
||||
use Method_Proxy_Trait, Tag_With_DNS_Prefetch_Trait;
|
||||
|
||||
/**
|
||||
* Registers tag hooks.
|
||||
*
|
||||
* @since 1.24.0
|
||||
*/
|
||||
public function register() {
|
||||
add_action( 'wp_head', $this->get_method_proxy_once( 'render' ) );
|
||||
|
||||
add_filter(
|
||||
'wp_resource_hints',
|
||||
$this->get_dns_prefetch_hints_callback( '//pagead2.googlesyndication.com' ),
|
||||
10,
|
||||
2
|
||||
);
|
||||
|
||||
$this->do_init_tag_action();
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the AdSense script tag.
|
||||
*
|
||||
* @since 1.24.0
|
||||
*/
|
||||
protected function render() {
|
||||
// If we haven't completed the account connection yet, we still insert the AdSense tag
|
||||
// because it is required for account verification.
|
||||
|
||||
$adsense_script_src = sprintf(
|
||||
'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=%s&host=%s',
|
||||
esc_attr( $this->tag_id ), // Site owner's web property code.
|
||||
'ca-host-pub-2644536267352236' // SiteKit's web property code.
|
||||
);
|
||||
|
||||
$adsense_script_attributes = array(
|
||||
'async' => true,
|
||||
'src' => $adsense_script_src,
|
||||
'crossorigin' => 'anonymous',
|
||||
);
|
||||
|
||||
$adsense_attributes = $this->get_tag_blocked_on_consent_attribute_array();
|
||||
|
||||
$auto_ads_opt = array();
|
||||
|
||||
$auto_ads_opt_filtered = apply_filters( 'googlesitekit_auto_ads_opt', $auto_ads_opt, $this->tag_id );
|
||||
|
||||
if ( is_array( $auto_ads_opt_filtered ) && ! empty( $auto_ads_opt_filtered ) ) {
|
||||
$strip_attributes = array(
|
||||
'google_ad_client' => '',
|
||||
'enable_page_level_ads' => '',
|
||||
);
|
||||
|
||||
$auto_ads_opt_filtered = array_diff_key( $auto_ads_opt_filtered, $strip_attributes );
|
||||
|
||||
$auto_ads_opt_sanitized = array();
|
||||
|
||||
foreach ( $auto_ads_opt_filtered as $key => $value ) {
|
||||
$new_key = 'data-';
|
||||
$new_key .= str_replace( '_', '-', $key );
|
||||
|
||||
$auto_ads_opt_sanitized[ $new_key ] = $value;
|
||||
}
|
||||
|
||||
$adsense_attributes = array_merge( $adsense_attributes, $auto_ads_opt_sanitized );
|
||||
}
|
||||
|
||||
printf( "\n<!-- %s -->\n", esc_html__( 'Google AdSense snippet added by Site Kit', 'google-site-kit' ) );
|
||||
BC_Functions::wp_print_script_tag( array_merge( $adsense_script_attributes, $adsense_attributes ) );
|
||||
printf( "\n<!-- %s -->\n", esc_html__( 'End Google AdSense snippet added by Site Kit', 'google-site-kit' ) );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user