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