2022-12-15 17:34:08 +01:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Class Google\Site_Kit\Modules\Analytics_4\Settings
|
|
|
|
|
*
|
|
|
|
|
* @package Google\Site_Kit\Modules\Analytics_4
|
|
|
|
|
* @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\Analytics_4;
|
|
|
|
|
|
|
|
|
|
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;
|
2023-12-07 09:44:11 +01:00
|
|
|
use Google\Site_Kit\Core\Storage\Setting_With_ViewOnly_Keys_Interface;
|
|
|
|
|
use Google\Site_Kit\Core\Util\Feature_Flags;
|
2024-02-16 11:03:01 +01:00
|
|
|
use Google\Site_Kit\Core\Util\Method_Proxy_Trait;
|
2022-12-15 17:34:08 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class for Analytics 4 settings.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.30.0
|
|
|
|
|
* @access private
|
|
|
|
|
* @ignore
|
|
|
|
|
*/
|
2023-12-07 09:44:11 +01:00
|
|
|
class Settings extends Module_Settings implements Setting_With_Owned_Keys_Interface, Setting_With_ViewOnly_Keys_Interface {
|
2023-04-26 17:39:43 +02:00
|
|
|
|
2022-12-15 17:34:08 +01:00
|
|
|
use Setting_With_Owned_Keys_Trait;
|
2024-02-16 11:03:01 +01:00
|
|
|
use Method_Proxy_Trait;
|
2022-12-15 17:34:08 +01:00
|
|
|
|
|
|
|
|
const OPTION = 'googlesitekit_analytics-4_settings';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Registers the setting in WordPress.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.30.0
|
|
|
|
|
*/
|
|
|
|
|
public function register() {
|
|
|
|
|
parent::register();
|
|
|
|
|
|
|
|
|
|
$this->register_owned_keys();
|
|
|
|
|
|
2024-02-16 11:03:01 +01:00
|
|
|
// Since migration of Analytics module settings into Analytics 4 settings,
|
|
|
|
|
// if data was saved previously, it needs to be recovered.
|
|
|
|
|
add_filter(
|
|
|
|
|
'option_' . self::OPTION,
|
|
|
|
|
$this->get_method_proxy( 'retrieve_missing_settings' ),
|
|
|
|
|
10
|
|
|
|
|
);
|
2023-04-26 17:39:43 +02:00
|
|
|
}
|
|
|
|
|
|
2022-12-15 17:34:08 +01:00
|
|
|
/**
|
|
|
|
|
* Returns keys for owned settings.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.30.0
|
|
|
|
|
*
|
|
|
|
|
* @return array An array of keys for owned settings.
|
|
|
|
|
*/
|
|
|
|
|
public function get_owned_keys() {
|
|
|
|
|
return array(
|
2024-02-16 11:03:01 +01:00
|
|
|
'accountID',
|
2022-12-15 17:34:08 +01:00
|
|
|
'propertyID',
|
|
|
|
|
'webDataStreamID',
|
|
|
|
|
'measurementID',
|
2023-01-25 20:43:46 +01:00
|
|
|
'googleTagID',
|
|
|
|
|
'googleTagAccountID',
|
|
|
|
|
'googleTagContainerID',
|
2022-12-15 17:34:08 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-07 09:44:11 +01:00
|
|
|
/**
|
|
|
|
|
* Returns keys for view-only settings.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.113.0
|
|
|
|
|
*
|
|
|
|
|
* @return array An array of keys for view-only settings.
|
|
|
|
|
*/
|
|
|
|
|
public function get_view_only_keys() {
|
2024-02-16 11:03:01 +01:00
|
|
|
return array( 'availableCustomDimensions', 'adSenseLinked' );
|
2023-12-07 09:44:11 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-15 17:34:08 +01:00
|
|
|
/**
|
|
|
|
|
* Gets the default value.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.30.0
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function get_default() {
|
|
|
|
|
return array(
|
2023-12-07 09:44:11 +01:00
|
|
|
'ownerID' => 0,
|
2024-02-16 11:03:01 +01:00
|
|
|
'accountID' => '',
|
|
|
|
|
'adsConversionID' => '',
|
2023-12-07 09:44:11 +01:00
|
|
|
'propertyID' => '',
|
|
|
|
|
'webDataStreamID' => '',
|
|
|
|
|
'measurementID' => '',
|
2024-02-16 11:03:01 +01:00
|
|
|
'trackingDisabled' => array( 'loggedinUsers' ),
|
2023-12-07 09:44:11 +01:00
|
|
|
'useSnippet' => true,
|
2024-02-16 11:03:01 +01:00
|
|
|
'canUseSnippet' => true,
|
2023-12-07 09:44:11 +01:00
|
|
|
'googleTagID' => '',
|
|
|
|
|
'googleTagAccountID' => '',
|
|
|
|
|
'googleTagContainerID' => '',
|
|
|
|
|
'googleTagLastSyncedAtMs' => 0,
|
|
|
|
|
'availableCustomDimensions' => null,
|
2024-02-16 11:03:01 +01:00
|
|
|
'propertyCreateTime' => 0,
|
|
|
|
|
'adSenseLinked' => false,
|
|
|
|
|
'adSenseLinkedLastSyncedAt' => 0,
|
2022-12-15 17:34:08 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the callback for sanitizing the setting's value before saving.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.30.0
|
|
|
|
|
*
|
|
|
|
|
* @return callable|null
|
|
|
|
|
*/
|
|
|
|
|
protected function get_sanitize_callback() {
|
|
|
|
|
return function( $option ) {
|
|
|
|
|
if ( is_array( $option ) ) {
|
|
|
|
|
if ( isset( $option['useSnippet'] ) ) {
|
|
|
|
|
$option['useSnippet'] = (bool) $option['useSnippet'];
|
|
|
|
|
}
|
2024-02-16 11:03:01 +01:00
|
|
|
if ( isset( $option['canUseSnippet'] ) ) {
|
|
|
|
|
$option['canUseSnippet'] = (bool) $option['canUseSnippet'];
|
|
|
|
|
}
|
2023-01-25 20:43:46 +01:00
|
|
|
if ( isset( $option['googleTagID'] ) ) {
|
|
|
|
|
if ( ! preg_match( '/^(G|GT|AW)-[a-zA-Z0-9]+$/', $option['googleTagID'] ) ) {
|
|
|
|
|
$option['googleTagID'] = '';
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-16 11:03:01 +01:00
|
|
|
if ( isset( $option['trackingDisabled'] ) ) {
|
|
|
|
|
// Prevent other options from being saved if 'loggedinUsers' is selected.
|
|
|
|
|
if ( in_array( 'loggedinUsers', $option['trackingDisabled'], true ) ) {
|
|
|
|
|
$option['trackingDisabled'] = array( 'loggedinUsers' );
|
|
|
|
|
} else {
|
|
|
|
|
$option['trackingDisabled'] = (array) $option['trackingDisabled'];
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-26 17:39:43 +02:00
|
|
|
|
|
|
|
|
$numeric_properties = array( 'googleTagAccountID', 'googleTagContainerID' );
|
|
|
|
|
foreach ( $numeric_properties as $numeric_property ) {
|
|
|
|
|
if ( isset( $option[ $numeric_property ] ) ) {
|
|
|
|
|
if ( ! is_numeric( $option[ $numeric_property ] ) || ! $option[ $numeric_property ] > 0 ) {
|
|
|
|
|
$option[ $numeric_property ] = '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-07 09:44:11 +01:00
|
|
|
|
2024-02-16 11:03:01 +01:00
|
|
|
if ( isset( $option['availableCustomDimensions'] ) ) {
|
2023-12-07 09:44:11 +01:00
|
|
|
if ( is_array( $option['availableCustomDimensions'] ) ) {
|
|
|
|
|
$valid_dimensions = array_filter(
|
|
|
|
|
$option['availableCustomDimensions'],
|
|
|
|
|
function( $dimension ) {
|
|
|
|
|
return is_string( $dimension ) && strpos( $dimension, 'googlesitekit_' ) === 0;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$option['availableCustomDimensions'] = array_values( $valid_dimensions );
|
|
|
|
|
} else {
|
|
|
|
|
$option['availableCustomDimensions'] = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-16 11:03:01 +01:00
|
|
|
|
|
|
|
|
if ( isset( $option['adSenseLinked'] ) ) {
|
|
|
|
|
$option['adSenseLinked'] = (bool) $option['adSenseLinked'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $option['adSenseLinkedLastSyncedAt'] ) ) {
|
|
|
|
|
if ( ! is_int( $option['adSenseLinkedLastSyncedAt'] ) ) {
|
|
|
|
|
$option['adSenseLinkedLastSyncedAt'] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-15 17:34:08 +01:00
|
|
|
}
|
2023-04-26 17:39:43 +02:00
|
|
|
|
2022-12-15 17:34:08 +01:00
|
|
|
return $option;
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-04-26 17:39:43 +02:00
|
|
|
|
|
|
|
|
/**
|
2024-02-16 11:03:01 +01:00
|
|
|
* Sync settings migrated from `Analytics` module if they are not
|
|
|
|
|
* present in `Analytics_4` settings.
|
2023-04-26 17:39:43 +02:00
|
|
|
*
|
2024-02-16 11:03:01 +01:00
|
|
|
* This ensures backward compatibility for the users who had Site Kit installed
|
|
|
|
|
* before migrating to the singular Analytics module. As some settings were defined
|
|
|
|
|
* in old `Analtyics` module and re-used here.
|
2023-04-26 17:39:43 +02:00
|
|
|
*
|
2024-02-16 11:03:01 +01:00
|
|
|
* @since 1.118.0
|
2023-04-26 17:39:43 +02:00
|
|
|
*
|
2024-02-16 11:03:01 +01:00
|
|
|
* @param array $option Analytics 4 settings.
|
|
|
|
|
* @return array Missing Analytics 4 settings array, or empty array if no setting is missing.
|
2023-04-26 17:39:43 +02:00
|
|
|
*/
|
2024-02-16 11:03:01 +01:00
|
|
|
protected function retrieve_missing_settings( $option ) {
|
|
|
|
|
if ( ! is_array( $option ) ) {
|
|
|
|
|
return $option;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$recovered_settings = array();
|
|
|
|
|
$keys_to_check = array(
|
|
|
|
|
'accountID',
|
|
|
|
|
'adsConversionID',
|
|
|
|
|
'canUseSnippet',
|
|
|
|
|
'trackingDisabled',
|
|
|
|
|
);
|
|
|
|
|
$missing_settings = array_diff( $keys_to_check, array_keys( $option ) );
|
|
|
|
|
|
|
|
|
|
if ( empty( $missing_settings ) ) {
|
|
|
|
|
return $option;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$analytics_settings = get_option( 'googlesitekit_analytics_settings' );
|
|
|
|
|
|
|
|
|
|
array_walk(
|
|
|
|
|
$missing_settings,
|
|
|
|
|
function( $setting ) use ( &$recovered_settings, $analytics_settings ) {
|
|
|
|
|
$recovered_settings[ $setting ] = $analytics_settings[ $setting ];
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( ! empty( $recovered_settings ) ) {
|
|
|
|
|
return $option + $recovered_settings;
|
|
|
|
|
}
|
2023-04-26 17:39:43 +02:00
|
|
|
|
2024-02-16 11:03:01 +01:00
|
|
|
return $option;
|
2023-04-26 17:39:43 +02:00
|
|
|
}
|
|
|
|
|
|
2022-12-15 17:34:08 +01:00
|
|
|
}
|