update
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\Ads\AMP_Tag
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\Ads
|
||||
* @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\Modules\Ads;
|
||||
|
||||
use Google\Site_Kit\Core\Modules\Tags\Module_AMP_Tag;
|
||||
use Google\Site_Kit\Core\Tags\Tag_With_Linker_Interface;
|
||||
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;
|
||||
use Google\Site_Kit\Core\Tags\Tag_With_Linker_Trait;
|
||||
|
||||
/**
|
||||
* Class for AMP tag.
|
||||
*
|
||||
* @since 1.125.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class AMP_Tag extends Module_AMP_Tag implements Tag_With_Linker_Interface {
|
||||
|
||||
use Method_Proxy_Trait, Tag_With_Linker_Trait;
|
||||
|
||||
/**
|
||||
* Sets the current home domain.
|
||||
*
|
||||
* @since 1.125.0
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
*/
|
||||
public function set_home_domain( $domain ) {
|
||||
$this->home_domain = $domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers tag hooks.
|
||||
*
|
||||
* @since 1.125.0
|
||||
*/
|
||||
public function register() {
|
||||
$render = $this->get_method_proxy_once( 'render' );
|
||||
|
||||
// Which actions are run depends on the version of the AMP Plugin
|
||||
// (https://amp-wp.org/) available. Version >=1.3 exposes a
|
||||
// new, `amp_print_analytics` action.
|
||||
// For all AMP modes, AMP plugin version >=1.3.
|
||||
add_action( 'amp_print_analytics', $render );
|
||||
// For AMP Standard and Transitional, AMP plugin version <1.3.
|
||||
add_action( 'wp_footer', $render, 20 );
|
||||
// For AMP Reader, AMP plugin version <1.3.
|
||||
add_action( 'amp_post_template_footer', $render, 20 );
|
||||
// For Web Stories plugin.
|
||||
add_action( 'web_stories_print_analytics', $render );
|
||||
|
||||
// Load amp-analytics component for AMP Reader.
|
||||
$this->enqueue_amp_reader_component_script( 'amp-analytics', 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js' );
|
||||
|
||||
$this->do_init_tag_action();
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs gtag <amp-analytics> tag.
|
||||
*
|
||||
* @since 1.125.0
|
||||
*/
|
||||
protected function render() {
|
||||
$config = $this->get_tag_config();
|
||||
|
||||
$gtag_amp_opt = array(
|
||||
'optoutElementId' => '__gaOptOutExtension',
|
||||
'vars' => array(
|
||||
'gtag_id' => $this->tag_id,
|
||||
'config' => $config,
|
||||
),
|
||||
);
|
||||
|
||||
printf( "\n<!-- %s -->\n", esc_html__( 'Google Ads AMP snippet added by Site Kit', 'google-site-kit' ) );
|
||||
|
||||
printf(
|
||||
'<amp-analytics type="gtag" data-credentials="include"%s><script type="application/json">%s</script></amp-analytics>',
|
||||
$this->get_tag_blocked_on_consent_attribute(), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
wp_json_encode( $gtag_amp_opt )
|
||||
);
|
||||
|
||||
printf( "\n<!-- %s -->\n", esc_html__( 'End Google Ads AMP snippet added by Site Kit', 'google-site-kit' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tag config as used in the gtag data vars.
|
||||
*
|
||||
* @since 1.125.0
|
||||
*
|
||||
* @return array Tag configuration.
|
||||
*/
|
||||
protected function get_tag_config() {
|
||||
$config = array(
|
||||
$this->tag_id => array(
|
||||
'groups' => 'default',
|
||||
),
|
||||
);
|
||||
|
||||
return $this->add_linker_to_tag_config( $config );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\Ads\Has_Tag_Guard
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\Ads
|
||||
* @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\Modules\Ads;
|
||||
|
||||
use Google\Site_Kit\Core\Modules\Tags\Module_Tag_Guard;
|
||||
|
||||
/**
|
||||
* Class for the Ads tag guard.
|
||||
*
|
||||
* @since 1.124.0
|
||||
* @since 1.128.0 Renamed class to be specific to presence of web tag.
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Has_Tag_Guard extends Module_Tag_Guard {
|
||||
/**
|
||||
* Modules tag_id value.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*
|
||||
* @var String
|
||||
*/
|
||||
protected $tag_id;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*
|
||||
* @param string $tag_id Modules web tag string value.
|
||||
*/
|
||||
public function __construct( $tag_id = '' ) {
|
||||
$this->tag_id = $tag_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether the guarded tag can be activated or not.
|
||||
*
|
||||
* @since 1.124.0
|
||||
* @since 1.128.0 Updated logic to check modules tag_id value..
|
||||
*
|
||||
* @return bool TRUE if guarded tag can be activated, otherwise FALSE or an error.
|
||||
*/
|
||||
public function can_activate() {
|
||||
return ! empty( $this->tag_id );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\Ads\PAX_Config
|
||||
*
|
||||
* @package Google\Site_Kit
|
||||
* @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\Modules\Ads;
|
||||
|
||||
use Google\Site_Kit\Context;
|
||||
use Google\Site_Kit\Core\Authentication\Token;
|
||||
|
||||
/**
|
||||
* Class representing PAX configuration.
|
||||
*
|
||||
* @since 1.128.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class PAX_Config {
|
||||
|
||||
/**
|
||||
* Context instance.
|
||||
*
|
||||
* @since 1.128.0
|
||||
* @var Context
|
||||
*/
|
||||
private $context;
|
||||
|
||||
/**
|
||||
* Token instance.
|
||||
*
|
||||
* @since 1.128.0
|
||||
* @var Token
|
||||
*/
|
||||
private $token;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.128.0
|
||||
*
|
||||
* @param Context $context Context instance.
|
||||
* @param Token $token Token instance.
|
||||
*/
|
||||
public function __construct( Context $context, Token $token ) {
|
||||
$this->context = $context;
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the configuration data.
|
||||
*
|
||||
* @since 1.128.0
|
||||
* @return array
|
||||
*/
|
||||
public function get() {
|
||||
$token = $this->token->get();
|
||||
|
||||
return array(
|
||||
'authAccess' => array(
|
||||
'oauthTokenAccess' => array(
|
||||
'token' => $token['access_token'] ?? '',
|
||||
),
|
||||
),
|
||||
'locale' => substr( $this->context->get_locale( 'user' ), 0, 2 ),
|
||||
'debuggingConfig' => array(
|
||||
'env' => $this->get_env(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the environment configuration.
|
||||
*
|
||||
* @since 1.128.0
|
||||
* @return string
|
||||
*/
|
||||
protected function get_env() {
|
||||
$allowed = array( 'PROD', 'QA_PROD' );
|
||||
|
||||
if ( defined( 'GOOGLESITEKIT_PAX_ENV' ) && in_array( GOOGLESITEKIT_PAX_ENV, $allowed, true ) ) {
|
||||
return GOOGLESITEKIT_PAX_ENV;
|
||||
}
|
||||
|
||||
return 'PROD';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\Ads\Settings
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\Ads
|
||||
* @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\Modules\Ads;
|
||||
|
||||
use Google\Site_Kit\Core\Modules\Module_Settings;
|
||||
use Google\Site_Kit\Core\Storage\Setting_With_Owned_Keys_Interface;
|
||||
use Google\Site_Kit\Core\Storage\Setting_With_Owned_Keys_Trait;
|
||||
|
||||
/**
|
||||
* Class for Ads settings.
|
||||
*
|
||||
* @since 1.122.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Settings extends Module_Settings implements Setting_With_Owned_Keys_Interface {
|
||||
use Setting_With_Owned_Keys_Trait;
|
||||
|
||||
const OPTION = 'googlesitekit_ads_settings';
|
||||
|
||||
/**
|
||||
* Registers the setting in WordPress.
|
||||
*
|
||||
* @since 1.122.0
|
||||
*/
|
||||
public function register() {
|
||||
parent::register();
|
||||
|
||||
$this->register_owned_keys();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default value.
|
||||
*
|
||||
* @since 1.122.0
|
||||
* @since 1.126.0 Added new settings fields for PAX.
|
||||
*
|
||||
* @return array An array of default settings values.
|
||||
*/
|
||||
protected function get_default() {
|
||||
return array(
|
||||
'conversionID' => '',
|
||||
'paxConversionID' => '',
|
||||
'extCustomerID' => '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns keys for owned settings.
|
||||
*
|
||||
* @since 1.122.0
|
||||
* @since 1.126.0 Added new settings fields for PAX.
|
||||
*
|
||||
* @return array An array of keys for owned settings.
|
||||
*/
|
||||
public function get_owned_keys() {
|
||||
return array(
|
||||
'conversionID',
|
||||
'paxConversionID',
|
||||
'extCustomerID',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\Ads\Tag_Matchers
|
||||
*
|
||||
* @package Google\Site_Kit\Core\Modules\Ads
|
||||
* @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\Modules\Ads;
|
||||
|
||||
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.124.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Tag_Matchers extends Module_Tag_Matchers implements Tag_Matchers_Interface {
|
||||
|
||||
/**
|
||||
* Holds array of regex tag matchers.
|
||||
*
|
||||
* @since 1.124.0
|
||||
*
|
||||
* @return array Array of regex matchers.
|
||||
*/
|
||||
public function regex_matchers() {
|
||||
return array(
|
||||
"/gtag\\s*\\(\\s*['|\"]config['|\"]\\s*,\\s*['|\"](AW-[0-9]+)['|\"]\\s*\\)/i",
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\Ads\Web_Tag
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\Ads
|
||||
* @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\Modules\Ads;
|
||||
|
||||
use Google\Site_Kit\Core\Modules\Tags\Module_Web_Tag;
|
||||
use Google\Site_Kit\Core\Tags\GTag;
|
||||
use Google\Site_Kit\Core\Tags\Tag_With_Linker_Interface;
|
||||
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;
|
||||
|
||||
/**
|
||||
* Class for Web tag.
|
||||
*
|
||||
* @since 1.124.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Web_Tag extends Module_Web_Tag implements Tag_With_Linker_Interface {
|
||||
|
||||
use Method_Proxy_Trait;
|
||||
|
||||
/**
|
||||
* Sets the current home domain.
|
||||
*
|
||||
* @since 1.125.0
|
||||
*
|
||||
* @param string $domain Domain name.
|
||||
*/
|
||||
public function set_home_domain( $domain ) {
|
||||
$this->home_domain = $domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers tag hooks.
|
||||
*
|
||||
* @since 1.124.0
|
||||
*/
|
||||
public function register() {
|
||||
// Set a lower priority here to let Analytics sets up its tag first.
|
||||
add_action(
|
||||
'googlesitekit_setup_gtag',
|
||||
$this->get_method_proxy( 'setup_gtag' ),
|
||||
20
|
||||
);
|
||||
|
||||
$this->do_init_tag_action();
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs gtag snippet.
|
||||
*
|
||||
* @since 1.124.0
|
||||
*/
|
||||
protected function render() {
|
||||
// Do nothing, gtag script is enqueued.
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures gtag script.
|
||||
*
|
||||
* @since 1.124.0
|
||||
*
|
||||
* @param GTag $gtag GTag instance.
|
||||
*/
|
||||
protected function setup_gtag( $gtag ) {
|
||||
$gtag->add_tag( $this->tag_id );
|
||||
|
||||
$filter_google_gtagjs = function ( $tag, $handle ) {
|
||||
if ( GTag::HANDLE !== $handle ) {
|
||||
return $tag;
|
||||
}
|
||||
|
||||
// Retain this comment for detection of Site Kit placed tag.
|
||||
$snippet_comment = sprintf( "\n<!-- %s -->\n", esc_html__( 'Google Ads snippet added by Site Kit', 'google-site-kit' ) );
|
||||
|
||||
return $snippet_comment . $tag;
|
||||
};
|
||||
|
||||
add_filter( 'script_loader_tag', $filter_google_gtagjs, 10, 2 );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user