3 * BackPress Scripts enqueue
5 * Classes were refactored from the WP_Scripts and WordPress script enqueue API.
10 * @uses _WP_Dependency
13 class WP_Dependencies {
15 * An array of registered handle objects.
21 public $registered = array();
24 * An array of queued _WP_Dependency handle objects.
30 public $queue = array();
33 * An array of _WP_Dependency handle objects to queue.
39 public $to_do = array();
42 * An array of _WP_Dependency handle objects already queued.
48 public $done = array();
51 * An array of additional arguments passed when a handle is registered.
53 * Arguments are appended to the item query string.
59 public $args = array();
62 * An array of handle groups to enqueue.
68 public $groups = array();
71 * A handle group to enqueue.
80 * Process the items and dependencies.
82 * Processes the items passed to it or the queue, and their dependencies.
87 * @param mixed $handles Optional. Items to be processed: Process queue (false), process item (string), process items (array of strings).
88 * @param mixed $group Group level: level (int), no groups (false).
89 * @return array Handles of items that have been processed.
91 public function do_items( $handles = false, $group = false ) {
93 * If nothing is passed, print the queue. If a string is passed,
94 * print that item. If an array is passed, print those items.
96 $handles = false === $handles ? $this->queue : (array) $handles;
97 $this->all_deps( $handles );
99 foreach( $this->to_do as $key => $handle ) {
100 if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) {
103 * A single item may alias a set of items, by having dependencies,
104 * but no source. Queuing the item queues the dependencies.
106 * Example: The extending class WP_Scripts is used to register 'scriptaculous' as a set of registered handles:
107 * <code>add( 'scriptaculous', false, array( 'scriptaculous-dragdrop', 'scriptaculous-slider', 'scriptaculous-controls' ) );</code>
109 * The src property is false.
111 if ( ! $this->registered[$handle]->src ) {
112 $this->done[] = $handle;
117 * Attempt to process the item. If successful,
118 * add the handle to the done array.
120 * Unset the item from the to_do array.
122 if ( $this->do_item( $handle, $group ) )
123 $this->done[] = $handle;
125 unset( $this->to_do[$key] );
133 * Process a dependency.
138 * @param string $handle Name of the item. Should be unique.
139 * @return bool True on success, false if not set.
141 public function do_item( $handle ) {
142 return isset($this->registered[$handle]);
146 * Determine dependencies.
148 * Recursively builds an array of items to process taking
149 * dependencies into account. Does NOT catch infinite loops.
154 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
155 * @param bool $recursion Internal flag that function is calling itself.
156 * @param mixed $group Group level: (int) level, (false) no groups.
157 * @return bool True on success, false on failure.
159 public function all_deps( $handles, $recursion = false, $group = false ) {
160 if ( !$handles = (array) $handles )
163 foreach ( $handles as $handle ) {
164 $handle_parts = explode('?', $handle);
165 $handle = $handle_parts[0];
166 $queued = in_array($handle, $this->to_do, true);
168 if ( in_array($handle, $this->done, true) ) // Already done
171 $moved = $this->set_group( $handle, $recursion, $group );
173 if ( $queued && !$moved ) // already queued and in the right group
177 if ( !isset($this->registered[$handle]) )
178 $keep_going = false; // Item doesn't exist.
179 elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
180 $keep_going = false; // Item requires dependencies that don't exist.
181 elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $group ) )
182 $keep_going = false; // Item requires dependencies that don't exist.
184 if ( ! $keep_going ) { // Either item or its dependencies don't exist.
186 return false; // Abort this branch.
188 continue; // We're at the top level. Move on to the next one.
191 if ( $queued ) // Already grabbed it and its dependencies.
194 if ( isset($handle_parts[1]) )
195 $this->args[$handle] = $handle_parts[1];
197 $this->to_do[] = $handle;
206 * Registers the item if no item of that name already exists.
211 * @param string $handle Unique item name.
212 * @param string $src The item url.
213 * @param array $deps Optional. An array of item handle strings on which this item depends.
214 * @param string $ver Optional. Version (used for cache busting).
215 * @param mixed $args Optional. Custom property of the item. NOT the class property $args. Examples: $media, $in_footer.
216 * @return bool True on success, false on failure.
218 public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
219 if ( isset($this->registered[$handle]) )
221 $this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
226 * Add extra item data.
228 * Adds data to a registered item.
233 * @param string $handle Name of the item. Should be unique.
234 * @param string $key The data key.
235 * @param mixed $value The data value.
236 * @return bool True on success, false on failure.
238 public function add_data( $handle, $key, $value ) {
239 if ( !isset( $this->registered[$handle] ) )
242 return $this->registered[$handle]->add_data( $key, $value );
246 * Get extra item data.
248 * Gets data associated with a registered item.
253 * @param string $handle Name of the item. Should be unique.
254 * @param string $key The data key.
255 * @return mixed Extra item data (string), false otherwise.
257 public function get_data( $handle, $key ) {
258 if ( !isset( $this->registered[$handle] ) )
261 if ( !isset( $this->registered[$handle]->extra[$key] ) )
264 return $this->registered[$handle]->extra[$key];
268 * Un-register an item or items.
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.
292 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
294 public function enqueue( $handles ) {
295 foreach ( (array) $handles as $handle ) {
296 $handle = explode('?', $handle);
297 if ( !in_array($handle[0], $this->queue) && isset($this->registered[$handle[0]]) ) {
298 $this->queue[] = $handle[0];
299 if ( isset($handle[1]) )
300 $this->args[$handle[0]] = $handle[1];
306 * Dequeue an item or items.
308 * Decodes handles and arguments, then dequeues handles
309 * and removes arguments from the class property $args.
314 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
316 public function dequeue( $handles ) {
317 foreach ( (array) $handles as $handle ) {
318 $handle = explode('?', $handle);
319 $key = array_search($handle[0], $this->queue);
320 if ( false !== $key ) {
321 unset($this->queue[$key]);
322 unset($this->args[$handle[0]]);
328 * Recursively search the passed dependency tree for $handle
332 * @param array $queue An array of queued _WP_Dependency handle objects.
333 * @param string $handle Name of the item. Should be unique.
334 * @return boolean Whether the handle is found after recursively searching the dependency tree.
336 protected function recurse_deps( $queue, $handle ) {
337 foreach ( $queue as $queued ) {
338 if ( ! isset( $this->registered[ $queued ] ) ) {
342 if ( in_array( $handle, $this->registered[ $queued ]->deps ) ) {
344 } elseif ( $this->recurse_deps( $this->registered[ $queued ]->deps, $handle ) ) {
353 * Query list for an item.
358 * @param string $handle Name of the item. Should be unique.
359 * @param string $list Property name of list array.
360 * @return bool Found, or object Item data.
362 public function query( $handle, $list = 'registered' ) {
365 case 'scripts': // back compat
366 if ( isset( $this->registered[ $handle ] ) )
367 return $this->registered[ $handle ];
372 if ( in_array( $handle, $this->queue ) ) {
375 return $this->recurse_deps( $this->queue, $handle );
378 case 'to_print': // back compat
379 return in_array( $handle, $this->to_do );
382 case 'printed': // back compat
383 return in_array( $handle, $this->done );
389 * Set item group, unless already in a lower group.
394 * @param string $handle Name of the item. Should be unique.
395 * @param bool $recursion Internal flag that calling function was called recursively.
396 * @param mixed $group Group level.
397 * @return bool Not already in the group or a lower group
399 public function set_group( $handle, $recursion, $group ) {
400 $group = (int) $group;
403 $group = min($this->group, $group);
405 $this->group = $group;
407 if ( isset($this->groups[$handle]) && $this->groups[$handle] <= $group )
410 $this->groups[$handle] = $group;
417 * Class _WP_Dependency
419 * Helper class to register a handle and associated data.
424 class _WP_Dependency {
444 * An array of handle dependencies.
450 public $deps = array();
453 * The handle version.
455 * Used for cache-busting.
464 * Additional arguments for the handle.
470 public $args = null; // Custom property, such as $in_footer or $media.
473 * Extra data to supply to the handle.
479 public $extra = array();
482 * Setup dependencies.
486 public function __construct() {
487 @list( $this->handle, $this->src, $this->deps, $this->ver, $this->args ) = func_get_args();
488 if ( ! is_array($this->deps) )
489 $this->deps = array();
498 * @param string $name The data key to add.
499 * @param mixed $data The data value to add.
500 * @return bool False if not scalar, true otherwise.
502 public function add_data( $name, $data ) {
503 if ( !is_scalar($name) )
505 $this->extra[$name] = $data;
509 } // _WP_Dependencies