author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Dependencies API: Scripts functions |
0 | 4 |
* |
5 |
* @since 2.6.0 |
|
6 |
* |
|
7 |
* @package WordPress |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
8 |
* @subpackage Dependencies |
0 | 9 |
*/ |
10 |
||
11 |
/** |
|
5 | 12 |
* Initialize $wp_scripts if it has not been set. |
13 |
* |
|
14 |
* @global WP_Scripts $wp_scripts |
|
15 |
* |
|
16 |
* @since 4.2.0 |
|
17 |
* |
|
18 |
* @return WP_Scripts WP_Scripts instance. |
|
19 |
*/ |
|
20 |
function wp_scripts() { |
|
21 |
global $wp_scripts; |
|
16 | 22 |
|
5 | 23 |
if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
24 |
$wp_scripts = new WP_Scripts(); |
|
25 |
} |
|
16 | 26 |
|
5 | 27 |
return $wp_scripts; |
28 |
} |
|
29 |
||
30 |
/** |
|
31 |
* Helper function to output a _doing_it_wrong message when applicable. |
|
32 |
* |
|
33 |
* @ignore |
|
34 |
* @since 4.2.0 |
|
16 | 35 |
* @since 5.5.0 Added the `$handle` parameter. |
5 | 36 |
* |
37 |
* @param string $function Function name. |
|
16 | 38 |
* @param string $handle Optional. Name of the script or stylesheet that was |
39 |
* registered or enqueued too early. Default empty. |
|
5 | 40 |
*/ |
16 | 41 |
function _wp_scripts_maybe_doing_it_wrong( $function, $handle = '' ) { |
42 |
if ( did_action( 'init' ) || did_action( 'wp_enqueue_scripts' ) |
|
43 |
|| did_action( 'admin_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' ) |
|
44 |
) { |
|
5 | 45 |
return; |
46 |
} |
|
47 |
||
16 | 48 |
$message = sprintf( |
49 |
/* translators: 1: wp_enqueue_scripts, 2: admin_enqueue_scripts, 3: login_enqueue_scripts */ |
|
50 |
__( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ), |
|
51 |
'<code>wp_enqueue_scripts</code>', |
|
52 |
'<code>admin_enqueue_scripts</code>', |
|
53 |
'<code>login_enqueue_scripts</code>' |
|
54 |
); |
|
55 |
||
56 |
if ( $handle ) { |
|
57 |
$message .= ' ' . sprintf( |
|
58 |
/* translators: %s: Name of the script or stylesheet. */ |
|
59 |
__( 'This notice was triggered by the %s handle.' ), |
|
60 |
'<code>' . $handle . '</code>' |
|
61 |
); |
|
62 |
} |
|
63 |
||
9 | 64 |
_doing_it_wrong( |
65 |
$function, |
|
16 | 66 |
$message, |
9 | 67 |
'3.3.0' |
68 |
); |
|
5 | 69 |
} |
70 |
||
71 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
* Prints scripts in document head that are in the $handles queue. |
0 | 73 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
* Called by admin-header.php and {@see 'wp_head'} hook. Since it is called by wp_head on every page load, |
0 | 75 |
* the function does not instantiate the WP_Scripts object unless script names are explicitly passed. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
* Makes use of already-instantiated $wp_scripts global if present. Use provided {@see 'wp_print_scripts'} |
0 | 77 |
* hook to register/enqueue new scripts. |
78 |
* |
|
16 | 79 |
* @see WP_Scripts::do_item() |
0 | 80 |
* @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. |
81 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
* @since 2.1.0 |
0 | 83 |
* |
5 | 84 |
* @param string|bool|array $handles Optional. Scripts to be printed. Default 'false'. |
16 | 85 |
* @return string[] On success, an array of handles of processed WP_Dependencies items; otherwise, an empty array. |
0 | 86 |
*/ |
87 |
function wp_print_scripts( $handles = false ) { |
|
16 | 88 |
global $wp_scripts; |
89 |
||
5 | 90 |
/** |
91 |
* Fires before scripts in the $handles queue are printed. |
|
92 |
* |
|
93 |
* @since 2.1.0 |
|
94 |
*/ |
|
0 | 95 |
do_action( 'wp_print_scripts' ); |
16 | 96 |
|
97 |
if ( '' === $handles ) { // For 'wp_head'. |
|
0 | 98 |
$handles = false; |
5 | 99 |
} |
100 |
||
101 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
|
0 | 102 |
|
5 | 103 |
if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
104 |
if ( ! $handles ) { |
|
0 | 105 |
return array(); // No need to instantiate if nothing is there. |
5 | 106 |
} |
0 | 107 |
} |
108 |
||
5 | 109 |
return wp_scripts()->do_items( $handles ); |
0 | 110 |
} |
111 |
||
112 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
113 |
* Adds extra code to a registered script. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
114 |
* |
9 | 115 |
* Code will only be added if the script is already in the queue. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
116 |
* Accepts a string $data containing the Code. If two or more code blocks |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
117 |
* are added to the same script $handle, they will be printed in the order |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
* they were added, i.e. the latter added code can redeclare the previous. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
119 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
120 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
121 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
122 |
* @see WP_Scripts::add_inline_script() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
124 |
* @param string $handle Name of the script to add the inline script to. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
* @param string $data String containing the javascript to be added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
* @param string $position Optional. Whether to add the inline script before the handle |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
127 |
* or after. Default 'after'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
* @return bool True on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
129 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
130 |
function wp_add_inline_script( $handle, $data, $position = 'after' ) { |
16 | 131 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
132 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
133 |
if ( false !== stripos( $data, '</script>' ) ) { |
9 | 134 |
_doing_it_wrong( |
135 |
__FUNCTION__, |
|
136 |
sprintf( |
|
137 |
/* translators: 1: <script>, 2: wp_add_inline_script() */ |
|
138 |
__( 'Do not pass %1$s tags to %2$s.' ), |
|
139 |
'<code><script></code>', |
|
140 |
'<code>wp_add_inline_script()</code>' |
|
141 |
), |
|
142 |
'4.5.0' |
|
143 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
$data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
146 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
return wp_scripts()->add_inline_script( $handle, $data, $position ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
148 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
/** |
0 | 151 |
* Register a new script. |
152 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
* Registers a script to be enqueued later using the wp_enqueue_script() function. |
0 | 154 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
155 |
* @see WP_Dependencies::add() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
* @see WP_Dependencies::add_data() |
0 | 157 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
* @since 2.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
* @since 4.3.0 A return value was added. |
0 | 160 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
161 |
* @param string $handle Name of the script. Should be unique. |
9 | 162 |
* @param string|bool $src Full URL of the script, or path of the script relative to the WordPress root directory. |
163 |
* If source is set to false, script is an alias of other scripts it depends on. |
|
16 | 164 |
* @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
* @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
* as a query string for cache busting purposes. If version is set to false, a version |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
167 |
* number is automatically added equal to current installed WordPress version. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
168 |
* If set to null, no version is added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
169 |
* @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
170 |
* Default 'false'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
171 |
* @return bool Whether the script has been registered. True on success, false on failure. |
0 | 172 |
*/ |
173 |
function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) { |
|
16 | 174 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
175 |
||
5 | 176 |
$wp_scripts = wp_scripts(); |
0 | 177 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
$registered = $wp_scripts->add( $handle, $src, $deps, $ver ); |
5 | 179 |
if ( $in_footer ) { |
0 | 180 |
$wp_scripts->add_data( $handle, 'group', 1 ); |
5 | 181 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
182 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
return $registered; |
0 | 184 |
} |
185 |
||
186 |
/** |
|
187 |
* Localize a script. |
|
188 |
* |
|
189 |
* Works only if the script has already been added. |
|
190 |
* |
|
191 |
* Accepts an associative array $l10n and creates a JavaScript object: |
|
5 | 192 |
* |
193 |
* "$object_name" = { |
|
194 |
* key: value, |
|
195 |
* key: value, |
|
196 |
* ... |
|
197 |
* } |
|
198 |
* |
|
16 | 199 |
* @see WP_Scripts::localize() |
5 | 200 |
* @link https://core.trac.wordpress.org/ticket/11520 |
0 | 201 |
* @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. |
202 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
* @since 2.2.0 |
0 | 204 |
* |
5 | 205 |
* @todo Documentation cleanup |
206 |
* |
|
0 | 207 |
* @param string $handle Script handle the data will be attached to. |
208 |
* @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable. |
|
209 |
* Example: '/[a-zA-Z0-9_]+/'. |
|
16 | 210 |
* @param array $l10n The data itself. The data can be either a single or multi-dimensional array. |
0 | 211 |
* @return bool True if the script was successfully localized, false otherwise. |
212 |
*/ |
|
213 |
function wp_localize_script( $handle, $object_name, $l10n ) { |
|
214 |
global $wp_scripts; |
|
16 | 215 |
|
5 | 216 |
if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
16 | 217 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
0 | 218 |
return false; |
219 |
} |
|
220 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
return $wp_scripts->localize( $handle, $object_name, $l10n ); |
0 | 222 |
} |
223 |
||
224 |
/** |
|
9 | 225 |
* Sets translated strings for a script. |
226 |
* |
|
227 |
* Works only if the script has already been added. |
|
228 |
* |
|
229 |
* @see WP_Scripts::set_translations() |
|
230 |
* @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. |
|
231 |
* |
|
232 |
* @since 5.0.0 |
|
233 |
* @since 5.1.0 The `$domain` parameter was made optional. |
|
234 |
* |
|
235 |
* @param string $handle Script handle the textdomain will be attached to. |
|
236 |
* @param string $domain Optional. Text domain. Default 'default'. |
|
237 |
* @param string $path Optional. The full file path to the directory containing translation files. |
|
238 |
* @return bool True if the text domain was successfully localized, false otherwise. |
|
239 |
*/ |
|
240 |
function wp_set_script_translations( $handle, $domain = 'default', $path = null ) { |
|
241 |
global $wp_scripts; |
|
16 | 242 |
|
9 | 243 |
if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
16 | 244 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
9 | 245 |
return false; |
246 |
} |
|
247 |
||
248 |
return $wp_scripts->set_translations( $handle, $domain, $path ); |
|
249 |
} |
|
250 |
||
251 |
/** |
|
0 | 252 |
* Remove a registered script. |
253 |
* |
|
254 |
* Note: there are intentional safeguards in place to prevent critical admin scripts, |
|
255 |
* such as jQuery core, from being unregistered. |
|
256 |
* |
|
257 |
* @see WP_Dependencies::remove() |
|
258 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
259 |
* @since 2.1.0 |
0 | 260 |
* |
261 |
* @param string $handle Name of the script to be removed. |
|
262 |
*/ |
|
263 |
function wp_deregister_script( $handle ) { |
|
16 | 264 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
0 | 265 |
|
266 |
/** |
|
267 |
* Do not allow accidental or negligent de-registering of critical scripts in the admin. |
|
268 |
* Show minimal remorse if the correct hook is used. |
|
269 |
*/ |
|
270 |
$current_filter = current_filter(); |
|
271 |
if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) || |
|
272 |
( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter ) |
|
273 |
) { |
|
16 | 274 |
$not_allowed = array( |
9 | 275 |
'jquery', |
276 |
'jquery-core', |
|
277 |
'jquery-migrate', |
|
278 |
'jquery-ui-core', |
|
279 |
'jquery-ui-accordion', |
|
280 |
'jquery-ui-autocomplete', |
|
281 |
'jquery-ui-button', |
|
282 |
'jquery-ui-datepicker', |
|
283 |
'jquery-ui-dialog', |
|
284 |
'jquery-ui-draggable', |
|
285 |
'jquery-ui-droppable', |
|
286 |
'jquery-ui-menu', |
|
287 |
'jquery-ui-mouse', |
|
288 |
'jquery-ui-position', |
|
289 |
'jquery-ui-progressbar', |
|
290 |
'jquery-ui-resizable', |
|
291 |
'jquery-ui-selectable', |
|
292 |
'jquery-ui-slider', |
|
293 |
'jquery-ui-sortable', |
|
294 |
'jquery-ui-spinner', |
|
295 |
'jquery-ui-tabs', |
|
296 |
'jquery-ui-tooltip', |
|
297 |
'jquery-ui-widget', |
|
298 |
'underscore', |
|
299 |
'backbone', |
|
0 | 300 |
); |
301 |
||
16 | 302 |
if ( in_array( $handle, $not_allowed, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
$message = sprintf( |
16 | 304 |
/* translators: 1: Script name, 2: wp_enqueue_scripts */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
305 |
__( 'Do not deregister the %1$s script in the administration area. To target the front-end theme, use the %2$s hook.' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
"<code>$handle</code>", |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
'<code>wp_enqueue_scripts</code>' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
_doing_it_wrong( __FUNCTION__, $message, '3.6.0' ); |
0 | 310 |
return; |
311 |
} |
|
312 |
} |
|
313 |
||
5 | 314 |
wp_scripts()->remove( $handle ); |
0 | 315 |
} |
316 |
||
317 |
/** |
|
318 |
* Enqueue a script. |
|
319 |
* |
|
320 |
* Registers the script if $src provided (does NOT overwrite), and enqueues it. |
|
321 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
322 |
* @see WP_Dependencies::add() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
* @see WP_Dependencies::add_data() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
* @see WP_Dependencies::enqueue() |
0 | 325 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
* @since 2.1.0 |
5 | 327 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
* @param string $handle Name of the script. Should be unique. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
329 |
* @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
330 |
* Default empty. |
16 | 331 |
* @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
332 |
* @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
* as a query string for cache busting purposes. If version is set to false, a version |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
334 |
* number is automatically added equal to current installed WordPress version. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
335 |
* If set to null, no version is added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
336 |
* @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
337 |
* Default 'false'. |
0 | 338 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) { |
16 | 340 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
5 | 341 |
|
16 | 342 |
$wp_scripts = wp_scripts(); |
5 | 343 |
|
344 |
if ( $src || $in_footer ) { |
|
345 |
$_handle = explode( '?', $handle ); |
|
346 |
||
347 |
if ( $src ) { |
|
348 |
$wp_scripts->add( $_handle[0], $src, $deps, $ver ); |
|
349 |
} |
|
350 |
||
351 |
if ( $in_footer ) { |
|
352 |
$wp_scripts->add_data( $_handle[0], 'group', 1 ); |
|
353 |
} |
|
0 | 354 |
} |
355 |
||
356 |
$wp_scripts->enqueue( $handle ); |
|
357 |
} |
|
358 |
||
359 |
/** |
|
360 |
* Remove a previously enqueued script. |
|
361 |
* |
|
362 |
* @see WP_Dependencies::dequeue() |
|
363 |
* |
|
364 |
* @since 3.1.0 |
|
365 |
* |
|
366 |
* @param string $handle Name of the script to be removed. |
|
367 |
*/ |
|
368 |
function wp_dequeue_script( $handle ) { |
|
16 | 369 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
0 | 370 |
|
5 | 371 |
wp_scripts()->dequeue( $handle ); |
0 | 372 |
} |
373 |
||
374 |
/** |
|
9 | 375 |
* Determines whether a script has been added to the queue. |
376 |
* |
|
377 |
* For more information on this and similar theme functions, check out |
|
378 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
379 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 380 |
* |
381 |
* @since 2.8.0 |
|
382 |
* @since 3.5.0 'enqueued' added as an alias of the 'queue' list. |
|
383 |
* |
|
384 |
* @param string $handle Name of the script. |
|
385 |
* @param string $list Optional. Status of the script to check. Default 'enqueued'. |
|
386 |
* Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
* @return bool Whether the script is queued. |
0 | 388 |
*/ |
389 |
function wp_script_is( $handle, $list = 'enqueued' ) { |
|
16 | 390 |
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
5 | 391 |
|
392 |
return (bool) wp_scripts()->query( $handle, $list ); |
|
393 |
} |
|
394 |
||
395 |
/** |
|
396 |
* Add metadata to a script. |
|
397 |
* |
|
398 |
* Works only if the script has already been added. |
|
399 |
* |
|
400 |
* Possible values for $key and $value: |
|
401 |
* 'conditional' string Comments for IE 6, lte IE 7, etc. |
|
402 |
* |
|
403 |
* @since 4.2.0 |
|
404 |
* |
|
9 | 405 |
* @see WP_Dependencies::add_data() |
5 | 406 |
* |
407 |
* @param string $handle Name of the script. |
|
408 |
* @param string $key Name of data point for which we're storing a value. |
|
409 |
* @param mixed $value String containing the data to be added. |
|
410 |
* @return bool True on success, false on failure. |
|
411 |
*/ |
|
9 | 412 |
function wp_script_add_data( $handle, $key, $value ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
return wp_scripts()->add_data( $handle, $key, $value ); |
0 | 414 |
} |