3 * BackPress Scripts enqueue.
5 * These classes were refactored from the WordPress WP_Scripts and WordPress
13 * BackPress Scripts enqueue class.
16 * @uses WP_Dependencies
19 class WP_Scripts extends WP_Dependencies {
20 public $base_url; // Full URL with trailing slash
22 public $default_version;
23 public $in_footer = array();
25 public $concat_version = '';
26 public $do_concat = false;
27 public $print_html = '';
28 public $print_code = '';
29 public $ext_handles = '';
30 public $ext_version = '';
33 public function __construct() {
35 add_action( 'init', array( $this, 'init' ), 0 );
38 public function init() {
40 * Fires when the WP_Scripts instance is initialized.
44 * @param WP_Scripts &$this WP_Scripts instance, passed by reference.
46 do_action_ref_array( 'wp_default_scripts', array(&$this) );
52 * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies.
54 * @param mixed $handles (optional) Scripts to be printed. (void) prints queue, (string) prints that script, (array of strings) prints those scripts.
55 * @param int $group (optional) If scripts were queued in groups prints this group number.
56 * @return array Scripts that have been printed
58 public function print_scripts( $handles = false, $group = false ) {
59 return $this->do_items( $handles, $group );
62 // Deprecated since 3.3, see print_extra_script()
63 public function print_scripts_l10n( $handle, $echo = true ) {
64 _deprecated_function( __FUNCTION__, '3.3', 'print_extra_script()' );
65 return $this->print_extra_script( $handle, $echo );
68 public function print_extra_script( $handle, $echo = true ) {
69 if ( !$output = $this->get_data( $handle, 'data' ) )
75 echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
76 echo "/* <![CDATA[ */\n";
84 public function do_item( $handle, $group = false ) {
85 if ( !parent::do_item($handle) )
88 if ( 0 === $group && $this->groups[$handle] > 0 ) {
89 $this->in_footer[] = $handle;
93 if ( false === $group && in_array($handle, $this->in_footer, true) )
94 $this->in_footer = array_diff( $this->in_footer, (array) $handle );
96 if ( null === $this->registered[$handle]->ver )
99 $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
101 if ( isset($this->args[$handle]) )
102 $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle];
104 $src = $this->registered[$handle]->src;
106 if ( $this->do_concat ) {
108 * Filter the script loader source.
112 * @param string $src Script loader source path.
113 * @param string $handle Script handle.
115 $srce = apply_filters( 'script_loader_src', $src, $handle );
116 if ( $this->in_default_dir($srce) ) {
117 $this->print_code .= $this->print_extra_script( $handle, false );
118 $this->concat .= "$handle,";
119 $this->concat_version .= "$handle$ver";
122 $this->ext_handles .= "$handle,";
123 $this->ext_version .= "$handle$ver";
127 $this->print_extra_script( $handle );
128 if ( !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
129 $src = $this->base_url . $src;
133 $src = add_query_arg('ver', $ver, $src);
135 /** This filter is documented in wp-includes/class.wp-scripts.php */
136 $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
141 if ( $this->do_concat )
142 $this->print_html .= "<script type='text/javascript' src='$src'></script>\n";
144 echo "<script type='text/javascript' src='$src'></script>\n";
152 * Localizes only if the script has already been added
154 public function localize( $handle, $object_name, $l10n ) {
155 if ( $handle === 'jquery' )
156 $handle = 'jquery-core';
158 if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
159 $after = $l10n['l10n_print_after'];
160 unset($l10n['l10n_print_after']);
163 foreach ( (array) $l10n as $key => $value ) {
164 if ( !is_scalar($value) )
167 $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
170 $script = "var $object_name = " . json_encode($l10n) . ';';
172 if ( !empty($after) )
173 $script .= "\n$after;";
175 $data = $this->get_data( $handle, 'data' );
177 if ( !empty( $data ) )
178 $script = "$data\n$script";
180 return $this->add_data( $handle, 'data', $script );
183 public function set_group( $handle, $recursion, $group = false ) {
185 if ( $this->registered[$handle]->args === 1 )
188 $grp = (int) $this->get_data( $handle, 'group' );
190 if ( false !== $group && $grp > $group )
193 return parent::set_group( $handle, $recursion, $grp );
196 public function all_deps( $handles, $recursion = false, $group = false ) {
197 $r = parent::all_deps( $handles, $recursion );
198 if ( ! $recursion ) {
200 * Filter the list of script dependencies left to print.
204 * @param array $to_do An array of script dependencies.
206 $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
211 public function do_head_items() {
212 $this->do_items(false, 0);
216 public function do_footer_items() {
217 $this->do_items(false, 1);
221 public function in_default_dir( $src ) {
222 if ( ! $this->default_dirs ) {
226 if ( 0 === strpos( $src, '/' . WPINC . '/js/l10n' ) ) {
230 foreach ( (array) $this->default_dirs as $test ) {
231 if ( 0 === strpos( $src, $test ) ) {
238 public function reset() {
239 $this->do_concat = false;
240 $this->print_code = '';
242 $this->concat_version = '';
243 $this->print_html = '';
244 $this->ext_version = '';
245 $this->ext_handles = '';