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 var $base_url; // Full URL with trailing slash
23 var $in_footer = array();
25 var $concat_version = '';
26 var $do_concat = false;
29 var $ext_handles = '';
30 var $ext_version = '';
33 function __construct() {
34 do_action_ref_array( 'wp_default_scripts', array(&$this) );
40 * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies.
42 * @param mixed handles (optional) Scripts to be printed. (void) prints queue, (string) prints that script, (array of strings) prints those scripts.
43 * @param int group (optional) If scripts were queued in groups prints this group number.
44 * @return array Scripts that have been printed
46 function print_scripts( $handles = false, $group = false ) {
47 return $this->do_items( $handles, $group );
50 function print_scripts_l10n( $handle, $echo = true ) {
51 if ( empty($this->registered[$handle]->extra['l10n']) || empty($this->registered[$handle]->extra['l10n'][0]) || !is_array($this->registered[$handle]->extra['l10n'][1]) )
54 $object_name = $this->registered[$handle]->extra['l10n'][0];
56 $data = "var $object_name = {\n";
58 foreach ( $this->registered[$handle]->extra['l10n'][1] as $var => $val ) {
59 if ( 'l10n_print_after' == $var ) {
63 $data .= "$eol\t$var: \"" . esc_js( $val ) . '"';
67 $data .= isset($after) ? "$after\n" : '';
70 echo "<script type='text/javascript'>\n";
71 echo "/* <![CDATA[ */\n";
81 function do_item( $handle, $group = false ) {
82 if ( !parent::do_item($handle) )
85 if ( 0 === $group && $this->groups[$handle] > 0 ) {
86 $this->in_footer[] = $handle;
90 if ( false === $group && in_array($handle, $this->in_footer, true) )
91 $this->in_footer = array_diff( $this->in_footer, (array) $handle );
93 if ( null === $this->registered[$handle]->ver )
96 $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
98 if ( isset($this->args[$handle]) )
99 $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle];
101 $src = $this->registered[$handle]->src;
103 if ( $this->do_concat ) {
104 $srce = apply_filters( 'script_loader_src', $src, $handle );
105 if ( $this->in_default_dir($srce) ) {
106 $this->print_code .= $this->print_scripts_l10n( $handle, false );
107 $this->concat .= "$handle,";
108 $this->concat_version .= "$handle$ver";
111 $this->ext_handles .= "$handle,";
112 $this->ext_version .= "$handle$ver";
116 $this->print_scripts_l10n( $handle );
117 if ( !preg_match('|^https?://|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
118 $src = $this->base_url . $src;
122 $src = add_query_arg('ver', $ver, $src);
123 $src = esc_url(apply_filters( 'script_loader_src', $src, $handle ));
125 if ( $this->do_concat )
126 $this->print_html .= "<script type='text/javascript' src='$src'></script>\n";
128 echo "<script type='text/javascript' src='$src'></script>\n";
136 * Localizes only if script has already been added
138 * @param string handle Script name
139 * @param string object_name Name of JS object to hold l10n info
140 * @param array l10n Array of JS var name => localized string
141 * @return bool Successful localization
143 function localize( $handle, $object_name, $l10n ) {
144 if ( !$object_name || !$l10n )
146 return $this->add_data( $handle, 'l10n', array( $object_name, $l10n ) );
149 function set_group( $handle, $recursion, $group = false ) {
150 $grp = isset($this->registered[$handle]->extra['group']) ? (int) $this->registered[$handle]->extra['group'] : 0;
151 if ( false !== $group && $grp > $group )
154 return parent::set_group( $handle, $recursion, $grp );
157 function all_deps( $handles, $recursion = false, $group = false ) {
158 $r = parent::all_deps( $handles, $recursion );
160 $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
164 function do_head_items() {
165 $this->do_items(false, 0);
169 function do_footer_items() {
170 if ( !empty($this->in_footer) ) {
171 foreach( $this->in_footer as $key => $handle ) {
172 if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) {
173 $this->do_item($handle);
174 $this->done[] = $handle;
175 unset( $this->in_footer[$key] );
182 function in_default_dir($src) {
183 if ( ! $this->default_dirs )
186 foreach ( (array) $this->default_dirs as $test ) {
187 if ( 0 === strpos($src, $test) )
194 $this->do_concat = false;
195 $this->print_code = '';
197 $this->concat_version = '';
198 $this->print_html = '';
199 $this->ext_version = '';
200 $this->ext_handles = '';