3 * Dependencies API: WP_Scripts class
8 * @subpackage Dependencies
12 * Core class used to register scripts.
15 * @uses WP_Dependencies
18 class WP_Scripts extends WP_Dependencies {
20 * Base URL for scripts.
22 * Full URL with trailing slash.
31 * URL of the content directory.
40 * Default version string for stylesheets.
46 public $default_version;
49 * Holds handles of scripts which are enqueued in footer.
55 public $in_footer = array();
58 * Holds a list of script handles which will be concatenated.
67 * Holds a string which contains script handles and their version.
74 public $concat_version = '';
77 * Whether to perform concatenation.
83 public $do_concat = false;
86 * Holds HTML markup of scripts and additional data if concatenation
93 public $print_html = '';
96 * Holds inline code if concatenation is enabled.
102 public $print_code = '';
105 * Holds a list of script handles which are not in the default directory
106 * if concatenation is enabled.
114 public $ext_handles = '';
117 * Holds a string which contains handles and versions of scripts which
118 * are not in the default directory if concatenation is enabled.
126 public $ext_version = '';
129 * List of default directories.
135 public $default_dirs;
143 public function __construct() {
145 add_action( 'init', array( $this, 'init' ), 0 );
149 * Initialize the class.
154 public function init() {
156 * Fires when the WP_Scripts instance is initialized.
160 * @param WP_Scripts &$this WP_Scripts instance, passed by reference.
162 do_action_ref_array( 'wp_default_scripts', array(&$this) );
168 * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies.
171 * @since 2.8.0 Added the `$group` parameter.
174 * @param mixed $handles Optional. Scripts to be printed. (void) prints queue, (string) prints
175 * that script, (array of strings) prints those scripts. Default false.
176 * @param int $group Optional. If scripts were queued in groups prints this group number.
178 * @return array Scripts that have been printed.
180 public function print_scripts( $handles = false, $group = false ) {
181 return $this->do_items( $handles, $group );
185 * Prints extra scripts of a registered script.
188 * @since 2.8.0 Added the `$echo` parameter.
192 * @see print_extra_script()
194 * @param string $handle The script's registered handle.
195 * @param bool $echo Optional. Whether to echo the extra script instead of just returning it.
197 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise.
199 public function print_scripts_l10n( $handle, $echo = true ) {
200 _deprecated_function( __FUNCTION__, '3.3', 'print_extra_script()' );
201 return $this->print_extra_script( $handle, $echo );
205 * Prints extra scripts of a registered script.
210 * @param string $handle The script's registered handle.
211 * @param bool $echo Optional. Whether to echo the extra script instead of just returning it.
213 * @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, true otherwise.
215 public function print_extra_script( $handle, $echo = true ) {
216 if ( !$output = $this->get_data( $handle, 'data' ) )
222 echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
223 echo "/* <![CDATA[ */\n";
232 * Processes a script dependency.
235 * @since 2.8.0 Added the `$group` parameter.
238 * @see WP_Dependencies::do_item()
240 * @param string $handle The script's registered handle.
241 * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false.
242 * @return bool True on success, false on failure.
244 public function do_item( $handle, $group = false ) {
245 if ( !parent::do_item($handle) )
248 if ( 0 === $group && $this->groups[$handle] > 0 ) {
249 $this->in_footer[] = $handle;
253 if ( false === $group && in_array($handle, $this->in_footer, true) )
254 $this->in_footer = array_diff( $this->in_footer, (array) $handle );
256 $obj = $this->registered[$handle];
258 if ( null === $obj->ver ) {
261 $ver = $obj->ver ? $obj->ver : $this->default_version;
264 if ( isset($this->args[$handle]) )
265 $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle];
268 $cond_before = $cond_after = '';
269 $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
271 if ( $conditional ) {
272 $cond_before = "<!--[if {$conditional}]>\n";
273 $cond_after = "<![endif]-->\n";
276 $before_handle = $this->print_inline_script( $handle, 'before', false );
277 $after_handle = $this->print_inline_script( $handle, 'after', false );
279 if ( $before_handle ) {
280 $before_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $before_handle );
283 if ( $after_handle ) {
284 $after_handle = sprintf( "<script type='text/javascript'>\n%s\n</script>\n", $after_handle );
287 if ( $this->do_concat ) {
289 * Filter the script loader source.
293 * @param string $src Script loader source path.
294 * @param string $handle Script handle.
296 $srce = apply_filters( 'script_loader_src', $src, $handle );
298 if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle ) ) {
299 $this->do_concat = false;
301 // Have to print the so-far concatenated scripts right away to maintain the right order.
304 } elseif ( $this->in_default_dir( $srce ) && ! $conditional ) {
305 $this->print_code .= $this->print_extra_script( $handle, false );
306 $this->concat .= "$handle,";
307 $this->concat_version .= "$handle$ver";
310 $this->ext_handles .= "$handle,";
311 $this->ext_version .= "$handle$ver";
315 $has_conditional_data = $conditional && $this->get_data( $handle, 'data' );
317 if ( $has_conditional_data ) {
321 $this->print_extra_script( $handle );
323 if ( $has_conditional_data ) {
327 // A single item may alias a set of items, by having dependencies, but no source.
332 if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
333 $src = $this->base_url . $src;
336 if ( ! empty( $ver ) )
337 $src = add_query_arg( 'ver', $ver, $src );
339 /** This filter is documented in wp-includes/class.wp-scripts.php */
340 $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
345 $tag = "{$cond_before}{$before_handle}<script type='text/javascript' src='$src'></script>\n{$after_handle}{$cond_after}";
348 * Filter the HTML script tag of an enqueued script.
352 * @param string $tag The `<script>` tag for the enqueued script.
353 * @param string $handle The script's registered handle.
354 * @param string $src The script's source URL.
356 $tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );
358 if ( $this->do_concat ) {
359 $this->print_html .= $tag;
368 * Adds extra code to a registered script.
373 * @param string $handle Name of the script to add the inline script to. Must be lowercase.
374 * @param string $data String containing the javascript to be added.
375 * @param string $position Optional. Whether to add the inline script before the handle
376 * or after. Default 'after'.
377 * @return bool True on success, false on failure.
379 public function add_inline_script( $handle, $data, $position = 'after' ) {
384 if ( 'after' !== $position ) {
385 $position = 'before';
388 $script = (array) $this->get_data( $handle, $position );
391 return $this->add_data( $handle, $position, $script );
395 * Prints inline scripts registered for a specific handle.
400 * @param string $handle Name of the script to add the inline script to. Must be lowercase.
401 * @param string $position Optional. Whether to add the inline script before the handle
402 * or after. Default 'after'.
403 * @param bool $echo Optional. Whether to echo the script instead of just returning it.
405 * @return string|false Script on success, false otherwise.
407 public function print_inline_script( $handle, $position = 'after', $echo = true ) {
408 $output = $this->get_data( $handle, $position );
410 if ( empty( $output ) ) {
414 $output = trim( implode( "\n", $output ), "\n" );
417 printf( "<script type='text/javascript'>\n%s\n</script>\n", $output );
424 * Localizes a script, only if the script has already been added.
429 * @param string $handle
430 * @param string $object_name
434 public function localize( $handle, $object_name, $l10n ) {
435 if ( $handle === 'jquery' )
436 $handle = 'jquery-core';
438 if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
439 $after = $l10n['l10n_print_after'];
440 unset($l10n['l10n_print_after']);
443 foreach ( (array) $l10n as $key => $value ) {
444 if ( !is_scalar($value) )
447 $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
450 $script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
452 if ( !empty($after) )
453 $script .= "\n$after;";
455 $data = $this->get_data( $handle, 'data' );
457 if ( !empty( $data ) )
458 $script = "$data\n$script";
460 return $this->add_data( $handle, 'data', $script );
469 * @see WP_Dependencies::set_group()
471 * @param string $handle Name of the item. Should be unique.
472 * @param bool $recursion Internal flag that calling function was called recursively.
473 * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false.
474 * @return bool Not already in the group or a lower group
476 public function set_group( $handle, $recursion, $group = false ) {
477 if ( isset( $this->registered[$handle]->args ) && $this->registered[$handle]->args === 1 )
480 $grp = (int) $this->get_data( $handle, 'group' );
482 if ( false !== $group && $grp > $group )
485 return parent::set_group( $handle, $recursion, $grp );
489 * Determines script dependencies.
494 * @see WP_Dependencies::all_deps()
496 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
497 * @param bool $recursion Internal flag that function is calling itself.
498 * @param int|false $group Optional. Group level: (int) level, (false) no groups. Default false.
499 * @return bool True on success, false on failure.
501 public function all_deps( $handles, $recursion = false, $group = false ) {
502 $r = parent::all_deps( $handles, $recursion, $group );
503 if ( ! $recursion ) {
505 * Filter the list of script dependencies left to print.
509 * @param array $to_do An array of script dependencies.
511 $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
517 * Processes items and dependencies for the head group.
522 * @see WP_Dependencies::do_items()
524 * @return array Handles of items that have been processed.
526 public function do_head_items() {
527 $this->do_items(false, 0);
532 * Processes items and dependencies for the footer group.
537 * @see WP_Dependencies::do_items()
539 * @return array Handles of items that have been processed.
541 public function do_footer_items() {
542 $this->do_items(false, 1);
547 * Whether a handle's source is in a default directory.
552 * @param string $src The source of the enqueued script.
553 * @return bool True if found, false if not.
555 public function in_default_dir( $src ) {
556 if ( ! $this->default_dirs ) {
560 if ( 0 === strpos( $src, '/' . WPINC . '/js/l10n' ) ) {
564 foreach ( (array) $this->default_dirs as $test ) {
565 if ( 0 === strpos( $src, $test ) ) {
573 * Resets class properties.
578 public function reset() {
579 $this->do_concat = false;
580 $this->print_code = '';
582 $this->concat_version = '';
583 $this->print_html = '';
584 $this->ext_version = '';
585 $this->ext_handles = '';