author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 18 | be944660c56a |
child 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Dependencies API: WP_Scripts class |
0 | 4 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5 |
* @since 2.6.0 |
0 | 6 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7 |
* @package WordPress |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
8 |
* @subpackage Dependencies |
0 | 9 |
*/ |
10 |
||
11 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
12 |
* Core class used to register scripts. |
0 | 13 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @since 2.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
16 |
* @see WP_Dependencies |
0 | 17 |
*/ |
18 |
class WP_Scripts extends WP_Dependencies { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
20 |
* Base URL for scripts. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
* Full URL with trailing slash. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
24 |
* @since 2.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
* @var string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
26 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
public $base_url; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
29 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
30 |
* URL of the content directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
33 |
* @var string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
34 |
*/ |
5 | 35 |
public $content_url; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
37 |
/** |
9 | 38 |
* Default version string for scripts. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
* @since 2.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
41 |
* @var string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
42 |
*/ |
5 | 43 |
public $default_version; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
46 |
* Holds handles of scripts which are enqueued in footer. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
47 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
* @var array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
*/ |
5 | 51 |
public $in_footer = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
* Holds a list of script handles which will be concatenated. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
* @var string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
*/ |
5 | 59 |
public $concat = ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
* Holds a string which contains script handles and their version. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
65 |
* @deprecated 3.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
* @var string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
67 |
*/ |
5 | 68 |
public $concat_version = ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
69 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
* Whether to perform concatenation. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
* @var bool |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
*/ |
5 | 76 |
public $do_concat = false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
* Holds HTML markup of scripts and additional data if concatenation |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
* is enabled. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
* @var string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
*/ |
5 | 85 |
public $print_html = ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
* Holds inline code if concatenation is enabled. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
* @var string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
*/ |
5 | 93 |
public $print_code = ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
96 |
* Holds a list of script handles which are not in the default directory |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
97 |
* if concatenation is enabled. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
98 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
99 |
* Unused in core. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
100 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
101 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
102 |
* @var string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
103 |
*/ |
5 | 104 |
public $ext_handles = ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
105 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
107 |
* Holds a string which contains handles and versions of scripts which |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
108 |
* are not in the default directory if concatenation is enabled. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
109 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
* Unused in core. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
111 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
112 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
113 |
* @var string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
114 |
*/ |
5 | 115 |
public $ext_version = ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
116 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
117 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
* List of default directories. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
119 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
120 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
121 |
* @var array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
122 |
*/ |
5 | 123 |
public $default_dirs; |
0 | 124 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
/** |
16 | 126 |
* Holds a string which contains the type attribute for script tag. |
127 |
* |
|
19 | 128 |
* If the active theme does not declare HTML5 support for 'script', |
16 | 129 |
* then it initializes as `type='text/javascript'`. |
130 |
* |
|
131 |
* @since 5.3.0 |
|
132 |
* @var string |
|
133 |
*/ |
|
134 |
private $type_attr = ''; |
|
135 |
||
136 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
* Constructor. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
* @since 2.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
*/ |
5 | 141 |
public function __construct() { |
0 | 142 |
$this->init(); |
143 |
add_action( 'init', array( $this, 'init' ), 0 ); |
|
144 |
} |
|
145 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
146 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
* Initialize the class. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
148 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
* @since 3.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
*/ |
5 | 151 |
public function init() { |
16 | 152 |
if ( |
153 |
function_exists( 'is_admin' ) && ! is_admin() |
|
154 |
&& |
|
155 |
function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'script' ) |
|
156 |
) { |
|
157 |
$this->type_attr = " type='text/javascript'"; |
|
158 |
} |
|
159 |
||
5 | 160 |
/** |
161 |
* Fires when the WP_Scripts instance is initialized. |
|
162 |
* |
|
163 |
* @since 2.6.0 |
|
164 |
* |
|
19 | 165 |
* @param WP_Scripts $wp_scripts WP_Scripts instance (passed by reference). |
5 | 166 |
*/ |
9 | 167 |
do_action_ref_array( 'wp_default_scripts', array( &$this ) ); |
0 | 168 |
} |
169 |
||
170 |
/** |
|
5 | 171 |
* Prints scripts. |
0 | 172 |
* |
173 |
* Prints the scripts passed to it or the print queue. Also prints all necessary dependencies. |
|
174 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
* @since 2.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
176 |
* @since 2.8.0 Added the `$group` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
177 |
* |
16 | 178 |
* @param string|string[]|false $handles Optional. Scripts to be printed: queue (false), |
179 |
* single script (string), or multiple scripts (array of strings). |
|
180 |
* Default false. |
|
181 |
* @param int|false $group Optional. Group level: level (int), no groups (false). |
|
182 |
* Default false. |
|
183 |
* @return string[] Handles of scripts that have been printed. |
|
0 | 184 |
*/ |
5 | 185 |
public function print_scripts( $handles = false, $group = false ) { |
0 | 186 |
return $this->do_items( $handles, $group ); |
187 |
} |
|
188 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
190 |
* Prints extra scripts of a registered script. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
* @since 2.1.0 |
19 | 193 |
* @since 2.8.0 Added the `$display` parameter. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
194 |
* @deprecated 3.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
196 |
* @see print_extra_script() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
197 |
* |
19 | 198 |
* @param string $handle The script's registered handle. |
199 |
* @param bool $display Optional. Whether to print the extra script |
|
200 |
* instead of just returning it. Default true. |
|
201 |
* @return bool|string|void Void if no data exists, extra scripts if `$display` is true, |
|
16 | 202 |
* true otherwise. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
*/ |
19 | 204 |
public function print_scripts_l10n( $handle, $display = true ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
205 |
_deprecated_function( __FUNCTION__, '3.3.0', 'WP_Scripts::print_extra_script()' ); |
19 | 206 |
return $this->print_extra_script( $handle, $display ); |
0 | 207 |
} |
208 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
209 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
210 |
* Prints extra scripts of a registered script. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
211 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
212 |
* @since 3.3.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
213 |
* |
19 | 214 |
* @param string $handle The script's registered handle. |
215 |
* @param bool $display Optional. Whether to print the extra script |
|
216 |
* instead of just returning it. Default true. |
|
217 |
* @return bool|string|void Void if no data exists, extra scripts if `$display` is true, |
|
16 | 218 |
* true otherwise. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
219 |
*/ |
19 | 220 |
public function print_extra_script( $handle, $display = true ) { |
16 | 221 |
$output = $this->get_data( $handle, 'data' ); |
222 |
if ( ! $output ) { |
|
0 | 223 |
return; |
9 | 224 |
} |
0 | 225 |
|
19 | 226 |
if ( ! $display ) { |
0 | 227 |
return $output; |
9 | 228 |
} |
0 | 229 |
|
16 | 230 |
printf( "<script%s id='%s-js-extra'>\n", $this->type_attr, esc_attr( $handle ) ); |
231 |
||
232 |
// CDATA is not needed for HTML 5. |
|
233 |
if ( $this->type_attr ) { |
|
234 |
echo "/* <![CDATA[ */\n"; |
|
235 |
} |
|
236 |
||
0 | 237 |
echo "$output\n"; |
16 | 238 |
|
239 |
if ( $this->type_attr ) { |
|
240 |
echo "/* ]]> */\n"; |
|
241 |
} |
|
242 |
||
0 | 243 |
echo "</script>\n"; |
244 |
||
245 |
return true; |
|
246 |
} |
|
247 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
249 |
* Processes a script dependency. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
250 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
* @since 2.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
* @since 2.8.0 Added the `$group` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
253 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
* @see WP_Dependencies::do_item() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
* |
9 | 256 |
* @param string $handle The script's registered handle. |
16 | 257 |
* @param int|false $group Optional. Group level: level (int), no groups (false). |
258 |
* Default false. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
259 |
* @return bool True on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
260 |
*/ |
5 | 261 |
public function do_item( $handle, $group = false ) { |
9 | 262 |
if ( ! parent::do_item( $handle ) ) { |
0 | 263 |
return false; |
9 | 264 |
} |
0 | 265 |
|
9 | 266 |
if ( 0 === $group && $this->groups[ $handle ] > 0 ) { |
0 | 267 |
$this->in_footer[] = $handle; |
268 |
return false; |
|
269 |
} |
|
270 |
||
9 | 271 |
if ( false === $group && in_array( $handle, $this->in_footer, true ) ) { |
0 | 272 |
$this->in_footer = array_diff( $this->in_footer, (array) $handle ); |
9 | 273 |
} |
0 | 274 |
|
9 | 275 |
$obj = $this->registered[ $handle ]; |
5 | 276 |
|
277 |
if ( null === $obj->ver ) { |
|
0 | 278 |
$ver = ''; |
5 | 279 |
} else { |
280 |
$ver = $obj->ver ? $obj->ver : $this->default_version; |
|
281 |
} |
|
0 | 282 |
|
9 | 283 |
if ( isset( $this->args[ $handle ] ) ) { |
284 |
$ver = $ver ? $ver . '&' . $this->args[ $handle ] : $this->args[ $handle ]; |
|
285 |
} |
|
0 | 286 |
|
9 | 287 |
$src = $obj->src; |
16 | 288 |
$cond_before = ''; |
289 |
$cond_after = ''; |
|
5 | 290 |
$conditional = isset( $obj->extra['conditional'] ) ? $obj->extra['conditional'] : ''; |
291 |
||
292 |
if ( $conditional ) { |
|
293 |
$cond_before = "<!--[if {$conditional}]>\n"; |
|
9 | 294 |
$cond_after = "<![endif]-->\n"; |
5 | 295 |
} |
0 | 296 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
297 |
$before_handle = $this->print_inline_script( $handle, 'before', false ); |
9 | 298 |
$after_handle = $this->print_inline_script( $handle, 'after', false ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
299 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
300 |
if ( $before_handle ) { |
16 | 301 |
$before_handle = sprintf( "<script%s id='%s-js-before'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $before_handle ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
302 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
304 |
if ( $after_handle ) { |
16 | 305 |
$after_handle = sprintf( "<script%s id='%s-js-after'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $after_handle ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
|
9 | 308 |
if ( $before_handle || $after_handle ) { |
16 | 309 |
$inline_script_tag = $cond_before . $before_handle . $after_handle . $cond_after; |
9 | 310 |
} else { |
311 |
$inline_script_tag = ''; |
|
312 |
} |
|
313 |
||
19 | 314 |
/* |
315 |
* Prevent concatenation of scripts if the text domain is defined |
|
316 |
* to ensure the dependency order is respected. |
|
317 |
*/ |
|
318 |
$translations_stop_concat = ! empty( $obj->textdomain ); |
|
319 |
||
16 | 320 |
$translations = $this->print_translations( $handle, false ); |
321 |
if ( $translations ) { |
|
322 |
$translations = sprintf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $translations ); |
|
323 |
} |
|
324 |
||
0 | 325 |
if ( $this->do_concat ) { |
5 | 326 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
* Filters the script loader source. |
5 | 328 |
* |
329 |
* @since 2.2.0 |
|
330 |
* |
|
331 |
* @param string $src Script loader source path. |
|
332 |
* @param string $handle Script handle. |
|
333 |
*/ |
|
0 | 334 |
$srce = apply_filters( 'script_loader_src', $src, $handle ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
335 |
|
19 | 336 |
if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations_stop_concat ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
337 |
$this->do_concat = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
338 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
// Have to print the so-far concatenated scripts right away to maintain the right order. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
340 |
_print_scripts(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
341 |
$this->reset(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
} elseif ( $this->in_default_dir( $srce ) && ! $conditional ) { |
9 | 343 |
$this->print_code .= $this->print_extra_script( $handle, false ); |
344 |
$this->concat .= "$handle,"; |
|
0 | 345 |
$this->concat_version .= "$handle$ver"; |
346 |
return true; |
|
347 |
} else { |
|
348 |
$this->ext_handles .= "$handle,"; |
|
349 |
$this->ext_version .= "$handle$ver"; |
|
350 |
} |
|
351 |
} |
|
352 |
||
5 | 353 |
$has_conditional_data = $conditional && $this->get_data( $handle, 'data' ); |
354 |
||
355 |
if ( $has_conditional_data ) { |
|
356 |
echo $cond_before; |
|
357 |
} |
|
358 |
||
0 | 359 |
$this->print_extra_script( $handle ); |
5 | 360 |
|
361 |
if ( $has_conditional_data ) { |
|
362 |
echo $cond_after; |
|
363 |
} |
|
364 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
365 |
// A single item may alias a set of items, by having dependencies, but no source. |
9 | 366 |
if ( ! $src ) { |
367 |
if ( $inline_script_tag ) { |
|
368 |
if ( $this->do_concat ) { |
|
369 |
$this->print_html .= $inline_script_tag; |
|
370 |
} else { |
|
371 |
echo $inline_script_tag; |
|
372 |
} |
|
373 |
} |
|
374 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
375 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
376 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
|
5 | 378 |
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) { |
0 | 379 |
$src = $this->base_url . $src; |
380 |
} |
|
381 |
||
9 | 382 |
if ( ! empty( $ver ) ) { |
5 | 383 |
$src = add_query_arg( 'ver', $ver, $src ); |
9 | 384 |
} |
0 | 385 |
|
5 | 386 |
/** This filter is documented in wp-includes/class.wp-scripts.php */ |
0 | 387 |
$src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) ); |
388 |
||
9 | 389 |
if ( ! $src ) { |
0 | 390 |
return true; |
9 | 391 |
} |
0 | 392 |
|
16 | 393 |
$tag = $translations . $cond_before . $before_handle; |
394 |
$tag .= sprintf( "<script%s src='%s' id='%s-js'></script>\n", $this->type_attr, $src, esc_attr( $handle ) ); |
|
395 |
$tag .= $after_handle . $cond_after; |
|
5 | 396 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
397 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
398 |
* Filters the HTML script tag of an enqueued script. |
5 | 399 |
* |
400 |
* @since 4.1.0 |
|
401 |
* |
|
402 |
* @param string $tag The `<script>` tag for the enqueued script. |
|
403 |
* @param string $handle The script's registered handle. |
|
404 |
* @param string $src The script's source URL. |
|
405 |
*/ |
|
406 |
$tag = apply_filters( 'script_loader_tag', $tag, $handle, $src ); |
|
407 |
||
408 |
if ( $this->do_concat ) { |
|
409 |
$this->print_html .= $tag; |
|
410 |
} else { |
|
411 |
echo $tag; |
|
412 |
} |
|
0 | 413 |
|
414 |
return true; |
|
415 |
} |
|
416 |
||
417 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
418 |
* Adds extra code to a registered script. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
420 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
421 |
* |
16 | 422 |
* @param string $handle Name of the script to add the inline script to. |
423 |
* Must be lowercase. |
|
18 | 424 |
* @param string $data String containing the JavaScript to be added. |
16 | 425 |
* @param string $position Optional. Whether to add the inline script |
426 |
* before the handle or after. Default 'after'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
427 |
* @return bool True on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
428 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
429 |
public function add_inline_script( $handle, $data, $position = 'after' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
430 |
if ( ! $data ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
431 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
432 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
433 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
434 |
if ( 'after' !== $position ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
435 |
$position = 'before'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
436 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
437 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
438 |
$script = (array) $this->get_data( $handle, $position ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
439 |
$script[] = $data; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
440 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
441 |
return $this->add_data( $handle, $position, $script ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
442 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
443 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
444 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
445 |
* Prints inline scripts registered for a specific handle. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
446 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
447 |
* @since 4.5.0 |
0 | 448 |
* |
16 | 449 |
* @param string $handle Name of the script to add the inline script to. |
450 |
* Must be lowercase. |
|
451 |
* @param string $position Optional. Whether to add the inline script |
|
452 |
* before the handle or after. Default 'after'. |
|
19 | 453 |
* @param bool $display Optional. Whether to print the script |
16 | 454 |
* instead of just returning it. Default true. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
455 |
* @return string|false Script on success, false otherwise. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
456 |
*/ |
19 | 457 |
public function print_inline_script( $handle, $position = 'after', $display = true ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
458 |
$output = $this->get_data( $handle, $position ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
459 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
460 |
if ( empty( $output ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
461 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
462 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
463 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
464 |
$output = trim( implode( "\n", $output ), "\n" ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
465 |
|
19 | 466 |
if ( $display ) { |
16 | 467 |
printf( "<script%s id='%s-js-%s'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), esc_attr( $position ), $output ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
468 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
469 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
470 |
return $output; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
471 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
472 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
473 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
474 |
* Localizes a script, only if the script has already been added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
475 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
476 |
* @since 2.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
477 |
* |
9 | 478 |
* @param string $handle Name of the script to attach data to. |
479 |
* @param string $object_name Name of the variable that will contain the data. |
|
480 |
* @param array $l10n Array of data to localize. |
|
481 |
* @return bool True on success, false on failure. |
|
0 | 482 |
*/ |
5 | 483 |
public function localize( $handle, $object_name, $l10n ) { |
16 | 484 |
if ( 'jquery' === $handle ) { |
0 | 485 |
$handle = 'jquery-core'; |
9 | 486 |
} |
0 | 487 |
|
9 | 488 |
if ( is_array( $l10n ) && isset( $l10n['l10n_print_after'] ) ) { // back compat, preserve the code in 'l10n_print_after' if present. |
0 | 489 |
$after = $l10n['l10n_print_after']; |
9 | 490 |
unset( $l10n['l10n_print_after'] ); |
0 | 491 |
} |
492 |
||
18 | 493 |
if ( ! is_array( $l10n ) ) { |
494 |
_doing_it_wrong( |
|
495 |
__METHOD__, |
|
496 |
sprintf( |
|
497 |
/* translators: 1: $l10n, 2: wp_add_inline_script() */ |
|
498 |
__( 'The %1$s parameter must be an array. To pass arbitrary data to scripts, use the %2$s function instead.' ), |
|
499 |
'<code>$l10n</code>', |
|
500 |
'<code>wp_add_inline_script()</code>' |
|
501 |
), |
|
502 |
'5.7.0' |
|
503 |
); |
|
504 |
} |
|
505 |
||
506 |
if ( is_string( $l10n ) ) { |
|
507 |
$l10n = html_entity_decode( $l10n, ENT_QUOTES, 'UTF-8' ); |
|
508 |
} else { |
|
509 |
foreach ( (array) $l10n as $key => $value ) { |
|
510 |
if ( ! is_scalar( $value ) ) { |
|
511 |
continue; |
|
512 |
} |
|
513 |
||
514 |
$l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); |
|
9 | 515 |
} |
0 | 516 |
} |
517 |
||
5 | 518 |
$script = "var $object_name = " . wp_json_encode( $l10n ) . ';'; |
0 | 519 |
|
9 | 520 |
if ( ! empty( $after ) ) { |
0 | 521 |
$script .= "\n$after;"; |
9 | 522 |
} |
0 | 523 |
|
524 |
$data = $this->get_data( $handle, 'data' ); |
|
525 |
||
9 | 526 |
if ( ! empty( $data ) ) { |
0 | 527 |
$script = "$data\n$script"; |
9 | 528 |
} |
0 | 529 |
|
530 |
return $this->add_data( $handle, 'data', $script ); |
|
531 |
} |
|
532 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
533 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
534 |
* Sets handle group. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
535 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
536 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
537 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
538 |
* @see WP_Dependencies::set_group() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
539 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
540 |
* @param string $handle Name of the item. Should be unique. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
541 |
* @param bool $recursion Internal flag that calling function was called recursively. |
16 | 542 |
* @param int|false $group Optional. Group level: level (int), no groups (false). |
543 |
* Default false. |
|
544 |
* @return bool Not already in the group or a lower group. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
545 |
*/ |
5 | 546 |
public function set_group( $handle, $recursion, $group = false ) { |
16 | 547 |
if ( isset( $this->registered[ $handle ]->args ) && 1 === $this->registered[ $handle ]->args ) { |
0 | 548 |
$grp = 1; |
9 | 549 |
} else { |
0 | 550 |
$grp = (int) $this->get_data( $handle, 'group' ); |
9 | 551 |
} |
0 | 552 |
|
9 | 553 |
if ( false !== $group && $grp > $group ) { |
0 | 554 |
$grp = $group; |
9 | 555 |
} |
0 | 556 |
|
557 |
return parent::set_group( $handle, $recursion, $grp ); |
|
558 |
} |
|
559 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
560 |
/** |
9 | 561 |
* Sets a translation textdomain. |
562 |
* |
|
563 |
* @since 5.0.0 |
|
564 |
* @since 5.1.0 The `$domain` parameter was made optional. |
|
565 |
* |
|
566 |
* @param string $handle Name of the script to register a translation domain to. |
|
567 |
* @param string $domain Optional. Text domain. Default 'default'. |
|
568 |
* @param string $path Optional. The full file path to the directory containing translation files. |
|
569 |
* @return bool True if the text domain was registered, false if not. |
|
570 |
*/ |
|
571 |
public function set_translations( $handle, $domain = 'default', $path = null ) { |
|
572 |
if ( ! isset( $this->registered[ $handle ] ) ) { |
|
573 |
return false; |
|
574 |
} |
|
575 |
||
576 |
/** @var \_WP_Dependency $obj */ |
|
577 |
$obj = $this->registered[ $handle ]; |
|
578 |
||
579 |
if ( ! in_array( 'wp-i18n', $obj->deps, true ) ) { |
|
580 |
$obj->deps[] = 'wp-i18n'; |
|
581 |
} |
|
582 |
||
583 |
return $obj->set_translations( $domain, $path ); |
|
584 |
} |
|
585 |
||
586 |
/** |
|
587 |
* Prints translations set for a specific handle. |
|
588 |
* |
|
589 |
* @since 5.0.0 |
|
590 |
* |
|
19 | 591 |
* @param string $handle Name of the script to add the inline script to. |
592 |
* Must be lowercase. |
|
593 |
* @param bool $display Optional. Whether to print the script |
|
594 |
* instead of just returning it. Default true. |
|
9 | 595 |
* @return string|false Script on success, false otherwise. |
596 |
*/ |
|
19 | 597 |
public function print_translations( $handle, $display = true ) { |
9 | 598 |
if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) { |
599 |
return false; |
|
600 |
} |
|
601 |
||
602 |
$domain = $this->registered[ $handle ]->textdomain; |
|
603 |
$path = $this->registered[ $handle ]->translations_path; |
|
604 |
||
605 |
$json_translations = load_script_textdomain( $handle, $domain, $path ); |
|
606 |
||
607 |
if ( ! $json_translations ) { |
|
19 | 608 |
return false; |
9 | 609 |
} |
610 |
||
611 |
$output = <<<JS |
|
612 |
( function( domain, translations ) { |
|
613 |
var localeData = translations.locale_data[ domain ] || translations.locale_data.messages; |
|
614 |
localeData[""].domain = domain; |
|
615 |
wp.i18n.setLocaleData( localeData, domain ); |
|
616 |
} )( "{$domain}", {$json_translations} ); |
|
617 |
JS; |
|
618 |
||
19 | 619 |
if ( $display ) { |
16 | 620 |
printf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $output ); |
9 | 621 |
} |
622 |
||
623 |
return $output; |
|
624 |
} |
|
625 |
||
626 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
627 |
* Determines script dependencies. |
9 | 628 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
629 |
* @since 2.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
630 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
631 |
* @see WP_Dependencies::all_deps() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
632 |
* |
16 | 633 |
* @param string|string[] $handles Item handle (string) or item handles (array of strings). |
634 |
* @param bool $recursion Optional. Internal flag that function is calling itself. |
|
635 |
* Default false. |
|
636 |
* @param int|false $group Optional. Group level: level (int), no groups (false). |
|
637 |
* Default false. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
638 |
* @return bool True on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
639 |
*/ |
5 | 640 |
public function all_deps( $handles, $recursion = false, $group = false ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
641 |
$r = parent::all_deps( $handles, $recursion, $group ); |
5 | 642 |
if ( ! $recursion ) { |
643 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
644 |
* Filters the list of script dependencies left to print. |
5 | 645 |
* |
646 |
* @since 2.3.0 |
|
647 |
* |
|
9 | 648 |
* @param string[] $to_do An array of script dependency handles. |
5 | 649 |
*/ |
0 | 650 |
$this->to_do = apply_filters( 'print_scripts_array', $this->to_do ); |
5 | 651 |
} |
0 | 652 |
return $r; |
653 |
} |
|
654 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
655 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
656 |
* Processes items and dependencies for the head group. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
657 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
658 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
659 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
660 |
* @see WP_Dependencies::do_items() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
661 |
* |
16 | 662 |
* @return string[] Handles of items that have been processed. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
663 |
*/ |
5 | 664 |
public function do_head_items() { |
9 | 665 |
$this->do_items( false, 0 ); |
0 | 666 |
return $this->done; |
667 |
} |
|
668 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
669 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
670 |
* Processes items and dependencies for the footer group. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
671 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
672 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
673 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
674 |
* @see WP_Dependencies::do_items() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
675 |
* |
16 | 676 |
* @return string[] Handles of items that have been processed. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
677 |
*/ |
5 | 678 |
public function do_footer_items() { |
9 | 679 |
$this->do_items( false, 1 ); |
0 | 680 |
return $this->done; |
681 |
} |
|
682 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
683 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
684 |
* Whether a handle's source is in a default directory. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
685 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
686 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
687 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
688 |
* @param string $src The source of the enqueued script. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
689 |
* @return bool True if found, false if not. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
690 |
*/ |
5 | 691 |
public function in_default_dir( $src ) { |
692 |
if ( ! $this->default_dirs ) { |
|
0 | 693 |
return true; |
5 | 694 |
} |
0 | 695 |
|
5 | 696 |
if ( 0 === strpos( $src, '/' . WPINC . '/js/l10n' ) ) { |
0 | 697 |
return false; |
5 | 698 |
} |
0 | 699 |
|
700 |
foreach ( (array) $this->default_dirs as $test ) { |
|
5 | 701 |
if ( 0 === strpos( $src, $test ) ) { |
0 | 702 |
return true; |
5 | 703 |
} |
0 | 704 |
} |
705 |
return false; |
|
706 |
} |
|
707 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
708 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
709 |
* Resets class properties. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
710 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
711 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
712 |
*/ |
5 | 713 |
public function reset() { |
9 | 714 |
$this->do_concat = false; |
715 |
$this->print_code = ''; |
|
716 |
$this->concat = ''; |
|
0 | 717 |
$this->concat_version = ''; |
9 | 718 |
$this->print_html = ''; |
719 |
$this->ext_version = ''; |
|
720 |
$this->ext_handles = ''; |
|
0 | 721 |
} |
722 |
} |