]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class.wp-scripts.php
Wordpress 3.3.1-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         // Deprecated since 3.3, see print_extra_script()
51         function print_scripts_l10n( $handle, $echo = true ) {
52                 _deprecated_function( __FUNCTION__, '3.3', 'print_extra_script()' );
53                 return $this->print_extra_script( $handle, $echo );
54         }
55
56         function print_extra_script( $handle, $echo = true ) {
57                 if ( !$output = $this->get_data( $handle, 'data' ) )
58                         return;
59
60                 if ( !$echo )
61                         return $output;
62
63                 echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
64                 echo "/* <![CDATA[ */\n";
65                 echo "$output\n";
66                 echo "/* ]]> */\n";
67                 echo "</script>\n";
68
69                 return true;
70         }
71
72         function do_item( $handle, $group = false ) {
73                 if ( !parent::do_item($handle) )
74                         return false;
75
76                 if ( 0 === $group && $this->groups[$handle] > 0 ) {
77                         $this->in_footer[] = $handle;
78                         return false;
79                 }
80
81                 if ( false === $group && in_array($handle, $this->in_footer, true) )
82                         $this->in_footer = array_diff( $this->in_footer, (array) $handle );
83
84                 if ( null === $this->registered[$handle]->ver )
85                         $ver = '';
86                 else
87                         $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
88
89                 if ( isset($this->args[$handle]) )
90                         $ver = $ver ? $ver . '&amp;' . $this->args[$handle] : $this->args[$handle];
91
92                 $src = $this->registered[$handle]->src;
93
94                 if ( $this->do_concat ) {
95                         $srce = apply_filters( 'script_loader_src', $src, $handle );
96                         if ( $this->in_default_dir($srce) ) {
97                                 $this->print_code .= $this->print_extra_script( $handle, false );
98                                 $this->concat .= "$handle,";
99                                 $this->concat_version .= "$handle$ver";
100                                 return true;
101                         } else {
102                                 $this->ext_handles .= "$handle,";
103                                 $this->ext_version .= "$handle$ver";
104                         }
105                 }
106
107                 $this->print_extra_script( $handle );
108                 if ( !preg_match('|^https?://|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
109                         $src = $this->base_url . $src;
110                 }
111
112                 if ( !empty($ver) )
113                         $src = add_query_arg('ver', $ver, $src);
114
115                 $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
116
117                 if ( $this->do_concat )
118                         $this->print_html .= "<script type='text/javascript' src='$src'></script>\n";
119                 else
120                         echo "<script type='text/javascript' src='$src'></script>\n";
121
122                 return true;
123         }
124
125         /**
126          * Localizes a script
127          *
128          * Localizes only if the script has already been added
129          */
130         function localize( $handle, $object_name, $l10n ) {
131                 if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
132                         $after = $l10n['l10n_print_after'];
133                         unset($l10n['l10n_print_after']);
134                 }
135
136                 foreach ( (array) $l10n as $key => $value ) {
137                         if ( !is_scalar($value) )
138                                 continue;
139
140                         $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
141                 }
142
143                 $script = "var $object_name = " . json_encode($l10n) . ';';
144
145                 if ( !empty($after) )
146                         $script .= "\n$after";
147
148                 $data = $this->get_data( $handle, 'data' );
149
150                 if ( !empty( $data ) )
151                         $script = "$data;\n$script";
152
153                 return $this->add_data( $handle, 'data', $script );
154         }
155
156         function set_group( $handle, $recursion, $group = false ) {
157
158                 if ( $this->registered[$handle]->args === 1 )
159                         $grp = 1;
160                 else
161                         $grp = (int) $this->get_data( $handle, 'group' );
162
163                 if ( false !== $group && $grp > $group )
164                         $grp = $group;
165
166                 return parent::set_group( $handle, $recursion, $grp );
167         }
168
169         function all_deps( $handles, $recursion = false, $group = false ) {
170                 $r = parent::all_deps( $handles, $recursion );
171                 if ( !$recursion )
172                         $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
173                 return $r;
174         }
175
176         function do_head_items() {
177                 $this->do_items(false, 0);
178                 return $this->done;
179         }
180
181         function do_footer_items() {
182                 $this->do_items(false, 1);
183                 return $this->done;
184         }
185
186         function in_default_dir($src) {
187                 if ( ! $this->default_dirs )
188                         return true;
189
190                 if ( 0 === strpos( $src, '/wp-includes/js/l10n' ) )
191                         return false;
192
193                 foreach ( (array) $this->default_dirs as $test ) {
194                         if ( 0 === strpos($src, $test) )
195                                 return true;
196                 }
197                 return false;
198         }
199
200         function reset() {
201                 $this->do_concat = false;
202                 $this->print_code = '';
203                 $this->concat = '';
204                 $this->concat_version = '';
205                 $this->print_html = '';
206                 $this->ext_version = '';
207                 $this->ext_handles = '';
208         }
209 }