17 * |
17 * |
18 * @return WP_Scripts WP_Scripts instance. |
18 * @return WP_Scripts WP_Scripts instance. |
19 */ |
19 */ |
20 function wp_scripts() { |
20 function wp_scripts() { |
21 global $wp_scripts; |
21 global $wp_scripts; |
|
22 |
22 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
23 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
23 $wp_scripts = new WP_Scripts(); |
24 $wp_scripts = new WP_Scripts(); |
24 } |
25 } |
|
26 |
25 return $wp_scripts; |
27 return $wp_scripts; |
26 } |
28 } |
27 |
29 |
28 /** |
30 /** |
29 * Helper function to output a _doing_it_wrong message when applicable. |
31 * Helper function to output a _doing_it_wrong message when applicable. |
30 * |
32 * |
31 * @ignore |
33 * @ignore |
32 * @since 4.2.0 |
34 * @since 4.2.0 |
|
35 * @since 5.5.0 Added the `$handle` parameter. |
33 * |
36 * |
34 * @param string $function Function name. |
37 * @param string $function Function name. |
35 */ |
38 * @param string $handle Optional. Name of the script or stylesheet that was |
36 function _wp_scripts_maybe_doing_it_wrong( $function ) { |
39 * registered or enqueued too early. Default empty. |
37 if ( did_action( 'init' ) || did_action( 'admin_enqueue_scripts' ) || did_action( 'wp_enqueue_scripts' ) || did_action( 'login_enqueue_scripts' ) ) { |
40 */ |
|
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 ) { |
38 return; |
45 return; |
|
46 } |
|
47 |
|
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 ); |
39 } |
62 } |
40 |
63 |
41 _doing_it_wrong( |
64 _doing_it_wrong( |
42 $function, |
65 $function, |
43 sprintf( |
66 $message, |
44 /* translators: 1: wp_enqueue_scripts, 2: admin_enqueue_scripts, 3: login_enqueue_scripts */ |
|
45 __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ), |
|
46 '<code>wp_enqueue_scripts</code>', |
|
47 '<code>admin_enqueue_scripts</code>', |
|
48 '<code>login_enqueue_scripts</code>' |
|
49 ), |
|
50 '3.3.0' |
67 '3.3.0' |
51 ); |
68 ); |
52 } |
69 } |
53 |
70 |
54 /** |
71 /** |
57 * Called by admin-header.php and {@see 'wp_head'} hook. Since it is called by wp_head on every page load, |
74 * Called by admin-header.php and {@see 'wp_head'} hook. Since it is called by wp_head on every page load, |
58 * the function does not instantiate the WP_Scripts object unless script names are explicitly passed. |
75 * the function does not instantiate the WP_Scripts object unless script names are explicitly passed. |
59 * Makes use of already-instantiated $wp_scripts global if present. Use provided {@see 'wp_print_scripts'} |
76 * Makes use of already-instantiated $wp_scripts global if present. Use provided {@see 'wp_print_scripts'} |
60 * hook to register/enqueue new scripts. |
77 * hook to register/enqueue new scripts. |
61 * |
78 * |
62 * @see WP_Scripts::do_items() |
79 * @see WP_Scripts::do_item() |
63 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. |
80 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. |
64 * |
81 * |
65 * @since 2.1.0 |
82 * @since 2.1.0 |
66 * |
83 * |
67 * @param string|bool|array $handles Optional. Scripts to be printed. Default 'false'. |
84 * @param string|bool|array $handles Optional. Scripts to be printed. Default 'false'. |
68 * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array. |
85 * @return string[] On success, an array of handles of processed WP_Dependencies items; otherwise, an empty array. |
69 */ |
86 */ |
70 function wp_print_scripts( $handles = false ) { |
87 function wp_print_scripts( $handles = false ) { |
|
88 global $wp_scripts; |
|
89 |
71 /** |
90 /** |
72 * Fires before scripts in the $handles queue are printed. |
91 * Fires before scripts in the $handles queue are printed. |
73 * |
92 * |
74 * @since 2.1.0 |
93 * @since 2.1.0 |
75 */ |
94 */ |
76 do_action( 'wp_print_scripts' ); |
95 do_action( 'wp_print_scripts' ); |
77 if ( '' === $handles ) { // for wp_head |
96 |
|
97 if ( '' === $handles ) { // For 'wp_head'. |
78 $handles = false; |
98 $handles = false; |
79 } |
99 } |
80 |
100 |
81 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
101 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
82 |
102 |
83 global $wp_scripts; |
|
84 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
103 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
85 if ( ! $handles ) { |
104 if ( ! $handles ) { |
86 return array(); // No need to instantiate if nothing is there. |
105 return array(); // No need to instantiate if nothing is there. |
87 } |
106 } |
88 } |
107 } |
107 * @param string $position Optional. Whether to add the inline script before the handle |
126 * @param string $position Optional. Whether to add the inline script before the handle |
108 * or after. Default 'after'. |
127 * or after. Default 'after'. |
109 * @return bool True on success, false on failure. |
128 * @return bool True on success, false on failure. |
110 */ |
129 */ |
111 function wp_add_inline_script( $handle, $data, $position = 'after' ) { |
130 function wp_add_inline_script( $handle, $data, $position = 'after' ) { |
112 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
131 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
113 |
132 |
114 if ( false !== stripos( $data, '</script>' ) ) { |
133 if ( false !== stripos( $data, '</script>' ) ) { |
115 _doing_it_wrong( |
134 _doing_it_wrong( |
116 __FUNCTION__, |
135 __FUNCTION__, |
117 sprintf( |
136 sprintf( |
140 * @since 4.3.0 A return value was added. |
159 * @since 4.3.0 A return value was added. |
141 * |
160 * |
142 * @param string $handle Name of the script. Should be unique. |
161 * @param string $handle Name of the script. Should be unique. |
143 * @param string|bool $src Full URL of the script, or path of the script relative to the WordPress root directory. |
162 * @param string|bool $src Full URL of the script, or path of the script relative to the WordPress root directory. |
144 * If source is set to false, script is an alias of other scripts it depends on. |
163 * If source is set to false, script is an alias of other scripts it depends on. |
145 * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array. |
164 * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. |
146 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL |
165 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL |
147 * as a query string for cache busting purposes. If version is set to false, a version |
166 * as a query string for cache busting purposes. If version is set to false, a version |
148 * number is automatically added equal to current installed WordPress version. |
167 * number is automatically added equal to current installed WordPress version. |
149 * If set to null, no version is added. |
168 * If set to null, no version is added. |
150 * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>. |
169 * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>. |
151 * Default 'false'. |
170 * Default 'false'. |
152 * @return bool Whether the script has been registered. True on success, false on failure. |
171 * @return bool Whether the script has been registered. True on success, false on failure. |
153 */ |
172 */ |
154 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) { |
173 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) { |
|
174 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
|
175 |
155 $wp_scripts = wp_scripts(); |
176 $wp_scripts = wp_scripts(); |
156 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
|
157 |
177 |
158 $registered = $wp_scripts->add( $handle, $src, $deps, $ver ); |
178 $registered = $wp_scripts->add( $handle, $src, $deps, $ver ); |
159 if ( $in_footer ) { |
179 if ( $in_footer ) { |
160 $wp_scripts->add_data( $handle, 'group', 1 ); |
180 $wp_scripts->add_data( $handle, 'group', 1 ); |
161 } |
181 } |
174 * key: value, |
194 * key: value, |
175 * key: value, |
195 * key: value, |
176 * ... |
196 * ... |
177 * } |
197 * } |
178 * |
198 * |
179 * @see WP_Dependencies::localize() |
199 * @see WP_Scripts::localize() |
180 * @link https://core.trac.wordpress.org/ticket/11520 |
200 * @link https://core.trac.wordpress.org/ticket/11520 |
181 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. |
201 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. |
182 * |
202 * |
183 * @since 2.2.0 |
203 * @since 2.2.0 |
184 * |
204 * |
185 * @todo Documentation cleanup |
205 * @todo Documentation cleanup |
186 * |
206 * |
187 * @param string $handle Script handle the data will be attached to. |
207 * @param string $handle Script handle the data will be attached to. |
188 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable. |
208 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable. |
189 * Example: '/[a-zA-Z0-9_]+/'. |
209 * Example: '/[a-zA-Z0-9_]+/'. |
190 * @param array $l10n The data itself. The data can be either a single or multi-dimensional array. |
210 * @param array $l10n The data itself. The data can be either a single or multi-dimensional array. |
191 * @return bool True if the script was successfully localized, false otherwise. |
211 * @return bool True if the script was successfully localized, false otherwise. |
192 */ |
212 */ |
193 function wp_localize_script( $handle, $object_name, $l10n ) { |
213 function wp_localize_script( $handle, $object_name, $l10n ) { |
194 global $wp_scripts; |
214 global $wp_scripts; |
|
215 |
195 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
216 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
196 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
217 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
197 return false; |
218 return false; |
198 } |
219 } |
199 |
220 |
200 return $wp_scripts->localize( $handle, $object_name, $l10n ); |
221 return $wp_scripts->localize( $handle, $object_name, $l10n ); |
201 } |
222 } |
216 * @param string $path Optional. The full file path to the directory containing translation files. |
237 * @param string $path Optional. The full file path to the directory containing translation files. |
217 * @return bool True if the text domain was successfully localized, false otherwise. |
238 * @return bool True if the text domain was successfully localized, false otherwise. |
218 */ |
239 */ |
219 function wp_set_script_translations( $handle, $domain = 'default', $path = null ) { |
240 function wp_set_script_translations( $handle, $domain = 'default', $path = null ) { |
220 global $wp_scripts; |
241 global $wp_scripts; |
|
242 |
221 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
243 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
222 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
244 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
223 return false; |
245 return false; |
224 } |
246 } |
225 |
247 |
226 return $wp_scripts->set_translations( $handle, $domain, $path ); |
248 return $wp_scripts->set_translations( $handle, $domain, $path ); |
227 } |
249 } |
237 * @since 2.1.0 |
259 * @since 2.1.0 |
238 * |
260 * |
239 * @param string $handle Name of the script to be removed. |
261 * @param string $handle Name of the script to be removed. |
240 */ |
262 */ |
241 function wp_deregister_script( $handle ) { |
263 function wp_deregister_script( $handle ) { |
242 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
264 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
243 |
265 |
244 /** |
266 /** |
245 * Do not allow accidental or negligent de-registering of critical scripts in the admin. |
267 * Do not allow accidental or negligent de-registering of critical scripts in the admin. |
246 * Show minimal remorse if the correct hook is used. |
268 * Show minimal remorse if the correct hook is used. |
247 */ |
269 */ |
248 $current_filter = current_filter(); |
270 $current_filter = current_filter(); |
249 if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) || |
271 if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) || |
250 ( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter ) |
272 ( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter ) |
251 ) { |
273 ) { |
252 $no = array( |
274 $not_allowed = array( |
253 'jquery', |
275 'jquery', |
254 'jquery-core', |
276 'jquery-core', |
255 'jquery-migrate', |
277 'jquery-migrate', |
256 'jquery-ui-core', |
278 'jquery-ui-core', |
257 'jquery-ui-accordion', |
279 'jquery-ui-accordion', |
275 'jquery-ui-widget', |
297 'jquery-ui-widget', |
276 'underscore', |
298 'underscore', |
277 'backbone', |
299 'backbone', |
278 ); |
300 ); |
279 |
301 |
280 if ( in_array( $handle, $no ) ) { |
302 if ( in_array( $handle, $not_allowed, true ) ) { |
281 $message = sprintf( |
303 $message = sprintf( |
282 /* translators: 1: script name, 2: wp_enqueue_scripts */ |
304 /* translators: 1: Script name, 2: wp_enqueue_scripts */ |
283 __( 'Do not deregister the %1$s script in the administration area. To target the front-end theme, use the %2$s hook.' ), |
305 __( 'Do not deregister the %1$s script in the administration area. To target the front-end theme, use the %2$s hook.' ), |
284 "<code>$handle</code>", |
306 "<code>$handle</code>", |
285 '<code>wp_enqueue_scripts</code>' |
307 '<code>wp_enqueue_scripts</code>' |
286 ); |
308 ); |
287 _doing_it_wrong( __FUNCTION__, $message, '3.6.0' ); |
309 _doing_it_wrong( __FUNCTION__, $message, '3.6.0' ); |
304 * @since 2.1.0 |
326 * @since 2.1.0 |
305 * |
327 * |
306 * @param string $handle Name of the script. Should be unique. |
328 * @param string $handle Name of the script. Should be unique. |
307 * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. |
329 * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. |
308 * Default empty. |
330 * Default empty. |
309 * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array. |
331 * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. |
310 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL |
332 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL |
311 * as a query string for cache busting purposes. If version is set to false, a version |
333 * as a query string for cache busting purposes. If version is set to false, a version |
312 * number is automatically added equal to current installed WordPress version. |
334 * number is automatically added equal to current installed WordPress version. |
313 * If set to null, no version is added. |
335 * If set to null, no version is added. |
314 * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>. |
336 * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>. |
315 * Default 'false'. |
337 * Default 'false'. |
316 */ |
338 */ |
317 function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) { |
339 function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) { |
|
340 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
|
341 |
318 $wp_scripts = wp_scripts(); |
342 $wp_scripts = wp_scripts(); |
319 |
|
320 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
|
321 |
343 |
322 if ( $src || $in_footer ) { |
344 if ( $src || $in_footer ) { |
323 $_handle = explode( '?', $handle ); |
345 $_handle = explode( '?', $handle ); |
324 |
346 |
325 if ( $src ) { |
347 if ( $src ) { |
363 * @param string $list Optional. Status of the script to check. Default 'enqueued'. |
385 * @param string $list Optional. Status of the script to check. Default 'enqueued'. |
364 * Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'. |
386 * Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'. |
365 * @return bool Whether the script is queued. |
387 * @return bool Whether the script is queued. |
366 */ |
388 */ |
367 function wp_script_is( $handle, $list = 'enqueued' ) { |
389 function wp_script_is( $handle, $list = 'enqueued' ) { |
368 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); |
390 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
369 |
391 |
370 return (bool) wp_scripts()->query( $handle, $list ); |
392 return (bool) wp_scripts()->query( $handle, $list ); |
371 } |
393 } |
372 |
394 |
373 /** |
395 /** |