2008-05-21 01:56:04 -04:00
|
|
|
<?php
|
2008-09-27 06:06:18 -04:00
|
|
|
/**
|
2016-02-27 15:34:29 -05:00
|
|
|
* Dependencies API: WP_Dependencies base class
|
2008-09-27 06:06:18 -04:00
|
|
|
*
|
2016-02-27 15:34:29 -05:00
|
|
|
* @since 2.6.0
|
2008-09-27 06:06:18 -04:00
|
|
|
*
|
2016-02-27 15:34:29 -05:00
|
|
|
* @package WordPress
|
|
|
|
* @subpackage Dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core base class extended to register items.
|
2008-09-27 06:06:18 -04:00
|
|
|
*
|
2016-02-27 15:34:29 -05:00
|
|
|
* @since 2.6.0
|
2017-07-01 12:58:42 -04:00
|
|
|
*
|
|
|
|
* @see _WP_Dependency
|
2008-09-27 06:06:18 -04:00
|
|
|
*/
|
2008-05-21 01:56:04 -04:00
|
|
|
class WP_Dependencies {
|
2013-09-20 13:44:17 -04:00
|
|
|
/**
|
2021-09-22 17:23:00 -04:00
|
|
|
* An array of all registered dependencies keyed by handle.
|
2013-09-20 13:44:17 -04:00
|
|
|
*
|
|
|
|
* @since 2.6.8
|
2021-09-22 17:23:00 -04:00
|
|
|
*
|
|
|
|
* @var _WP_Dependency[]
|
2013-09-20 13:44:17 -04:00
|
|
|
*/
|
2014-05-19 02:14:14 -04:00
|
|
|
public $registered = array();
|
2013-09-20 13:44:17 -04:00
|
|
|
|
|
|
|
/**
|
2021-09-22 17:23:00 -04:00
|
|
|
* An array of handles of queued dependencies.
|
2013-09-20 13:44:17 -04:00
|
|
|
*
|
|
|
|
* @since 2.6.8
|
2021-09-22 17:23:00 -04:00
|
|
|
*
|
2019-11-05 16:27:02 -05:00
|
|
|
* @var string[]
|
2013-09-20 13:44:17 -04:00
|
|
|
*/
|
2014-05-19 02:14:14 -04:00
|
|
|
public $queue = array();
|
2013-09-20 13:44:17 -04:00
|
|
|
|
|
|
|
/**
|
2021-09-22 17:23:00 -04:00
|
|
|
* An array of handles of dependencies to queue.
|
2013-09-20 13:44:17 -04:00
|
|
|
*
|
|
|
|
* @since 2.6.0
|
2021-09-22 17:23:00 -04:00
|
|
|
*
|
2019-11-05 16:27:02 -05:00
|
|
|
* @var string[]
|
2013-09-20 13:44:17 -04:00
|
|
|
*/
|
2014-05-19 02:14:14 -04:00
|
|
|
public $to_do = array();
|
2013-09-20 13:44:17 -04:00
|
|
|
|
|
|
|
/**
|
2021-09-22 17:23:00 -04:00
|
|
|
* An array of handles of dependencies already queued.
|
2013-09-20 13:44:17 -04:00
|
|
|
*
|
|
|
|
* @since 2.6.0
|
2021-09-22 17:23:00 -04:00
|
|
|
*
|
2019-11-05 16:27:02 -05:00
|
|
|
* @var string[]
|
2013-09-20 13:44:17 -04:00
|
|
|
*/
|
2014-05-19 02:14:14 -04:00
|
|
|
public $done = array();
|
2013-09-20 13:44:17 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of additional arguments passed when a handle is registered.
|
|
|
|
*
|
|
|
|
* Arguments are appended to the item query string.
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
2021-09-22 17:23:00 -04:00
|
|
|
*
|
2013-09-20 13:44:17 -04:00
|
|
|
* @var array
|
|
|
|
*/
|
2014-05-19 02:14:14 -04:00
|
|
|
public $args = array();
|
2013-09-20 13:44:17 -04:00
|
|
|
|
|
|
|
/**
|
2021-09-22 17:23:00 -04:00
|
|
|
* An array of dependency groups to enqueue.
|
|
|
|
*
|
|
|
|
* Each entry is keyed by handle and represents the integer group level or boolean
|
|
|
|
* false if the handle has no group.
|
2013-09-20 13:44:17 -04:00
|
|
|
*
|
|
|
|
* @since 2.8.0
|
2021-09-22 17:23:00 -04:00
|
|
|
*
|
|
|
|
* @var (int|false)[]
|
2013-09-20 13:44:17 -04:00
|
|
|
*/
|
2014-05-19 02:14:14 -04:00
|
|
|
public $groups = array();
|
2013-09-20 13:44:17 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A handle group to enqueue.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
2021-09-22 17:23:00 -04:00
|
|
|
*
|
2016-03-06 14:50:27 -05:00
|
|
|
* @deprecated 4.5.0
|
2013-09-20 13:44:17 -04:00
|
|
|
* @var int
|
|
|
|
*/
|
2014-05-19 02:14:14 -04:00
|
|
|
public $group = 0;
|
2008-05-21 01:56:04 -04:00
|
|
|
|
2020-02-25 08:42:05 -05:00
|
|
|
/**
|
|
|
|
* Cached lookup array of flattened queued items and dependencies.
|
|
|
|
*
|
|
|
|
* @since 5.4.0
|
2021-09-22 17:23:00 -04:00
|
|
|
*
|
2020-02-25 08:42:05 -05:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $all_queued_deps;
|
|
|
|
|
2021-12-07 12:46:01 -05:00
|
|
|
/**
|
|
|
|
* List of assets enqueued before details were registered.
|
|
|
|
*
|
|
|
|
* @since 5.9.0
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $queued_before_register = array();
|
|
|
|
|
2008-05-21 01:56:04 -04:00
|
|
|
/**
|
2016-02-27 15:34:29 -05:00
|
|
|
* Processes the items and dependencies.
|
2013-09-20 15:54:10 -04:00
|
|
|
*
|
|
|
|
* Processes the items passed to it or the queue, and their dependencies.
|
2008-05-21 01:56:04 -04:00
|
|
|
*
|
2016-02-26 08:23:25 -05:00
|
|
|
* @since 2.6.0
|
2016-02-26 08:50:25 -05:00
|
|
|
* @since 2.8.0 Added the `$group` parameter.
|
2008-05-21 01:56:04 -04:00
|
|
|
*
|
2020-02-02 19:19:03 -05:00
|
|
|
* @param string|string[]|false $handles Optional. Items to be processed: queue (false),
|
|
|
|
* single item (string), or multiple items (array of strings).
|
|
|
|
* Default false.
|
2021-09-22 17:23:00 -04:00
|
|
|
* @param int|false $group Optional. Group level: level (int), no group (false).
|
2019-11-05 16:27:02 -05:00
|
|
|
* @return string[] Array of handles of items that have been processed.
|
2008-05-21 01:56:04 -04:00
|
|
|
*/
|
2013-09-20 15:54:10 -04:00
|
|
|
public function do_items( $handles = false, $group = false ) {
|
2014-11-23 23:58:22 -05:00
|
|
|
/*
|
2013-09-20 15:54:10 -04:00
|
|
|
* If nothing is passed, print the queue. If a string is passed,
|
|
|
|
* print that item. If an array is passed, print those items.
|
|
|
|
*/
|
2008-05-21 01:56:04 -04:00
|
|
|
$handles = false === $handles ? $this->queue : (array) $handles;
|
|
|
|
$this->all_deps( $handles );
|
|
|
|
|
2015-08-25 16:28:22 -04:00
|
|
|
foreach ( $this->to_do as $key => $handle ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! in_array( $handle, $this->done, true ) && isset( $this->registered[ $handle ] ) ) {
|
2014-11-23 23:58:22 -05:00
|
|
|
/*
|
2013-09-20 15:54:10 -04:00
|
|
|
* Attempt to process the item. If successful,
|
|
|
|
* add the handle to the done array.
|
|
|
|
*
|
|
|
|
* Unset the item from the to_do array.
|
|
|
|
*/
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $this->do_item( $handle, $group ) ) {
|
2009-01-14 09:18:51 -05:00
|
|
|
$this->done[] = $handle;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2009-01-14 09:18:51 -05:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
unset( $this->to_do[ $key ] );
|
2008-05-21 01:56:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->done;
|
|
|
|
}
|
|
|
|
|
2013-09-20 15:54:10 -04:00
|
|
|
/**
|
2016-02-27 15:34:29 -05:00
|
|
|
* Processes a dependency.
|
2013-09-20 15:54:10 -04:00
|
|
|
*
|
|
|
|
* @since 2.6.0
|
Script Loader: Add the `$group` parameter to `WP_Dependencies::do_item()`.
Previously, the method was called with two parameters in `::do_items()`, while the method signature only included one parameter.
Technically, this was not an issue as `WP_Dependencies::do_item()` is a placeholder meant to be overwritten when extending the class. When handling scripts, `WP_Dependencies` is extended with `WP_Scripts`, and the `$group` parameter was only used in `WP_Scripts::do_item()`, which does expect a second argument.
However, officially adding the parameter to `WP_Dependencies::do_item()` signature prevents code misunderstanding and avoids a warning in PHP code inspection tools.
Props kaggdesign, soulseekah, azaozz, SergeyBiryukov.
Fixes #43627.
Built from https://develop.svn.wordpress.org/trunk@47769
git-svn-id: http://core.svn.wordpress.org/trunk@47545 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-05-06 12:27:12 -04:00
|
|
|
* @since 5.5.0 Added the `$group` parameter.
|
2013-09-20 15:54:10 -04:00
|
|
|
*
|
Script Loader: Add the `$group` parameter to `WP_Dependencies::do_item()`.
Previously, the method was called with two parameters in `::do_items()`, while the method signature only included one parameter.
Technically, this was not an issue as `WP_Dependencies::do_item()` is a placeholder meant to be overwritten when extending the class. When handling scripts, `WP_Dependencies` is extended with `WP_Scripts`, and the `$group` parameter was only used in `WP_Scripts::do_item()`, which does expect a second argument.
However, officially adding the parameter to `WP_Dependencies::do_item()` signature prevents code misunderstanding and avoids a warning in PHP code inspection tools.
Props kaggdesign, soulseekah, azaozz, SergeyBiryukov.
Fixes #43627.
Built from https://develop.svn.wordpress.org/trunk@47769
git-svn-id: http://core.svn.wordpress.org/trunk@47545 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-05-06 12:27:12 -04:00
|
|
|
* @param string $handle Name of the item. Should be unique.
|
2021-09-22 17:23:00 -04:00
|
|
|
* @param int|false $group Optional. Group level: level (int), no group (false).
|
Script Loader: Add the `$group` parameter to `WP_Dependencies::do_item()`.
Previously, the method was called with two parameters in `::do_items()`, while the method signature only included one parameter.
Technically, this was not an issue as `WP_Dependencies::do_item()` is a placeholder meant to be overwritten when extending the class. When handling scripts, `WP_Dependencies` is extended with `WP_Scripts`, and the `$group` parameter was only used in `WP_Scripts::do_item()`, which does expect a second argument.
However, officially adding the parameter to `WP_Dependencies::do_item()` signature prevents code misunderstanding and avoids a warning in PHP code inspection tools.
Props kaggdesign, soulseekah, azaozz, SergeyBiryukov.
Fixes #43627.
Built from https://develop.svn.wordpress.org/trunk@47769
git-svn-id: http://core.svn.wordpress.org/trunk@47545 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-05-06 12:27:12 -04:00
|
|
|
* Default false.
|
2013-09-20 15:54:10 -04:00
|
|
|
* @return bool True on success, false if not set.
|
|
|
|
*/
|
Script Loader: Add the `$group` parameter to `WP_Dependencies::do_item()`.
Previously, the method was called with two parameters in `::do_items()`, while the method signature only included one parameter.
Technically, this was not an issue as `WP_Dependencies::do_item()` is a placeholder meant to be overwritten when extending the class. When handling scripts, `WP_Dependencies` is extended with `WP_Scripts`, and the `$group` parameter was only used in `WP_Scripts::do_item()`, which does expect a second argument.
However, officially adding the parameter to `WP_Dependencies::do_item()` signature prevents code misunderstanding and avoids a warning in PHP code inspection tools.
Props kaggdesign, soulseekah, azaozz, SergeyBiryukov.
Fixes #43627.
Built from https://develop.svn.wordpress.org/trunk@47769
git-svn-id: http://core.svn.wordpress.org/trunk@47545 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2020-05-06 12:27:12 -04:00
|
|
|
public function do_item( $handle, $group = false ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
return isset( $this->registered[ $handle ] );
|
2008-05-21 01:56:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-27 15:34:29 -05:00
|
|
|
* Determines dependencies.
|
2008-05-21 01:56:04 -04:00
|
|
|
*
|
2013-09-20 15:54:10 -04:00
|
|
|
* Recursively builds an array of items to process taking
|
|
|
|
* dependencies into account. Does NOT catch infinite loops.
|
2008-05-21 01:56:04 -04:00
|
|
|
*
|
2013-09-20 15:54:10 -04:00
|
|
|
* @since 2.1.0
|
2016-02-26 08:23:25 -05:00
|
|
|
* @since 2.6.0 Moved from `WP_Scripts`.
|
2016-02-26 08:50:25 -05:00
|
|
|
* @since 2.8.0 Added the `$group` parameter.
|
2010-11-18 14:12:48 -05:00
|
|
|
*
|
2020-02-02 19:19:03 -05:00
|
|
|
* @param string|string[] $handles Item handle (string) or item handles (array of strings).
|
|
|
|
* @param bool $recursion Optional. Internal flag that function is calling itself.
|
|
|
|
* Default false.
|
2021-09-22 17:23:00 -04:00
|
|
|
* @param int|false $group Optional. Group level: level (int), no group (false).
|
2020-02-02 19:19:03 -05:00
|
|
|
* Default false.
|
2013-09-20 15:54:10 -04:00
|
|
|
* @return bool True on success, false on failure.
|
2008-05-21 01:56:04 -04:00
|
|
|
*/
|
2013-09-20 15:54:10 -04:00
|
|
|
public function all_deps( $handles, $recursion = false, $group = false ) {
|
2019-07-02 19:42:58 -04:00
|
|
|
$handles = (array) $handles;
|
|
|
|
if ( ! $handles ) {
|
2008-05-21 01:56:04 -04:00
|
|
|
return false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2008-05-21 01:56:04 -04:00
|
|
|
|
|
|
|
foreach ( $handles as $handle ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$handle_parts = explode( '?', $handle );
|
|
|
|
$handle = $handle_parts[0];
|
|
|
|
$queued = in_array( $handle, $this->to_do, true );
|
2008-05-21 01:56:04 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
if ( in_array( $handle, $this->done, true ) ) { // Already done.
|
2008-05-21 01:56:04 -04:00
|
|
|
continue;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2008-05-21 01:56:04 -04:00
|
|
|
|
2016-03-06 14:50:27 -05:00
|
|
|
$moved = $this->set_group( $handle, $recursion, $group );
|
|
|
|
$new_group = $this->groups[ $handle ];
|
2009-05-24 19:47:49 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
if ( $queued && ! $moved ) { // Already queued and in the right group.
|
2009-01-14 09:18:51 -05:00
|
|
|
continue;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2009-01-14 09:18:51 -05:00
|
|
|
|
2008-05-21 01:56:04 -04:00
|
|
|
$keep_going = true;
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! isset( $this->registered[ $handle ] ) ) {
|
2013-09-20 15:54:10 -04:00
|
|
|
$keep_going = false; // Item doesn't exist.
|
2017-11-30 18:11:00 -05:00
|
|
|
} elseif ( $this->registered[ $handle ]->deps && array_diff( $this->registered[ $handle ]->deps, array_keys( $this->registered ) ) ) {
|
2013-09-20 15:54:10 -04:00
|
|
|
$keep_going = false; // Item requires dependencies that don't exist.
|
2017-11-30 18:11:00 -05:00
|
|
|
} elseif ( $this->registered[ $handle ]->deps && ! $this->all_deps( $this->registered[ $handle ]->deps, true, $new_group ) ) {
|
2013-09-20 15:54:10 -04:00
|
|
|
$keep_going = false; // Item requires dependencies that don't exist.
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2008-05-21 01:56:04 -04:00
|
|
|
|
2013-09-20 15:54:10 -04:00
|
|
|
if ( ! $keep_going ) { // Either item or its dependencies don't exist.
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $recursion ) {
|
2008-05-21 01:56:04 -04:00
|
|
|
return false; // Abort this branch.
|
2017-11-30 18:11:00 -05:00
|
|
|
} else {
|
2011-12-13 18:45:31 -05:00
|
|
|
continue; // We're at the top level. Move on to the next one.
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2008-08-09 01:36:14 -04:00
|
|
|
}
|
2008-05-21 01:56:04 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $queued ) { // Already grabbed it and its dependencies.
|
2009-04-22 17:01:36 -04:00
|
|
|
continue;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2009-04-22 17:01:36 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( isset( $handle_parts[1] ) ) {
|
|
|
|
$this->args[ $handle ] = $handle_parts[1];
|
|
|
|
}
|
2009-04-22 17:01:36 -04:00
|
|
|
|
2009-01-14 09:18:51 -05:00
|
|
|
$this->to_do[] = $handle;
|
2008-05-21 01:56:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-20 15:54:10 -04:00
|
|
|
* Register an item.
|
2008-05-21 01:56:04 -04:00
|
|
|
*
|
2013-09-20 15:54:10 -04:00
|
|
|
* Registers the item if no item of that name already exists.
|
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2016-02-26 08:23:25 -05:00
|
|
|
* @since 2.6.0 Moved from `WP_Scripts`.
|
2008-05-21 01:56:04 -04:00
|
|
|
*
|
2016-03-14 18:37:26 -04:00
|
|
|
* @param string $handle Name of the item. Should be unique.
|
2020-02-02 19:19:03 -05:00
|
|
|
* @param string|bool $src Full URL of the item, or path of the item relative
|
|
|
|
* to the WordPress root directory. If source is set to false,
|
|
|
|
* item is an alias of other items it depends on.
|
|
|
|
* @param string[] $deps Optional. An array of registered item handles this item depends on.
|
|
|
|
* Default empty array.
|
|
|
|
* @param string|bool|null $ver Optional. String specifying item version number, if it has one,
|
|
|
|
* which is added to the URL as a query string for cache busting purposes.
|
|
|
|
* If version is set to false, a version number is automatically added
|
|
|
|
* equal to current installed WordPress version.
|
2016-03-14 18:37:26 -04:00
|
|
|
* If set to null, no version is added.
|
2020-02-02 19:19:03 -05:00
|
|
|
* @param mixed $args Optional. Custom property of the item. NOT the class property $args.
|
|
|
|
* Examples: $media, $in_footer.
|
2015-05-10 15:57:25 -04:00
|
|
|
* @return bool Whether the item has been registered. True on success, false on failure.
|
2008-05-21 01:56:04 -04:00
|
|
|
*/
|
2013-09-20 15:54:10 -04:00
|
|
|
public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( isset( $this->registered[ $handle ] ) ) {
|
2008-05-21 01:56:04 -04:00
|
|
|
return false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
|
|
|
$this->registered[ $handle ] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
|
2021-12-07 12:46:01 -05:00
|
|
|
|
|
|
|
// If the item was enqueued before the details were registered, enqueue it now.
|
|
|
|
if ( array_key_exists( $handle, $this->queued_before_register ) ) {
|
|
|
|
if ( ! is_null( $this->queued_before_register[ $handle ] ) ) {
|
|
|
|
$this->enqueue( $handle . '?' . $this->queued_before_register[ $handle ] );
|
|
|
|
} else {
|
|
|
|
$this->enqueue( $handle );
|
|
|
|
}
|
|
|
|
|
|
|
|
unset( $this->queued_before_register[ $handle ] );
|
|
|
|
}
|
|
|
|
|
2008-05-21 01:56:04 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-20 15:54:10 -04:00
|
|
|
* Add extra item data.
|
2008-05-21 01:56:04 -04:00
|
|
|
*
|
2013-09-20 15:54:10 -04:00
|
|
|
* Adds data to a registered item.
|
2008-05-21 01:56:04 -04:00
|
|
|
*
|
2013-09-20 15:54:10 -04:00
|
|
|
* @since 2.6.0
|
|
|
|
*
|
|
|
|
* @param string $handle Name of the item. Should be unique.
|
|
|
|
* @param string $key The data key.
|
2020-03-24 16:56:09 -04:00
|
|
|
* @param mixed $value The data value.
|
2013-09-20 15:54:10 -04:00
|
|
|
* @return bool True on success, false on failure.
|
2008-05-21 01:56:04 -04:00
|
|
|
*/
|
2013-09-20 15:54:10 -04:00
|
|
|
public function add_data( $handle, $key, $value ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! isset( $this->registered[ $handle ] ) ) {
|
2008-05-21 01:56:04 -04:00
|
|
|
return false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2011-07-28 14:24:00 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
return $this->registered[ $handle ]->add_data( $key, $value );
|
2011-07-28 14:24:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-20 15:54:10 -04:00
|
|
|
* Get extra item data.
|
2011-07-28 14:24:00 -04:00
|
|
|
*
|
2013-09-20 15:54:10 -04:00
|
|
|
* Gets data associated with a registered item.
|
2011-07-28 14:24:00 -04:00
|
|
|
*
|
2013-09-20 15:54:10 -04:00
|
|
|
* @since 3.3.0
|
2011-07-28 14:24:00 -04:00
|
|
|
*
|
2013-09-20 15:54:10 -04:00
|
|
|
* @param string $handle Name of the item. Should be unique.
|
|
|
|
* @param string $key The data key.
|
2020-03-24 16:56:09 -04:00
|
|
|
* @return mixed Extra item data (string), false otherwise.
|
2011-07-28 14:24:00 -04:00
|
|
|
*/
|
2013-09-20 15:54:10 -04:00
|
|
|
public function get_data( $handle, $key ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! isset( $this->registered[ $handle ] ) ) {
|
2011-07-28 14:24:00 -04:00
|
|
|
return false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2011-07-28 14:24:00 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! isset( $this->registered[ $handle ]->extra[ $key ] ) ) {
|
2011-07-28 14:24:00 -04:00
|
|
|
return false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2011-07-28 14:24:00 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
return $this->registered[ $handle ]->extra[ $key ];
|
2008-05-21 01:56:04 -04:00
|
|
|
}
|
|
|
|
|
2013-09-20 15:54:10 -04:00
|
|
|
/**
|
|
|
|
* Un-register an item or items.
|
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2016-02-26 08:23:25 -05:00
|
|
|
* @since 2.6.0 Moved from `WP_Scripts`.
|
2013-09-20 15:54:10 -04:00
|
|
|
*
|
2020-02-02 19:19:03 -05:00
|
|
|
* @param string|string[] $handles Item handle (string) or item handles (array of strings).
|
2013-09-20 15:54:10 -04:00
|
|
|
*/
|
|
|
|
public function remove( $handles ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
foreach ( (array) $handles as $handle ) {
|
|
|
|
unset( $this->registered[ $handle ] );
|
|
|
|
}
|
2008-05-21 01:56:04 -04:00
|
|
|
}
|
|
|
|
|
2013-09-20 15:54:10 -04:00
|
|
|
/**
|
|
|
|
* Queue an item or items.
|
|
|
|
*
|
|
|
|
* Decodes handles and arguments, then queues handles and stores
|
|
|
|
* arguments in the class property $args. For example in extending
|
|
|
|
* classes, $args is appended to the item url as a query string.
|
|
|
|
* Note $args is NOT the $args property of items in the $registered array.
|
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2016-02-26 08:23:25 -05:00
|
|
|
* @since 2.6.0 Moved from `WP_Scripts`.
|
2013-09-20 15:54:10 -04:00
|
|
|
*
|
2020-02-02 19:19:03 -05:00
|
|
|
* @param string|string[] $handles Item handle (string) or item handles (array of strings).
|
2013-09-20 15:54:10 -04:00
|
|
|
*/
|
|
|
|
public function enqueue( $handles ) {
|
2008-05-21 01:56:04 -04:00
|
|
|
foreach ( (array) $handles as $handle ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$handle = explode( '?', $handle );
|
2020-02-25 08:42:05 -05:00
|
|
|
|
|
|
|
if ( ! in_array( $handle[0], $this->queue, true ) && isset( $this->registered[ $handle[0] ] ) ) {
|
2008-05-21 01:56:04 -04:00
|
|
|
$this->queue[] = $handle[0];
|
2020-02-25 08:42:05 -05:00
|
|
|
|
|
|
|
// Reset all dependencies so they must be recalculated in recurse_deps().
|
|
|
|
$this->all_queued_deps = null;
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( isset( $handle[1] ) ) {
|
|
|
|
$this->args[ $handle[0] ] = $handle[1];
|
|
|
|
}
|
2021-12-07 12:49:01 -05:00
|
|
|
} elseif ( ! isset( $this->registered[ $handle[0] ] ) ) {
|
2021-12-07 12:46:01 -05:00
|
|
|
$this->queued_before_register[ $handle[0] ] = null; // $args
|
|
|
|
|
|
|
|
if ( isset( $handle[1] ) ) {
|
|
|
|
$this->queued_before_register[ $handle[0] ] = $handle[1];
|
|
|
|
}
|
2008-05-21 01:56:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-20 15:54:10 -04:00
|
|
|
/**
|
|
|
|
* Dequeue an item or items.
|
|
|
|
*
|
|
|
|
* Decodes handles and arguments, then dequeues handles
|
|
|
|
* and removes arguments from the class property $args.
|
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2016-02-26 08:23:25 -05:00
|
|
|
* @since 2.6.0 Moved from `WP_Scripts`.
|
2013-09-20 15:54:10 -04:00
|
|
|
*
|
2020-02-02 19:19:03 -05:00
|
|
|
* @param string|string[] $handles Item handle (string) or item handles (array of strings).
|
2013-09-20 15:54:10 -04:00
|
|
|
*/
|
|
|
|
public function dequeue( $handles ) {
|
2009-10-18 01:51:30 -04:00
|
|
|
foreach ( (array) $handles as $handle ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$handle = explode( '?', $handle );
|
2020-02-25 08:42:05 -05:00
|
|
|
$key = array_search( $handle[0], $this->queue, true );
|
|
|
|
|
2009-10-18 01:51:30 -04:00
|
|
|
if ( false !== $key ) {
|
2020-02-25 08:42:05 -05:00
|
|
|
// Reset all dependencies so they must be recalculated in recurse_deps().
|
|
|
|
$this->all_queued_deps = null;
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
unset( $this->queue[ $key ] );
|
|
|
|
unset( $this->args[ $handle[0] ] );
|
2021-12-07 12:49:01 -05:00
|
|
|
} elseif ( array_key_exists( $handle[0], $this->queued_before_register ) ) {
|
2021-12-07 12:46:01 -05:00
|
|
|
unset( $this->queued_before_register[ $handle[0] ] );
|
2009-10-18 01:51:30 -04:00
|
|
|
}
|
|
|
|
}
|
2008-05-21 01:56:04 -04:00
|
|
|
}
|
|
|
|
|
2014-07-19 20:29:16 -04:00
|
|
|
/**
|
2021-09-22 17:23:00 -04:00
|
|
|
* Recursively search the passed dependency tree for a handle.
|
2014-07-19 20:29:16 -04:00
|
|
|
*
|
|
|
|
* @since 4.0.0
|
|
|
|
*
|
2018-03-25 15:33:31 -04:00
|
|
|
* @param string[] $queue An array of queued _WP_Dependency handles.
|
|
|
|
* @param string $handle Name of the item. Should be unique.
|
2015-06-26 21:03:25 -04:00
|
|
|
* @return bool Whether the handle is found after recursively searching the dependency tree.
|
2014-07-19 20:29:16 -04:00
|
|
|
*/
|
|
|
|
protected function recurse_deps( $queue, $handle ) {
|
2020-02-25 08:42:05 -05:00
|
|
|
if ( isset( $this->all_queued_deps ) ) {
|
|
|
|
return isset( $this->all_queued_deps[ $handle ] );
|
|
|
|
}
|
2014-07-19 20:29:16 -04:00
|
|
|
|
2020-02-25 08:42:05 -05:00
|
|
|
$all_deps = array_fill_keys( $queue, true );
|
|
|
|
$queues = array();
|
|
|
|
$done = array();
|
|
|
|
|
|
|
|
while ( $queue ) {
|
|
|
|
foreach ( $queue as $queued ) {
|
|
|
|
if ( ! isset( $done[ $queued ] ) && isset( $this->registered[ $queued ] ) ) {
|
|
|
|
$deps = $this->registered[ $queued ]->deps;
|
|
|
|
if ( $deps ) {
|
|
|
|
$all_deps += array_fill_keys( $deps, true );
|
|
|
|
array_push( $queues, $deps );
|
|
|
|
}
|
|
|
|
$done[ $queued ] = true;
|
|
|
|
}
|
2014-07-19 20:29:16 -04:00
|
|
|
}
|
2020-02-25 08:42:05 -05:00
|
|
|
$queue = array_pop( $queues );
|
2014-07-19 20:29:16 -04:00
|
|
|
}
|
|
|
|
|
2020-02-25 08:42:05 -05:00
|
|
|
$this->all_queued_deps = $all_deps;
|
|
|
|
|
|
|
|
return isset( $this->all_queued_deps[ $handle ] );
|
2014-07-19 20:29:16 -04:00
|
|
|
}
|
|
|
|
|
2013-09-20 15:54:10 -04:00
|
|
|
/**
|
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/class-wp-query.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$list` parameter to `$status` in `WP_Dependencies::query()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #55327.
Built from https://develop.svn.wordpress.org/trunk@53281
git-svn-id: http://core.svn.wordpress.org/trunk@52870 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-04-26 10:28:08 -04:00
|
|
|
* Query the list for an item.
|
2013-09-20 15:54:10 -04:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2016-02-26 08:23:25 -05:00
|
|
|
* @since 2.6.0 Moved from `WP_Scripts`.
|
2013-09-20 15:54:10 -04:00
|
|
|
*
|
|
|
|
* @param string $handle Name of the item. Should be unique.
|
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/class-wp-query.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$list` parameter to `$status` in `WP_Dependencies::query()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #55327.
Built from https://develop.svn.wordpress.org/trunk@53281
git-svn-id: http://core.svn.wordpress.org/trunk@52870 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-04-26 10:28:08 -04:00
|
|
|
* @param string $status Optional. Status of the item to query. Default 'registered'.
|
2016-02-25 16:26:26 -05:00
|
|
|
* @return bool|_WP_Dependency Found, or object Item data.
|
2013-09-20 15:54:10 -04:00
|
|
|
*/
|
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/class-wp-query.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$list` parameter to `$status` in `WP_Dependencies::query()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #55327.
Built from https://develop.svn.wordpress.org/trunk@53281
git-svn-id: http://core.svn.wordpress.org/trunk@52870 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-04-26 10:28:08 -04:00
|
|
|
public function query( $handle, $status = 'registered' ) {
|
|
|
|
switch ( $status ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
case 'registered':
|
2020-01-28 19:45:18 -05:00
|
|
|
case 'scripts': // Back compat.
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( isset( $this->registered[ $handle ] ) ) {
|
2012-08-30 14:57:57 -04:00
|
|
|
return $this->registered[ $handle ];
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2012-08-30 14:57:57 -04:00
|
|
|
return false;
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
case 'enqueued':
|
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/class-wp-query.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$list` parameter to `$status` in `WP_Dependencies::query()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #55327.
Built from https://develop.svn.wordpress.org/trunk@53281
git-svn-id: http://core.svn.wordpress.org/trunk@52870 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-04-26 10:28:08 -04:00
|
|
|
case 'queue': // Back compat.
|
2020-02-25 08:42:05 -05:00
|
|
|
if ( in_array( $handle, $this->queue, true ) ) {
|
2014-07-19 20:29:16 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return $this->recurse_deps( $this->queue, $handle );
|
2012-08-30 14:57:57 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
case 'to_do':
|
2020-01-28 19:45:18 -05:00
|
|
|
case 'to_print': // Back compat.
|
2020-02-25 08:42:05 -05:00
|
|
|
return in_array( $handle, $this->to_do, true );
|
2012-08-30 14:57:57 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
case 'done':
|
2020-01-28 19:45:18 -05:00
|
|
|
case 'printed': // Back compat.
|
2020-02-25 08:42:05 -05:00
|
|
|
return in_array( $handle, $this->done, true );
|
2012-08-30 14:57:57 -04:00
|
|
|
}
|
Code Modernization: Rename parameters that use reserved keywords in `wp-includes/class-wp-query.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit renames the `$list` parameter to `$status` in `WP_Dependencies::query()`.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #55327.
Built from https://develop.svn.wordpress.org/trunk@53281
git-svn-id: http://core.svn.wordpress.org/trunk@52870 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-04-26 10:28:08 -04:00
|
|
|
|
2008-05-21 01:56:04 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-20 15:54:10 -04:00
|
|
|
/**
|
|
|
|
* Set item group, unless already in a lower group.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*
|
2020-02-02 19:19:03 -05:00
|
|
|
* @param string $handle Name of the item. Should be unique.
|
|
|
|
* @param bool $recursion Internal flag that calling function was called recursively.
|
2021-09-22 17:23:00 -04:00
|
|
|
* @param int|false $group Group level: level (int), no group (false).
|
2020-02-02 19:19:03 -05:00
|
|
|
* @return bool Not already in the group or a lower group.
|
2013-09-20 15:54:10 -04:00
|
|
|
*/
|
|
|
|
public function set_group( $handle, $recursion, $group ) {
|
2009-01-14 09:18:51 -05:00
|
|
|
$group = (int) $group;
|
|
|
|
|
2016-03-06 14:50:27 -05:00
|
|
|
if ( isset( $this->groups[ $handle ] ) && $this->groups[ $handle ] <= $group ) {
|
|
|
|
return false;
|
2016-02-20 17:11:25 -05:00
|
|
|
}
|
|
|
|
|
2016-03-06 14:50:27 -05:00
|
|
|
$this->groups[ $handle ] = $group;
|
2009-01-14 09:18:51 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-26 14:06:39 -04:00
|
|
|
}
|