author | ymh <ymh.work@gmail.com> |
Wed, 21 Sep 2022 18:19:35 +0200 | |
changeset 18 | be944660c56a |
parent 16 | a86126ab1dd4 |
child 19 | 3d72ae0968f4 |
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 |
* |
|
128 |
* If the current theme does not declare HTML5 support for 'script', |
|
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 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
* @param WP_Scripts $this 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 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
* @since 2.8.0 Added the `$echo` parameter. |
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
198 |
* @param string $handle The script's registered handle. |
16 | 199 |
* @param bool $echo Optional. Whether to echo the extra script |
200 |
* instead of just returning it. Default true. |
|
201 |
* @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, |
|
202 |
* true otherwise. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
*/ |
5 | 204 |
public function print_scripts_l10n( $handle, $echo = 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()' ); |
0 | 206 |
return $this->print_extra_script( $handle, $echo ); |
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 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
214 |
* @param string $handle The script's registered handle. |
16 | 215 |
* @param bool $echo Optional. Whether to echo the extra script |
216 |
* instead of just returning it. Default true. |
|
217 |
* @return bool|string|void Void if no data exists, extra scripts if `$echo` is true, |
|
218 |
* true otherwise. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
219 |
*/ |
5 | 220 |
public function print_extra_script( $handle, $echo = true ) { |
16 | 221 |
$output = $this->get_data( $handle, 'data' ); |
222 |
if ( ! $output ) { |
|
0 | 223 |
return; |
9 | 224 |
} |
0 | 225 |
|
9 | 226 |
if ( ! $echo ) { |
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 |
||
16 | 314 |
$translations = $this->print_translations( $handle, false ); |
315 |
if ( $translations ) { |
|
316 |
$translations = sprintf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $translations ); |
|
317 |
} |
|
318 |
||
0 | 319 |
if ( $this->do_concat ) { |
5 | 320 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
* Filters the script loader source. |
5 | 322 |
* |
323 |
* @since 2.2.0 |
|
324 |
* |
|
325 |
* @param string $src Script loader source path. |
|
326 |
* @param string $handle Script handle. |
|
327 |
*/ |
|
0 | 328 |
$srce = apply_filters( 'script_loader_src', $src, $handle ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
329 |
|
16 | 330 |
if ( $this->in_default_dir( $srce ) && ( $before_handle || $after_handle || $translations ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
331 |
$this->do_concat = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
332 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
// 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
|
334 |
_print_scripts(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
335 |
$this->reset(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
336 |
} elseif ( $this->in_default_dir( $srce ) && ! $conditional ) { |
9 | 337 |
$this->print_code .= $this->print_extra_script( $handle, false ); |
338 |
$this->concat .= "$handle,"; |
|
0 | 339 |
$this->concat_version .= "$handle$ver"; |
340 |
return true; |
|
341 |
} else { |
|
342 |
$this->ext_handles .= "$handle,"; |
|
343 |
$this->ext_version .= "$handle$ver"; |
|
344 |
} |
|
345 |
} |
|
346 |
||
5 | 347 |
$has_conditional_data = $conditional && $this->get_data( $handle, 'data' ); |
348 |
||
349 |
if ( $has_conditional_data ) { |
|
350 |
echo $cond_before; |
|
351 |
} |
|
352 |
||
0 | 353 |
$this->print_extra_script( $handle ); |
5 | 354 |
|
355 |
if ( $has_conditional_data ) { |
|
356 |
echo $cond_after; |
|
357 |
} |
|
358 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
// A single item may alias a set of items, by having dependencies, but no source. |
9 | 360 |
if ( ! $src ) { |
361 |
if ( $inline_script_tag ) { |
|
362 |
if ( $this->do_concat ) { |
|
363 |
$this->print_html .= $inline_script_tag; |
|
364 |
} else { |
|
365 |
echo $inline_script_tag; |
|
366 |
} |
|
367 |
} |
|
368 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
return true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
370 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
371 |
|
5 | 372 |
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $this->content_url && 0 === strpos( $src, $this->content_url ) ) ) { |
0 | 373 |
$src = $this->base_url . $src; |
374 |
} |
|
375 |
||
9 | 376 |
if ( ! empty( $ver ) ) { |
5 | 377 |
$src = add_query_arg( 'ver', $ver, $src ); |
9 | 378 |
} |
0 | 379 |
|
5 | 380 |
/** This filter is documented in wp-includes/class.wp-scripts.php */ |
0 | 381 |
$src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) ); |
382 |
||
9 | 383 |
if ( ! $src ) { |
0 | 384 |
return true; |
9 | 385 |
} |
0 | 386 |
|
16 | 387 |
$tag = $translations . $cond_before . $before_handle; |
388 |
$tag .= sprintf( "<script%s src='%s' id='%s-js'></script>\n", $this->type_attr, $src, esc_attr( $handle ) ); |
|
389 |
$tag .= $after_handle . $cond_after; |
|
5 | 390 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
* Filters the HTML script tag of an enqueued script. |
5 | 393 |
* |
394 |
* @since 4.1.0 |
|
395 |
* |
|
396 |
* @param string $tag The `<script>` tag for the enqueued script. |
|
397 |
* @param string $handle The script's registered handle. |
|
398 |
* @param string $src The script's source URL. |
|
399 |
*/ |
|
400 |
$tag = apply_filters( 'script_loader_tag', $tag, $handle, $src ); |
|
401 |
||
402 |
if ( $this->do_concat ) { |
|
403 |
$this->print_html .= $tag; |
|
404 |
} else { |
|
405 |
echo $tag; |
|
406 |
} |
|
0 | 407 |
|
408 |
return true; |
|
409 |
} |
|
410 |
||
411 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
* Adds extra code to a registered script. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
414 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
* |
16 | 416 |
* @param string $handle Name of the script to add the inline script to. |
417 |
* Must be lowercase. |
|
18 | 418 |
* @param string $data String containing the JavaScript to be added. |
16 | 419 |
* @param string $position Optional. Whether to add the inline script |
420 |
* before the handle or after. Default 'after'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
421 |
* @return bool True on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
422 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
423 |
public function add_inline_script( $handle, $data, $position = 'after' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
424 |
if ( ! $data ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
425 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
426 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
427 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
428 |
if ( 'after' !== $position ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
429 |
$position = 'before'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
430 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
431 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
432 |
$script = (array) $this->get_data( $handle, $position ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
433 |
$script[] = $data; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
434 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
435 |
return $this->add_data( $handle, $position, $script ); |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
439 |
* Prints inline scripts registered for a specific handle. |
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 |
* @since 4.5.0 |
0 | 442 |
* |
16 | 443 |
* @param string $handle Name of the script to add the inline script to. |
444 |
* Must be lowercase. |
|
445 |
* @param string $position Optional. Whether to add the inline script |
|
446 |
* before the handle or after. Default 'after'. |
|
447 |
* @param bool $echo Optional. Whether to echo the script |
|
448 |
* instead of just returning it. Default true. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
449 |
* @return string|false Script on success, false otherwise. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
450 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
451 |
public function print_inline_script( $handle, $position = 'after', $echo = true ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
452 |
$output = $this->get_data( $handle, $position ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
453 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
454 |
if ( empty( $output ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
455 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
456 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
457 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
458 |
$output = trim( implode( "\n", $output ), "\n" ); |
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 ( $echo ) { |
16 | 461 |
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
|
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 |
return $output; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
465 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
466 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
467 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
468 |
* 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
|
469 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
470 |
* @since 2.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
471 |
* |
9 | 472 |
* @param string $handle Name of the script to attach data to. |
473 |
* @param string $object_name Name of the variable that will contain the data. |
|
474 |
* @param array $l10n Array of data to localize. |
|
475 |
* @return bool True on success, false on failure. |
|
0 | 476 |
*/ |
5 | 477 |
public function localize( $handle, $object_name, $l10n ) { |
16 | 478 |
if ( 'jquery' === $handle ) { |
0 | 479 |
$handle = 'jquery-core'; |
9 | 480 |
} |
0 | 481 |
|
9 | 482 |
if ( is_array( $l10n ) && isset( $l10n['l10n_print_after'] ) ) { // back compat, preserve the code in 'l10n_print_after' if present. |
0 | 483 |
$after = $l10n['l10n_print_after']; |
9 | 484 |
unset( $l10n['l10n_print_after'] ); |
0 | 485 |
} |
486 |
||
18 | 487 |
if ( ! is_array( $l10n ) ) { |
488 |
_doing_it_wrong( |
|
489 |
__METHOD__, |
|
490 |
sprintf( |
|
491 |
/* translators: 1: $l10n, 2: wp_add_inline_script() */ |
|
492 |
__( 'The %1$s parameter must be an array. To pass arbitrary data to scripts, use the %2$s function instead.' ), |
|
493 |
'<code>$l10n</code>', |
|
494 |
'<code>wp_add_inline_script()</code>' |
|
495 |
), |
|
496 |
'5.7.0' |
|
497 |
); |
|
498 |
} |
|
499 |
||
500 |
if ( is_string( $l10n ) ) { |
|
501 |
$l10n = html_entity_decode( $l10n, ENT_QUOTES, 'UTF-8' ); |
|
502 |
} else { |
|
503 |
foreach ( (array) $l10n as $key => $value ) { |
|
504 |
if ( ! is_scalar( $value ) ) { |
|
505 |
continue; |
|
506 |
} |
|
507 |
||
508 |
$l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' ); |
|
9 | 509 |
} |
0 | 510 |
} |
511 |
||
5 | 512 |
$script = "var $object_name = " . wp_json_encode( $l10n ) . ';'; |
0 | 513 |
|
9 | 514 |
if ( ! empty( $after ) ) { |
0 | 515 |
$script .= "\n$after;"; |
9 | 516 |
} |
0 | 517 |
|
518 |
$data = $this->get_data( $handle, 'data' ); |
|
519 |
||
9 | 520 |
if ( ! empty( $data ) ) { |
0 | 521 |
$script = "$data\n$script"; |
9 | 522 |
} |
0 | 523 |
|
524 |
return $this->add_data( $handle, 'data', $script ); |
|
525 |
} |
|
526 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
527 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
528 |
* Sets handle group. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
529 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
530 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
531 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
532 |
* @see WP_Dependencies::set_group() |
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 |
* @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
|
535 |
* @param bool $recursion Internal flag that calling function was called recursively. |
16 | 536 |
* @param int|false $group Optional. Group level: level (int), no groups (false). |
537 |
* Default false. |
|
538 |
* @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
|
539 |
*/ |
5 | 540 |
public function set_group( $handle, $recursion, $group = false ) { |
16 | 541 |
if ( isset( $this->registered[ $handle ]->args ) && 1 === $this->registered[ $handle ]->args ) { |
0 | 542 |
$grp = 1; |
9 | 543 |
} else { |
0 | 544 |
$grp = (int) $this->get_data( $handle, 'group' ); |
9 | 545 |
} |
0 | 546 |
|
9 | 547 |
if ( false !== $group && $grp > $group ) { |
0 | 548 |
$grp = $group; |
9 | 549 |
} |
0 | 550 |
|
551 |
return parent::set_group( $handle, $recursion, $grp ); |
|
552 |
} |
|
553 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
554 |
/** |
9 | 555 |
* Sets a translation textdomain. |
556 |
* |
|
557 |
* @since 5.0.0 |
|
558 |
* @since 5.1.0 The `$domain` parameter was made optional. |
|
559 |
* |
|
560 |
* @param string $handle Name of the script to register a translation domain to. |
|
561 |
* @param string $domain Optional. Text domain. Default 'default'. |
|
562 |
* @param string $path Optional. The full file path to the directory containing translation files. |
|
563 |
* @return bool True if the text domain was registered, false if not. |
|
564 |
*/ |
|
565 |
public function set_translations( $handle, $domain = 'default', $path = null ) { |
|
566 |
if ( ! isset( $this->registered[ $handle ] ) ) { |
|
567 |
return false; |
|
568 |
} |
|
569 |
||
570 |
/** @var \_WP_Dependency $obj */ |
|
571 |
$obj = $this->registered[ $handle ]; |
|
572 |
||
573 |
if ( ! in_array( 'wp-i18n', $obj->deps, true ) ) { |
|
574 |
$obj->deps[] = 'wp-i18n'; |
|
575 |
} |
|
576 |
||
577 |
return $obj->set_translations( $domain, $path ); |
|
578 |
} |
|
579 |
||
580 |
/** |
|
581 |
* Prints translations set for a specific handle. |
|
582 |
* |
|
583 |
* @since 5.0.0 |
|
584 |
* |
|
16 | 585 |
* @param string $handle Name of the script to add the inline script to. |
586 |
* Must be lowercase. |
|
587 |
* @param bool $echo Optional. Whether to echo the script |
|
588 |
* instead of just returning it. Default true. |
|
9 | 589 |
* @return string|false Script on success, false otherwise. |
590 |
*/ |
|
591 |
public function print_translations( $handle, $echo = true ) { |
|
592 |
if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) { |
|
593 |
return false; |
|
594 |
} |
|
595 |
||
596 |
$domain = $this->registered[ $handle ]->textdomain; |
|
597 |
$path = $this->registered[ $handle ]->translations_path; |
|
598 |
||
599 |
$json_translations = load_script_textdomain( $handle, $domain, $path ); |
|
600 |
||
601 |
if ( ! $json_translations ) { |
|
602 |
// Register empty locale data object to ensure the domain still exists. |
|
603 |
$json_translations = '{ "locale_data": { "messages": { "": {} } } }'; |
|
604 |
} |
|
605 |
||
606 |
$output = <<<JS |
|
607 |
( function( domain, translations ) { |
|
608 |
var localeData = translations.locale_data[ domain ] || translations.locale_data.messages; |
|
609 |
localeData[""].domain = domain; |
|
610 |
wp.i18n.setLocaleData( localeData, domain ); |
|
611 |
} )( "{$domain}", {$json_translations} ); |
|
612 |
JS; |
|
613 |
||
614 |
if ( $echo ) { |
|
16 | 615 |
printf( "<script%s id='%s-js-translations'>\n%s\n</script>\n", $this->type_attr, esc_attr( $handle ), $output ); |
9 | 616 |
} |
617 |
||
618 |
return $output; |
|
619 |
} |
|
620 |
||
621 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
622 |
* Determines script dependencies. |
9 | 623 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
624 |
* @since 2.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
625 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
626 |
* @see WP_Dependencies::all_deps() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
627 |
* |
16 | 628 |
* @param string|string[] $handles Item handle (string) or item handles (array of strings). |
629 |
* @param bool $recursion Optional. Internal flag that function is calling itself. |
|
630 |
* Default false. |
|
631 |
* @param int|false $group Optional. Group level: level (int), no groups (false). |
|
632 |
* Default false. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
633 |
* @return bool True on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
634 |
*/ |
5 | 635 |
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
|
636 |
$r = parent::all_deps( $handles, $recursion, $group ); |
5 | 637 |
if ( ! $recursion ) { |
638 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
639 |
* Filters the list of script dependencies left to print. |
5 | 640 |
* |
641 |
* @since 2.3.0 |
|
642 |
* |
|
9 | 643 |
* @param string[] $to_do An array of script dependency handles. |
5 | 644 |
*/ |
0 | 645 |
$this->to_do = apply_filters( 'print_scripts_array', $this->to_do ); |
5 | 646 |
} |
0 | 647 |
return $r; |
648 |
} |
|
649 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
650 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
651 |
* Processes items and dependencies for the head group. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
652 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
653 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
654 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
655 |
* @see WP_Dependencies::do_items() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
656 |
* |
16 | 657 |
* @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
|
658 |
*/ |
5 | 659 |
public function do_head_items() { |
9 | 660 |
$this->do_items( false, 0 ); |
0 | 661 |
return $this->done; |
662 |
} |
|
663 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
664 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
665 |
* Processes items and dependencies for the footer group. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
666 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
667 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
668 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
669 |
* @see WP_Dependencies::do_items() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
670 |
* |
16 | 671 |
* @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
|
672 |
*/ |
5 | 673 |
public function do_footer_items() { |
9 | 674 |
$this->do_items( false, 1 ); |
0 | 675 |
return $this->done; |
676 |
} |
|
677 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
678 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
679 |
* 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
|
680 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
681 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
682 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
683 |
* @param string $src The source of the enqueued script. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
684 |
* @return bool True if found, false if not. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
685 |
*/ |
5 | 686 |
public function in_default_dir( $src ) { |
687 |
if ( ! $this->default_dirs ) { |
|
0 | 688 |
return true; |
5 | 689 |
} |
0 | 690 |
|
5 | 691 |
if ( 0 === strpos( $src, '/' . WPINC . '/js/l10n' ) ) { |
0 | 692 |
return false; |
5 | 693 |
} |
0 | 694 |
|
695 |
foreach ( (array) $this->default_dirs as $test ) { |
|
5 | 696 |
if ( 0 === strpos( $src, $test ) ) { |
0 | 697 |
return true; |
5 | 698 |
} |
0 | 699 |
} |
700 |
return false; |
|
701 |
} |
|
702 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
703 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
704 |
* Resets class properties. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
705 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
706 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
707 |
*/ |
5 | 708 |
public function reset() { |
9 | 709 |
$this->do_concat = false; |
710 |
$this->print_code = ''; |
|
711 |
$this->concat = ''; |
|
0 | 712 |
$this->concat_version = ''; |
9 | 713 |
$this->print_html = ''; |
714 |
$this->ext_version = ''; |
|
715 |
$this->ext_handles = ''; |
|
0 | 716 |
} |
717 |
} |