3 * BackPress Scripts enqueue.
5 * These classes were refactored from the WordPress WP_Scripts and WordPress
13 * BackPress enqueued dependiences class.
16 * @uses _WP_Dependency
19 class WP_Dependencies {
20 var $registered = array();
25 var $groups = array();
31 * Process the items passed to it or the queue. Processes all dependencies.
33 * @param mixed $handles (optional) items to be processed. (void) processes queue, (string) process that item, (array of strings) process those items
34 * @return array Items that have been processed
36 function do_items( $handles = false, $group = false ) {
37 // Print the queue if nothing is passed. If a string is passed, print that script. If an array is passed, print those scripts.
38 $handles = false === $handles ? $this->queue : (array) $handles;
39 $this->all_deps( $handles );
41 foreach( $this->to_do as $key => $handle ) {
42 if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) {
44 if ( ! $this->registered[$handle]->src ) { // Defines a group.
45 $this->done[] = $handle;
49 if ( $this->do_item( $handle, $group ) )
50 $this->done[] = $handle;
52 unset( $this->to_do[$key] );
59 function do_item( $handle ) {
60 return isset($this->registered[$handle]);
64 * Determines dependencies
66 * Recursively builds array of items to process taking dependencies into account. Does NOT catch infinite loops.
69 * @param mixed $handles Accepts (string) dep name or (array of strings) dep names
70 * @param bool $recursion Used internally when function calls itself
72 function all_deps( $handles, $recursion = false, $group = false ) {
73 if ( !$handles = (array) $handles )
76 foreach ( $handles as $handle ) {
77 $handle_parts = explode('?', $handle);
78 $handle = $handle_parts[0];
79 $queued = in_array($handle, $this->to_do, true);
81 if ( in_array($handle, $this->done, true) ) // Already done
84 $moved = $this->set_group( $handle, $recursion, $group );
86 if ( $queued && !$moved ) // already queued and in the right group
90 if ( !isset($this->registered[$handle]) )
91 $keep_going = false; // Script doesn't exist
92 elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
93 $keep_going = false; // Script requires deps which don't exist (not a necessary check. efficiency?)
94 elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $group ) )
95 $keep_going = false; // Script requires deps which don't exist
97 if ( !$keep_going ) { // Either script or its deps don't exist.
99 return false; // Abort this branch.
101 continue; // We're at the top level. Move on to the next one.
104 if ( $queued ) // Already grobbed it and its deps
107 if ( isset($handle_parts[1]) )
108 $this->args[$handle] = $handle_parts[1];
110 $this->to_do[] = $handle;
119 * Adds the item only if no item of that name already exists
121 * @param string $handle Script name
122 * @param string $src Script url
123 * @param array $deps (optional) Array of script names on which this script depends
124 * @param string $ver (optional) Script version (used for cache busting)
125 * @return array Hierarchical array of dependencies
127 function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
128 if ( isset($this->registered[$handle]) )
130 $this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
137 * Adds data only if script has already been added.
139 * @param string $handle Script name
141 * @param mixed $value
142 * @return bool success
144 function add_data( $handle, $key, $value ) {
145 if ( !isset( $this->registered[$handle] ) )
148 return $this->registered[$handle]->add_data( $key, $value );
154 * Gets data associated with a certain handle.
158 * @param string $handle Script name
162 function get_data( $handle, $key ) {
163 if ( !isset( $this->registered[$handle] ) )
166 if ( !isset( $this->registered[$handle]->extra[$key] ) )
169 return $this->registered[$handle]->extra[$key];
172 function remove( $handles ) {
173 foreach ( (array) $handles as $handle )
174 unset($this->registered[$handle]);
177 function enqueue( $handles ) {
178 foreach ( (array) $handles as $handle ) {
179 $handle = explode('?', $handle);
180 if ( !in_array($handle[0], $this->queue) && isset($this->registered[$handle[0]]) ) {
181 $this->queue[] = $handle[0];
182 if ( isset($handle[1]) )
183 $this->args[$handle[0]] = $handle[1];
188 function dequeue( $handles ) {
189 foreach ( (array) $handles as $handle ) {
190 $handle = explode('?', $handle);
191 $key = array_search($handle[0], $this->queue);
192 if ( false !== $key ) {
193 unset($this->queue[$key]);
194 unset($this->args[$handle[0]]);
200 function query( $handle, $list = 'registered' ) {
203 case 'scripts': // back compat
204 if ( isset( $this->registered[ $handle ] ) )
205 return $this->registered[ $handle ];
210 return in_array( $handle, $this->queue );
213 case 'to_print': // back compat
214 return in_array( $handle, $this->to_do );
217 case 'printed': // back compat
218 return in_array( $handle, $this->done );
223 function set_group( $handle, $recursion, $group ) {
224 $group = (int) $group;
227 $group = min($this->group, $group);
229 $this->group = $group;
231 if ( isset($this->groups[$handle]) && $this->groups[$handle] <= $group )
234 $this->groups[$handle] = $group;
240 class _WP_Dependency {
247 var $extra = array();
249 function __construct() {
250 @list( $this->handle, $this->src, $this->deps, $this->ver, $this->args ) = func_get_args();
251 if ( ! is_array($this->deps) )
252 $this->deps = array();
255 function add_data( $name, $data ) {
256 if ( !is_scalar($name) )
258 $this->extra[$name] = $data;