class.wp-dependencies.php 11 KB

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