15 * |
15 * |
16 * @see _WP_Dependency |
16 * @see _WP_Dependency |
17 */ |
17 */ |
18 class WP_Dependencies { |
18 class WP_Dependencies { |
19 /** |
19 /** |
20 * An array of registered handle objects. |
20 * An array of all registered dependencies keyed by handle. |
21 * |
21 * |
22 * @since 2.6.8 |
22 * @since 2.6.8 |
|
23 * |
|
24 * @var _WP_Dependency[] |
|
25 */ |
|
26 public $registered = array(); |
|
27 |
|
28 /** |
|
29 * An array of handles of queued dependencies. |
|
30 * |
|
31 * @since 2.6.8 |
|
32 * |
|
33 * @var string[] |
|
34 */ |
|
35 public $queue = array(); |
|
36 |
|
37 /** |
|
38 * An array of handles of dependencies to queue. |
|
39 * |
|
40 * @since 2.6.0 |
|
41 * |
|
42 * @var string[] |
|
43 */ |
|
44 public $to_do = array(); |
|
45 |
|
46 /** |
|
47 * An array of handles of dependencies already queued. |
|
48 * |
|
49 * @since 2.6.0 |
|
50 * |
|
51 * @var string[] |
|
52 */ |
|
53 public $done = array(); |
|
54 |
|
55 /** |
|
56 * An array of additional arguments passed when a handle is registered. |
|
57 * |
|
58 * Arguments are appended to the item query string. |
|
59 * |
|
60 * @since 2.6.0 |
|
61 * |
23 * @var array |
62 * @var array |
24 */ |
63 */ |
25 public $registered = array(); |
|
26 |
|
27 /** |
|
28 * An array of handles of queued objects. |
|
29 * |
|
30 * @since 2.6.8 |
|
31 * @var string[] |
|
32 */ |
|
33 public $queue = array(); |
|
34 |
|
35 /** |
|
36 * An array of handles of objects to queue. |
|
37 * |
|
38 * @since 2.6.0 |
|
39 * @var string[] |
|
40 */ |
|
41 public $to_do = array(); |
|
42 |
|
43 /** |
|
44 * An array of handles of objects already queued. |
|
45 * |
|
46 * @since 2.6.0 |
|
47 * @var string[] |
|
48 */ |
|
49 public $done = array(); |
|
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 */ |
|
59 public $args = array(); |
64 public $args = array(); |
60 |
65 |
61 /** |
66 /** |
62 * An array of handle groups to enqueue. |
67 * An array of dependency groups to enqueue. |
|
68 * |
|
69 * Each entry is keyed by handle and represents the integer group level or boolean |
|
70 * false if the handle has no group. |
63 * |
71 * |
64 * @since 2.8.0 |
72 * @since 2.8.0 |
65 * @var array |
73 * |
|
74 * @var (int|false)[] |
66 */ |
75 */ |
67 public $groups = array(); |
76 public $groups = array(); |
68 |
77 |
69 /** |
78 /** |
70 * A handle group to enqueue. |
79 * A handle group to enqueue. |
71 * |
80 * |
72 * @since 2.8.0 |
81 * @since 2.8.0 |
|
82 * |
73 * @deprecated 4.5.0 |
83 * @deprecated 4.5.0 |
74 * @var int |
84 * @var int |
75 */ |
85 */ |
76 public $group = 0; |
86 public $group = 0; |
77 |
87 |
78 /** |
88 /** |
79 * Cached lookup array of flattened queued items and dependencies. |
89 * Cached lookup array of flattened queued items and dependencies. |
80 * |
90 * |
81 * @since 5.4.0 |
91 * @since 5.4.0 |
|
92 * |
82 * @var array |
93 * @var array |
83 */ |
94 */ |
84 private $all_queued_deps; |
95 private $all_queued_deps; |
|
96 |
|
97 /** |
|
98 * List of assets enqueued before details were registered. |
|
99 * |
|
100 * @since 5.9.0 |
|
101 * |
|
102 * @var array |
|
103 */ |
|
104 private $queued_before_register = array(); |
85 |
105 |
86 /** |
106 /** |
87 * Processes the items and dependencies. |
107 * Processes the items and dependencies. |
88 * |
108 * |
89 * Processes the items passed to it or the queue, and their dependencies. |
109 * Processes the items passed to it or the queue, and their dependencies. |
92 * @since 2.8.0 Added the `$group` parameter. |
112 * @since 2.8.0 Added the `$group` parameter. |
93 * |
113 * |
94 * @param string|string[]|false $handles Optional. Items to be processed: queue (false), |
114 * @param string|string[]|false $handles Optional. Items to be processed: queue (false), |
95 * single item (string), or multiple items (array of strings). |
115 * single item (string), or multiple items (array of strings). |
96 * Default false. |
116 * Default false. |
97 * @param int|false $group Optional. Group level: level (int), no groups (false). |
117 * @param int|false $group Optional. Group level: level (int), no group (false). |
98 * @return string[] Array of handles of items that have been processed. |
118 * @return string[] Array of handles of items that have been processed. |
99 */ |
119 */ |
100 public function do_items( $handles = false, $group = false ) { |
120 public function do_items( $handles = false, $group = false ) { |
101 /* |
121 /* |
102 * If nothing is passed, print the queue. If a string is passed, |
122 * If nothing is passed, print the queue. If a string is passed, |
129 * |
149 * |
130 * @since 2.6.0 |
150 * @since 2.6.0 |
131 * @since 5.5.0 Added the `$group` parameter. |
151 * @since 5.5.0 Added the `$group` parameter. |
132 * |
152 * |
133 * @param string $handle Name of the item. Should be unique. |
153 * @param string $handle Name of the item. Should be unique. |
134 * @param int|false $group Optional. Group level: level (int), no groups (false). |
154 * @param int|false $group Optional. Group level: level (int), no group (false). |
135 * Default false. |
155 * Default false. |
136 * @return bool True on success, false if not set. |
156 * @return bool True on success, false if not set. |
137 */ |
157 */ |
138 public function do_item( $handle, $group = false ) { |
158 public function do_item( $handle, $group = false ) { |
139 return isset( $this->registered[ $handle ] ); |
159 return isset( $this->registered[ $handle ] ); |
150 * @since 2.8.0 Added the `$group` parameter. |
170 * @since 2.8.0 Added the `$group` parameter. |
151 * |
171 * |
152 * @param string|string[] $handles Item handle (string) or item handles (array of strings). |
172 * @param string|string[] $handles Item handle (string) or item handles (array of strings). |
153 * @param bool $recursion Optional. Internal flag that function is calling itself. |
173 * @param bool $recursion Optional. Internal flag that function is calling itself. |
154 * Default false. |
174 * Default false. |
155 * @param int|false $group Optional. Group level: level (int), no groups (false). |
175 * @param int|false $group Optional. Group level: level (int), no group (false). |
156 * Default false. |
176 * Default false. |
157 * @return bool True on success, false on failure. |
177 * @return bool True on success, false on failure. |
158 */ |
178 */ |
159 public function all_deps( $handles, $recursion = false, $group = false ) { |
179 public function all_deps( $handles, $recursion = false, $group = false ) { |
160 $handles = (array) $handles; |
180 $handles = (array) $handles; |
235 public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) { |
255 public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) { |
236 if ( isset( $this->registered[ $handle ] ) ) { |
256 if ( isset( $this->registered[ $handle ] ) ) { |
237 return false; |
257 return false; |
238 } |
258 } |
239 $this->registered[ $handle ] = new _WP_Dependency( $handle, $src, $deps, $ver, $args ); |
259 $this->registered[ $handle ] = new _WP_Dependency( $handle, $src, $deps, $ver, $args ); |
|
260 |
|
261 // If the item was enqueued before the details were registered, enqueue it now. |
|
262 if ( array_key_exists( $handle, $this->queued_before_register ) ) { |
|
263 if ( ! is_null( $this->queued_before_register[ $handle ] ) ) { |
|
264 $this->enqueue( $handle . '?' . $this->queued_before_register[ $handle ] ); |
|
265 } else { |
|
266 $this->enqueue( $handle ); |
|
267 } |
|
268 |
|
269 unset( $this->queued_before_register[ $handle ] ); |
|
270 } |
|
271 |
240 return true; |
272 return true; |
241 } |
273 } |
242 |
274 |
243 /** |
275 /** |
244 * Add extra item data. |
276 * Add extra item data. |
321 $this->all_queued_deps = null; |
353 $this->all_queued_deps = null; |
322 |
354 |
323 if ( isset( $handle[1] ) ) { |
355 if ( isset( $handle[1] ) ) { |
324 $this->args[ $handle[0] ] = $handle[1]; |
356 $this->args[ $handle[0] ] = $handle[1]; |
325 } |
357 } |
|
358 } elseif ( ! isset( $this->registered[ $handle[0] ] ) ) { |
|
359 $this->queued_before_register[ $handle[0] ] = null; // $args |
|
360 |
|
361 if ( isset( $handle[1] ) ) { |
|
362 $this->queued_before_register[ $handle[0] ] = $handle[1]; |
|
363 } |
326 } |
364 } |
327 } |
365 } |
328 } |
366 } |
329 |
367 |
330 /** |
368 /** |
347 // Reset all dependencies so they must be recalculated in recurse_deps(). |
385 // Reset all dependencies so they must be recalculated in recurse_deps(). |
348 $this->all_queued_deps = null; |
386 $this->all_queued_deps = null; |
349 |
387 |
350 unset( $this->queue[ $key ] ); |
388 unset( $this->queue[ $key ] ); |
351 unset( $this->args[ $handle[0] ] ); |
389 unset( $this->args[ $handle[0] ] ); |
352 } |
390 } elseif ( array_key_exists( $handle[0], $this->queued_before_register ) ) { |
353 } |
391 unset( $this->queued_before_register[ $handle[0] ] ); |
354 } |
392 } |
355 |
393 } |
356 /** |
394 } |
357 * Recursively search the passed dependency tree for $handle. |
395 |
|
396 /** |
|
397 * Recursively search the passed dependency tree for a handle. |
358 * |
398 * |
359 * @since 4.0.0 |
399 * @since 4.0.0 |
360 * |
400 * |
361 * @param string[] $queue An array of queued _WP_Dependency handles. |
401 * @param string[] $queue An array of queued _WP_Dependency handles. |
362 * @param string $handle Name of the item. Should be unique. |
402 * @param string $handle Name of the item. Should be unique. |
389 |
429 |
390 return isset( $this->all_queued_deps[ $handle ] ); |
430 return isset( $this->all_queued_deps[ $handle ] ); |
391 } |
431 } |
392 |
432 |
393 /** |
433 /** |
394 * Query list for an item. |
434 * Query the list for an item. |
395 * |
435 * |
396 * @since 2.1.0 |
436 * @since 2.1.0 |
397 * @since 2.6.0 Moved from `WP_Scripts`. |
437 * @since 2.6.0 Moved from `WP_Scripts`. |
398 * |
438 * |
399 * @param string $handle Name of the item. Should be unique. |
439 * @param string $handle Name of the item. Should be unique. |
400 * @param string $list Optional. Property name of list array. Default 'registered'. |
440 * @param string $status Optional. Status of the item to query. Default 'registered'. |
401 * @return bool|_WP_Dependency Found, or object Item data. |
441 * @return bool|_WP_Dependency Found, or object Item data. |
402 */ |
442 */ |
403 public function query( $handle, $list = 'registered' ) { |
443 public function query( $handle, $status = 'registered' ) { |
404 switch ( $list ) { |
444 switch ( $status ) { |
405 case 'registered': |
445 case 'registered': |
406 case 'scripts': // Back compat. |
446 case 'scripts': // Back compat. |
407 if ( isset( $this->registered[ $handle ] ) ) { |
447 if ( isset( $this->registered[ $handle ] ) ) { |
408 return $this->registered[ $handle ]; |
448 return $this->registered[ $handle ]; |
409 } |
449 } |
410 return false; |
450 return false; |
411 |
451 |
412 case 'enqueued': |
452 case 'enqueued': |
413 case 'queue': |
453 case 'queue': // Back compat. |
414 if ( in_array( $handle, $this->queue, true ) ) { |
454 if ( in_array( $handle, $this->queue, true ) ) { |
415 return true; |
455 return true; |
416 } |
456 } |
417 return $this->recurse_deps( $this->queue, $handle ); |
457 return $this->recurse_deps( $this->queue, $handle ); |
418 |
458 |
422 |
462 |
423 case 'done': |
463 case 'done': |
424 case 'printed': // Back compat. |
464 case 'printed': // Back compat. |
425 return in_array( $handle, $this->done, true ); |
465 return in_array( $handle, $this->done, true ); |
426 } |
466 } |
|
467 |
427 return false; |
468 return false; |
428 } |
469 } |
429 |
470 |
430 /** |
471 /** |
431 * Set item group, unless already in a lower group. |
472 * Set item group, unless already in a lower group. |
432 * |
473 * |
433 * @since 2.8.0 |
474 * @since 2.8.0 |
434 * |
475 * |
435 * @param string $handle Name of the item. Should be unique. |
476 * @param string $handle Name of the item. Should be unique. |
436 * @param bool $recursion Internal flag that calling function was called recursively. |
477 * @param bool $recursion Internal flag that calling function was called recursively. |
437 * @param int|false $group Group level: level (int), no groups (false). |
478 * @param int|false $group Group level: level (int), no group (false). |
438 * @return bool Not already in the group or a lower group. |
479 * @return bool Not already in the group or a lower group. |
439 */ |
480 */ |
440 public function set_group( $handle, $recursion, $group ) { |
481 public function set_group( $handle, $recursion, $group ) { |
441 $group = (int) $group; |
482 $group = (int) $group; |
442 |
483 |