]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/functions.wp-scripts.php
WordPress 3.6.1-scripts
[autoinstalls/wordpress.git] / wp-includes / functions.wp-scripts.php
1 <?php
2 /**
3  * BackPress script procedural API.
4  *
5  * @package BackPress
6  * @since r16
7  */
8
9 /**
10  * Prints script tags in document head.
11  *
12  * Called by admin-header.php and by wp_head hook. Since it is called by wp_head
13  * on every page load, the function does not instantiate the WP_Scripts object
14  * unless script names are explicitly passed. Does make use of already
15  * instantiated $wp_scripts if present. Use provided wp_print_scripts hook to
16  * register/enqueue new scripts.
17  *
18  * @since r16
19  * @see WP_Dependencies::print_scripts()
20  */
21 function wp_print_scripts( $handles = false ) {
22         do_action( 'wp_print_scripts' );
23         if ( '' === $handles ) // for wp_head
24                 $handles = false;
25
26         global $wp_scripts;
27         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
28                 if ( ! did_action( 'init' ) )
29                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
30                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
31
32                 if ( !$handles )
33                         return array(); // No need to instantiate if nothing is there.
34                 else
35                         $wp_scripts = new WP_Scripts();
36         }
37
38         return $wp_scripts->do_items( $handles );
39 }
40
41 /**
42  * Register new Javascript file.
43  *
44  * @since r16
45  * @param string $handle Script name
46  * @param string $src Script url
47  * @param array $deps (optional) Array of script names on which this script depends
48  * @param string|bool $ver (optional) Script version (used for cache busting), set to null to disable
49  * @param bool $in_footer (optional) Whether to enqueue the script before </head> or before </body>
50  * @return null
51  */
52 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
53         global $wp_scripts;
54         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
55                 if ( ! did_action( 'init' ) )
56                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
57                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
58                 $wp_scripts = new WP_Scripts();
59         }
60
61         $wp_scripts->add( $handle, $src, $deps, $ver );
62         if ( $in_footer )
63                 $wp_scripts->add_data( $handle, 'group', 1 );
64 }
65
66 /**
67  * Wrapper for $wp_scripts->localize().
68  *
69  * Used to localize a script.
70  * Works only if the script has already been added.
71  * Accepts an associative array $l10n and creates JS object:
72  * "$object_name" = {
73  *   key: value,
74  *   key: value,
75  *   ...
76  * }
77  * See http://core.trac.wordpress.org/ticket/11520 for more information.
78  *
79  * @since r16
80  *
81  * @param string $handle The script handle that was registered or used in script-loader
82  * @param string $object_name Name for the created JS object. This is passed directly so it should be qualified JS variable /[a-zA-Z0-9_]+/
83  * @param array $l10n Associative PHP array containing the translated strings. HTML entities will be converted and the array will be JSON encoded.
84  * @return bool Whether the localization was added successfully.
85  */
86 function wp_localize_script( $handle, $object_name, $l10n ) {
87         global $wp_scripts;
88         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
89                 if ( ! did_action( 'init' ) )
90                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
91                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
92
93                 return false;
94         }
95
96         return $wp_scripts->localize( $handle, $object_name, $l10n );
97 }
98
99 /**
100  * Remove a registered script.
101  *
102  * @since r16
103  * @see WP_Scripts::remove() For parameter information.
104  */
105 function wp_deregister_script( $handle ) {
106         global $wp_scripts;
107         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
108                 if ( ! did_action( 'init' ) )
109                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
110                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
111                 $wp_scripts = new WP_Scripts();
112         }
113
114         // Do not allow accidental or negligent deregistering of critical scripts in the admin. Show minimal remorse if the correct hook is used.
115         if ( is_admin() && 'admin_enqueue_scripts' !== current_filter() ) {
116                 $no = array(
117                         'jquery', 'jquery-core', 'jquery-migrate', 'jquery-ui-core', 'jquery-ui-accordion',
118                         'jquery-ui-autocomplete', 'jquery-ui-button', 'jquery-ui-datepicker', 'jquery-ui-dialog',
119                         'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-menu', 'jquery-ui-mouse',
120                         'jquery-ui-position', 'jquery-ui-progressbar', 'jquery-ui-resizable', 'jquery-ui-selectable',
121                         'jquery-ui-slider', 'jquery-ui-sortable', 'jquery-ui-spinner', 'jquery-ui-tabs',
122                         'jquery-ui-tooltip', 'jquery-ui-widget',
123                         'underscore', 'backbone',
124                 );
125
126                 if ( in_array( $handle, $no ) ) {
127                         $message = sprintf( __( 'Do not deregister the %1$s script in the administration area. To target the frontend theme, use the %2$s hook.' ),
128                                 "<code>$handle</code>", '<code>wp_enqueue_scripts</code>' );
129                         _doing_it_wrong( __FUNCTION__, $message, '3.6' );
130                         return;
131                 }
132         }
133
134         $wp_scripts->remove( $handle );
135 }
136
137 /**
138  * Enqueues script.
139  *
140  * Registers the script if src provided (does NOT overwrite) and enqueues.
141  *
142  * @since r16
143  * @see wp_register_script() For parameter information.
144  */
145 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
146         global $wp_scripts;
147         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
148                 if ( ! did_action( 'init' ) )
149                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
150                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
151                 $wp_scripts = new WP_Scripts();
152         }
153
154         if ( $src ) {
155                 $_handle = explode('?', $handle);
156                 $wp_scripts->add( $_handle[0], $src, $deps, $ver );
157                 if ( $in_footer )
158                         $wp_scripts->add_data( $_handle[0], 'group', 1 );
159         }
160         $wp_scripts->enqueue( $handle );
161 }
162
163 /**
164  * Remove an enqueued script.
165  *
166  * @since WP 3.1
167  * @see WP_Scripts::dequeue() For parameter information.
168  */
169 function wp_dequeue_script( $handle ) {
170         global $wp_scripts;
171         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
172                 if ( ! did_action( 'init' ) )
173                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
174                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
175                 $wp_scripts = new WP_Scripts();
176         }
177
178         $wp_scripts->dequeue( $handle );
179 }
180
181 /**
182  * Check whether script has been added to WordPress Scripts.
183  *
184  * By default, checks if the script has been enqueued. You can also
185  * pass 'registered' to $list, to see if the script is registered,
186  * and you can check processing statuses with 'to_do' and 'done'.
187  *
188  * @since WP unknown; BP unknown
189  *
190  * @param string $handle Name of the script.
191  * @param string $list Optional. Defaults to 'enqueued'. Values are
192  *      'registered', 'enqueued' (or 'queue'), 'to_do', and 'done'.
193  * @return bool Whether script is in the list.
194  */
195 function wp_script_is( $handle, $list = 'enqueued' ) {
196         global $wp_scripts;
197         if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
198                 if ( ! did_action( 'init' ) )
199                         _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
200                                 '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>login_enqueue_scripts</code>' ), '3.3' );
201                 $wp_scripts = new WP_Scripts();
202         }
203
204         return (bool) $wp_scripts->query( $handle, $list );
205 }