1<?php
2/**
3 * Dependencies API: WP_Dependencies base class
4 *
5 * @since 2.6.0
6 *
7 * @package WordPress
8 * @subpackage Dependencies
9 */
10
11/**
12 * Core base class extended to register items.
13 *
14 * @since 2.6.0
15 *
16 * @see _WP_Dependency
17 */
18#[AllowDynamicProperties]
19class WP_Dependencies {
20 /**
21 * An array of all registered dependencies keyed by handle.
22 *
23 * @since 2.6.8
24 *
25 * @var _WP_Dependency[]
26 */
27 public $registered = array();
28
29 /**
30 * An array of handles of queued dependencies.
31 *
32 * @since 2.6.8
33 *
34 * @var string[]
35 */
36 public $queue = array();
37
38 /**
39 * An array of handles of dependencies to queue.
40 *
41 * @since 2.6.0
42 *
43 * @var string[]
44 */
45 public $to_do = array();
46
47 /**
48 * An array of handles of dependencies already queued.
49 *
50 * @since 2.6.0
51 *
52 * @var string[]
53 */
54 public $done = array();
55
56 /**
57 * An array of additional arguments passed when a handle is registered.
58 *
59 * Arguments are appended to the item query string.
60 *
61 * @since 2.6.0
62 *
63 * @var array
64 */
65 public $args = array();
66
67 /**
68 * An array of dependency groups to enqueue.
69 *
70 * Each entry is keyed by handle and represents the integer group level or boolean
71 * false if the handle has no group.
72 *
73 * @since 2.8.0
74 *
75 * @var (int|false)[]
76 */
77 public $groups = array();
78
79 /**
80 * A handle group to enqueue.
81 *
82 * @since 2.8.0
83 *
84 * @deprecated 4.5.0
85 * @var int
86 */
87 public $group = 0;
88
89 /**
90 * Cached lookup array of flattened queued items and dependencies.
91 *
92 * @since 5.4.0
93 *
94 * @var array
95 */
96 private $all_queued_deps;
97
98 /**
99 * List of assets enqueued before details were registered.
100 *
101 * @since 5.9.0
102 *
103 * @var array
104 */
105 private $queued_before_register = array();
106
107 /**
108 * List of handles for dependencies encountered which themselves have missing dependencies.
109 *
110 * A dependency handle is added to this list when it is discovered to have missing dependencies. At this time, a
111 * warning is emitted with {@see _doing_it_wrong()}. The handle is then added to this list, so that duplicate
112 * warnings don't occur.
113 *
114 * @since 6.9.1
115 * @var string[]
116 */
117 private $dependencies_with_missing_dependencies = array();
118
119 /**
120 * Processes the items and dependencies.
121 *
122 * Processes the items passed to it or the queue, and their dependencies.
123 *
124 * @since 2.6.0
125 * @since 2.8.0 Added the `$group` parameter.
126 *
127 * @param string|string[]|false $handles Optional. Items to be processed: queue (false),
128 * single item (string), or multiple items (array of strings).
129 * Default false.
130 * @param int|false $group Optional. Group level: level (int), no group (false).
131 * @return string[] Array of handles of items that have been processed.
132 */
133 public function do_items( $handles = false, $group = false ) {
134 /*
135 * If nothing is passed, print the queue. If a string is passed,
136 * print that item. If an array is passed, print those items.
137 */
138 $handles = false === $handles ? $this->queue : (array) $handles;
139 $this->all_deps( $handles );
140
141 foreach ( $this->to_do as $key => $handle ) {
142 if ( ! in_array( $handle, $this->done, true ) && isset( $this->registered[ $handle ] ) ) {
143 /*
144 * Attempt to process the item. If successful,
145 * add the handle to the done array.
146 *
147 * Unset the item from the to_do array.
148 */
149 if ( $this->do_item( $handle, $group ) ) {
150 $this->done[] = $handle;
151 }
152
153 unset( $this->to_do[ $key ] );
154 }
155 }
156
157 return $this->done;
158 }
159
160 /**
161 * Processes a dependency.
162 *
163 * @since 2.6.0
164 * @since 5.5.0 Added the `$group` parameter.
165 *
166 * @param string $handle Name of the item. Should be unique.
167 * @param int|false $group Optional. Group level: level (int), no group (false).
168 * Default false.
169 * @return bool True on success, false if not set.
170 */
171 public function do_item( $handle, $group = false ) {
172 return isset( $this->registered[ $handle ] );
173 }
174
175 /**
176 * Determines dependencies.
177 *
178 * Recursively builds an array of items to process taking
179 * dependencies into account. Does NOT catch infinite loops.
180 *
181 * @since 2.1.0
182 * @since 2.6.0 Moved from `WP_Scripts`.
183 * @since 2.8.0 Added the `$group` parameter.
184 *
185 * @param string|string[] $handles Item handle (string) or item handles (array of strings).
186 * @param bool $recursion Optional. Internal flag that function is calling itself.
187 * Default false.
188 * @param int|false $group Optional. Group level: level (int), no group (false).
189 * Default false.
190 * @return bool True on success, false on failure.
191 */
192 public function all_deps( $handles, $recursion = false, $group = false ) {
193 $handles = (array) $handles;
194 if ( ! $handles ) {
195 return false;
196 }
197
198 foreach ( $handles as $handle ) {
199 $handle_parts = explode( '?', $handle );
200 $handle = $handle_parts[0];
201 $queued = in_array( $handle, $this->to_do, true );
202
203 if ( in_array( $handle, $this->done, true ) ) { // Already done.
204 continue;
205 }
206
207 $moved = $this->set_group( $handle, $recursion, $group );
208 $new_group = $this->groups[ $handle ];
209
210 if ( $queued && ! $moved ) { // Already queued and in the right group.
211 continue;
212 }
213
214 $keep_going = true;
215 $missing_dependencies = array();
216 if ( isset( $this->registered[ $handle ] ) && count( $this->registered[ $handle ]->deps ) > 0 ) {
217 $missing_dependencies = array_diff( $this->registered[ $handle ]->deps, array_keys( $this->registered ) );
218 }
219 if ( ! isset( $this->registered[ $handle ] ) ) {
220 $keep_going = false; // Item doesn't exist.
221 } elseif ( count( $missing_dependencies ) > 0 ) {
222 if ( ! in_array( $handle, $this->dependencies_with_missing_dependencies, true ) ) {
223 _doing_it_wrong(
224 get_class( $this ) . '::add',
225 $this->get_dependency_warning_message( $handle, $missing_dependencies ),
226 '6.9.1'
227 );
228 $this->dependencies_with_missing_dependencies[] = $handle;
229 }
230 $keep_going = false; // Item requires dependencies that don't exist.
231 } elseif ( $this->registered[ $handle ]->deps && ! $this->all_deps( $this->registered[ $handle ]->deps, true, $new_group ) ) {
232 $keep_going = false; // Item requires dependencies that don't exist.
233 }
234
235 if ( ! $keep_going ) { // Either item or its dependencies don't exist.
236 if ( $recursion ) {
237 return false; // Abort this branch.
238 } else {
239 continue; // We're at the top level. Move on to the next one.
240 }
241 }
242
243 if ( $queued ) { // Already grabbed it and its dependencies.
244 continue;
245 }
246
247 if ( isset( $handle_parts[1] ) ) {
248 $this->args[ $handle ] = $handle_parts[1];
249 }
250
251 $this->to_do[] = $handle;
252 }
253
254 return true;
255 }
256
257 /**
258 * Register an item.
259 *
260 * Registers the item if no item of that name already exists.
261 *
262 * @since 2.1.0
263 * @since 2.6.0 Moved from `WP_Scripts`.
264 *
265 * @param string $handle Name of the item. Should be unique.
266 * @param string|false $src Full URL of the item, or path of the item relative
267 * to the WordPress root directory. If source is set to false,
268 * the item is an alias of other items it depends on.
269 * @param string[] $deps Optional. An array of registered item handles this item depends on.
270 * Default empty array.
271 * @param string|bool|null $ver Optional. String specifying item version number, if it has one,
272 * which is added to the URL as a query string for cache busting purposes.
273 * If version is set to false, a version number is automatically added
274 * equal to current installed WordPress version.
275 * If set to null, no version is added.
276 * @param mixed $args Optional. Custom property of the item. NOT the class property $args.
277 * Examples: $media, $in_footer.
278 * @return bool Whether the item has been registered. True on success, false on failure.
279 */
280 public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
281 if ( isset( $this->registered[ $handle ] ) ) {
282 return false;
283 }
284 $this->registered[ $handle ] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
285
286 // If the item was enqueued before the details were registered, enqueue it now.
287 if ( array_key_exists( $handle, $this->queued_before_register ) ) {
288 if ( ! is_null( $this->queued_before_register[ $handle ] ) ) {
289 $this->enqueue( $handle . '?' . $this->queued_before_register[ $handle ] );
290 } else {
291 $this->enqueue( $handle );
292 }
293
294 unset( $this->queued_before_register[ $handle ] );
295 }
296
297 return true;
298 }
299
300 /**
301 * Add extra item data.
302 *
303 * Adds data to a registered item.
304 *
305 * @since 2.6.0
306 *
307 * @param string $handle Name of the item. Should be unique.
308 * @param string $key The data key.
309 * @param mixed $value The data value.
310 * @return bool True on success, false on failure.
311 */
312 public function add_data( $handle, $key, $value ) {
313 if ( ! isset( $this->registered[ $handle ] ) ) {
314 return false;
315 }
316 if ( 'conditional' === $key && '_required-conditional-dependency_' !== $value ) {
317 _deprecated_argument(
318 'WP_Dependencies->add_data()',
319 '6.9.0',
320 __( 'IE conditional comments are ignored by all supported browsers.' )
321 );
322 }
323
324 return $this->registered[ $handle ]->add_data( $key, $value );
325 }
326
327 /**
328 * Get extra item data.
329 *
330 * Gets data associated with a registered item.
331 *
332 * @since 3.3.0
333 *
334 * @param string $handle Name of the item. Should be unique.
335 * @param string $key The data key.
336 * @return mixed Extra item data (string), false otherwise.
337 */
338 public function get_data( $handle, $key ) {
339 if ( ! isset( $this->registered[ $handle ] ) ) {
340 return false;
341 }
342
343 if ( ! isset( $this->registered[ $handle ]->extra[ $key ] ) ) {
344 return false;
345 }
346
347 return $this->registered[ $handle ]->extra[ $key ];
348 }
349
350 /**
351 * Un-register an item or items.
352 *
353 * @since 2.1.0
354 * @since 2.6.0 Moved from `WP_Scripts`.
355 *
356 * @param string|string[] $handles Item handle (string) or item handles (array of strings).
357 */
358 public function remove( $handles ) {
359 foreach ( (array) $handles as $handle ) {
360 unset( $this->registered[ $handle ] );
361 }
362 }
363
364 /**
365 * Queue an item or items.
366 *
367 * Decodes handles and arguments, then queues handles and stores
368 * arguments in the class property $args. For example in extending
369 * classes, $args is appended to the item url as a query string.
370 * Note $args is NOT the $args property of items in the $registered array.
371 *
372 * @since 2.1.0
373 * @since 2.6.0 Moved from `WP_Scripts`.
374 *
375 * @param string|string[] $handles Item handle (string) or item handles (array of strings).
376 */
377 public function enqueue( $handles ) {
378 foreach ( (array) $handles as $handle ) {
379 $handle = explode( '?', $handle );
380
381 if ( ! in_array( $handle[0], $this->queue, true ) && isset( $this->registered[ $handle[0] ] ) ) {
382 $this->queue[] = $handle[0];
383
384 // Reset all dependencies so they must be recalculated in recurse_deps().
385 $this->all_queued_deps = null;
386
387 if ( isset( $handle[1] ) ) {
388 $this->args[ $handle[0] ] = $handle[1];
389 }
390 } elseif ( ! isset( $this->registered[ $handle[0] ] ) ) {
391 $this->queued_before_register[ $handle[0] ] = null; // $args
392
393 if ( isset( $handle[1] ) ) {
394 $this->queued_before_register[ $handle[0] ] = $handle[1];
395 }
396 }
397 }
398 }
399
400 /**
401 * Dequeue an item or items.
402 *
403 * Decodes handles and arguments, then dequeues handles
404 * and removes arguments from the class property $args.
405 *
406 * @since 2.1.0
407 * @since 2.6.0 Moved from `WP_Scripts`.
408 *
409 * @param string|string[] $handles Item handle (string) or item handles (array of strings).
410 */
411 public function dequeue( $handles ) {
412 foreach ( (array) $handles as $handle ) {
413 $handle = explode( '?', $handle );
414 $key = array_search( $handle[0], $this->queue, true );
415
416 if ( false !== $key ) {
417 // Reset all dependencies so they must be recalculated in recurse_deps().
418 $this->all_queued_deps = null;
419
420 unset( $this->queue[ $key ] );
421 unset( $this->args[ $handle[0] ] );
422 } elseif ( array_key_exists( $handle[0], $this->queued_before_register ) ) {
423 unset( $this->queued_before_register[ $handle[0] ] );
424 }
425 }
426 }
427
428 /**
429 * Recursively search the passed dependency tree for a handle.
430 *
431 * @since 4.0.0
432 *
433 * @param string[] $queue An array of queued _WP_Dependency handles.
434 * @param string $handle Name of the item. Should be unique.
435 * @return bool Whether the handle is found after recursively searching the dependency tree.
436 */
437 protected function recurse_deps( $queue, $handle ) {
438 if ( isset( $this->all_queued_deps ) ) {
439 return isset( $this->all_queued_deps[ $handle ] );
440 }
441
442 $all_deps = array_fill_keys( $queue, true );
443 $queues = array();
444 $done = array();
445
446 while ( $queue ) {
447 foreach ( $queue as $queued ) {
448 if ( ! isset( $done[ $queued ] ) && isset( $this->registered[ $queued ] ) ) {
449 $deps = $this->registered[ $queued ]->deps;
450 if ( $deps ) {
451 $all_deps += array_fill_keys( $deps, true );
452 array_push( $queues, $deps );
453 }
454 $done[ $queued ] = true;
455 }
456 }
457 $queue = array_pop( $queues );
458 }
459
460 $this->all_queued_deps = $all_deps;
461
462 return isset( $this->all_queued_deps[ $handle ] );
463 }
464
465 /**
466 * Query the list for an item.
467 *
468 * @since 2.1.0
469 * @since 2.6.0 Moved from `WP_Scripts`.
470 *
471 * @param string $handle Name of the item. Should be unique.
472 * @param string $status Optional. Status of the item to query. Default 'registered'.
473 * @return bool|_WP_Dependency Found, or object Item data.
474 */
475 public function query( $handle, $status = 'registered' ) {
476 switch ( $status ) {
477 case 'registered':
478 case 'scripts': // Back compat.
479 if ( isset( $this->registered[ $handle ] ) ) {
480 return $this->registered[ $handle ];
481 }
482 return false;
483
484 case 'enqueued':
485 case 'queue': // Back compat.
486 if ( in_array( $handle, $this->queue, true ) ) {
487 return true;
488 }
489 return $this->recurse_deps( $this->queue, $handle );
490
491 case 'to_do':
492 case 'to_print': // Back compat.
493 return in_array( $handle, $this->to_do, true );
494
495 case 'done':
496 case 'printed': // Back compat.
497 return in_array( $handle, $this->done, true );
498 }
499
500 return false;
501 }
502
503 /**
504 * Set item group, unless already in a lower group.
505 *
506 * @since 2.8.0
507 *
508 * @param string $handle Name of the item. Should be unique.
509 * @param bool $recursion Internal flag that calling function was called recursively.
510 * @param int|false $group Group level: level (int), no group (false).
511 * @return bool Not already in the group or a lower group.
512 */
513 public function set_group( $handle, $recursion, $group ) {
514 $group = (int) $group;
515
516 if ( isset( $this->groups[ $handle ] ) && $this->groups[ $handle ] <= $group ) {
517 return false;
518 }
519
520 $this->groups[ $handle ] = $group;
521
522 return true;
523 }
524
525 /**
526 * Get etag header for cache validation.
527 *
528 * @since 6.7.0
529 *
530 * @global string $wp_version The WordPress version string.
531 *
532 * @param string[] $load Array of script or style handles to load.
533 * @return string Etag header.
534 */
535 public function get_etag( $load ) {
536 /*
537 * Note: wp_get_wp_version() is not used here, as this file can be included
538 * via wp-admin/load-scripts.php or wp-admin/load-styles.php, in which case
539 * wp-includes/functions.php is not loaded.
540 */
541 global $wp_version;
542
543 $etag = "WP:{$wp_version};";
544
545 foreach ( $load as $handle ) {
546 if ( ! array_key_exists( $handle, $this->registered ) ) {
547 continue;
548 }
549
550 $ver = $this->registered[ $handle ]->ver ?? $wp_version;
551 $etag .= "{$handle}:{$ver};";
552 }
553
554 /*
555 * This is not intended to be cryptographically secure, just a fast way to get
556 * a fixed length string based on the script versions. As this file does not
557 * load the full WordPress environment, it is not possible to use the salted
558 * wp_hash() function.
559 */
560 return 'W/"' . md5( $etag ) . '"';
561 }
562
563 /**
564 * Gets a dependency warning message for a handle.
565 *
566 * @since 6.9.1
567 *
568 * @param string $handle Handle with missing dependencies.
569 * @param string[] $missing_dependency_handles Missing dependency handles.
570 * @return string Formatted, localized warning message.
571 */
572 protected function get_dependency_warning_message( $handle, $missing_dependency_handles ) {
573 return sprintf(
574 /* translators: 1: Handle, 2: List of missing dependency handles. */
575 __( 'The handle "%1$s" was enqueued with dependencies that are not registered: %2$s.' ),
576 $handle,
577 implode( wp_get_list_item_separator(), $missing_dependency_handles )
578 );
579 }
580}
581