initial commit
This commit is contained in:
@ -0,0 +1,112 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\Optimize\Settings
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\Optimize
|
||||
* @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\Optimize;
|
||||
|
||||
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 Optimize 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_optimize_settings';
|
||||
|
||||
/**
|
||||
* Registers the setting in WordPress.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public function register() {
|
||||
parent::register();
|
||||
|
||||
$this->register_legacy_keys_migration(
|
||||
array(
|
||||
'AMPExperimentJson' => 'ampExperimentJSON',
|
||||
'ampExperimentJson' => 'ampExperimentJSON',
|
||||
'optimize_id' => 'optimizeID',
|
||||
'optimizeId' => 'optimizeID',
|
||||
)
|
||||
);
|
||||
|
||||
$this->register_owned_keys();
|
||||
|
||||
add_filter(
|
||||
'option_' . self::OPTION,
|
||||
function ( $option ) {
|
||||
// Migrate legacy values where this was saved as decoded JSON object.
|
||||
if ( is_array( $option ) && array_key_exists( 'ampExperimentJSON', $option ) && ! is_string( $option['ampExperimentJSON'] ) ) {
|
||||
if ( empty( $option['ampExperimentJSON'] ) ) {
|
||||
$option['ampExperimentJSON'] = '';
|
||||
} else {
|
||||
$option['ampExperimentJSON'] = wp_json_encode( $option['ampExperimentJSON'] );
|
||||
}
|
||||
}
|
||||
|
||||
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(
|
||||
'optimizeID',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default value.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_default() {
|
||||
return array(
|
||||
'ownerID' => 0,
|
||||
'ampExperimentJSON' => '',
|
||||
'optimizeID' => '',
|
||||
'placeAntiFlickerSnippet' => false,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the callback for sanitizing the setting's value before saving.
|
||||
*
|
||||
* @since 1.39.0
|
||||
*
|
||||
* @return callable|null
|
||||
*/
|
||||
protected function get_sanitize_callback() {
|
||||
return function( $option ) {
|
||||
if ( is_array( $option ) ) {
|
||||
if ( isset( $option['placeAntiFlickerSnippet'] ) ) {
|
||||
$option['placeAntiFlickerSnippet'] = (bool) $option['placeAntiFlickerSnippet'];
|
||||
}
|
||||
}
|
||||
return $option;
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\Optimize\Tag_Guard
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\Optimize
|
||||
* @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\Optimize;
|
||||
|
||||
use Google\Site_Kit\Core\Modules\Tags\Module_Tag_Guard;
|
||||
|
||||
/**
|
||||
* Tag guard class for the Optimize module that blocks the tag placement if it is disabled.
|
||||
*
|
||||
* @since 1.39.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Tag_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['placeAntiFlickerSnippet'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $settings['placeAntiFlickerSnippet'];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Modules\Optimize\Web_Tag
|
||||
*
|
||||
* @package Google\Site_Kit\Modules\Optimize
|
||||
* @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\Optimize;
|
||||
|
||||
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.39.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Web_Tag extends Module_Web_Tag {
|
||||
|
||||
use Method_Proxy_Trait;
|
||||
|
||||
/**
|
||||
* Registers tag hooks.
|
||||
*
|
||||
* @since 1.39.0
|
||||
*/
|
||||
public function register() {
|
||||
add_action( 'wp_head', $this->get_method_proxy_once( 'render' ), 1 );
|
||||
|
||||
$this->do_init_tag_action();
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the Optimize anti-flicker script tag.
|
||||
*
|
||||
* @since 1.39.0
|
||||
*/
|
||||
protected function render() {
|
||||
|
||||
$anti_flicker_script = sprintf(
|
||||
"(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;
|
||||
h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};
|
||||
(a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;
|
||||
})(window,document.documentElement,'async-hide','dataLayer',4000,
|
||||
{'%s':true});",
|
||||
esc_js( $this->tag_id )
|
||||
);
|
||||
|
||||
printf( "\n<!-- %s -->\n", esc_html__( 'Anti-flicker snippet added by Site Kit', 'google-site-kit' ) );
|
||||
echo '<style>.async-hide { opacity: 0 !important} </style>';
|
||||
BC_Functions::wp_print_inline_script_tag( $anti_flicker_script );
|
||||
printf( "\n<!-- %s -->\n", esc_html__( 'End Anti-flicker snippet added by Site Kit', 'google-site-kit' ) );
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user