author | ymh <ymh.work@gmail.com> |
Tue, 22 Oct 2019 16:11:46 +0200 | |
changeset 15 | 3d4e9c994f10 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
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_Dependencies base 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 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
9 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
10 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
11 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
12 |
* Core base class extended to register items. |
0 | 13 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @since 2.6.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_Dependency |
0 | 17 |
*/ |
18 |
class WP_Dependencies { |
|
19 |
/** |
|
20 |
* An array of registered handle objects. |
|
21 |
* |
|
22 |
* @since 2.6.8 |
|
23 |
* @var array |
|
24 |
*/ |
|
5 | 25 |
public $registered = array(); |
0 | 26 |
|
27 |
/** |
|
28 |
* An array of queued _WP_Dependency handle objects. |
|
29 |
* |
|
30 |
* @since 2.6.8 |
|
31 |
* @var array |
|
32 |
*/ |
|
5 | 33 |
public $queue = array(); |
0 | 34 |
|
35 |
/** |
|
36 |
* An array of _WP_Dependency handle objects to queue. |
|
37 |
* |
|
38 |
* @since 2.6.0 |
|
39 |
* @var array |
|
40 |
*/ |
|
5 | 41 |
public $to_do = array(); |
0 | 42 |
|
43 |
/** |
|
44 |
* An array of _WP_Dependency handle objects already queued. |
|
45 |
* |
|
46 |
* @since 2.6.0 |
|
47 |
* @var array |
|
48 |
*/ |
|
5 | 49 |
public $done = array(); |
0 | 50 |
|
51 |
/** |
|
52 |
* An array of additional arguments passed when a handle is registered. |
|
53 |
* |
|
54 |
* Arguments are appended to the item query string. |
|
55 |
* |
|
56 |
* @since 2.6.0 |
|
57 |
* @var array |
|
58 |
*/ |
|
5 | 59 |
public $args = array(); |
0 | 60 |
|
61 |
/** |
|
62 |
* An array of handle groups to enqueue. |
|
63 |
* |
|
64 |
* @since 2.8.0 |
|
65 |
* @var array |
|
66 |
*/ |
|
5 | 67 |
public $groups = array(); |
0 | 68 |
|
69 |
/** |
|
70 |
* A handle group to enqueue. |
|
71 |
* |
|
72 |
* @since 2.8.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
* @deprecated 4.5.0 |
0 | 74 |
* @var int |
75 |
*/ |
|
5 | 76 |
public $group = 0; |
0 | 77 |
|
78 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
* Processes the items and dependencies. |
0 | 80 |
* |
81 |
* Processes the items passed to it or the queue, and their dependencies. |
|
82 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
* @since 2.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
* @since 2.8.0 Added the `$group` parameter. |
0 | 85 |
* |
86 |
* @param mixed $handles Optional. Items to be processed: Process queue (false), process item (string), process items (array of strings). |
|
87 |
* @param mixed $group Group level: level (int), no groups (false). |
|
88 |
* @return array Handles of items that have been processed. |
|
89 |
*/ |
|
90 |
public function do_items( $handles = false, $group = false ) { |
|
5 | 91 |
/* |
0 | 92 |
* If nothing is passed, print the queue. If a string is passed, |
93 |
* print that item. If an array is passed, print those items. |
|
94 |
*/ |
|
95 |
$handles = false === $handles ? $this->queue : (array) $handles; |
|
96 |
$this->all_deps( $handles ); |
|
97 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
98 |
foreach ( $this->to_do as $key => $handle ) { |
9 | 99 |
if ( ! in_array( $handle, $this->done, true ) && isset( $this->registered[ $handle ] ) ) { |
5 | 100 |
/* |
0 | 101 |
* Attempt to process the item. If successful, |
102 |
* add the handle to the done array. |
|
103 |
* |
|
104 |
* Unset the item from the to_do array. |
|
105 |
*/ |
|
9 | 106 |
if ( $this->do_item( $handle, $group ) ) { |
0 | 107 |
$this->done[] = $handle; |
9 | 108 |
} |
0 | 109 |
|
9 | 110 |
unset( $this->to_do[ $key ] ); |
0 | 111 |
} |
112 |
} |
|
113 |
||
114 |
return $this->done; |
|
115 |
} |
|
116 |
||
117 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
* Processes a dependency. |
0 | 119 |
* |
120 |
* @since 2.6.0 |
|
121 |
* |
|
122 |
* @param string $handle Name of the item. Should be unique. |
|
123 |
* @return bool True on success, false if not set. |
|
124 |
*/ |
|
125 |
public function do_item( $handle ) { |
|
9 | 126 |
return isset( $this->registered[ $handle ] ); |
0 | 127 |
} |
128 |
||
129 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
130 |
* Determines dependencies. |
0 | 131 |
* |
132 |
* Recursively builds an array of items to process taking |
|
133 |
* dependencies into account. Does NOT catch infinite loops. |
|
134 |
* |
|
135 |
* @since 2.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
136 |
* @since 2.6.0 Moved from `WP_Scripts`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
* @since 2.8.0 Added the `$group` parameter. |
0 | 138 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
* @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
* @param bool $recursion Internal flag that function is calling itself. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
* @param int|false $group Group level: (int) level, (false) no groups. |
0 | 142 |
* @return bool True on success, false on failure. |
143 |
*/ |
|
144 |
public function all_deps( $handles, $recursion = false, $group = false ) { |
|
9 | 145 |
if ( ! $handles = (array) $handles ) { |
0 | 146 |
return false; |
9 | 147 |
} |
0 | 148 |
|
149 |
foreach ( $handles as $handle ) { |
|
9 | 150 |
$handle_parts = explode( '?', $handle ); |
151 |
$handle = $handle_parts[0]; |
|
152 |
$queued = in_array( $handle, $this->to_do, true ); |
|
0 | 153 |
|
9 | 154 |
if ( in_array( $handle, $this->done, true ) ) { // Already done |
0 | 155 |
continue; |
9 | 156 |
} |
0 | 157 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
$moved = $this->set_group( $handle, $recursion, $group ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
$new_group = $this->groups[ $handle ]; |
0 | 160 |
|
9 | 161 |
if ( $queued && ! $moved ) { // already queued and in the right group |
0 | 162 |
continue; |
9 | 163 |
} |
0 | 164 |
|
165 |
$keep_going = true; |
|
9 | 166 |
if ( ! isset( $this->registered[ $handle ] ) ) { |
0 | 167 |
$keep_going = false; // Item doesn't exist. |
9 | 168 |
} elseif ( $this->registered[ $handle ]->deps && array_diff( $this->registered[ $handle ]->deps, array_keys( $this->registered ) ) ) { |
0 | 169 |
$keep_going = false; // Item requires dependencies that don't exist. |
9 | 170 |
} elseif ( $this->registered[ $handle ]->deps && ! $this->all_deps( $this->registered[ $handle ]->deps, true, $new_group ) ) { |
0 | 171 |
$keep_going = false; // Item requires dependencies that don't exist. |
9 | 172 |
} |
0 | 173 |
|
174 |
if ( ! $keep_going ) { // Either item or its dependencies don't exist. |
|
9 | 175 |
if ( $recursion ) { |
0 | 176 |
return false; // Abort this branch. |
9 | 177 |
} else { |
0 | 178 |
continue; // We're at the top level. Move on to the next one. |
9 | 179 |
} |
0 | 180 |
} |
181 |
||
9 | 182 |
if ( $queued ) { // Already grabbed it and its dependencies. |
0 | 183 |
continue; |
9 | 184 |
} |
0 | 185 |
|
9 | 186 |
if ( isset( $handle_parts[1] ) ) { |
187 |
$this->args[ $handle ] = $handle_parts[1]; |
|
188 |
} |
|
0 | 189 |
|
190 |
$this->to_do[] = $handle; |
|
191 |
} |
|
192 |
||
193 |
return true; |
|
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* Register an item. |
|
198 |
* |
|
199 |
* Registers the item if no item of that name already exists. |
|
200 |
* |
|
201 |
* @since 2.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
202 |
* @since 2.6.0 Moved from `WP_Scripts`. |
0 | 203 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
204 |
* @param string $handle Name of the item. Should be unique. |
9 | 205 |
* @param string|bool $src Full URL of the item, or path of the item relative to the WordPress root directory. |
206 |
* If source is set to false, item is an alias of other items it depends on. |
|
207 |
* @param string[] $deps Optional. An array of registered item handles this item depends on. Default empty array. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
* @param string|bool|null $ver Optional. String specifying item version number, if it has one, which is added to the URL |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
209 |
* as a query string for cache busting purposes. If version is set to false, a version |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
210 |
* number is automatically added equal to current installed WordPress version. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
211 |
* If set to null, no version is added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
212 |
* @param mixed $args Optional. Custom property of the item. NOT the class property $args. Examples: $media, $in_footer. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
213 |
* @return bool Whether the item has been registered. True on success, false on failure. |
0 | 214 |
*/ |
215 |
public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) { |
|
9 | 216 |
if ( isset( $this->registered[ $handle ] ) ) { |
0 | 217 |
return false; |
9 | 218 |
} |
219 |
$this->registered[ $handle ] = new _WP_Dependency( $handle, $src, $deps, $ver, $args ); |
|
0 | 220 |
return true; |
221 |
} |
|
222 |
||
223 |
/** |
|
224 |
* Add extra item data. |
|
225 |
* |
|
226 |
* Adds data to a registered item. |
|
227 |
* |
|
228 |
* @since 2.6.0 |
|
229 |
* |
|
230 |
* @param string $handle Name of the item. Should be unique. |
|
231 |
* @param string $key The data key. |
|
232 |
* @param mixed $value The data value. |
|
233 |
* @return bool True on success, false on failure. |
|
234 |
*/ |
|
235 |
public function add_data( $handle, $key, $value ) { |
|
9 | 236 |
if ( ! isset( $this->registered[ $handle ] ) ) { |
0 | 237 |
return false; |
9 | 238 |
} |
0 | 239 |
|
9 | 240 |
return $this->registered[ $handle ]->add_data( $key, $value ); |
0 | 241 |
} |
242 |
||
243 |
/** |
|
244 |
* Get extra item data. |
|
245 |
* |
|
246 |
* Gets data associated with a registered item. |
|
247 |
* |
|
248 |
* @since 3.3.0 |
|
249 |
* |
|
250 |
* @param string $handle Name of the item. Should be unique. |
|
251 |
* @param string $key The data key. |
|
252 |
* @return mixed Extra item data (string), false otherwise. |
|
253 |
*/ |
|
254 |
public function get_data( $handle, $key ) { |
|
9 | 255 |
if ( ! isset( $this->registered[ $handle ] ) ) { |
0 | 256 |
return false; |
9 | 257 |
} |
0 | 258 |
|
9 | 259 |
if ( ! isset( $this->registered[ $handle ]->extra[ $key ] ) ) { |
0 | 260 |
return false; |
9 | 261 |
} |
0 | 262 |
|
9 | 263 |
return $this->registered[ $handle ]->extra[ $key ]; |
0 | 264 |
} |
265 |
||
266 |
/** |
|
267 |
* Un-register an item or items. |
|
268 |
* |
|
269 |
* @since 2.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
270 |
* @since 2.6.0 Moved from `WP_Scripts`. |
0 | 271 |
* |
272 |
* @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
|
273 |
* @return void |
|
274 |
*/ |
|
275 |
public function remove( $handles ) { |
|
9 | 276 |
foreach ( (array) $handles as $handle ) { |
277 |
unset( $this->registered[ $handle ] ); |
|
278 |
} |
|
0 | 279 |
} |
280 |
||
281 |
/** |
|
282 |
* Queue an item or items. |
|
283 |
* |
|
284 |
* Decodes handles and arguments, then queues handles and stores |
|
285 |
* arguments in the class property $args. For example in extending |
|
286 |
* classes, $args is appended to the item url as a query string. |
|
287 |
* Note $args is NOT the $args property of items in the $registered array. |
|
288 |
* |
|
289 |
* @since 2.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
290 |
* @since 2.6.0 Moved from `WP_Scripts`. |
0 | 291 |
* |
292 |
* @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
|
293 |
*/ |
|
294 |
public function enqueue( $handles ) { |
|
295 |
foreach ( (array) $handles as $handle ) { |
|
9 | 296 |
$handle = explode( '?', $handle ); |
297 |
if ( ! in_array( $handle[0], $this->queue ) && isset( $this->registered[ $handle[0] ] ) ) { |
|
0 | 298 |
$this->queue[] = $handle[0]; |
9 | 299 |
if ( isset( $handle[1] ) ) { |
300 |
$this->args[ $handle[0] ] = $handle[1]; |
|
301 |
} |
|
0 | 302 |
} |
303 |
} |
|
304 |
} |
|
305 |
||
306 |
/** |
|
307 |
* Dequeue an item or items. |
|
308 |
* |
|
309 |
* Decodes handles and arguments, then dequeues handles |
|
310 |
* and removes arguments from the class property $args. |
|
311 |
* |
|
312 |
* @since 2.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
313 |
* @since 2.6.0 Moved from `WP_Scripts`. |
0 | 314 |
* |
315 |
* @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
|
316 |
*/ |
|
317 |
public function dequeue( $handles ) { |
|
318 |
foreach ( (array) $handles as $handle ) { |
|
9 | 319 |
$handle = explode( '?', $handle ); |
320 |
$key = array_search( $handle[0], $this->queue ); |
|
0 | 321 |
if ( false !== $key ) { |
9 | 322 |
unset( $this->queue[ $key ] ); |
323 |
unset( $this->args[ $handle[0] ] ); |
|
0 | 324 |
} |
325 |
} |
|
326 |
} |
|
327 |
||
328 |
/** |
|
5 | 329 |
* Recursively search the passed dependency tree for $handle |
330 |
* |
|
331 |
* @since 4.0.0 |
|
332 |
* |
|
9 | 333 |
* @param string[] $queue An array of queued _WP_Dependency handles. |
334 |
* @param string $handle Name of the item. Should be unique. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
335 |
* @return bool Whether the handle is found after recursively searching the dependency tree. |
5 | 336 |
*/ |
337 |
protected function recurse_deps( $queue, $handle ) { |
|
338 |
foreach ( $queue as $queued ) { |
|
339 |
if ( ! isset( $this->registered[ $queued ] ) ) { |
|
340 |
continue; |
|
341 |
} |
|
342 |
||
343 |
if ( in_array( $handle, $this->registered[ $queued ]->deps ) ) { |
|
344 |
return true; |
|
345 |
} elseif ( $this->recurse_deps( $this->registered[ $queued ]->deps, $handle ) ) { |
|
346 |
return true; |
|
347 |
} |
|
348 |
} |
|
349 |
||
350 |
return false; |
|
351 |
} |
|
352 |
||
353 |
/** |
|
0 | 354 |
* Query list for an item. |
355 |
* |
|
356 |
* @since 2.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
* @since 2.6.0 Moved from `WP_Scripts`. |
0 | 358 |
* |
359 |
* @param string $handle Name of the item. Should be unique. |
|
360 |
* @param string $list Property name of list array. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
361 |
* @return bool|_WP_Dependency Found, or object Item data. |
0 | 362 |
*/ |
363 |
public function query( $handle, $list = 'registered' ) { |
|
364 |
switch ( $list ) { |
|
9 | 365 |
case 'registered': |
0 | 366 |
case 'scripts': // back compat |
9 | 367 |
if ( isset( $this->registered[ $handle ] ) ) { |
0 | 368 |
return $this->registered[ $handle ]; |
9 | 369 |
} |
0 | 370 |
return false; |
371 |
||
9 | 372 |
case 'enqueued': |
373 |
case 'queue': |
|
5 | 374 |
if ( in_array( $handle, $this->queue ) ) { |
375 |
return true; |
|
376 |
} |
|
377 |
return $this->recurse_deps( $this->queue, $handle ); |
|
0 | 378 |
|
9 | 379 |
case 'to_do': |
0 | 380 |
case 'to_print': // back compat |
381 |
return in_array( $handle, $this->to_do ); |
|
382 |
||
9 | 383 |
case 'done': |
0 | 384 |
case 'printed': // back compat |
385 |
return in_array( $handle, $this->done ); |
|
386 |
} |
|
387 |
return false; |
|
388 |
} |
|
389 |
||
390 |
/** |
|
391 |
* Set item group, unless already in a lower group. |
|
392 |
* |
|
393 |
* @since 2.8.0 |
|
394 |
* |
|
395 |
* @param string $handle Name of the item. Should be unique. |
|
396 |
* @param bool $recursion Internal flag that calling function was called recursively. |
|
397 |
* @param mixed $group Group level. |
|
398 |
* @return bool Not already in the group or a lower group |
|
399 |
*/ |
|
400 |
public function set_group( $handle, $recursion, $group ) { |
|
401 |
$group = (int) $group; |
|
402 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
403 |
if ( isset( $this->groups[ $handle ] ) && $this->groups[ $handle ] <= $group ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
404 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
405 |
} |
0 | 406 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
407 |
$this->groups[ $handle ] = $group; |
0 | 408 |
|
409 |
return true; |
|
410 |
} |
|
411 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
412 |
} |