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