You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

415 lines
11 KiB

  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. * @package WordPress
  14. * @since 2.6.0
  15. * @uses _WP_Dependency
  16. */
  17. class WP_Dependencies {
  18. /**
  19. * An array of registered handle objects.
  20. *
  21. * @access public
  22. * @since 2.6.8
  23. * @var array
  24. */
  25. public $registered = array();
  26. /**
  27. * An array of queued _WP_Dependency handle objects.
  28. *
  29. * @access public
  30. * @since 2.6.8
  31. * @var array
  32. */
  33. public $queue = array();
  34. /**
  35. * An array of _WP_Dependency handle objects to queue.
  36. *
  37. * @access public
  38. * @since 2.6.0
  39. * @var array
  40. */
  41. public $to_do = array();
  42. /**
  43. * An array of _WP_Dependency handle objects already queued.
  44. *
  45. * @access public
  46. * @since 2.6.0
  47. * @var array
  48. */
  49. public $done = array();
  50. /**
  51. * An array of additional arguments passed when a handle is registered.
  52. *
  53. * Arguments are appended to the item query string.
  54. *
  55. * @access public
  56. * @since 2.6.0
  57. * @var array
  58. */
  59. public $args = array();
  60. /**
  61. * An array of handle groups to enqueue.
  62. *
  63. * @access public
  64. * @since 2.8.0
  65. * @var array
  66. */
  67. public $groups = array();
  68. /**
  69. * A handle group to enqueue.
  70. *
  71. * @access public
  72. * @since 2.8.0
  73. * @deprecated 4.5.0
  74. * @var int
  75. */
  76. public $group = 0;
  77. /**
  78. * Processes the items and dependencies.
  79. *
  80. * Processes the items passed to it or the queue, and their dependencies.
  81. *
  82. * @access public
  83. * @since 2.6.0
  84. * @since 2.8.0 Added the `$group` parameter.
  85. *
  86. * @param mixed $handles Optional. Items to be processed: Process queue (false), process item (string), process items (array of strings).
  87. * @param mixed $group Group level: level (int), no groups (false).
  88. * @return array Handles of items that have been processed.
  89. */
  90. public function do_items( $handles = false, $group = false ) {
  91. /*
  92. * If nothing is passed, print the queue. If a string is passed,
  93. * print that item. If an array is passed, print those items.
  94. */
  95. $handles = false === $handles ? $this->queue : (array) $handles;
  96. $this->all_deps( $handles );
  97. foreach ( $this->to_do as $key => $handle ) {
  98. if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) {
  99. /*
  100. * Attempt to process the item. If successful,
  101. * add the handle to the done array.
  102. *
  103. * Unset the item from the to_do array.
  104. */
  105. if ( $this->do_item( $handle, $group ) )
  106. $this->done[] = $handle;
  107. unset( $this->to_do[$key] );
  108. }
  109. }
  110. return $this->done;
  111. }
  112. /**
  113. * Processes a dependency.
  114. *
  115. * @access public
  116. * @since 2.6.0
  117. *
  118. * @param string $handle Name of the item. Should be unique.
  119. * @return bool True on success, false if not set.
  120. */
  121. public function do_item( $handle ) {
  122. return isset($this->registered[$handle]);
  123. }
  124. /**
  125. * Determines dependencies.
  126. *
  127. * Recursively builds an array of items to process taking
  128. * dependencies into account. Does NOT catch infinite loops.
  129. *
  130. * @access public
  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 mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
  136. * @param bool $recursion Internal flag that function is calling itself.
  137. * @param int|false $group Group level: (int) level, (false) no groups.
  138. * @return bool True on success, false on failure.
  139. */
  140. public function all_deps( $handles, $recursion = false, $group = false ) {
  141. if ( !$handles = (array) $handles )
  142. return false;
  143. foreach ( $handles as $handle ) {
  144. $handle_parts = explode('?', $handle);
  145. $handle = $handle_parts[0];
  146. $queued = in_array($handle, $this->to_do, true);
  147. if ( in_array($handle, $this->done, true) ) // Already done
  148. continue;
  149. $moved = $this->set_group( $handle, $recursion, $group );
  150. $new_group = $this->groups[ $handle ];
  151. if ( $queued && !$moved ) // already queued and in the right group
  152. continue;
  153. $keep_going = true;
  154. if ( !isset($this->registered[$handle]) )
  155. $keep_going = false; // Item doesn't exist.
  156. elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
  157. $keep_going = false; // Item requires dependencies that don't exist.
  158. elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $new_group ) )
  159. $keep_going = false; // Item requires dependencies that don't exist.
  160. if ( ! $keep_going ) { // Either item or its dependencies don't exist.
  161. if ( $recursion )
  162. return false; // Abort this branch.
  163. else
  164. continue; // We're at the top level. Move on to the next one.
  165. }
  166. if ( $queued ) // Already grabbed it and its dependencies.
  167. continue;
  168. if ( isset($handle_parts[1]) )
  169. $this->args[$handle] = $handle_parts[1];
  170. $this->to_do[] = $handle;
  171. }
  172. return true;
  173. }
  174. /**
  175. * Register an item.
  176. *
  177. * Registers the item if no item of that name already exists.
  178. *
  179. * @access public
  180. * @since 2.1.0
  181. * @since 2.6.0 Moved from `WP_Scripts`.
  182. *
  183. * @param string $handle Name of the item. Should be unique.
  184. * @param string $src Full URL of the item, or path of the item relative to the WordPress root directory.
  185. * @param array $deps Optional. An array of registered item handles this item depends on. Default empty array.
  186. * @param string|bool|null $ver Optional. String specifying item version number, if it has one, which is added to the URL
  187. * as a query string for cache busting purposes. If version is set to false, a version
  188. * number is automatically added equal to current installed WordPress version.
  189. * If set to null, no version is added.
  190. * @param mixed $args Optional. Custom property of the item. NOT the class property $args. Examples: $media, $in_footer.
  191. * @return bool Whether the item has been registered. True on success, false on failure.
  192. */
  193. public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
  194. if ( isset($this->registered[$handle]) )
  195. return false;
  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. * @access public
  205. * @since 2.6.0
  206. *
  207. * @param string $handle Name of the item. Should be unique.
  208. * @param string $key The data key.
  209. * @param mixed $value The data value.
  210. * @return bool True on success, false on failure.
  211. */
  212. public function add_data( $handle, $key, $value ) {
  213. if ( !isset( $this->registered[$handle] ) )
  214. return false;
  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. * @access public
  223. * @since 3.3.0
  224. *
  225. * @param string $handle Name of the item. Should be unique.
  226. * @param string $key The data key.
  227. * @return mixed Extra item data (string), false otherwise.
  228. */
  229. public function get_data( $handle, $key ) {
  230. if ( !isset( $this->registered[$handle] ) )
  231. return false;
  232. if ( !isset( $this->registered[$handle]->extra[$key] ) )
  233. return false;
  234. return $this->registered[$handle]->extra[$key];
  235. }
  236. /**
  237. * Un-register an item or items.
  238. *
  239. * @access public
  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. * Queue an item or items.
  252. *
  253. * Decodes handles and arguments, then queues handles and stores
  254. * arguments in the class property $args. For example in extending
  255. * classes, $args is appended to the item url as a query string.
  256. * Note $args is NOT the $args property of items in the $registered array.
  257. *
  258. * @access public
  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. * Dequeue an item or items.
  276. *
  277. * Decodes handles and arguments, then dequeues handles
  278. * and removes arguments from the class property $args.
  279. *
  280. * @access public
  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 array $queue An array of queued _WP_Dependency handle objects.
  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. * @access public
  322. * @since 2.1.0
  323. * @since 2.6.0 Moved from `WP_Scripts`.
  324. *
  325. * @param string $handle Name of the item. Should be unique.
  326. * @param string $list Property name of list array.
  327. * @return bool|_WP_Dependency Found, or object Item data.
  328. */
  329. public function query( $handle, $list = 'registered' ) {
  330. switch ( $list ) {
  331. case 'registered' :
  332. case 'scripts': // back compat
  333. if ( isset( $this->registered[ $handle ] ) )
  334. return $this->registered[ $handle ];
  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. * @access public
  355. * @since 2.8.0
  356. *
  357. * @param string $handle Name of the item. Should be unique.
  358. * @param bool $recursion Internal flag that calling function was called recursively.
  359. * @param mixed $group Group level.
  360. * @return bool Not already in the group or a lower group
  361. */
  362. public function set_group( $handle, $recursion, $group ) {
  363. $group = (int) $group;
  364. if ( isset( $this->groups[ $handle ] ) && $this->groups[ $handle ] <= $group ) {
  365. return false;
  366. }
  367. $this->groups[ $handle ] = $group;
  368. return true;
  369. }
  370. }