Brak opisu

class.wp-dependencies.php 14KB

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