PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/VTTE/sitios-vtte
PHP | 449 lines | 173 code | 52 blank | 224 comment | 33 complexity | f0141689a554ded8c4deeef407fcae22 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 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. *
  119. * @param string $handle Name of the item. Should be unique.
  120. * @return bool True on success, false if not set.
  121. */
  122. public function do_item( $handle ) {
  123. return isset( $this->registered[ $handle ] );
  124. }
  125. /**
  126. * Determines dependencies.
  127. *
  128. * Recursively builds an array of items to process taking
  129. * dependencies into account. Does NOT catch infinite loops.
  130. *
  131. * @since 2.1.0
  132. * @since 2.6.0 Moved from `WP_Scripts`.
  133. * @since 2.8.0 Added the `$group` parameter.
  134. *
  135. * @param string|string[] $handles Item handle (string) or item handles (array of strings).
  136. * @param bool $recursion Optional. Internal flag that function is calling itself.
  137. * Default false.
  138. * @param int|false $group Optional. Group level: level (int), no groups (false).
  139. * Default false.
  140. * @return bool True on success, false on failure.
  141. */
  142. public function all_deps( $handles, $recursion = false, $group = false ) {
  143. $handles = (array) $handles;
  144. if ( ! $handles ) {
  145. return false;
  146. }
  147. foreach ( $handles as $handle ) {
  148. $handle_parts = explode( '?', $handle );
  149. $handle = $handle_parts[0];
  150. $queued = in_array( $handle, $this->to_do, true );
  151. if ( in_array( $handle, $this->done, true ) ) { // Already done.
  152. continue;
  153. }
  154. $moved = $this->set_group( $handle, $recursion, $group );
  155. $new_group = $this->groups[ $handle ];
  156. if ( $queued && ! $moved ) { // Already queued and in the right group.
  157. continue;
  158. }
  159. $keep_going = true;
  160. if ( ! isset( $this->registered[ $handle ] ) ) {
  161. $keep_going = false; // Item doesn't exist.
  162. } elseif ( $this->registered[ $handle ]->deps && array_diff( $this->registered[ $handle ]->deps, array_keys( $this->registered ) ) ) {
  163. $keep_going = false; // Item requires dependencies that don't exist.
  164. } elseif ( $this->registered[ $handle ]->deps && ! $this->all_deps( $this->registered[ $handle ]->deps, true, $new_group ) ) {
  165. $keep_going = false; // Item requires dependencies that don't exist.
  166. }
  167. if ( ! $keep_going ) { // Either item or its dependencies don't exist.
  168. if ( $recursion ) {
  169. return false; // Abort this branch.
  170. } else {
  171. continue; // We're at the top level. Move on to the next one.
  172. }
  173. }
  174. if ( $queued ) { // Already grabbed it and its dependencies.
  175. continue;
  176. }
  177. if ( isset( $handle_parts[1] ) ) {
  178. $this->args[ $handle ] = $handle_parts[1];
  179. }
  180. $this->to_do[] = $handle;
  181. }
  182. return true;
  183. }
  184. /**
  185. * Register an item.
  186. *
  187. * Registers the item if no item of that name already exists.
  188. *
  189. * @since 2.1.0
  190. * @since 2.6.0 Moved from `WP_Scripts`.
  191. *
  192. * @param string $handle Name of the item. Should be unique.
  193. * @param string|bool $src Full URL of the item, or path of the item relative
  194. * to the WordPress root directory. If source is set to false,
  195. * item is an alias of other items it depends on.
  196. * @param string[] $deps Optional. An array of registered item handles this item depends on.
  197. * Default empty array.
  198. * @param string|bool|null $ver Optional. String specifying item version number, if it has one,
  199. * which is added to the URL as a query string for cache busting purposes.
  200. * If version is set to false, a version number is automatically added
  201. * equal to current installed WordPress version.
  202. * If set to null, no version is added.
  203. * @param mixed $args Optional. Custom property of the item. NOT the class property $args.
  204. * Examples: $media, $in_footer.
  205. * @return bool Whether the item has been registered. True on success, false on failure.
  206. */
  207. public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
  208. if ( isset( $this->registered[ $handle ] ) ) {
  209. return false;
  210. }
  211. $this->registered[ $handle ] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
  212. return true;
  213. }
  214. /**
  215. * Add extra item data.
  216. *
  217. * Adds data to a registered item.
  218. *
  219. * @since 2.6.0
  220. *
  221. * @param string $handle Name of the item. Should be unique.
  222. * @param string $key The data key.
  223. * @param mixed $value The data value.
  224. * @return bool True on success, false on failure.
  225. */
  226. public function add_data( $handle, $key, $value ) {
  227. if ( ! isset( $this->registered[ $handle ] ) ) {
  228. return false;
  229. }
  230. return $this->registered[ $handle ]->add_data( $key, $value );
  231. }
  232. /**
  233. * Get extra item data.
  234. *
  235. * Gets data associated with a registered item.
  236. *
  237. * @since 3.3.0
  238. *
  239. * @param string $handle Name of the item. Should be unique.
  240. * @param string $key The data key.
  241. * @return mixed Extra item data (string), false otherwise.
  242. */
  243. public function get_data( $handle, $key ) {
  244. if ( ! isset( $this->registered[ $handle ] ) ) {
  245. return false;
  246. }
  247. if ( ! isset( $this->registered[ $handle ]->extra[ $key ] ) ) {
  248. return false;
  249. }
  250. return $this->registered[ $handle ]->extra[ $key ];
  251. }
  252. /**
  253. * Un-register an item or items.
  254. *
  255. * @since 2.1.0
  256. * @since 2.6.0 Moved from `WP_Scripts`.
  257. *
  258. * @param string|string[] $handles Item handle (string) or item handles (array of strings).
  259. */
  260. public function remove( $handles ) {
  261. foreach ( (array) $handles as $handle ) {
  262. unset( $this->registered[ $handle ] );
  263. }
  264. }
  265. /**
  266. * Queue an item or items.
  267. *
  268. * Decodes handles and arguments, then queues handles and stores
  269. * arguments in the class property $args. For example in extending
  270. * classes, $args is appended to the item url as a query string.
  271. * Note $args is NOT the $args property of items in the $registered array.
  272. *
  273. * @since 2.1.0
  274. * @since 2.6.0 Moved from `WP_Scripts`.
  275. *
  276. * @param string|string[] $handles Item handle (string) or item handles (array of strings).
  277. */
  278. public function enqueue( $handles ) {
  279. foreach ( (array) $handles as $handle ) {
  280. $handle = explode( '?', $handle );
  281. if ( ! in_array( $handle[0], $this->queue, true ) && isset( $this->registered[ $handle[0] ] ) ) {
  282. $this->queue[] = $handle[0];
  283. // Reset all dependencies so they must be recalculated in recurse_deps().
  284. $this->all_queued_deps = null;
  285. if ( isset( $handle[1] ) ) {
  286. $this->args[ $handle[0] ] = $handle[1];
  287. }
  288. }
  289. }
  290. }
  291. /**
  292. * Dequeue an item or items.
  293. *
  294. * Decodes handles and arguments, then dequeues handles
  295. * and removes arguments from the class property $args.
  296. *
  297. * @since 2.1.0
  298. * @since 2.6.0 Moved from `WP_Scripts`.
  299. *
  300. * @param string|string[] $handles Item handle (string) or item handles (array of strings).
  301. */
  302. public function dequeue( $handles ) {
  303. foreach ( (array) $handles as $handle ) {
  304. $handle = explode( '?', $handle );
  305. $key = array_search( $handle[0], $this->queue, true );
  306. if ( false !== $key ) {
  307. // Reset all dependencies so they must be recalculated in recurse_deps().
  308. $this->all_queued_deps = null;
  309. unset( $this->queue[ $key ] );
  310. unset( $this->args[ $handle[0] ] );
  311. }
  312. }
  313. }
  314. /**
  315. * Recursively search the passed dependency tree for $handle.
  316. *
  317. * @since 4.0.0
  318. *
  319. * @param string[] $queue An array of queued _WP_Dependency handles.
  320. * @param string $handle Name of the item. Should be unique.
  321. * @return bool Whether the handle is found after recursively searching the dependency tree.
  322. */
  323. protected function recurse_deps( $queue, $handle ) {
  324. if ( isset( $this->all_queued_deps ) ) {
  325. return isset( $this->all_queued_deps[ $handle ] );
  326. }
  327. $all_deps = array_fill_keys( $queue, true );
  328. $queues = array();
  329. $done = array();
  330. while ( $queue ) {
  331. foreach ( $queue as $queued ) {
  332. if ( ! isset( $done[ $queued ] ) && isset( $this->registered[ $queued ] ) ) {
  333. $deps = $this->registered[ $queued ]->deps;
  334. if ( $deps ) {
  335. $all_deps += array_fill_keys( $deps, true );
  336. array_push( $queues, $deps );
  337. }
  338. $done[ $queued ] = true;
  339. }
  340. }
  341. $queue = array_pop( $queues );
  342. }
  343. $this->all_queued_deps = $all_deps;
  344. return isset( $this->all_queued_deps[ $handle ] );
  345. }
  346. /**
  347. * Query list for an item.
  348. *
  349. * @since 2.1.0
  350. * @since 2.6.0 Moved from `WP_Scripts`.
  351. *
  352. * @param string $handle Name of the item. Should be unique.
  353. * @param string $list Optional. Property name of list array. Default 'registered'.
  354. * @return bool|_WP_Dependency Found, or object Item data.
  355. */
  356. public function query( $handle, $list = 'registered' ) {
  357. switch ( $list ) {
  358. case 'registered':
  359. case 'scripts': // Back compat.
  360. if ( isset( $this->registered[ $handle ] ) ) {
  361. return $this->registered[ $handle ];
  362. }
  363. return false;
  364. case 'enqueued':
  365. case 'queue':
  366. if ( in_array( $handle, $this->queue, true ) ) {
  367. return true;
  368. }
  369. return $this->recurse_deps( $this->queue, $handle );
  370. case 'to_do':
  371. case 'to_print': // Back compat.
  372. return in_array( $handle, $this->to_do, true );
  373. case 'done':
  374. case 'printed': // Back compat.
  375. return in_array( $handle, $this->done, true );
  376. }
  377. return false;
  378. }
  379. /**
  380. * Set item group, unless already in a lower group.
  381. *
  382. * @since 2.8.0
  383. *
  384. * @param string $handle Name of the item. Should be unique.
  385. * @param bool $recursion Internal flag that calling function was called recursively.
  386. * @param int|false $group Group level: level (int), no groups (false).
  387. * @return bool Not already in the group or a lower group.
  388. */
  389. public function set_group( $handle, $recursion, $group ) {
  390. $group = (int) $group;
  391. if ( isset( $this->groups[ $handle ] ) && $this->groups[ $handle ] <= $group ) {
  392. return false;
  393. }
  394. $this->groups[ $handle ] = $group;
  395. return true;
  396. }
  397. }