0
|
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 { |
5
|
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; |
0
|
32 |
|
5
|
33 |
public function __construct() { |
0
|
34 |
$this->init(); |
|
35 |
add_action( 'init', array( $this, 'init' ), 0 ); |
|
36 |
} |
|
37 |
|
5
|
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 |
*/ |
0
|
46 |
do_action_ref_array( 'wp_default_scripts', array(&$this) ); |
|
47 |
} |
|
48 |
|
|
49 |
/** |
5
|
50 |
* Prints scripts. |
0
|
51 |
* |
|
52 |
* Prints the scripts passed to it or the print queue. Also prints all necessary dependencies. |
|
53 |
* |
5
|
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. |
0
|
59 |
*/ |
5
|
60 |
public function print_scripts( $handles = false, $group = false ) { |
0
|
61 |
return $this->do_items( $handles, $group ); |
|
62 |
} |
|
63 |
|
|
64 |
// Deprecated since 3.3, see print_extra_script() |
5
|
65 |
public function print_scripts_l10n( $handle, $echo = true ) { |
0
|
66 |
_deprecated_function( __FUNCTION__, '3.3', 'print_extra_script()' ); |
|
67 |
return $this->print_extra_script( $handle, $echo ); |
|
68 |
} |
|
69 |
|
5
|
70 |
public function print_extra_script( $handle, $echo = true ) { |
0
|
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 |
|
5
|
86 |
public function do_item( $handle, $group = false ) { |
0
|
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 |
|
5
|
98 |
$obj = $this->registered[$handle]; |
|
99 |
|
|
100 |
if ( null === $obj->ver ) { |
0
|
101 |
$ver = ''; |
5
|
102 |
} else { |
|
103 |
$ver = $obj->ver ? $obj->ver : $this->default_version; |
|
104 |
} |
0
|
105 |
|
|
106 |
if ( isset($this->args[$handle]) ) |
|
107 |
$ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle]; |
|
108 |
|
5
|
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 |
} |
0
|
117 |
|
|
118 |
if ( $this->do_concat ) { |
5
|
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 |
*/ |
0
|
127 |
$srce = apply_filters( 'script_loader_src', $src, $handle ); |
5
|
128 |
if ( $this->in_default_dir( $srce ) && ! $conditional ) { |
0
|
129 |
$this->print_code .= $this->print_extra_script( $handle, false ); |
|
130 |
$this->concat .= "$handle,"; |
|
131 |
$this->concat_version .= "$handle$ver"; |
|
132 |
return true; |
|
133 |
} else { |
|
134 |
$this->ext_handles .= "$handle,"; |
|
135 |
$this->ext_version .= "$handle$ver"; |
|
136 |
} |
|
137 |
} |
|
138 |
|
5
|
139 |
$has_conditional_data = $conditional && $this->get_data( $handle, 'data' ); |
|
140 |
|
|
141 |
if ( $has_conditional_data ) { |
|
142 |
echo $cond_before; |
|
143 |
} |
|
144 |
|
0
|
145 |
$this->print_extra_script( $handle ); |
5
|
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 ) ) ) { |
0
|
152 |
$src = $this->base_url . $src; |
|
153 |
} |
|
154 |
|
5
|
155 |
if ( ! empty( $ver ) ) |
|
156 |
$src = add_query_arg( 'ver', $ver, $src ); |
0
|
157 |
|
5
|
158 |
/** This filter is documented in wp-includes/class.wp-scripts.php */ |
0
|
159 |
$src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) ); |
|
160 |
|
|
161 |
if ( ! $src ) |
|
162 |
return true; |
|
163 |
|
5
|
164 |
$tag = "{$cond_before}<script type='text/javascript' src='$src'></script>\n{$cond_after}"; |
|
165 |
|
|
166 |
/** |
|
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 |
} |
0
|
182 |
|
|
183 |
return true; |
|
184 |
} |
|
185 |
|
|
186 |
/** |
|
187 |
* Localizes a script |
|
188 |
* |
|
189 |
* Localizes only if the script has already been added |
|
190 |
*/ |
5
|
191 |
public function localize( $handle, $object_name, $l10n ) { |
0
|
192 |
if ( $handle === 'jquery' ) |
|
193 |
$handle = 'jquery-core'; |
|
194 |
|
|
195 |
if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present |
|
196 |
$after = $l10n['l10n_print_after']; |
|
197 |
unset($l10n['l10n_print_after']); |
|
198 |
} |
|
199 |
|
|
200 |
foreach ( (array) $l10n as $key => $value ) { |
|
201 |
if ( !is_scalar($value) ) |
|
202 |
continue; |
|
203 |
|
|
204 |
$l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8'); |
|
205 |
} |
|
206 |
|
5
|
207 |
$script = "var $object_name = " . wp_json_encode( $l10n ) . ';'; |
0
|
208 |
|
|
209 |
if ( !empty($after) ) |
|
210 |
$script .= "\n$after;"; |
|
211 |
|
|
212 |
$data = $this->get_data( $handle, 'data' ); |
|
213 |
|
|
214 |
if ( !empty( $data ) ) |
|
215 |
$script = "$data\n$script"; |
|
216 |
|
|
217 |
return $this->add_data( $handle, 'data', $script ); |
|
218 |
} |
|
219 |
|
5
|
220 |
public function set_group( $handle, $recursion, $group = false ) { |
0
|
221 |
|
|
222 |
if ( $this->registered[$handle]->args === 1 ) |
|
223 |
$grp = 1; |
|
224 |
else |
|
225 |
$grp = (int) $this->get_data( $handle, 'group' ); |
|
226 |
|
|
227 |
if ( false !== $group && $grp > $group ) |
|
228 |
$grp = $group; |
|
229 |
|
|
230 |
return parent::set_group( $handle, $recursion, $grp ); |
|
231 |
} |
|
232 |
|
5
|
233 |
public function all_deps( $handles, $recursion = false, $group = false ) { |
0
|
234 |
$r = parent::all_deps( $handles, $recursion ); |
5
|
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 |
*/ |
0
|
243 |
$this->to_do = apply_filters( 'print_scripts_array', $this->to_do ); |
5
|
244 |
} |
0
|
245 |
return $r; |
|
246 |
} |
|
247 |
|
5
|
248 |
public function do_head_items() { |
0
|
249 |
$this->do_items(false, 0); |
|
250 |
return $this->done; |
|
251 |
} |
|
252 |
|
5
|
253 |
public function do_footer_items() { |
0
|
254 |
$this->do_items(false, 1); |
|
255 |
return $this->done; |
|
256 |
} |
|
257 |
|
5
|
258 |
public function in_default_dir( $src ) { |
|
259 |
if ( ! $this->default_dirs ) { |
0
|
260 |
return true; |
5
|
261 |
} |
0
|
262 |
|
5
|
263 |
if ( 0 === strpos( $src, '/' . WPINC . '/js/l10n' ) ) { |
0
|
264 |
return false; |
5
|
265 |
} |
0
|
266 |
|
|
267 |
foreach ( (array) $this->default_dirs as $test ) { |
5
|
268 |
if ( 0 === strpos( $src, $test ) ) { |
0
|
269 |
return true; |
5
|
270 |
} |
0
|
271 |
} |
|
272 |
return false; |
|
273 |
} |
|
274 |
|
5
|
275 |
public function reset() { |
0
|
276 |
$this->do_concat = false; |
|
277 |
$this->print_code = ''; |
|
278 |
$this->concat = ''; |
|
279 |
$this->concat_version = ''; |
|
280 |
$this->print_html = ''; |
|
281 |
$this->ext_version = ''; |
|
282 |
$this->ext_handles = ''; |
|
283 |
} |
|
284 |
} |