]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class.wp-scripts.php
WordPress 4.4.2-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         public $base_url; // Full URL with trailing slash
21         public $content_url;
22         public $default_version;
23         public $in_footer = array();
24         public $concat = '';
25         public $concat_version = '';
26         public $do_concat = false;
27         public $print_html = '';
28         public $print_code = '';
29         public $ext_handles = '';
30         public $ext_version = '';
31         public $default_dirs;
32
33         public function __construct() {
34                 $this->init();
35                 add_action( 'init', array( $this, 'init' ), 0 );
36         }
37
38         /**
39          * @access public
40          */
41         public function init() {
42                 /**
43                  * Fires when the WP_Scripts instance is initialized.
44                  *
45                  * @since 2.6.0
46                  *
47                  * @param WP_Scripts &$this WP_Scripts instance, passed by reference.
48                  */
49                 do_action_ref_array( 'wp_default_scripts', array(&$this) );
50         }
51
52         /**
53          * Prints scripts.
54          *
55          * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies.
56          *
57          * @param mixed $handles Optional. Scripts to be printed. (void) prints queue, (string) prints
58          *                       that script, (array of strings) prints those scripts. Default false.
59          * @param int   $group   Optional. If scripts were queued in groups prints this group number.
60          *                       Default false.
61          * @return array Scripts that have been printed.
62          */
63         public function print_scripts( $handles = false, $group = false ) {
64                 return $this->do_items( $handles, $group );
65         }
66
67         /**
68          * @deprecated 3.3
69          * @see print_extra_script()
70          *
71          * @param string $handle
72          * @param bool   $echo
73          * @return bool|string|void
74          */
75         public function print_scripts_l10n( $handle, $echo = true ) {
76                 _deprecated_function( __FUNCTION__, '3.3', 'print_extra_script()' );
77                 return $this->print_extra_script( $handle, $echo );
78         }
79
80         /**
81          * @param string $handle
82          * @param bool   $echo
83          * @return bool|string|void
84          */
85         public function print_extra_script( $handle, $echo = true ) {
86                 if ( !$output = $this->get_data( $handle, 'data' ) )
87                         return;
88
89                 if ( !$echo )
90                         return $output;
91
92                 echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
93                 echo "/* <![CDATA[ */\n";
94                 echo "$output\n";
95                 echo "/* ]]> */\n";
96                 echo "</script>\n";
97
98                 return true;
99         }
100
101         /**
102          * @param string   $handle Name of the item. Should be unique.
103          * @param int|bool $group
104          * @return bool True on success, false if not set.
105          */
106         public function do_item( $handle, $group = false ) {
107                 if ( !parent::do_item($handle) )
108                         return false;
109
110                 if ( 0 === $group && $this->groups[$handle] > 0 ) {
111                         $this->in_footer[] = $handle;
112                         return false;
113                 }
114
115                 if ( false === $group && in_array($handle, $this->in_footer, true) )
116                         $this->in_footer = array_diff( $this->in_footer, (array) $handle );
117
118                 $obj = $this->registered[$handle];
119
120                 if ( null === $obj->ver ) {
121                         $ver = '';
122                 } else {
123                         $ver = $obj->ver ? $obj->ver : $this->default_version;
124                 }
125
126                 if ( isset($this->args[$handle]) )
127                         $ver = $ver ? $ver . '&amp;' . $this->args[$handle] : $this->args[$handle];
128
129                 $src = $obj->src;
130                 $cond_before = $cond_after = '';
131                 $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : '';
132
133                 if ( $conditional ) {
134                         $cond_before = "<!--[if {$conditional}]>\n";
135                         $cond_after = "<![endif]-->\n";
136                 }
137
138                 if ( $this->do_concat ) {
139                         /**
140                          * Filter the script loader source.
141                          *
142                          * @since 2.2.0
143                          *
144                          * @param string $src    Script loader source path.
145                          * @param string $handle Script handle.
146                          */
147                         $srce = apply_filters( 'script_loader_src', $src, $handle );
148                         if ( $this->in_default_dir( $srce ) && ! $conditional ) {
149                                 $this->print_code .= $this->print_extra_script( $handle, false );
150                                 $this->concat .= "$handle,";
151                                 $this->concat_version .= "$handle$ver";
152                                 return true;
153                         } else {
154                                 $this->ext_handles .= "$handle,";
155                                 $this->ext_version .= "$handle$ver";
156                         }
157                 }
158
159                 $has_conditional_data = $conditional && $this->get_data( $handle, 'data' );
160
161                 if ( $has_conditional_data ) {
162                         echo $cond_before;
163                 }
164
165                 $this->print_extra_script( $handle );
166
167                 if ( $has_conditional_data ) {
168                         echo $cond_after;
169                 }
170
171                 if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) {
172                         $src = $this->base_url . $src;
173                 }
174
175                 if ( ! empty( $ver ) )
176                         $src = add_query_arg( 'ver', $ver, $src );
177
178                 /** This filter is documented in wp-includes/class.wp-scripts.php */
179                 $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
180
181                 if ( ! $src )
182                         return true;
183
184                 $tag = "{$cond_before}<script type='text/javascript' src='$src'></script>\n{$cond_after}";
185
186                 /**
187                  * Filter the HTML script tag of an enqueued script.
188                  *
189                  * @since 4.1.0
190                  *
191                  * @param string $tag    The `<script>` tag for the enqueued script.
192                  * @param string $handle The script's registered handle.
193                  * @param string $src    The script's source URL.
194                  */
195                 $tag = apply_filters( 'script_loader_tag', $tag, $handle, $src );
196
197                 if ( $this->do_concat ) {
198                         $this->print_html .= $tag;
199                 } else {
200                         echo $tag;
201                 }
202
203                 return true;
204         }
205
206         /**
207          * Localizes a script, only if the script has already been added
208          *
209          * @param string $handle
210          * @param string $object_name
211          * @param array $l10n
212          * @return bool
213          */
214         public function localize( $handle, $object_name, $l10n ) {
215                 if ( $handle === 'jquery' )
216                         $handle = 'jquery-core';
217
218                 if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
219                         $after = $l10n['l10n_print_after'];
220                         unset($l10n['l10n_print_after']);
221                 }
222
223                 foreach ( (array) $l10n as $key => $value ) {
224                         if ( !is_scalar($value) )
225                                 continue;
226
227                         $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
228                 }
229
230                 $script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
231
232                 if ( !empty($after) )
233                         $script .= "\n$after;";
234
235                 $data = $this->get_data( $handle, 'data' );
236
237                 if ( !empty( $data ) )
238                         $script = "$data\n$script";
239
240                 return $this->add_data( $handle, 'data', $script );
241         }
242
243         /**
244          * @param string $handle    Name of the item. Should be unique.
245          * @param bool   $recursion Internal flag that calling function was called recursively.
246          * @param mixed  $group     Group level.
247          * @return bool Not already in the group or a lower group
248          */
249         public function set_group( $handle, $recursion, $group = false ) {
250                 if ( isset( $this->registered[$handle]->args ) && $this->registered[$handle]->args === 1 )
251                         $grp = 1;
252                 else
253                         $grp = (int) $this->get_data( $handle, 'group' );
254
255                 if ( false !== $group && $grp > $group )
256                         $grp = $group;
257
258                 return parent::set_group( $handle, $recursion, $grp );
259         }
260
261         /**
262          * @param mixed $handles   Item handle and argument (string) or item handles and arguments (array of strings).
263          * @param bool  $recursion Internal flag that function is calling itself.
264          * @param mixed $group     Group level: (int) level, (false) no groups.
265          * @return bool True on success, false on failure.
266          */
267         public function all_deps( $handles, $recursion = false, $group = false ) {
268                 $r = parent::all_deps( $handles, $recursion );
269                 if ( ! $recursion ) {
270                         /**
271                          * Filter the list of script dependencies left to print.
272                          *
273                          * @since 2.3.0
274                          *
275                          * @param array $to_do An array of script dependencies.
276                          */
277                         $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
278                 }
279                 return $r;
280         }
281
282         /**
283          * @return array
284          */
285         public function do_head_items() {
286                 $this->do_items(false, 0);
287                 return $this->done;
288         }
289
290         /**
291          * @return array
292          */
293         public function do_footer_items() {
294                 $this->do_items(false, 1);
295                 return $this->done;
296         }
297
298         /**
299          * @param string $src
300          * @return bool
301          */
302         public function in_default_dir( $src ) {
303                 if ( ! $this->default_dirs ) {
304                         return true;
305                 }
306
307                 if ( 0 === strpos( $src, '/' . WPINC . '/js/l10n' ) ) {
308                         return false;
309                 }
310
311                 foreach ( (array) $this->default_dirs as $test ) {
312                         if ( 0 === strpos( $src, $test ) ) {
313                                 return true;
314                         }
315                 }
316                 return false;
317         }
318
319         /**
320          * @access public
321          */
322         public function reset() {
323                 $this->do_concat = false;
324                 $this->print_code = '';
325                 $this->concat = '';
326                 $this->concat_version = '';
327                 $this->print_html = '';
328                 $this->ext_version = '';
329                 $this->ext_handles = '';
330         }
331 }