PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class.wp-dependencies.php

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