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