3 * Dependencies API: WP_Dependencies base class
8 * @subpackage Dependencies
12 * Core base class extended to register items.
16 * @uses _WP_Dependency
18 class WP_Dependencies {
20 * An array of registered handle objects.
26 public $registered = array();
29 * An array of queued _WP_Dependency handle objects.
35 public $queue = array();
38 * An array of _WP_Dependency handle objects to queue.
44 public $to_do = array();
47 * An array of _WP_Dependency handle objects already queued.
53 public $done = array();
56 * An array of additional arguments passed when a handle is registered.
58 * Arguments are appended to the item query string.
64 public $args = array();
67 * An array of handle groups to enqueue.
73 public $groups = array();
76 * A handle group to enqueue.
86 * Processes the items and dependencies.
88 * Processes the items passed to it or the queue, and their dependencies.
92 * @since 2.8.0 Added the `$group` parameter.
94 * @param mixed $handles Optional. Items to be processed: Process queue (false), process item (string), process items (array of strings).
95 * @param mixed $group Group level: level (int), no groups (false).
96 * @return array Handles of items that have been processed.
98 public function do_items( $handles = false, $group = false ) {
100 * If nothing is passed, print the queue. If a string is passed,
101 * print that item. If an array is passed, print those items.
103 $handles = false === $handles ? $this->queue : (array) $handles;
104 $this->all_deps( $handles );
106 foreach ( $this->to_do as $key => $handle ) {
107 if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) {
109 * Attempt to process the item. If successful,
110 * add the handle to the done array.
112 * Unset the item from the to_do array.
114 if ( $this->do_item( $handle, $group ) )
115 $this->done[] = $handle;
117 unset( $this->to_do[$key] );
125 * Processes a dependency.
130 * @param string $handle Name of the item. Should be unique.
131 * @return bool True on success, false if not set.
133 public function do_item( $handle ) {
134 return isset($this->registered[$handle]);
138 * Determines dependencies.
140 * Recursively builds an array of items to process taking
141 * dependencies into account. Does NOT catch infinite loops.
145 * @since 2.6.0 Moved from `WP_Scripts`.
146 * @since 2.8.0 Added the `$group` parameter.
148 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
149 * @param bool $recursion Internal flag that function is calling itself.
150 * @param int|false $group Group level: (int) level, (false) no groups.
151 * @return bool True on success, false on failure.
153 public function all_deps( $handles, $recursion = false, $group = false ) {
154 if ( !$handles = (array) $handles )
157 foreach ( $handles as $handle ) {
158 $handle_parts = explode('?', $handle);
159 $handle = $handle_parts[0];
160 $queued = in_array($handle, $this->to_do, true);
162 if ( in_array($handle, $this->done, true) ) // Already done
165 $moved = $this->set_group( $handle, $recursion, $group );
166 $new_group = $this->groups[ $handle ];
168 if ( $queued && !$moved ) // already queued and in the right group
172 if ( !isset($this->registered[$handle]) )
173 $keep_going = false; // Item doesn't exist.
174 elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
175 $keep_going = false; // Item requires dependencies that don't exist.
176 elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $new_group ) )
177 $keep_going = false; // Item requires dependencies that don't exist.
179 if ( ! $keep_going ) { // Either item or its dependencies don't exist.
181 return false; // Abort this branch.
183 continue; // We're at the top level. Move on to the next one.
186 if ( $queued ) // Already grabbed it and its dependencies.
189 if ( isset($handle_parts[1]) )
190 $this->args[$handle] = $handle_parts[1];
192 $this->to_do[] = $handle;
201 * Registers the item if no item of that name already exists.
205 * @since 2.6.0 Moved from `WP_Scripts`.
207 * @param string $handle Name of the item. Should be unique.
208 * @param string $src Full URL of the item, or path of the item relative to the WordPress root directory.
209 * @param array $deps Optional. An array of registered item handles this item depends on. Default empty array.
210 * @param string|bool|null $ver Optional. String specifying item version number, if it has one, which is added to the URL
211 * as a query string for cache busting purposes. If version is set to false, a version
212 * number is automatically added equal to current installed WordPress version.
213 * If set to null, no version is added.
214 * @param mixed $args Optional. Custom property of the item. NOT the class property $args. Examples: $media, $in_footer.
215 * @return bool Whether the item has been registered. True on success, false on failure.
217 public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
218 if ( isset($this->registered[$handle]) )
220 $this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
225 * Add extra item data.
227 * Adds data to a registered item.
232 * @param string $handle Name of the item. Should be unique.
233 * @param string $key The data key.
234 * @param mixed $value The data value.
235 * @return bool True on success, false on failure.
237 public function add_data( $handle, $key, $value ) {
238 if ( !isset( $this->registered[$handle] ) )
241 return $this->registered[$handle]->add_data( $key, $value );
245 * Get extra item data.
247 * Gets data associated with a registered item.
252 * @param string $handle Name of the item. Should be unique.
253 * @param string $key The data key.
254 * @return mixed Extra item data (string), false otherwise.
256 public function get_data( $handle, $key ) {
257 if ( !isset( $this->registered[$handle] ) )
260 if ( !isset( $this->registered[$handle]->extra[$key] ) )
263 return $this->registered[$handle]->extra[$key];
267 * Un-register an item or items.
271 * @since 2.6.0 Moved from `WP_Scripts`.
273 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
276 public function remove( $handles ) {
277 foreach ( (array) $handles as $handle )
278 unset($this->registered[$handle]);
282 * Queue an item or items.
284 * Decodes handles and arguments, then queues handles and stores
285 * arguments in the class property $args. For example in extending
286 * classes, $args is appended to the item url as a query string.
287 * Note $args is NOT the $args property of items in the $registered array.
291 * @since 2.6.0 Moved from `WP_Scripts`.
293 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
295 public function enqueue( $handles ) {
296 foreach ( (array) $handles as $handle ) {
297 $handle = explode('?', $handle);
298 if ( !in_array($handle[0], $this->queue) && isset($this->registered[$handle[0]]) ) {
299 $this->queue[] = $handle[0];
300 if ( isset($handle[1]) )
301 $this->args[$handle[0]] = $handle[1];
307 * Dequeue an item or items.
309 * Decodes handles and arguments, then dequeues handles
310 * and removes arguments from the class property $args.
314 * @since 2.6.0 Moved from `WP_Scripts`.
316 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
318 public function dequeue( $handles ) {
319 foreach ( (array) $handles as $handle ) {
320 $handle = explode('?', $handle);
321 $key = array_search($handle[0], $this->queue);
322 if ( false !== $key ) {
323 unset($this->queue[$key]);
324 unset($this->args[$handle[0]]);
330 * Recursively search the passed dependency tree for $handle
334 * @param array $queue An array of queued _WP_Dependency handle objects.
335 * @param string $handle Name of the item. Should be unique.
336 * @return bool Whether the handle is found after recursively searching the dependency tree.
338 protected function recurse_deps( $queue, $handle ) {
339 foreach ( $queue as $queued ) {
340 if ( ! isset( $this->registered[ $queued ] ) ) {
344 if ( in_array( $handle, $this->registered[ $queued ]->deps ) ) {
346 } elseif ( $this->recurse_deps( $this->registered[ $queued ]->deps, $handle ) ) {
355 * Query list for an item.
359 * @since 2.6.0 Moved from `WP_Scripts`.
361 * @param string $handle Name of the item. Should be unique.
362 * @param string $list Property name of list array.
363 * @return bool|_WP_Dependency Found, or object Item data.
365 public function query( $handle, $list = 'registered' ) {
368 case 'scripts': // back compat
369 if ( isset( $this->registered[ $handle ] ) )
370 return $this->registered[ $handle ];
375 if ( in_array( $handle, $this->queue ) ) {
378 return $this->recurse_deps( $this->queue, $handle );
381 case 'to_print': // back compat
382 return in_array( $handle, $this->to_do );
385 case 'printed': // back compat
386 return in_array( $handle, $this->done );
392 * Set item group, unless already in a lower group.
397 * @param string $handle Name of the item. Should be unique.
398 * @param bool $recursion Internal flag that calling function was called recursively.
399 * @param mixed $group Group level.
400 * @return bool Not already in the group or a lower group
402 public function set_group( $handle, $recursion, $group ) {
403 $group = (int) $group;
405 if ( isset( $this->groups[ $handle ] ) && $this->groups[ $handle ] <= $group ) {
409 $this->groups[ $handle ] = $group;