-
Notifications
You must be signed in to change notification settings - Fork 292
/
Script.php
174 lines (152 loc) · 4.43 KB
/
Script.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?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' );
}
}