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