15 * @package BackPress |
15 * @package BackPress |
16 * @uses WP_Dependencies |
16 * @uses WP_Dependencies |
17 * @since r16 |
17 * @since r16 |
18 */ |
18 */ |
19 class WP_Scripts extends WP_Dependencies { |
19 class WP_Scripts extends WP_Dependencies { |
20 var $base_url; // Full URL with trailing slash |
20 public $base_url; // Full URL with trailing slash |
21 var $content_url; |
21 public $content_url; |
22 var $default_version; |
22 public $default_version; |
23 var $in_footer = array(); |
23 public $in_footer = array(); |
24 var $concat = ''; |
24 public $concat = ''; |
25 var $concat_version = ''; |
25 public $concat_version = ''; |
26 var $do_concat = false; |
26 public $do_concat = false; |
27 var $print_html = ''; |
27 public $print_html = ''; |
28 var $print_code = ''; |
28 public $print_code = ''; |
29 var $ext_handles = ''; |
29 public $ext_handles = ''; |
30 var $ext_version = ''; |
30 public $ext_version = ''; |
31 var $default_dirs; |
31 public $default_dirs; |
32 |
32 |
33 function __construct() { |
33 public function __construct() { |
34 $this->init(); |
34 $this->init(); |
35 add_action( 'init', array( $this, 'init' ), 0 ); |
35 add_action( 'init', array( $this, 'init' ), 0 ); |
36 } |
36 } |
37 |
37 |
38 function init() { |
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 */ |
39 do_action_ref_array( 'wp_default_scripts', array(&$this) ); |
46 do_action_ref_array( 'wp_default_scripts', array(&$this) ); |
40 } |
47 } |
41 |
48 |
42 /** |
49 /** |
43 * Prints scripts |
50 * Prints scripts. |
44 * |
51 * |
45 * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies. |
52 * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies. |
46 * |
53 * |
47 * @param mixed $handles (optional) Scripts to be printed. (void) prints queue, (string) prints that script, (array of strings) prints those scripts. |
54 * @param mixed $handles Optional. Scripts to be printed. (void) prints queue, (string) prints |
48 * @param int $group (optional) If scripts were queued in groups prints this group number. |
55 * that script, (array of strings) prints those scripts. Default false. |
49 * @return array Scripts that have been printed |
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. |
50 */ |
59 */ |
51 function print_scripts( $handles = false, $group = false ) { |
60 public function print_scripts( $handles = false, $group = false ) { |
52 return $this->do_items( $handles, $group ); |
61 return $this->do_items( $handles, $group ); |
53 } |
62 } |
54 |
63 |
55 // Deprecated since 3.3, see print_extra_script() |
64 // Deprecated since 3.3, see print_extra_script() |
56 function print_scripts_l10n( $handle, $echo = true ) { |
65 public function print_scripts_l10n( $handle, $echo = true ) { |
57 _deprecated_function( __FUNCTION__, '3.3', 'print_extra_script()' ); |
66 _deprecated_function( __FUNCTION__, '3.3', 'print_extra_script()' ); |
58 return $this->print_extra_script( $handle, $echo ); |
67 return $this->print_extra_script( $handle, $echo ); |
59 } |
68 } |
60 |
69 |
61 function print_extra_script( $handle, $echo = true ) { |
70 public function print_extra_script( $handle, $echo = true ) { |
62 if ( !$output = $this->get_data( $handle, 'data' ) ) |
71 if ( !$output = $this->get_data( $handle, 'data' ) ) |
63 return; |
72 return; |
64 |
73 |
65 if ( !$echo ) |
74 if ( !$echo ) |
66 return $output; |
75 return $output; |
84 } |
93 } |
85 |
94 |
86 if ( false === $group && in_array($handle, $this->in_footer, true) ) |
95 if ( false === $group && in_array($handle, $this->in_footer, true) ) |
87 $this->in_footer = array_diff( $this->in_footer, (array) $handle ); |
96 $this->in_footer = array_diff( $this->in_footer, (array) $handle ); |
88 |
97 |
89 if ( null === $this->registered[$handle]->ver ) |
98 $obj = $this->registered[$handle]; |
|
99 |
|
100 if ( null === $obj->ver ) { |
90 $ver = ''; |
101 $ver = ''; |
91 else |
102 } else { |
92 $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version; |
103 $ver = $obj->ver ? $obj->ver : $this->default_version; |
|
104 } |
93 |
105 |
94 if ( isset($this->args[$handle]) ) |
106 if ( isset($this->args[$handle]) ) |
95 $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle]; |
107 $ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle]; |
96 |
108 |
97 $src = $this->registered[$handle]->src; |
109 $src = $obj->src; |
|
110 $cond_before = $cond_after = ''; |
|
111 $conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : ''; |
|
112 |
|
113 if ( $conditional ) { |
|
114 $cond_before = "<!--[if {$conditional}]>\n"; |
|
115 $cond_after = "<![endif]-->\n"; |
|
116 } |
98 |
117 |
99 if ( $this->do_concat ) { |
118 if ( $this->do_concat ) { |
|
119 /** |
|
120 * Filter the script loader source. |
|
121 * |
|
122 * @since 2.2.0 |
|
123 * |
|
124 * @param string $src Script loader source path. |
|
125 * @param string $handle Script handle. |
|
126 */ |
100 $srce = apply_filters( 'script_loader_src', $src, $handle ); |
127 $srce = apply_filters( 'script_loader_src', $src, $handle ); |
101 if ( $this->in_default_dir($srce) ) { |
128 if ( $this->in_default_dir( $srce ) && ! $conditional ) { |
102 $this->print_code .= $this->print_extra_script( $handle, false ); |
129 $this->print_code .= $this->print_extra_script( $handle, false ); |
103 $this->concat .= "$handle,"; |
130 $this->concat .= "$handle,"; |
104 $this->concat_version .= "$handle$ver"; |
131 $this->concat_version .= "$handle$ver"; |
105 return true; |
132 return true; |
106 } else { |
133 } else { |
107 $this->ext_handles .= "$handle,"; |
134 $this->ext_handles .= "$handle,"; |
108 $this->ext_version .= "$handle$ver"; |
135 $this->ext_version .= "$handle$ver"; |
109 } |
136 } |
110 } |
137 } |
111 |
138 |
|
139 $has_conditional_data = $conditional && $this->get_data( $handle, 'data' ); |
|
140 |
|
141 if ( $has_conditional_data ) { |
|
142 echo $cond_before; |
|
143 } |
|
144 |
112 $this->print_extra_script( $handle ); |
145 $this->print_extra_script( $handle ); |
113 if ( !preg_match('|^(https?:)?//|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) { |
146 |
|
147 if ( $has_conditional_data ) { |
|
148 echo $cond_after; |
|
149 } |
|
150 |
|
151 if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) { |
114 $src = $this->base_url . $src; |
152 $src = $this->base_url . $src; |
115 } |
153 } |
116 |
154 |
117 if ( !empty($ver) ) |
155 if ( ! empty( $ver ) ) |
118 $src = add_query_arg('ver', $ver, $src); |
156 $src = add_query_arg( 'ver', $ver, $src ); |
119 |
157 |
|
158 /** This filter is documented in wp-includes/class.wp-scripts.php */ |
120 $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) ); |
159 $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) ); |
121 |
160 |
122 if ( ! $src ) |
161 if ( ! $src ) |
123 return true; |
162 return true; |
124 |
163 |
125 if ( $this->do_concat ) |
164 $tag = "{$cond_before}<script type='text/javascript' src='$src'></script>\n{$cond_after}"; |
126 $this->print_html .= "<script type='text/javascript' src='$src'></script>\n"; |
165 |
127 else |
166 /** |
128 echo "<script type='text/javascript' src='$src'></script>\n"; |
167 * Filter the HTML script tag of an enqueued script. |
|
168 * |
|
169 * @since 4.1.0 |
|
170 * |
|
171 * @param string $tag The `<script>` tag for the enqueued script. |
|
172 * @param string $handle The script's registered handle. |
|
173 * @param string $src The script's source URL. |
|
174 */ |
|
175 $tag = apply_filters( 'script_loader_tag', $tag, $handle, $src ); |
|
176 |
|
177 if ( $this->do_concat ) { |
|
178 $this->print_html .= $tag; |
|
179 } else { |
|
180 echo $tag; |
|
181 } |
129 |
182 |
130 return true; |
183 return true; |
131 } |
184 } |
132 |
185 |
133 /** |
186 /** |
134 * Localizes a script |
187 * Localizes a script |
135 * |
188 * |
136 * Localizes only if the script has already been added |
189 * Localizes only if the script has already been added |
137 */ |
190 */ |
138 function localize( $handle, $object_name, $l10n ) { |
191 public function localize( $handle, $object_name, $l10n ) { |
139 if ( $handle === 'jquery' ) |
192 if ( $handle === 'jquery' ) |
140 $handle = 'jquery-core'; |
193 $handle = 'jquery-core'; |
141 |
194 |
142 if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present |
195 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']; |
196 $after = $l10n['l10n_print_after']; |
175 $grp = $group; |
228 $grp = $group; |
176 |
229 |
177 return parent::set_group( $handle, $recursion, $grp ); |
230 return parent::set_group( $handle, $recursion, $grp ); |
178 } |
231 } |
179 |
232 |
180 function all_deps( $handles, $recursion = false, $group = false ) { |
233 public function all_deps( $handles, $recursion = false, $group = false ) { |
181 $r = parent::all_deps( $handles, $recursion ); |
234 $r = parent::all_deps( $handles, $recursion ); |
182 if ( !$recursion ) |
235 if ( ! $recursion ) { |
|
236 /** |
|
237 * Filter the list of script dependencies left to print. |
|
238 * |
|
239 * @since 2.3.0 |
|
240 * |
|
241 * @param array $to_do An array of script dependencies. |
|
242 */ |
183 $this->to_do = apply_filters( 'print_scripts_array', $this->to_do ); |
243 $this->to_do = apply_filters( 'print_scripts_array', $this->to_do ); |
|
244 } |
184 return $r; |
245 return $r; |
185 } |
246 } |
186 |
247 |
187 function do_head_items() { |
248 public function do_head_items() { |
188 $this->do_items(false, 0); |
249 $this->do_items(false, 0); |
189 return $this->done; |
250 return $this->done; |
190 } |
251 } |
191 |
252 |
192 function do_footer_items() { |
253 public function do_footer_items() { |
193 $this->do_items(false, 1); |
254 $this->do_items(false, 1); |
194 return $this->done; |
255 return $this->done; |
195 } |
256 } |
196 |
257 |
197 function in_default_dir($src) { |
258 public function in_default_dir( $src ) { |
198 if ( ! $this->default_dirs ) |
259 if ( ! $this->default_dirs ) { |
199 return true; |
260 return true; |
200 |
261 } |
201 if ( 0 === strpos( $src, '/wp-includes/js/l10n' ) ) |
262 |
|
263 if ( 0 === strpos( $src, '/' . WPINC . '/js/l10n' ) ) { |
202 return false; |
264 return false; |
|
265 } |
203 |
266 |
204 foreach ( (array) $this->default_dirs as $test ) { |
267 foreach ( (array) $this->default_dirs as $test ) { |
205 if ( 0 === strpos($src, $test) ) |
268 if ( 0 === strpos( $src, $test ) ) { |
206 return true; |
269 return true; |
|
270 } |
207 } |
271 } |
208 return false; |
272 return false; |
209 } |
273 } |
210 |
274 |
211 function reset() { |
275 public function reset() { |
212 $this->do_concat = false; |
276 $this->do_concat = false; |
213 $this->print_code = ''; |
277 $this->print_code = ''; |
214 $this->concat = ''; |
278 $this->concat = ''; |
215 $this->concat_version = ''; |
279 $this->concat_version = ''; |
216 $this->print_html = ''; |
280 $this->print_html = ''; |