initial commit

This commit is contained in:
2024-04-29 13:12:44 +05:45
commit 34887303c5
19300 changed files with 5268802 additions and 0 deletions

View File

@@ -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' ) );
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,192 @@
<?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;
/**
* Class for AdSense settings.
*
* @since 1.2.0
* @access private
* @ignore
*/
class Settings extends Module_Settings implements Setting_With_Owned_Keys_Interface {
use Setting_With_Legacy_Keys_Trait, Setting_With_Owned_Keys_Trait;
const OPTION = 'googlesitekit_adsense_settings';
/**
* 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;
}
);
}
/**
* 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',
);
}
/**
* Gets the default value.
*
* @since 1.2.0
*
* @return array
*/
protected function get_default() {
return array(
'ownerID' => 0,
'accountID' => '',
'autoAdsDisabled' => array(),
'clientID' => '',
'accountStatus' => '',
'siteStatus' => '',
'accountSetupComplete' => false,
'siteSetupComplete' => false,
'useSnippet' => true,
'webStoriesAdUnit' => '',
);
}
/**
* 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'];
}
}
return $option;
};
}
}

View File

@@ -0,0 +1,48 @@
<?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.
*
* @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.
if ( is_singular( 'web-story' ) && empty( $settings['webStoriesAdUnit'] ) ) {
return false;
}
return ! empty( $settings['useSnippet'] ) && ! empty( $settings['clientID'] );
}
}

View File

@@ -0,0 +1,98 @@
<?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',
esc_attr( $this->tag_id )
);
$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' ) );
}
}