PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/mhoofman/wordpress-heroku
PHP | 509 lines | 169 code | 54 blank | 286 comment | 33 complexity | b7ab545f5b406a17f6bb6adc5c3f6f75 MD5 | raw file
  1. <?php
  2. /**
  3. * BackPress Scripts enqueue
  4. *
  5. * Classes were refactored from the WP_Scripts and WordPress script enqueue API.
  6. *
  7. * @since BackPress r74
  8. *
  9. * @package BackPress
  10. * @uses _WP_Dependency
  11. * @since r74
  12. */
  13. class WP_Dependencies {
  14. /**
  15. * An array of registered handle objects.
  16. *
  17. * @access public
  18. * @since 2.6.8
  19. * @var array
  20. */
  21. public $registered = array();
  22. /**
  23. * An array of queued _WP_Dependency handle objects.
  24. *
  25. * @access public
  26. * @since 2.6.8
  27. * @var array
  28. */
  29. public $queue = array();
  30. /**
  31. * An array of _WP_Dependency handle objects to queue.
  32. *
  33. * @access public
  34. * @since 2.6.0
  35. * @var array
  36. */
  37. public $to_do = array();
  38. /**
  39. * An array of _WP_Dependency handle objects already queued.
  40. *
  41. * @access public
  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. * @access public
  52. * @since 2.6.0
  53. * @var array
  54. */
  55. public $args = array();
  56. /**
  57. * An array of handle groups to enqueue.
  58. *
  59. * @access public
  60. * @since 2.8.0
  61. * @var array
  62. */
  63. public $groups = array();
  64. /**
  65. * A handle group to enqueue.
  66. *
  67. * @access public
  68. * @since 2.8.0
  69. * @var int
  70. */
  71. public $group = 0;
  72. /**
  73. * Process the items and dependencies.
  74. *
  75. * Processes the items passed to it or the queue, and their dependencies.
  76. *
  77. * @access public
  78. * @since 2.1.0
  79. *
  80. * @param mixed $handles Optional. Items to be processed: Process queue (false), process item (string), process items (array of strings).
  81. * @param mixed $group Group level: level (int), no groups (false).
  82. * @return array Handles of items that have been processed.
  83. */
  84. public function do_items( $handles = false, $group = false ) {
  85. /*
  86. * If nothing is passed, print the queue. If a string is passed,
  87. * print that item. If an array is passed, print those items.
  88. */
  89. $handles = false === $handles ? $this->queue : (array) $handles;
  90. $this->all_deps( $handles );
  91. foreach( $this->to_do as $key => $handle ) {
  92. if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) {
  93. /*
  94. * A single item may alias a set of items, by having dependencies,
  95. * but no source. Queuing the item queues the dependencies.
  96. *
  97. * Example: The extending class WP_Scripts is used to register 'scriptaculous' as a set of registered handles:
  98. * <code>add( 'scriptaculous', false, array( 'scriptaculous-dragdrop', 'scriptaculous-slider', 'scriptaculous-controls' ) );</code>
  99. *
  100. * The src property is false.
  101. */
  102. if ( ! $this->registered[$handle]->src ) {
  103. $this->done[] = $handle;
  104. continue;
  105. }
  106. /*
  107. * Attempt to process the item. If successful,
  108. * add the handle to the done array.
  109. *
  110. * Unset the item from the to_do array.
  111. */
  112. if ( $this->do_item( $handle, $group ) )
  113. $this->done[] = $handle;
  114. unset( $this->to_do[$key] );
  115. }
  116. }
  117. return $this->done;
  118. }
  119. /**
  120. * Process a dependency.
  121. *
  122. * @access public
  123. * @since 2.6.0
  124. *
  125. * @param string $handle Name of the item. Should be unique.
  126. * @return bool True on success, false if not set.
  127. */
  128. public function do_item( $handle ) {
  129. return isset($this->registered[$handle]);
  130. }
  131. /**
  132. * Determine dependencies.
  133. *
  134. * Recursively builds an array of items to process taking
  135. * dependencies into account. Does NOT catch infinite loops.
  136. *
  137. * @access public
  138. * @since 2.1.0
  139. *
  140. * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
  141. * @param bool $recursion Internal flag that function is calling itself.
  142. * @param mixed $group Group level: (int) level, (false) no groups.
  143. * @return bool True on success, false on failure.
  144. */
  145. public function all_deps( $handles, $recursion = false, $group = false ) {
  146. if ( !$handles = (array) $handles )
  147. return false;
  148. foreach ( $handles as $handle ) {
  149. $handle_parts = explode('?', $handle);
  150. $handle = $handle_parts[0];
  151. $queued = in_array($handle, $this->to_do, true);
  152. if ( in_array($handle, $this->done, true) ) // Already done
  153. continue;
  154. $moved = $this->set_group( $handle, $recursion, $group );
  155. if ( $queued && !$moved ) // already queued and in the right group
  156. continue;
  157. $keep_going = true;
  158. if ( !isset($this->registered[$handle]) )
  159. $keep_going = false; // Item doesn't exist.
  160. elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
  161. $keep_going = false; // Item requires dependencies that don't exist.
  162. elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $group ) )
  163. $keep_going = false; // Item requires dependencies that don't exist.
  164. if ( ! $keep_going ) { // Either item or its dependencies don't exist.
  165. if ( $recursion )
  166. return false; // Abort this branch.
  167. else
  168. continue; // We're at the top level. Move on to the next one.
  169. }
  170. if ( $queued ) // Already grabbed it and its dependencies.
  171. continue;
  172. if ( isset($handle_parts[1]) )
  173. $this->args[$handle] = $handle_parts[1];
  174. $this->to_do[] = $handle;
  175. }
  176. return true;
  177. }
  178. /**
  179. * Register an item.
  180. *
  181. * Registers the item if no item of that name already exists.
  182. *
  183. * @access public
  184. * @since 2.1.0
  185. *
  186. * @param string $handle Unique item name.
  187. * @param string $src The item url.
  188. * @param array $deps Optional. An array of item handle strings on which this item depends.
  189. * @param string $ver Optional. Version (used for cache busting).
  190. * @param mixed $args Optional. Custom property of the item. NOT the class property $args. Examples: $media, $in_footer.
  191. * @return bool 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. *
  242. * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
  243. * @return void
  244. */
  245. public function remove( $handles ) {
  246. foreach ( (array) $handles as $handle )
  247. unset($this->registered[$handle]);
  248. }
  249. /**
  250. * Queue an item or items.
  251. *
  252. * Decodes handles and arguments, then queues handles and stores
  253. * arguments in the class property $args. For example in extending
  254. * classes, $args is appended to the item url as a query string.
  255. * Note $args is NOT the $args property of items in the $registered array.
  256. *
  257. * @access public
  258. * @since 2.1.0
  259. *
  260. * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
  261. */
  262. public function enqueue( $handles ) {
  263. foreach ( (array) $handles as $handle ) {
  264. $handle = explode('?', $handle);
  265. if ( !in_array($handle[0], $this->queue) && isset($this->registered[$handle[0]]) ) {
  266. $this->queue[] = $handle[0];
  267. if ( isset($handle[1]) )
  268. $this->args[$handle[0]] = $handle[1];
  269. }
  270. }
  271. }
  272. /**
  273. * Dequeue an item or items.
  274. *
  275. * Decodes handles and arguments, then dequeues handles
  276. * and removes arguments from the class property $args.
  277. *
  278. * @access public
  279. * @since 2.1.0
  280. *
  281. * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
  282. */
  283. public function dequeue( $handles ) {
  284. foreach ( (array) $handles as $handle ) {
  285. $handle = explode('?', $handle);
  286. $key = array_search($handle[0], $this->queue);
  287. if ( false !== $key ) {
  288. unset($this->queue[$key]);
  289. unset($this->args[$handle[0]]);
  290. }
  291. }
  292. }
  293. /**
  294. * Recursively search the passed dependency tree for $handle
  295. *
  296. * @since 4.0.0
  297. *
  298. * @param array $queue An array of queued _WP_Dependency handle objects.
  299. * @param string $handle Name of the item. Should be unique.
  300. * @return boolean Whether the handle is found after recursively searching the dependency tree.
  301. */
  302. protected function recurse_deps( $queue, $handle ) {
  303. foreach ( $queue as $queued ) {
  304. if ( ! isset( $this->registered[ $queued ] ) ) {
  305. continue;
  306. }
  307. if ( in_array( $handle, $this->registered[ $queued ]->deps ) ) {
  308. return true;
  309. } elseif ( $this->recurse_deps( $this->registered[ $queued ]->deps, $handle ) ) {
  310. return true;
  311. }
  312. }
  313. return false;
  314. }
  315. /**
  316. * Query list for an item.
  317. *
  318. * @access public
  319. * @since 2.1.0
  320. *
  321. * @param string $handle Name of the item. Should be unique.
  322. * @param string $list Property name of list array.
  323. * @return bool Found, or object Item data.
  324. */
  325. public function query( $handle, $list = 'registered' ) {
  326. switch ( $list ) {
  327. case 'registered' :
  328. case 'scripts': // back compat
  329. if ( isset( $this->registered[ $handle ] ) )
  330. return $this->registered[ $handle ];
  331. return false;
  332. case 'enqueued' :
  333. case 'queue' :
  334. if ( in_array( $handle, $this->queue ) ) {
  335. return true;
  336. }
  337. return $this->recurse_deps( $this->queue, $handle );
  338. case 'to_do' :
  339. case 'to_print': // back compat
  340. return in_array( $handle, $this->to_do );
  341. case 'done' :
  342. case 'printed': // back compat
  343. return in_array( $handle, $this->done );
  344. }
  345. return false;
  346. }
  347. /**
  348. * Set item group, unless already in a lower group.
  349. *
  350. * @access public
  351. * @since 2.8.0
  352. *
  353. * @param string $handle Name of the item. Should be unique.
  354. * @param bool $recursion Internal flag that calling function was called recursively.
  355. * @param mixed $group Group level.
  356. * @return bool Not already in the group or a lower group
  357. */
  358. public function set_group( $handle, $recursion, $group ) {
  359. $group = (int) $group;
  360. if ( $recursion )
  361. $group = min($this->group, $group);
  362. else
  363. $this->group = $group;
  364. if ( isset($this->groups[$handle]) && $this->groups[$handle] <= $group )
  365. return false;
  366. $this->groups[$handle] = $group;
  367. return true;
  368. }
  369. } // WP_Dependencies
  370. /**
  371. * Class _WP_Dependency
  372. *
  373. * Helper class to register a handle and associated data.
  374. *
  375. * @access private
  376. * @since 2.6.0
  377. */
  378. class _WP_Dependency {
  379. /**
  380. * The handle name.
  381. *
  382. * @access public
  383. * @since 2.6.0
  384. * @var null
  385. */
  386. public $handle;
  387. /**
  388. * The handle source.
  389. *
  390. * @access public
  391. * @since 2.6.0
  392. * @var null
  393. */
  394. public $src;
  395. /**
  396. * An array of handle dependencies.
  397. *
  398. * @access public
  399. * @since 2.6.0
  400. * @var array
  401. */
  402. public $deps = array();
  403. /**
  404. * The handle version.
  405. *
  406. * Used for cache-busting.
  407. *
  408. * @access public
  409. * @since 2.6.0
  410. * @var bool|string
  411. */
  412. public $ver = false;
  413. /**
  414. * Additional arguments for the handle.
  415. *
  416. * @access public
  417. * @since 2.6.0
  418. * @var null
  419. */
  420. public $args = null; // Custom property, such as $in_footer or $media.
  421. /**
  422. * Extra data to supply to the handle.
  423. *
  424. * @access public
  425. * @since 2.6.0
  426. * @var array
  427. */
  428. public $extra = array();
  429. /**
  430. * Setup dependencies.
  431. *
  432. * @since 2.6.0
  433. */
  434. public function __construct() {
  435. @list( $this->handle, $this->src, $this->deps, $this->ver, $this->args ) = func_get_args();
  436. if ( ! is_array($this->deps) )
  437. $this->deps = array();
  438. }
  439. /**
  440. * Add handle data.
  441. *
  442. * @access public
  443. * @since 2.6.0
  444. *
  445. * @param string $name The data key to add.
  446. * @param mixed $data The data value to add.
  447. * @return bool False if not scalar, true otherwise.
  448. */
  449. public function add_data( $name, $data ) {
  450. if ( !is_scalar($name) )
  451. return false;
  452. $this->extra[$name] = $data;
  453. return true;
  454. }
  455. } // _WP_Dependencies