]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/functions.wp-scripts.php
WordPress 4.6.3
[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.0' );
47 }
48
49 /**
50  * Prints scripts in document head that are in the $handles queue.
51  *
52  * Called by admin-header.php and {@see '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 {@see '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__, sprintf(
111                         /* translators: 1: <script>, 2: wp_add_inline_script() */
112                         __( 'Do not pass %1$s tags to %2$s.' ),
113                         '<code>&lt;script&gt;</code>',
114                         '<code>wp_add_inline_script()</code>'
115                 ), '4.5.0' );
116                 $data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
117         }
118
119         return wp_scripts()->add_inline_script( $handle, $data, $position );
120 }
121
122 /**
123  * Register a new script.
124  *
125  * Registers a script to be enqueued later using the wp_enqueue_script() function.
126  *
127  * @see WP_Dependencies::add()
128  * @see WP_Dependencies::add_data()
129  *
130  * @since 2.1.0
131  * @since 4.3.0 A return value was added.
132  *
133  * @param string           $handle    Name of the script. Should be unique.
134  * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
135  * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
136  * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
137  *                                    as a query string for cache busting purposes. If version is set to false, a version
138  *                                    number is automatically added equal to current installed WordPress version.
139  *                                    If set to null, no version is added.
140  * @param bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
141  *                                    Default 'false'.
142  * @return bool Whether the script has been registered. True on success, false on failure.
143  */
144 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
145         $wp_scripts = wp_scripts();
146         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
147
148         $registered = $wp_scripts->add( $handle, $src, $deps, $ver );
149         if ( $in_footer ) {
150                 $wp_scripts->add_data( $handle, 'group', 1 );
151         }
152
153         return $registered;
154 }
155
156 /**
157  * Localize a script.
158  *
159  * Works only if the script has already been added.
160  *
161  * Accepts an associative array $l10n and creates a JavaScript object:
162  *
163  *     "$object_name" = {
164  *         key: value,
165  *         key: value,
166  *         ...
167  *     }
168  *
169  *
170  * @see WP_Dependencies::localize()
171  * @link https://core.trac.wordpress.org/ticket/11520
172  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
173  *
174  * @since 2.2.0
175  *
176  * @todo Documentation cleanup
177  *
178  * @param string $handle      Script handle the data will be attached to.
179  * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
180  *                            Example: '/[a-zA-Z0-9_]+/'.
181  * @param array $l10n         The data itself. The data can be either a single or multi-dimensional array.
182  * @return bool True if the script was successfully localized, false otherwise.
183  */
184 function wp_localize_script( $handle, $object_name, $l10n ) {
185         global $wp_scripts;
186         if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
187                 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
188                 return false;
189         }
190
191         return $wp_scripts->localize( $handle, $object_name, $l10n );
192 }
193
194 /**
195  * Remove a registered script.
196  *
197  * Note: there are intentional safeguards in place to prevent critical admin scripts,
198  * such as jQuery core, from being unregistered.
199  *
200  * @see WP_Dependencies::remove()
201  *
202  * @since 2.1.0
203  *
204  * @param string $handle Name of the script to be removed.
205  */
206 function wp_deregister_script( $handle ) {
207         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
208
209         /**
210          * Do not allow accidental or negligent de-registering of critical scripts in the admin.
211          * Show minimal remorse if the correct hook is used.
212          */
213         $current_filter = current_filter();
214         if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) ||
215                 ( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter )
216         ) {
217                 $no = array(
218                         'jquery', 'jquery-core', 'jquery-migrate', 'jquery-ui-core', 'jquery-ui-accordion',
219                         'jquery-ui-autocomplete', 'jquery-ui-button', 'jquery-ui-datepicker', 'jquery-ui-dialog',
220                         'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-menu', 'jquery-ui-mouse',
221                         'jquery-ui-position', 'jquery-ui-progressbar', 'jquery-ui-resizable', 'jquery-ui-selectable',
222                         'jquery-ui-slider', 'jquery-ui-sortable', 'jquery-ui-spinner', 'jquery-ui-tabs',
223                         'jquery-ui-tooltip', 'jquery-ui-widget', 'underscore', 'backbone',
224                 );
225
226                 if ( in_array( $handle, $no ) ) {
227                         $message = sprintf( __( 'Do not deregister the %1$s script in the administration area. To target the front-end theme, use the %2$s hook.' ),
228                                 "<code>$handle</code>", '<code>wp_enqueue_scripts</code>' );
229                         _doing_it_wrong( __FUNCTION__, $message, '3.6.0' );
230                         return;
231                 }
232         }
233
234         wp_scripts()->remove( $handle );
235 }
236
237 /**
238  * Enqueue a script.
239  *
240  * Registers the script if $src provided (does NOT overwrite), and enqueues it.
241  *
242  * @see WP_Dependencies::add()
243  * @see WP_Dependencies::add_data()
244  * @see WP_Dependencies::enqueue()
245  *
246  * @since 2.1.0
247  *
248  * @param string           $handle    Name of the script. Should be unique.
249  * @param string           $src       Full URL of the script, or path of the script relative to the WordPress root directory.
250  * @param array            $deps      Optional. An array of registered script handles this script depends on. Default empty array.
251  * @param string|bool|null $ver       Optional. String specifying script version number, if it has one, which is added to the URL
252  *                                    as a query string for cache busting purposes. If version is set to false, a version
253  *                                    number is automatically added equal to current installed WordPress version.
254  *                                    If set to null, no version is added.
255  * @param bool             $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
256  *                                    Default 'false'.
257  */
258 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
259         $wp_scripts = wp_scripts();
260
261         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
262
263
264         if ( $src || $in_footer ) {
265                 $_handle = explode( '?', $handle );
266
267                 if ( $src ) {
268                         $wp_scripts->add( $_handle[0], $src, $deps, $ver );
269                 }
270
271                 if ( $in_footer ) {
272                         $wp_scripts->add_data( $_handle[0], 'group', 1 );
273                 }
274         }
275
276         $wp_scripts->enqueue( $handle );
277 }
278
279 /**
280  * Remove a previously enqueued script.
281  *
282  * @see WP_Dependencies::dequeue()
283  *
284  * @since 3.1.0
285  *
286  * @param string $handle Name of the script to be removed.
287  */
288 function wp_dequeue_script( $handle ) {
289         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
290
291         wp_scripts()->dequeue( $handle );
292 }
293
294 /**
295  * Check whether a script has been added to the queue.
296  *
297  * @since 2.8.0
298  * @since 3.5.0 'enqueued' added as an alias of the 'queue' list.
299  *
300  * @param string $handle Name of the script.
301  * @param string $list   Optional. Status of the script to check. Default 'enqueued'.
302  *                       Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
303  * @return bool Whether the script is queued.
304  */
305 function wp_script_is( $handle, $list = 'enqueued' ) {
306         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
307
308         return (bool) wp_scripts()->query( $handle, $list );
309 }
310
311 /**
312  * Add metadata to a script.
313  *
314  * Works only if the script has already been added.
315  *
316  * Possible values for $key and $value:
317  * 'conditional' string Comments for IE 6, lte IE 7, etc.
318  *
319  * @since 4.2.0
320  *
321  * @see WP_Dependency::add_data()
322  *
323  * @param string $handle Name of the script.
324  * @param string $key    Name of data point for which we're storing a value.
325  * @param mixed  $value  String containing the data to be added.
326  * @return bool True on success, false on failure.
327  */
328 function wp_script_add_data( $handle, $key, $value ){
329         return wp_scripts()->add_data( $handle, $key, $value );
330 }