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