class-wp-dependencies.php 14 KB

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