]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class.wp-scripts.php
Wordpress 3.0-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                 if ( null === $this->registered[$handle]->ver )
94                         $ver = '';
95                 else
96                         $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
97
98                 if ( isset($this->args[$handle]) )
99                         $ver = $ver ? $ver . '&amp;' . $this->args[$handle] : $this->args[$handle];
100
101                 $src = $this->registered[$handle]->src;
102
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";
109                                 return true;
110                         } else {
111                                 $this->ext_handles .= "$handle,";
112                                 $this->ext_version .= "$handle$ver";
113                         }
114                 }
115
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;
119                 }
120
121                 if ( !empty($ver) )
122                         $src = add_query_arg('ver', $ver, $src);
123                 $src = esc_url(apply_filters( 'script_loader_src', $src, $handle ));
124
125                 if ( $this->do_concat )
126                         $this->print_html .= "<script type='text/javascript' src='$src'></script>\n";
127                 else
128                         echo "<script type='text/javascript' src='$src'></script>\n";
129
130                 return true;
131         }
132
133         /**
134          * Localizes a script
135          *
136          * Localizes only if script has already been added
137          *
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
142          */
143         function localize( $handle, $object_name, $l10n ) {
144                 if ( !$object_name || !$l10n )
145                         return false;
146                 return $this->add_data( $handle, 'l10n', array( $object_name, $l10n ) );
147         }
148
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 )
152                         $grp = $group;
153
154                 return parent::set_group( $handle, $recursion, $grp );
155         }
156
157         function all_deps( $handles, $recursion = false, $group = false ) {
158                 $r = parent::all_deps( $handles, $recursion );
159                 if ( !$recursion )
160                         $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
161                 return $r;
162         }
163
164         function do_head_items() {
165                 $this->do_items(false, 0);
166                 return $this->done;
167         }
168
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] );
176                                 }
177                         }
178                 }
179                 return $this->done;
180         }
181
182         function in_default_dir($src) {
183                 if ( ! $this->default_dirs )
184                         return true;
185
186                 foreach ( (array) $this->default_dirs as $test ) {
187                         if ( 0 === strpos($src, $test) )
188                                 return true;
189                 }
190                 return false;
191         }
192
193         function reset() {
194                 $this->do_concat = false;
195                 $this->print_code = '';
196                 $this->concat = '';
197                 $this->concat_version = '';
198                 $this->print_html = '';
199                 $this->ext_version = '';
200                 $this->ext_handles = '';
201         }
202 }