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();
28 function WP_Dependencies() {
29 $args = func_get_args();
30 call_user_func_array( array(&$this, '__construct'), $args );
33 function __construct() {}
38 * Process the items passed to it or the queue. Processes all dependencies.
40 * @param mixed handles (optional) items to be processed. (void) processes queue, (string) process that item, (array of strings) process those items
41 * @return array Items that have been processed
43 function do_items( $handles = false, $group = false ) {
44 // Print the queue if nothing is passed. If a string is passed, print that script. If an array is passed, print those scripts.
45 $handles = false === $handles ? $this->queue : (array) $handles;
46 $this->all_deps( $handles );
48 foreach( $this->to_do as $key => $handle ) {
49 if ( !in_array($handle, $this->done) && isset($this->registered[$handle]) ) {
51 if ( ! $this->registered[$handle]->src ) { // Defines a group.
52 $this->done[] = $handle;
56 if ( $this->do_item( $handle, $group ) )
57 $this->done[] = $handle;
59 unset( $this->to_do[$key] );
66 function do_item( $handle ) {
67 return isset($this->registered[$handle]);
71 * Determines dependencies
73 * Recursively builds array of items to process taking dependencies into account. Does NOT catch infinite loops.
76 * @param mixed handles Accepts (string) dep name or (array of strings) dep names
77 * @param bool recursion Used internally when function calls itself
79 function all_deps( $handles, $recursion = false, $group = false ) {
80 if ( !$handles = (array) $handles )
83 foreach ( $handles as $handle ) {
84 $handle_parts = explode('?', $handle);
85 $handle = $handle_parts[0];
86 $queued = in_array($handle, $this->to_do, true);
88 if ( in_array($handle, $this->done, true) ) // Already done
91 $moved = $this->set_group( $handle, $recursion, $group );
93 if ( $queued && !$moved ) // already queued and in the right group
97 if ( !isset($this->registered[$handle]) )
98 $keep_going = false; // Script doesn't exist
99 elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
100 $keep_going = false; // Script requires deps which don't exist (not a necessary check. efficiency?)
101 elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $group ) )
102 $keep_going = false; // Script requires deps which don't exist
104 if ( !$keep_going ) { // Either script or its deps don't exist.
106 return false; // Abort this branch.
108 continue; // We're at the top level. Move on to the next one.
111 if ( $queued ) // Already grobbed it and its deps
114 if ( isset($handle_parts[1]) )
115 $this->args[$handle] = $handle_parts[1];
117 $this->to_do[] = $handle;
126 * Adds the item only if no item of that name already exists
128 * @param string handle Script name
129 * @param string src Script url
130 * @param array deps (optional) Array of script names on which this script depends
131 * @param string ver (optional) Script version (used for cache busting)
132 * @return array Hierarchical array of dependencies
134 function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
135 if ( isset($this->registered[$handle]) )
137 $this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
144 * Adds data only if script has already been added
146 * @param string handle Script name
147 * @param string data_name Name of object in which to store extra data
148 * @param array data Array of extra data
149 * @return bool success
151 function add_data( $handle, $data_name, $data ) {
152 if ( !isset($this->registered[$handle]) )
154 return $this->registered[$handle]->add_data( $data_name, $data );
157 function remove( $handles ) {
158 foreach ( (array) $handles as $handle )
159 unset($this->registered[$handle]);
162 function enqueue( $handles ) {
163 foreach ( (array) $handles as $handle ) {
164 $handle = explode('?', $handle);
165 if ( !in_array($handle[0], $this->queue) && isset($this->registered[$handle[0]]) ) {
166 $this->queue[] = $handle[0];
167 if ( isset($handle[1]) )
168 $this->args[$handle[0]] = $handle[1];
173 function dequeue( $handles ) {
174 foreach ( (array) $handles as $handle ) {
175 $handle = explode('?', $handle);
176 $key = array_search($handle[0], $this->queue);
177 if ( false !== $key ) {
178 unset($this->queue[$key]);
179 unset($this->args[$handle[0]]);
184 function query( $handle, $list = 'registered' ) { // registered, queue, done, to_do
187 case 'scripts': // back compat
188 if ( isset($this->registered[$handle]) )
189 return $this->registered[$handle];
191 case 'to_print': // back compat
192 case 'printed': // back compat
193 if ( 'to_print' == $list )
198 if ( in_array($handle, $this->$list) )
205 function set_group( $handle, $recursion, $group ) {
206 $group = (int) $group;
209 $group = min($this->group, $group);
211 $this->group = $group;
213 if ( isset($this->groups[$handle]) && $this->groups[$handle] <= $group )
216 $this->groups[$handle] = $group;
222 class _WP_Dependency {
229 var $extra = array();
231 function _WP_Dependency() {
232 @list($this->handle, $this->src, $this->deps, $this->ver, $this->args) = func_get_args();
233 if ( !is_array($this->deps) )
234 $this->deps = array();
239 function add_data( $name, $data ) {
240 if ( !is_scalar($name) )
242 $this->extra[$name] = $data;