Wordpress 2.9.2-scripts
[autoinstalls/wordpress.git] / wp-includes / class.wp-scripts.php
1 <?php
2 /**
3  * BackPress Scripts enqueue.
4  *
5  * These classes were refactored from the WordPress WP_Scripts and WordPress
6  * script enqueue API.
7  *
8  * @package BackPress
9  * @since r16
10  */
11
12 /**
13  * BackPress Scripts enqueue class.
14  *
15  * @package BackPress
16  * @uses WP_Dependencies
17  * @since r16
18  */
19 class WP_Scripts extends WP_Dependencies {
20         var $base_url; // Full URL with trailing slash
21         var $content_url;
22         var $default_version;
23         var $in_footer = array();
24         var $concat = '';
25         var $concat_version = '';
26         var $do_concat = false;
27         var $print_html = '';
28         var $print_code = '';
29         var $ext_handles = '';
30         var $ext_version = '';
31         var $default_dirs;
32
33         function __construct() {
34                 do_action_ref_array( 'wp_default_scripts', array(&$this) );
35         }
36
37         /**
38          * Prints scripts
39          *
40          * Prints the scripts passed to it or the print queue.  Also prints all necessary dependencies.
41          *
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
45          */
46         function print_scripts( $handles = false, $group = false ) {
47                 return $this->do_items( $handles, $group );
48         }
49
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]) )
52                         return false;
53
54                 $object_name = $this->registered[$handle]->extra['l10n'][0];
55
56                 $data = "var $object_name = {\n";
57                 $eol = '';
58                 foreach ( $this->registered[$handle]->extra['l10n'][1] as $var => $val ) {
59                         if ( 'l10n_print_after' == $var ) {
60                                 $after = $val;
61                                 continue;
62                         }
63                         $data .= "$eol\t$var: \"" . esc_js( $val ) . '"';
64                         $eol = ",\n";
65                 }
66                 $data .= "\n};\n";
67                 $data .= isset($after) ? "$after\n" : '';
68
69                 if ( $echo ) {
70                         echo "<script type='text/javascript'>\n";
71                         echo "/* <![CDATA[ */\n";
72                         echo $data;
73                         echo "/* ]]> */\n";
74                         echo "</script>\n";
75                         return true;
76                 } else {
77                         return $data;
78                 }
79         }
80
81         function do_item( $handle, $group = false ) {
82                 if ( !parent::do_item($handle) )
83                         return false;
84
85                 if ( 0 === $group && $this->groups[$handle] > 0 ) {
86                         $this->in_footer[] = $handle;
87                         return false;
88                 }
89
90                 if ( false === $group && in_array($handle, $this->in_footer, true) )
91                         $this->in_footer = array_diff( $this->in_footer, (array) $handle );
92
93                 $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
94                 if ( isset($this->args[$handle]) )
95                         $ver .= '&amp;' . $this->args[$handle];
96
97                 $src = $this->registered[$handle]->src;
98
99                 if ( $this->do_concat ) {
100                         $srce = apply_filters( 'script_loader_src', $src, $handle );
101                         if ( $this->in_default_dir($srce) ) {
102                                 $this->print_code .= $this->print_scripts_l10n( $handle, false );
103                                 $this->concat .= "$handle,";
104                                 $this->concat_version .= "$handle$ver";
105                                 return true;
106                         } else {
107                                 $this->ext_handles .= "$handle,";
108                                 $this->ext_version .= "$handle$ver";
109                         }
110                 }
111
112                 $this->print_scripts_l10n( $handle );
113                 if ( !preg_match('|^https?://|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
114                         $src = $this->base_url . $src;
115                 }
116
117                 $src = add_query_arg('ver', $ver, $src);
118                 $src = esc_url(apply_filters( 'script_loader_src', $src, $handle ));
119
120                 if ( $this->do_concat )
121                         $this->print_html .= "<script type='text/javascript' src='$src'></script>\n";
122                 else
123                         echo "<script type='text/javascript' src='$src'></script>\n";
124
125                 return true;
126         }
127
128         /**
129          * Localizes a script
130          *
131          * Localizes only if script has already been added
132          *
133          * @param string handle Script name
134          * @param string object_name Name of JS object to hold l10n info
135          * @param array l10n Array of JS var name => localized string
136          * @return bool Successful localization
137          */
138         function localize( $handle, $object_name, $l10n ) {
139                 if ( !$object_name || !$l10n )
140                         return false;
141                 return $this->add_data( $handle, 'l10n', array( $object_name, $l10n ) );
142         }
143
144         function set_group( $handle, $recursion, $group = false ) {
145                 $grp = isset($this->registered[$handle]->extra['group']) ? (int) $this->registered[$handle]->extra['group'] : 0;
146                 if ( false !== $group && $grp > $group )
147                         $grp = $group;
148
149                 return parent::set_group( $handle, $recursion, $grp );
150         }
151
152         function all_deps( $handles, $recursion = false, $group = false ) {
153                 $r = parent::all_deps( $handles, $recursion );
154                 if ( !$recursion )
155                         $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
156                 return $r;
157         }
158
159         function do_head_items() {
160                 $this->do_items(false, 0);
161                 return $this->done;
162         }
163
164         function do_footer_items() {
165                 if ( !empty($this->in_footer) ) {
166                         foreach( $this->in_footer as $key => $handle ) {
167                                 if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) {
168                                         $this->do_item($handle);
169                                         $this->done[] = $handle;
170                                         unset( $this->in_footer[$key] );
171                                 }
172                         }
173                 }
174                 return $this->done;
175         }
176
177         function in_default_dir($src) {
178                 if ( ! $this->default_dirs )
179                         return true;
180
181                 foreach ( (array) $this->default_dirs as $test ) {
182                         if ( 0 === strpos($src, $test) )
183                                 return true;
184                 }
185                 return false;
186         }
187
188         function reset() {
189                 $this->do_concat = false;
190                 $this->print_code = '';
191                 $this->concat = '';
192                 $this->concat_version = '';
193                 $this->print_html = '';
194                 $this->ext_version = '';
195                 $this->ext_handles = '';
196         }
197 }