update
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Assets\Asset
|
||||
*
|
||||
* @package Google\Site_Kit
|
||||
* @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\Core\Assets;
|
||||
|
||||
use Google\Site_Kit\Context;
|
||||
|
||||
/**
|
||||
* Class representing a single asset.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
abstract class Asset {
|
||||
|
||||
// Various page contexts for Site Kit in the WordPress Admin.
|
||||
const CONTEXT_ADMIN_GLOBAL = 'admin-global';
|
||||
const CONTEXT_ADMIN_POST_EDITOR = 'admin-post-editor';
|
||||
const CONTEXT_ADMIN_POSTS = 'admin-posts';
|
||||
const CONTEXT_ADMIN_SITEKIT = 'admin-sitekit';
|
||||
|
||||
/**
|
||||
* Unique asset handle.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var string
|
||||
*/
|
||||
protected $handle;
|
||||
|
||||
/**
|
||||
* Asset arguments.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @var array
|
||||
*/
|
||||
protected $args = array();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @since 1.37.0 Add the 'load_contexts' argument.
|
||||
*
|
||||
* @param string $handle Unique asset handle.
|
||||
* @param array $args {
|
||||
* Associative array of asset arguments.
|
||||
*
|
||||
* @type string $src Required asset source URL.
|
||||
* @type array $dependencies List of asset dependencies. Default empty array.
|
||||
* @type string $version Asset version. Default is the version of Site Kit.
|
||||
* @type bool $fallback Whether to only register as a fallback. Default false.
|
||||
* @type callable $before_print Optional callback to execute before printing. Default none.
|
||||
* @type string[] $load_contexts Optional array of page context values to determine on which page types to load this asset (see the `CONTEXT_` variables above).
|
||||
* }
|
||||
*/
|
||||
public function __construct( $handle, array $args ) {
|
||||
$this->handle = $handle;
|
||||
$this->args = wp_parse_args(
|
||||
$args,
|
||||
array(
|
||||
'src' => '',
|
||||
'dependencies' => array(),
|
||||
'version' => GOOGLESITEKIT_VERSION,
|
||||
'fallback' => false,
|
||||
'before_print' => null,
|
||||
'load_contexts' => array( self::CONTEXT_ADMIN_SITEKIT ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the notice handle.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return string Unique notice handle.
|
||||
*/
|
||||
public function get_handle() {
|
||||
return $this->handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if the specified context exists for the current request.
|
||||
*
|
||||
* @since 1.37.0
|
||||
*
|
||||
* @param string $context Context value (see the `CONTEXT_` variables above).
|
||||
* @return bool TRUE if context exists; FALSE otherwise.
|
||||
*/
|
||||
public function has_context( $context ) {
|
||||
return in_array( $context, $this->args['load_contexts'], true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the asset.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @since 1.15.0 Adds $context parameter.
|
||||
*
|
||||
* @param Context $context Plugin context.
|
||||
*/
|
||||
abstract public function register( Context $context );
|
||||
|
||||
/**
|
||||
* Enqueues the asset.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
abstract public function enqueue();
|
||||
|
||||
/**
|
||||
* Executes the extra callback if defined before printing the asset.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*/
|
||||
final public function before_print() {
|
||||
if ( ! is_callable( $this->args['before_print'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
call_user_func( $this->args['before_print'], $this->handle );
|
||||
}
|
||||
}
|
||||
1101
wp-content/plugins/google-site-kit/includes/Core/Assets/Assets.php
Normal file
1101
wp-content/plugins/google-site-kit/includes/Core/Assets/Assets.php
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Assets\Manifest
|
||||
*
|
||||
* @package GoogleSite_Kit
|
||||
* @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\Core\Assets;
|
||||
|
||||
use Google\Site_Kit\Plugin;
|
||||
|
||||
/**
|
||||
* Assets manifest.
|
||||
*
|
||||
* @since 1.15.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Manifest {
|
||||
|
||||
/**
|
||||
* Entries as $handle => [ $filename, $hash ] map.
|
||||
*
|
||||
* @since 1.48.0
|
||||
* @var array
|
||||
*/
|
||||
private static $data;
|
||||
|
||||
/**
|
||||
* Gets the manifest entry for the given handle.
|
||||
*
|
||||
* @since 1.48.0
|
||||
*
|
||||
* @param string $handle Asset handle to get manifest data for.
|
||||
* @return array List of $filename and $hash, or `null` for both if not found.
|
||||
*/
|
||||
public static function get( $handle ) {
|
||||
if ( null === self::$data ) {
|
||||
self::load();
|
||||
}
|
||||
|
||||
if ( isset( self::$data[ $handle ] ) ) {
|
||||
return self::$data[ $handle ];
|
||||
}
|
||||
|
||||
return array( null, null );
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the generated manifest file.
|
||||
*
|
||||
* @since 1.48.0
|
||||
*/
|
||||
private static function load() {
|
||||
$path = Plugin::instance()->context()->path( 'dist/manifest.php' );
|
||||
|
||||
if ( file_exists( $path ) ) {
|
||||
// If the include fails, $data will be `false`
|
||||
// so this should only be attempted once.
|
||||
self::$data = include $path;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Assets\Script
|
||||
*
|
||||
* @package Google\Site_Kit
|
||||
* @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\Core\Assets;
|
||||
|
||||
use Google\Site_Kit\Context;
|
||||
use Google\Site_Kit\Core\Util\BC_Functions;
|
||||
|
||||
/**
|
||||
* Class representing a single script.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Script extends Asset {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $handle Unique script handle.
|
||||
* @param array $args {
|
||||
* Associative array of script arguments.
|
||||
*
|
||||
* @type string $src Required script source URL.
|
||||
* @type array $dependencies List of script dependencies. Default empty array.
|
||||
* @type string $version Script version. Default is the version of Site Kit.
|
||||
* @type bool $fallback Whether to only register as a fallback. Default false.
|
||||
* @type callable $before_print Optional callback to execute before printing. Default none.
|
||||
* @type bool $in_footer Whether to load script in footer. Default true.
|
||||
* @type string $execution How to handle script execution, e.g. 'defer'. Default empty string.
|
||||
* }
|
||||
*/
|
||||
public function __construct( $handle, array $args ) {
|
||||
parent::__construct( $handle, $args );
|
||||
|
||||
$this->args = wp_parse_args(
|
||||
$this->args,
|
||||
array(
|
||||
'in_footer' => true,
|
||||
'execution' => '',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the script.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @since 1.15.0 Adds $context parameter.
|
||||
*
|
||||
* @param Context $context Plugin context.
|
||||
*/
|
||||
public function register( Context $context ) {
|
||||
if ( $this->args['fallback'] && wp_script_is( $this->handle, 'registered' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$src = $this->args['src'];
|
||||
$version = $this->args['version'];
|
||||
|
||||
if ( $src ) {
|
||||
$entry = Manifest::get( $this->handle );
|
||||
|
||||
if ( is_array( $entry[0] ) ) {
|
||||
// If the first entry item is an array, we can assume `$entry` is an array of entries in the format filename => hash.
|
||||
// In this scenario we want to match the nested entry against the filename provided in `$src`.
|
||||
$src_filename = basename( $src );
|
||||
|
||||
foreach ( $entry as $entry_pair ) {
|
||||
if ( $this->is_matching_manifest_entry( $entry_pair, $src_filename ) ) {
|
||||
list( $filename, $hash ) = $entry_pair;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Otherwise, `$entry` will be a single entry in the format filename => hash.
|
||||
list( $filename, $hash ) = $entry;
|
||||
}
|
||||
|
||||
if ( $filename ) {
|
||||
$src = $context->url( 'dist/assets/js/' . $filename );
|
||||
$version = $hash;
|
||||
}
|
||||
}
|
||||
|
||||
wp_register_script(
|
||||
$this->handle,
|
||||
$src,
|
||||
(array) $this->args['dependencies'],
|
||||
$version,
|
||||
$this->args['in_footer']
|
||||
);
|
||||
|
||||
if ( ! empty( $this->args['execution'] ) ) {
|
||||
wp_script_add_data( $this->handle, 'script_execution', $this->args['execution'] );
|
||||
}
|
||||
|
||||
if ( ! empty( $src ) ) {
|
||||
$this->set_locale_data();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues the script.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function enqueue() {
|
||||
wp_enqueue_script( $this->handle );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the provided manifest entry matches the given filename.
|
||||
*
|
||||
* @since 1.89.0
|
||||
*
|
||||
* @param array $entry Array of filename, hash.
|
||||
* @param string $src_filename Filename to check.
|
||||
* @return bool
|
||||
*/
|
||||
private function is_matching_manifest_entry( array $entry, $src_filename ) {
|
||||
list ( $filename, $hash ) = $entry;
|
||||
|
||||
if ( ! isset( $hash ) ) {
|
||||
// If the hash is not set, it means the hash is embedded in the entry filename.
|
||||
// Remove the hash then compare to the src filename.
|
||||
$entry_filename_without_hash = preg_replace( '/-[a-f0-9]+\.js$/', '.js', $filename );
|
||||
if ( $src_filename === $entry_filename_without_hash ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $filename === $src_filename ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets locale data for the script, if it has translations.
|
||||
*
|
||||
* @since 1.21.0
|
||||
*/
|
||||
private function set_locale_data() {
|
||||
$json_translations = load_script_textdomain( $this->handle, 'google-site-kit' );
|
||||
if ( ! $json_translations ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$output = <<<JS
|
||||
( function( domain, translations ) {
|
||||
try {
|
||||
var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
|
||||
localeData[""].domain = domain;
|
||||
googlesitekit.i18n.setLocaleData( localeData, domain );
|
||||
} catch {
|
||||
}
|
||||
} )( "google-site-kit", {$json_translations} );
|
||||
JS;
|
||||
|
||||
wp_add_inline_script( $this->handle, $output, 'before' );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Assets\Script_Data
|
||||
*
|
||||
* @package Google\Site_Kit\Core\Assets
|
||||
* @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\Core\Assets;
|
||||
|
||||
/**
|
||||
* Class for virtual "data-only" scripts.
|
||||
*
|
||||
* @since 1.5.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
class Script_Data extends Script {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param string $handle Unique script handle.
|
||||
* @param array $args {
|
||||
* Associative array of script arguments.
|
||||
*
|
||||
* @type callable $data_callback Required. Function to return JSON-encodable data.
|
||||
* @type string $global Required. Name of global variable to assign data to in Javascript.
|
||||
* @type array $dependencies Optional. List of script dependencies. Default empty array.
|
||||
* }
|
||||
*/
|
||||
public function __construct( $handle, array $args ) {
|
||||
// Ensure required keys are always set.
|
||||
$args = $args + array(
|
||||
'data_callback' => null,
|
||||
'global' => '',
|
||||
);
|
||||
// SRC will always be false.
|
||||
$args['src'] = false;
|
||||
|
||||
parent::__construct( $handle, $args );
|
||||
|
||||
// Lazy-load script data before handle is to be printed.
|
||||
$this->args['before_print'] = function ( $handle ) {
|
||||
if ( empty( $this->args['global'] ) || ! is_callable( $this->args['data_callback'] ) ) {
|
||||
return;
|
||||
}
|
||||
$data = call_user_func( $this->args['data_callback'], $handle );
|
||||
$this->add_script_data( $data );
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given data to the script handle's 'data' key.
|
||||
*
|
||||
* 'data' is the key used by `wp_localize_script`, which is output
|
||||
* in older versions of WP even if the handle has no src (such as an alias).
|
||||
* This is done manually instead of using `wp_localize_script` to avoid casting
|
||||
* top-level keys to strings as this function is primarily intended for
|
||||
* providing an array of translations to Javascript rather than arbitrary data.
|
||||
*
|
||||
* @see \WP_Scripts::localize
|
||||
*
|
||||
* @since 1.5.0
|
||||
*
|
||||
* @param mixed $data Data to be assigned to the defined global.
|
||||
*/
|
||||
private function add_script_data( $data ) {
|
||||
$script_data = wp_scripts()->get_data( $this->handle, 'data' ) ?: '';
|
||||
$js = sprintf(
|
||||
'var %s = %s;',
|
||||
preg_replace( '[^\w\d_-]', '', $this->args['global'] ), // Ensure only a-zA-Z0-9_- are allowed.
|
||||
wp_json_encode( $data )
|
||||
);
|
||||
wp_scripts()->add_data( $this->handle, 'data', trim( "$script_data\n$js" ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Google\Site_Kit\Core\Assets\Stylesheet
|
||||
*
|
||||
* @package Google\Site_Kit
|
||||
* @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\Core\Assets;
|
||||
|
||||
use Google\Site_Kit\Context;
|
||||
|
||||
/**
|
||||
* Class representing a single stylesheet.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access private
|
||||
* @ignore
|
||||
*/
|
||||
final class Stylesheet extends Asset {
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $handle Unique stylesheet handle.
|
||||
* @param array $args {
|
||||
* Associative array of stylesheet arguments.
|
||||
*
|
||||
* @type string $src Required stylesheet source URL.
|
||||
* @type array $dependencies List of stylesheet dependencies. Default empty array.
|
||||
* @type string $version Stylesheet version. Default is the version of Site Kit.
|
||||
* @type bool $fallback Whether to only register as a fallback. Default false.
|
||||
* @type callable $before_print Optional callback to execute before printing. Default none.
|
||||
* @type string $media Media for which the stylesheet is defined. Default 'all'.
|
||||
* }
|
||||
*/
|
||||
public function __construct( $handle, array $args ) {
|
||||
parent::__construct( $handle, $args );
|
||||
|
||||
$this->args = wp_parse_args(
|
||||
$this->args,
|
||||
array(
|
||||
'media' => 'all',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the stylesheet.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @since 1.15.0 Adds $context parameter.
|
||||
*
|
||||
* @param Context $context Plugin context.
|
||||
*/
|
||||
public function register( Context $context ) {
|
||||
if ( $this->args['fallback'] && wp_style_is( $this->handle, 'registered' ) ) {
|
||||
return;
|
||||
}
|
||||
$src = $this->args['src'];
|
||||
$version = $this->args['version'];
|
||||
|
||||
list( $filename, $hash ) = Manifest::get( $this->handle );
|
||||
|
||||
if ( $filename ) {
|
||||
$src = $context->url( 'dist/assets/css/' . $filename );
|
||||
$version = $hash;
|
||||
}
|
||||
|
||||
wp_register_style(
|
||||
$this->handle,
|
||||
$src,
|
||||
(array) $this->args['dependencies'],
|
||||
$version,
|
||||
$this->args['media']
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues the stylesheet.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function enqueue() {
|
||||
wp_enqueue_style( $this->handle );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user