]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/functions.wp-scripts.php
WordPress 4.1-scripts
[autoinstalls/wordpress.git] / wp-includes / functions.wp-scripts.php
1 <?php
2 /**
3  * BackPress Scripts Procedural API
4  *
5  * @since 2.6.0
6  *
7  * @package WordPress
8  * @subpackage BackPress
9  */
10
11 /**
12  * Print scripts in document head that are in the $handles queue.
13  *
14  * Called by admin-header.php and wp_head hook. Since it is called by wp_head on every page load,
15  * the function does not instantiate the WP_Scripts object unless script names are explicitly passed.
16  * Makes use of already-instantiated $wp_scripts global if present. Use provided wp_print_scripts
17  * hook to register/enqueue new scripts.
18  *
19  * @see WP_Scripts::do_items()
20  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
21  *
22  * @since 2.6.0
23  *
24  * @param string|bool|array $handles Optional. Scripts to be printed. Default 'false'.
25  * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
26  */
27 function wp_print_scripts( $handles = false ) {
28         /**
29          * Fires before scripts in the $handles queue are printed.
30          *
31          * @since 2.1.0
32          */
33         do_action( 'wp_print_scripts' );
34         if ( '' === $handles ) // for wp_head
35                 $handles = false;
36
37         global $wp_scripts;
38         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
39                 if ( ! did_action( 'init' ) )
40                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
41                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
42
43                 if ( !$handles )
44                         return array(); // No need to instantiate if nothing is there.
45                 else
46                         $wp_scripts = new WP_Scripts();
47         }
48
49         return $wp_scripts->do_items( $handles );
50 }
51
52 /**
53  * Register a new script.
54  *
55  * Registers a script to be linked later using the wp_enqueue_script() function.
56  *
57  * @see WP_Dependencies::add(), WP_Dependencies::add_data()
58  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
59  *
60  * @since 2.6.0
61  *
62  * @param string      $handle    Name of the script. Should be unique.
63  * @param string      $src       Path to the script from the WordPress root directory. Example: '/js/myscript.js'.
64  * @param array       $deps      Optional. An array of registered script handles this script depends on. Set to false if there
65  *                               are no dependencies. Default empty array.
66  * @param string|bool $ver       Optional. String specifying script version number, if it has one, which is concatenated
67  *                               to end of path as a query string. If no version is specified or set to false, a version
68  *                               number is automatically added equal to current installed WordPress version.
69  *                               If set to null, no version is added. Default 'false'. Accepts 'false', 'null', or 'string'.
70  * @param bool        $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
71  *                               Default 'false'. Accepts 'false' or 'true'.
72  */
73 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
74         global $wp_scripts;
75         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
76                 if ( ! did_action( 'init' ) )
77                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
78                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
79                 $wp_scripts = new WP_Scripts();
80         }
81
82         $wp_scripts->add( $handle, $src, $deps, $ver );
83         if ( $in_footer )
84                 $wp_scripts->add_data( $handle, 'group', 1 );
85 }
86
87 /**
88  * Localize a script.
89  *
90  * Works only if the script has already been added.
91  *
92  * Accepts an associative array $l10n and creates a JavaScript object:
93  *
94  *     "$object_name" = {
95  *         key: value,
96  *         key: value,
97  *         ...
98  *     }
99  *
100  *
101  * @see WP_Dependencies::localize()
102  * @link https://core.trac.wordpress.org/ticket/11520
103  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
104  *
105  * @since 2.6.0
106  *
107  * @todo Documentation cleanup
108  *
109  * @param string $handle      Script handle the data will be attached to.
110  * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
111  *                            Example: '/[a-zA-Z0-9_]+/'.
112  * @param array $l10n         The data itself. The data can be either a single or multi-dimensional array.
113  * @return bool True if the script was successfully localized, false otherwise.
114  */
115 function wp_localize_script( $handle, $object_name, $l10n ) {
116         global $wp_scripts;
117         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
118                 if ( ! did_action( 'init' ) )
119                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
120                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
121
122                 return false;
123         }
124
125         return $wp_scripts->localize( $handle, $object_name, $l10n );
126 }
127
128 /**
129  * Remove a registered script.
130  *
131  * Note: there are intentional safeguards in place to prevent critical admin scripts,
132  * such as jQuery core, from being unregistered.
133  *
134  * @see WP_Dependencies::remove()
135  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
136  *
137  * @since 2.6.0
138  *
139  * @param string $handle Name of the script to be removed.
140  */
141 function wp_deregister_script( $handle ) {
142         global $wp_scripts;
143         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
144                 if ( ! did_action( 'init' ) )
145                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
146                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
147                 $wp_scripts = new WP_Scripts();
148         }
149
150         /**
151          * Do not allow accidental or negligent de-registering of critical scripts in the admin.
152          * Show minimal remorse if the correct hook is used.
153          */
154         $current_filter = current_filter();
155         if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) ||
156                 ( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter )
157         ) {
158                 $no = array(
159                         'jquery', 'jquery-core', 'jquery-migrate', 'jquery-ui-core', 'jquery-ui-accordion',
160                         'jquery-ui-autocomplete', 'jquery-ui-button', 'jquery-ui-datepicker', 'jquery-ui-dialog',
161                         'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-menu', 'jquery-ui-mouse',
162                         'jquery-ui-position', 'jquery-ui-progressbar', 'jquery-ui-resizable', 'jquery-ui-selectable',
163                         'jquery-ui-slider', 'jquery-ui-sortable', 'jquery-ui-spinner', 'jquery-ui-tabs',
164                         'jquery-ui-tooltip', 'jquery-ui-widget', 'underscore', 'backbone',
165                 );
166
167                 if ( in_array( $handle, $no ) ) {
168                         $message = sprintf( __( 'Do not deregister the %1$s script in the administration area. To target the frontend theme, use the %2$s hook.' ),
169                                 "<code>$handle</code>", '<code>wp_enqueue_scripts</code>' );
170                         _doing_it_wrong( __FUNCTION__, $message, '3.6' );
171                         return;
172                 }
173         }
174
175         $wp_scripts->remove( $handle );
176 }
177
178 /**
179  * Enqueue a script.
180  *
181  * Registers the script if $src provided (does NOT overwrite), and enqueues it.
182  *
183  * @see WP_Dependencies::add(), WP_Dependencies::add_data(), WP_Dependencies::enqueue()
184  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
185  *
186  * @since 2.6.0
187
188  * @param string      $handle    Name of the script.
189  * @param string|bool $src       Path to the script from the root directory of WordPress. Example: '/js/myscript.js'.
190  * @param array       $deps      An array of registered handles this script depends on. Default empty array.
191  * @param string|bool $ver       Optional. String specifying the script version number, if it has one. This parameter
192  *                               is used to ensure that the correct version is sent to the client regardless of caching,
193  *                               and so should be included if a version number is available and makes sense for the script.
194  * @param bool        $in_footer Optional. Whether to enqueue the script before </head> or before </body>.
195  *                               Default 'false'. Accepts 'false' or 'true'.
196  */
197 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
198         global $wp_scripts;
199         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
200                 if ( ! did_action( 'init' ) )
201                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
202                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
203                 $wp_scripts = new WP_Scripts();
204         }
205
206         if ( $src ) {
207                 $_handle = explode('?', $handle);
208                 $wp_scripts->add( $_handle[0], $src, $deps, $ver );
209                 if ( $in_footer )
210                         $wp_scripts->add_data( $_handle[0], 'group', 1 );
211         }
212         $wp_scripts->enqueue( $handle );
213 }
214
215 /**
216  * Remove a previously enqueued script.
217  *
218  * @see WP_Dependencies::dequeue()
219  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
220  *
221  * @since 3.1.0
222  *
223  * @param string $handle Name of the script to be removed.
224  */
225 function wp_dequeue_script( $handle ) {
226         global $wp_scripts;
227         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
228                 if ( ! did_action( 'init' ) )
229                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
230                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
231                 $wp_scripts = new WP_Scripts();
232         }
233
234         $wp_scripts->dequeue( $handle );
235 }
236
237 /**
238  * Check whether a script has been added to the queue.
239  *
240  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
241  *
242  * @since 2.8.0
243  * @since 3.5.0 'enqueued' added as an alias of the 'queue' list.
244  *
245  * @param string $handle Name of the script.
246  * @param string $list   Optional. Status of the script to check. Default 'enqueued'.
247  *                       Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
248  * @return bool Whether the script script is queued.
249  */
250 function wp_script_is( $handle, $list = 'enqueued' ) {
251         global $wp_scripts;
252         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
253                 if ( ! did_action( 'init' ) )
254                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
255                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
256                 $wp_scripts = new WP_Scripts();
257         }
258
259         return (bool) $wp_scripts->query( $handle, $list );
260 }