class.wp-dependencies.php 11 KB

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