WP_Dependencies

Core base class extended to register items.

Defined (1)

The class is defined in the following location(s).

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