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

https://gitlab.com/morganestes/wordpress-develop · PHP · 411 lines · 159 code · 44 blank · 208 comment · 30 complexity · 3b1ec278655669edc750c0334f5e3256 MD5 · raw file

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