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