]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/functions.wp-scripts.php
WordPress 4.5
[autoinstalls/wordpress.git] / wp-includes / functions.wp-scripts.php
1 <?php
2 /**
3  * Dependencies API: Scripts functions
4  *
5  * @since 2.6.0
6  *
7  * @package WordPress
8  * @subpackage Dependencies
9  */
10
11 /**
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;
22         if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
23                 $wp_scripts = new WP_Scripts();
24         }
25         return $wp_scripts;
26 }
27
28 /**
29  * Helper function to output a _doing_it_wrong message when applicable.
30  *
31  * @ignore
32  * @since 4.2.0
33  *
34  * @param string $function Function name.
35  */
36 function _wp_scripts_maybe_doing_it_wrong( $function ) {
37         if ( did_action( 'init' ) ) {
38                 return;
39         }
40
41         _doing_it_wrong( $function, sprintf(
42                 __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
43                 '<code>wp_enqueue_scripts</code>',
44                 '<code>admin_enqueue_scripts</code>',
45                 '<code>login_enqueue_scripts</code>'
46         ), '3.3' );
47 }
48
49 /**
50  * Print scripts in document head that are in the $handles queue.
51  *
52  * Called by admin-header.php and wp_head hook. Since it is called by wp_head on every page load,
53  * the function does not instantiate the WP_Scripts object unless script names are explicitly passed.
54  * Makes use of already-instantiated $wp_scripts global if present. Use provided wp_print_scripts
55  * hook to register/enqueue new scripts.
56  *
57  * @see WP_Scripts::do_items()
58  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
59  *
60  * @since 2.1.0
61  *
62  * @param string|bool|array $handles Optional. Scripts to be printed. Default 'false'.
63  * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
64  */
65 function wp_print_scripts( $handles = false ) {
66         /**
67          * Fires before scripts in the $handles queue are printed.
68          *
69          * @since 2.1.0
70          */
71         do_action( 'wp_print_scripts' );
72         if ( '' === $handles ) { // for wp_head
73                 $handles = false;
74         }
75
76         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
77
78         global $wp_scripts;
79         if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
80                 if ( ! $handles ) {
81                         return array(); // No need to instantiate if nothing is there.
82                 }
83         }
84
85         return wp_scripts()->do_items( $handles );
86 }
87
88 /**
89  * Adds extra code to a registered script.
90  *
91  * Code will only be added if the script in already in the queue.
92  * Accepts a string $data containing the Code. If two or more code blocks
93  * are added to the same script $handle, they will be printed in the order
94  * they were added, i.e. the latter added code can redeclare the previous.
95  *
96  * @since 4.5.0
97  *
98  * @see WP_Scripts::add_inline_script()
99  *
100  * @param string $handle   Name of the script to add the inline script to.
101  * @param string $data     String containing the javascript to be added.
102  * @param string $position Optional. Whether to add the inline script before the handle
103  *                         or after. Default 'after'.
104  * @return bool True on success, false on failure.
105  */
106 function wp_add_inline_script( $handle, $data, $position = 'after' ) {
107         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
108
109         if ( false !== stripos( $data, '</script>' ) ) {
110                 _doing_it_wrong( __FUNCTION__, __( 'Do not pass script tags to wp_add_inline_script().' ), '4.5.0' );
111                 $data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
112         }
113
114         return wp_scripts()->add_inline_script( $handle, $data, $position );
115 }
116
117 /**
118  * Register a new script.
119  *
120  * Registers a script to be enqueued later using the wp_enqueue_script() function.
121  *
122  * @see WP_Dependencies::add()
123  * @see WP_Dependencies::add_data()
124  *
125  * @since 2.1.0
126  * @since 4.3.0 A return value was added.
127  *
128  * @param string           $handle    Name of the script. Should be unique.
129  * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
130  * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
131  * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
132  *                                    as a query string for cache busting purposes. If version is set to false, a version
133  *                                    number is automatically added equal to current installed WordPress version.
134  *                                    If set to null, no version is added.
135  * @param bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
136  *                                    Default 'false'.
137  * @return bool Whether the script has been registered. True on success, false on failure.
138  */
139 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
140         $wp_scripts = wp_scripts();
141         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
142
143         $registered = $wp_scripts->add( $handle, $src, $deps, $ver );
144         if ( $in_footer ) {
145                 $wp_scripts->add_data( $handle, 'group', 1 );
146         }
147
148         return $registered;
149 }
150
151 /**
152  * Localize a script.
153  *
154  * Works only if the script has already been added.
155  *
156  * Accepts an associative array $l10n and creates a JavaScript object:
157  *
158  *     "$object_name" = {
159  *         key: value,
160  *         key: value,
161  *         ...
162  *     }
163  *
164  *
165  * @see WP_Dependencies::localize()
166  * @link https://core.trac.wordpress.org/ticket/11520
167  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
168  *
169  * @since 2.2.0
170  *
171  * @todo Documentation cleanup
172  *
173  * @param string $handle      Script handle the data will be attached to.
174  * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
175  *                            Example: '/[a-zA-Z0-9_]+/'.
176  * @param array $l10n         The data itself. The data can be either a single or multi-dimensional array.
177  * @return bool True if the script was successfully localized, false otherwise.
178  */
179 function wp_localize_script( $handle, $object_name, $l10n ) {
180         global $wp_scripts;
181         if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
182                 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
183                 return false;
184         }
185
186         return $wp_scripts->localize( $handle, $object_name, $l10n );
187 }
188
189 /**
190  * Remove a registered script.
191  *
192  * Note: there are intentional safeguards in place to prevent critical admin scripts,
193  * such as jQuery core, from being unregistered.
194  *
195  * @see WP_Dependencies::remove()
196  *
197  * @since 2.1.0
198  *
199  * @param string $handle Name of the script to be removed.
200  */
201 function wp_deregister_script( $handle ) {
202         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
203
204         /**
205          * Do not allow accidental or negligent de-registering of critical scripts in the admin.
206          * Show minimal remorse if the correct hook is used.
207          */
208         $current_filter = current_filter();
209         if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) ||
210                 ( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter )
211         ) {
212                 $no = array(
213                         'jquery', 'jquery-core', 'jquery-migrate', 'jquery-ui-core', 'jquery-ui-accordion',
214                         'jquery-ui-autocomplete', 'jquery-ui-button', 'jquery-ui-datepicker', 'jquery-ui-dialog',
215                         'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-menu', 'jquery-ui-mouse',
216                         'jquery-ui-position', 'jquery-ui-progressbar', 'jquery-ui-resizable', 'jquery-ui-selectable',
217                         'jquery-ui-slider', 'jquery-ui-sortable', 'jquery-ui-spinner', 'jquery-ui-tabs',
218                         'jquery-ui-tooltip', 'jquery-ui-widget', 'underscore', 'backbone',
219                 );
220
221                 if ( in_array( $handle, $no ) ) {
222                         $message = sprintf( __( 'Do not deregister the %1$s script in the administration area. To target the front-end theme, use the %2$s hook.' ),
223                                 "<code>$handle</code>", '<code>wp_enqueue_scripts</code>' );
224                         _doing_it_wrong( __FUNCTION__, $message, '3.6' );
225                         return;
226                 }
227         }
228
229         wp_scripts()->remove( $handle );
230 }
231
232 /**
233  * Enqueue a script.
234  *
235  * Registers the script if $src provided (does NOT overwrite), and enqueues it.
236  *
237  * @see WP_Dependencies::add()
238  * @see WP_Dependencies::add_data()
239  * @see WP_Dependencies::enqueue()
240  *
241  * @since 2.1.0
242  *
243  * @param string           $handle    Name of the script. Should be unique.
244  * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
245  * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
246  * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
247  *                                    as a query string for cache busting purposes. If version is set to false, a version
248  *                                    number is automatically added equal to current installed WordPress version.
249  *                                    If set to null, no version is added.
250  * @param bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
251  *                                    Default 'false'.
252  */
253 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
254         $wp_scripts = wp_scripts();
255
256         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
257
258
259         if ( $src || $in_footer ) {
260                 $_handle = explode( '?', $handle );
261
262                 if ( $src ) {
263                         $wp_scripts->add( $_handle[0], $src, $deps, $ver );
264                 }
265
266                 if ( $in_footer ) {
267                         $wp_scripts->add_data( $_handle[0], 'group', 1 );
268                 }
269         }
270
271         $wp_scripts->enqueue( $handle );
272 }
273
274 /**
275  * Remove a previously enqueued script.
276  *
277  * @see WP_Dependencies::dequeue()
278  *
279  * @since 3.1.0
280  *
281  * @param string $handle Name of the script to be removed.
282  */
283 function wp_dequeue_script( $handle ) {
284         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
285
286         wp_scripts()->dequeue( $handle );
287 }
288
289 /**
290  * Check whether a script has been added to the queue.
291  *
292  * @since 2.8.0
293  * @since 3.5.0 'enqueued' added as an alias of the 'queue' list.
294  *
295  * @param string $handle Name of the script.
296  * @param string $list   Optional. Status of the script to check. Default 'enqueued'.
297  *                       Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
298  * @return bool Whether the script is queued.
299  */
300 function wp_script_is( $handle, $list = 'enqueued' ) {
301         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
302
303         return (bool) wp_scripts()->query( $handle, $list );
304 }
305
306 /**
307  * Add metadata to a script.
308  *
309  * Works only if the script has already been added.
310  *
311  * Possible values for $key and $value:
312  * 'conditional' string Comments for IE 6, lte IE 7, etc.
313  *
314  * @since 4.2.0
315  *
316  * @see WP_Dependency::add_data()
317  *
318  * @param string $handle Name of the script.
319  * @param string $key    Name of data point for which we're storing a value.
320  * @param mixed  $value  String containing the data to be added.
321  * @return bool True on success, false on failure.
322  */
323 function wp_script_add_data( $handle, $key, $value ){
324         return wp_scripts()->add_data( $handle, $key, $value );
325 }