1 <?php |
1 <?php |
2 /** |
2 /** |
3 * BackPress Scripts enqueue. |
3 * Dependencies API: WP_Scripts class |
4 * |
4 * |
5 * These classes were refactored from the WordPress WP_Scripts and WordPress |
5 * @since 2.6.0 |
6 * script enqueue API. |
|
7 * |
6 * |
8 * @package BackPress |
7 * @package WordPress |
9 * @since r16 |
8 * @subpackage Dependencies |
10 */ |
9 */ |
11 |
10 |
12 /** |
11 /** |
13 * BackPress Scripts enqueue class. |
12 * Core class used to register scripts. |
14 * |
13 * |
15 * @package BackPress |
14 * @since 2.1.0 |
16 * @uses WP_Dependencies |
15 * |
17 * @since r16 |
16 * @see WP_Dependencies |
18 */ |
17 */ |
19 class WP_Scripts extends WP_Dependencies { |
18 class WP_Scripts extends WP_Dependencies { |
20 public $base_url; // Full URL with trailing slash |
19 /** |
|
20 * Base URL for scripts. |
|
21 * |
|
22 * Full URL with trailing slash. |
|
23 * |
|
24 * @since 2.6.0 |
|
25 * @var string |
|
26 */ |
|
27 public $base_url; |
|
28 |
|
29 /** |
|
30 * URL of the content directory. |
|
31 * |
|
32 * @since 2.8.0 |
|
33 * @var string |
|
34 */ |
21 public $content_url; |
35 public $content_url; |
|
36 |
|
37 /** |
|
38 * Default version string for stylesheets. |
|
39 * |
|
40 * @since 2.6.0 |
|
41 * @var string |
|
42 */ |
22 public $default_version; |
43 public $default_version; |
|
44 |
|
45 /** |
|
46 * Holds handles of scripts which are enqueued in footer. |
|
47 * |
|
48 * @since 2.8.0 |
|
49 * @var array |
|
50 */ |
23 public $in_footer = array(); |
51 public $in_footer = array(); |
|
52 |
|
53 /** |
|
54 * Holds a list of script handles which will be concatenated. |
|
55 * |
|
56 * @since 2.8.0 |
|
57 * @var string |
|
58 */ |
24 public $concat = ''; |
59 public $concat = ''; |
|
60 |
|
61 /** |
|
62 * Holds a string which contains script handles and their version. |
|
63 * |
|
64 * @since 2.8.0 |
|
65 * @deprecated 3.4.0 |
|
66 * @var string |
|
67 */ |
25 public $concat_version = ''; |
68 public $concat_version = ''; |
|
69 |
|
70 /** |
|
71 * Whether to perform concatenation. |
|
72 * |
|
73 * @since 2.8.0 |
|
74 * @var bool |
|
75 */ |
26 public $do_concat = false; |
76 public $do_concat = false; |
|
77 |
|
78 /** |
|
79 * Holds HTML markup of scripts and additional data if concatenation |
|
80 * is enabled. |
|
81 * |
|
82 * @since 2.8.0 |
|
83 * @var string |
|
84 */ |
27 public $print_html = ''; |
85 public $print_html = ''; |
|
86 |
|
87 /** |
|
88 * Holds inline code if concatenation is enabled. |
|
89 * |
|
90 * @since 2.8.0 |
|
91 * @var string |
|
92 */ |
28 public $print_code = ''; |
93 public $print_code = ''; |
|
94 |
|
95 /** |
|
96 * Holds a list of script handles which are not in the default directory |
|
97 * if concatenation is enabled. |
|
98 * |
|
99 * Unused in core. |
|
100 * |
|
101 * @since 2.8.0 |
|
102 * @var string |
|
103 */ |
29 public $ext_handles = ''; |
104 public $ext_handles = ''; |
|
105 |
|
106 /** |
|
107 * Holds a string which contains handles and versions of scripts which |
|
108 * are not in the default directory if concatenation is enabled. |
|
109 * |
|
110 * Unused in core. |
|
111 * |
|
112 * @since 2.8.0 |
|
113 * @var string |
|
114 */ |
30 public $ext_version = ''; |
115 public $ext_version = ''; |
|
116 |
|
117 /** |
|
118 * List of default directories. |
|
119 * |
|
120 * @since 2.8.0 |
|
121 * @var array |
|
122 */ |
31 public $default_dirs; |
123 public $default_dirs; |
32 |
124 |
|
125 /** |
|
126 * Constructor. |
|
127 * |
|
128 * @since 2.6.0 |
|
129 */ |
33 public function __construct() { |
130 public function __construct() { |
34 $this->init(); |
131 $this->init(); |
35 add_action( 'init', array( $this, 'init' ), 0 ); |
132 add_action( 'init', array( $this, 'init' ), 0 ); |
36 } |
133 } |
37 |
134 |
|
135 /** |
|
136 * Initialize the class. |
|
137 * |
|
138 * @since 3.4.0 |
|
139 */ |
38 public function init() { |
140 public function init() { |
39 /** |
141 /** |
40 * Fires when the WP_Scripts instance is initialized. |
142 * Fires when the WP_Scripts instance is initialized. |
41 * |
143 * |
42 * @since 2.6.0 |
144 * @since 2.6.0 |
43 * |
145 * |
44 * @param WP_Scripts &$this WP_Scripts instance, passed by reference. |
146 * @param WP_Scripts $this WP_Scripts instance (passed by reference). |
45 */ |
147 */ |
46 do_action_ref_array( 'wp_default_scripts', array(&$this) ); |
148 do_action_ref_array( 'wp_default_scripts', array(&$this) ); |
47 } |
149 } |
48 |
150 |
49 /** |
151 /** |
50 * Prints scripts. |
152 * Prints scripts. |
51 * |
153 * |
52 * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies. |
154 * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies. |
|
155 * |
|
156 * @since 2.1.0 |
|
157 * @since 2.8.0 Added the `$group` parameter. |
53 * |
158 * |
54 * @param mixed $handles Optional. Scripts to be printed. (void) prints queue, (string) prints |
159 * @param mixed $handles Optional. Scripts to be printed. (void) prints queue, (string) prints |
55 * that script, (array of strings) prints those scripts. Default false. |
160 * that script, (array of strings) prints those scripts. Default false. |
56 * @param int $group Optional. If scripts were queued in groups prints this group number. |
161 * @param int $group Optional. If scripts were queued in groups prints this group number. |
57 * Default false. |
162 * Default false. |
59 */ |
164 */ |
60 public function print_scripts( $handles = false, $group = false ) { |
165 public function print_scripts( $handles = false, $group = false ) { |
61 return $this->do_items( $handles, $group ); |
166 return $this->do_items( $handles, $group ); |
62 } |
167 } |
63 |
168 |
64 // Deprecated since 3.3, see print_extra_script() |
169 /** |
|
170 * Prints extra scripts of a registered script. |
|
171 * |
|
172 * @since 2.1.0 |
|
173 * @since 2.8.0 Added the `$echo` parameter. |
|
174 * @deprecated 3.3.0 |
|
175 * |
|
176 * @see print_extra_script() |
|
177 * |
|
178 * @param string $handle The script's registered handle. |
|
179 * @param bool $echo Optional. Whether to echo the extra script instead of just returning it. |
|
180 * Default true. |
|
181 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise. |
|
182 */ |
65 public function print_scripts_l10n( $handle, $echo = true ) { |
183 public function print_scripts_l10n( $handle, $echo = true ) { |
66 _deprecated_function( __FUNCTION__, '3.3', 'print_extra_script()' ); |
184 _deprecated_function( __FUNCTION__, '3.3.0', 'WP_Scripts::print_extra_script()' ); |
67 return $this->print_extra_script( $handle, $echo ); |
185 return $this->print_extra_script( $handle, $echo ); |
68 } |
186 } |
69 |
187 |
|
188 /** |
|
189 * Prints extra scripts of a registered script. |
|
190 * |
|
191 * @since 3.3.0 |
|
192 * |
|
193 * @param string $handle The script's registered handle. |
|
194 * @param bool $echo Optional. Whether to echo the extra script instead of just returning it. |
|
195 * Default true. |
|
196 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise. |
|
197 */ |
70 public function print_extra_script( $handle, $echo = true ) { |
198 public function print_extra_script( $handle, $echo = true ) { |
71 if ( !$output = $this->get_data( $handle, 'data' ) ) |
199 if ( !$output = $this->get_data( $handle, 'data' ) ) |
72 return; |
200 return; |
73 |
201 |
74 if ( !$echo ) |
202 if ( !$echo ) |
113 if ( $conditional ) { |
253 if ( $conditional ) { |
114 $cond_before = "<!--[if {$conditional}]>\n"; |
254 $cond_before = "<!--[if {$conditional}]>\n"; |
115 $cond_after = "<![endif]-->\n"; |
255 $cond_after = "<![endif]-->\n"; |
116 } |
256 } |
117 |
257 |
|
258 $before_handle = $this->print_inline_script( $handle, 'before', false ); |
|
259 $after_handle = $this->print_inline_script( $handle, 'after', false ); |
|
260 |
|
261 if ( $before_handle ) { |
|
262 $before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle ); |
|
263 } |
|
264 |
|
265 if ( $after_handle ) { |
|
266 $after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle ); |
|
267 } |
|
268 |
118 if ( $this->do_concat ) { |
269 if ( $this->do_concat ) { |
119 /** |
270 /** |
120 * Filter the script loader source. |
271 * Filters the script loader source. |
121 * |
272 * |
122 * @since 2.2.0 |
273 * @since 2.2.0 |
123 * |
274 * |
124 * @param string $src Script loader source path. |
275 * @param string $src Script loader source path. |
125 * @param string $handle Script handle. |
276 * @param string $handle Script handle. |
126 */ |
277 */ |
127 $srce = apply_filters( 'script_loader_src', $src, $handle ); |
278 $srce = apply_filters( 'script_loader_src', $src, $handle ); |
128 if ( $this->in_default_dir( $srce ) && ! $conditional ) { |
279 |
|
280 if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle ) ) { |
|
281 $this->do_concat = false; |
|
282 |
|
283 // Have to print the so-far concatenated scripts right away to maintain the right order. |
|
284 _print_scripts(); |
|
285 $this->reset(); |
|
286 } elseif ( $this->in_default_dir( $srce ) && ! $conditional ) { |
129 $this->print_code .= $this->print_extra_script( $handle, false ); |
287 $this->print_code .= $this->print_extra_script( $handle, false ); |
130 $this->concat .= "$handle,"; |
288 $this->concat .= "$handle,"; |
131 $this->concat_version .= "$handle$ver"; |
289 $this->concat_version .= "$handle$ver"; |
132 return true; |
290 return true; |
133 } else { |
291 } else { |
182 |
345 |
183 return true; |
346 return true; |
184 } |
347 } |
185 |
348 |
186 /** |
349 /** |
187 * Localizes a script |
350 * Adds extra code to a registered script. |
188 * |
351 * |
189 * Localizes only if the script has already been added |
352 * @since 4.5.0 |
|
353 * |
|
354 * @param string $handle Name of the script to add the inline script to. Must be lowercase. |
|
355 * @param string $data String containing the javascript to be added. |
|
356 * @param string $position Optional. Whether to add the inline script before the handle |
|
357 * or after. Default 'after'. |
|
358 * @return bool True on success, false on failure. |
|
359 */ |
|
360 public function add_inline_script( $handle, $data, $position = 'after' ) { |
|
361 if ( ! $data ) { |
|
362 return false; |
|
363 } |
|
364 |
|
365 if ( 'after' !== $position ) { |
|
366 $position = 'before'; |
|
367 } |
|
368 |
|
369 $script = (array) $this->get_data( $handle, $position ); |
|
370 $script[] = $data; |
|
371 |
|
372 return $this->add_data( $handle, $position, $script ); |
|
373 } |
|
374 |
|
375 /** |
|
376 * Prints inline scripts registered for a specific handle. |
|
377 * |
|
378 * @since 4.5.0 |
|
379 * |
|
380 * @param string $handle Name of the script to add the inline script to. Must be lowercase. |
|
381 * @param string $position Optional. Whether to add the inline script before the handle |
|
382 * or after. Default 'after'. |
|
383 * @param bool $echo Optional. Whether to echo the script instead of just returning it. |
|
384 * Default true. |
|
385 * @return string|false Script on success, false otherwise. |
|
386 */ |
|
387 public function print_inline_script( $handle, $position = 'after', $echo = true ) { |
|
388 $output = $this->get_data( $handle, $position ); |
|
389 |
|
390 if ( empty( $output ) ) { |
|
391 return false; |
|
392 } |
|
393 |
|
394 $output = trim( implode( "\n", $output ), "\n" ); |
|
395 |
|
396 if ( $echo ) { |
|
397 printf( "<script type='text/javascript'>\n%s\n</script>\n", $output ); |
|
398 } |
|
399 |
|
400 return $output; |
|
401 } |
|
402 |
|
403 /** |
|
404 * Localizes a script, only if the script has already been added. |
|
405 * |
|
406 * @since 2.1.0 |
|
407 * |
|
408 * @param string $handle |
|
409 * @param string $object_name |
|
410 * @param array $l10n |
|
411 * @return bool |
190 */ |
412 */ |
191 public function localize( $handle, $object_name, $l10n ) { |
413 public function localize( $handle, $object_name, $l10n ) { |
192 if ( $handle === 'jquery' ) |
414 if ( $handle === 'jquery' ) |
193 $handle = 'jquery-core'; |
415 $handle = 'jquery-core'; |
194 |
416 |
215 $script = "$data\n$script"; |
437 $script = "$data\n$script"; |
216 |
438 |
217 return $this->add_data( $handle, 'data', $script ); |
439 return $this->add_data( $handle, 'data', $script ); |
218 } |
440 } |
219 |
441 |
|
442 /** |
|
443 * Sets handle group. |
|
444 * |
|
445 * @since 2.8.0 |
|
446 * |
|
447 * @see WP_Dependencies::set_group() |
|
448 * |
|
449 * @param string $handle Name of the item. Should be unique. |
|
450 * @param bool $recursion Internal flag that calling function was called recursively. |
|
451 * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
|
452 * @return bool Not already in the group or a lower group |
|
453 */ |
220 public function set_group( $handle, $recursion, $group = false ) { |
454 public function set_group( $handle, $recursion, $group = false ) { |
221 |
455 if ( isset( $this->registered[$handle]->args ) && $this->registered[$handle]->args === 1 ) |
222 if ( $this->registered[$handle]->args === 1 ) |
|
223 $grp = 1; |
456 $grp = 1; |
224 else |
457 else |
225 $grp = (int) $this->get_data( $handle, 'group' ); |
458 $grp = (int) $this->get_data( $handle, 'group' ); |
226 |
459 |
227 if ( false !== $group && $grp > $group ) |
460 if ( false !== $group && $grp > $group ) |
228 $grp = $group; |
461 $grp = $group; |
229 |
462 |
230 return parent::set_group( $handle, $recursion, $grp ); |
463 return parent::set_group( $handle, $recursion, $grp ); |
231 } |
464 } |
232 |
465 |
|
466 /** |
|
467 * Determines script dependencies. |
|
468 * |
|
469 * @since 2.1.0 |
|
470 * |
|
471 * @see WP_Dependencies::all_deps() |
|
472 * |
|
473 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
|
474 * @param bool $recursion Internal flag that function is calling itself. |
|
475 * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false. |
|
476 * @return bool True on success, false on failure. |
|
477 */ |
233 public function all_deps( $handles, $recursion = false, $group = false ) { |
478 public function all_deps( $handles, $recursion = false, $group = false ) { |
234 $r = parent::all_deps( $handles, $recursion ); |
479 $r = parent::all_deps( $handles, $recursion, $group ); |
235 if ( ! $recursion ) { |
480 if ( ! $recursion ) { |
236 /** |
481 /** |
237 * Filter the list of script dependencies left to print. |
482 * Filters the list of script dependencies left to print. |
238 * |
483 * |
239 * @since 2.3.0 |
484 * @since 2.3.0 |
240 * |
485 * |
241 * @param array $to_do An array of script dependencies. |
486 * @param array $to_do An array of script dependencies. |
242 */ |
487 */ |
243 $this->to_do = apply_filters( 'print_scripts_array', $this->to_do ); |
488 $this->to_do = apply_filters( 'print_scripts_array', $this->to_do ); |
244 } |
489 } |
245 return $r; |
490 return $r; |
246 } |
491 } |
247 |
492 |
|
493 /** |
|
494 * Processes items and dependencies for the head group. |
|
495 * |
|
496 * @since 2.8.0 |
|
497 * |
|
498 * @see WP_Dependencies::do_items() |
|
499 * |
|
500 * @return array Handles of items that have been processed. |
|
501 */ |
248 public function do_head_items() { |
502 public function do_head_items() { |
249 $this->do_items(false, 0); |
503 $this->do_items(false, 0); |
250 return $this->done; |
504 return $this->done; |
251 } |
505 } |
252 |
506 |
|
507 /** |
|
508 * Processes items and dependencies for the footer group. |
|
509 * |
|
510 * @since 2.8.0 |
|
511 * |
|
512 * @see WP_Dependencies::do_items() |
|
513 * |
|
514 * @return array Handles of items that have been processed. |
|
515 */ |
253 public function do_footer_items() { |
516 public function do_footer_items() { |
254 $this->do_items(false, 1); |
517 $this->do_items(false, 1); |
255 return $this->done; |
518 return $this->done; |
256 } |
519 } |
257 |
520 |
|
521 /** |
|
522 * Whether a handle's source is in a default directory. |
|
523 * |
|
524 * @since 2.8.0 |
|
525 * |
|
526 * @param string $src The source of the enqueued script. |
|
527 * @return bool True if found, false if not. |
|
528 */ |
258 public function in_default_dir( $src ) { |
529 public function in_default_dir( $src ) { |
259 if ( ! $this->default_dirs ) { |
530 if ( ! $this->default_dirs ) { |
260 return true; |
531 return true; |
261 } |
532 } |
262 |
533 |