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();
26 function WP_Dependencies() {
27 $args = func_get_args();
28 call_user_func_array( array(&$this, '__construct'), $args );
31 function __construct() {}
36 * Process the items passed to it or the queue. Processes all dependencies.
38 * @param mixed handles (optional) items to be processed. (void) processes queue, (string) process that item, (array of strings) process those items
39 * @return array Items that have been processed
41 function do_items( $handles = false ) {
42 // Print the queue if nothing is passed. If a string is passed, print that script. If an array is passed, print those scripts.
43 $handles = false === $handles ? $this->queue : (array) $handles;
44 $this->all_deps( $handles );
46 foreach( $this->to_do as $handle ) {
47 if ( !in_array($handle, $this->done) && isset($this->registered[$handle]) ) {
48 if ( $this->registered[$handle]->src ) { // Else it defines a group.
49 $this->do_item( $handle );
51 $this->done[] = $handle;
55 $this->to_do = array();
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 ) {
73 if ( !$handles = (array) $handles )
76 foreach ( $handles as $handle ) {
77 $handle = explode('?', $handle);
78 if ( isset($handle[1]) )
79 $this->args[$handle[0]] = $handle[1];
82 if ( isset($this->to_do[$handle]) ) // Already grobbed it and its deps
86 if ( !isset($this->registered[$handle]) )
87 $keep_going = false; // Script doesn't exist
88 elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
89 $keep_going = false; // Script requires deps which don't exist (not a necessary check. efficiency?)
90 elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true ) )
91 $keep_going = false; // Script requires deps which don't exist
93 if ( !$keep_going ) { // Either script or its deps don't exist.
95 return false; // Abort this branch.
97 continue; // We're at the top level. Move on to the next one.
100 $this->to_do[$handle] = true;
103 if ( !$recursion ) // at the end
104 $this->to_do = array_keys( $this->to_do );
111 * Adds the item only if no item of that name already exists
113 * @param string handle Script name
114 * @param string src Script url
115 * @param array deps (optional) Array of script names on which this script depends
116 * @param string ver (optional) Script version (used for cache busting)
117 * @return array Hierarchical array of dependencies
119 function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
120 if ( isset($this->registered[$handle]) )
122 $this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
129 * Adds data only if script has already been added
131 * @param string handle Script name
132 * @param string data_name Name of object in which to store extra data
133 * @param array data Array of extra data
134 * @return bool success
136 function add_data( $handle, $data_name, $data ) {
137 if ( !isset($this->registered[$handle]) )
139 return $this->registered[$handle]->add_data( $data_name, $data );
142 function remove( $handles ) {
143 foreach ( (array) $handles as $handle )
144 unset($this->registered[$handle]);
147 function enqueue( $handles ) {
148 foreach ( (array) $handles as $handle ) {
149 $handle = explode('?', $handle);
150 if ( !in_array($handle[0], $this->queue) && isset($this->registered[$handle[0]]) ) {
151 $this->queue[] = $handle[0];
152 if ( isset($handle[1]) )
153 $this->args[$handle[0]] = $handle[1];
158 function dequeue( $handles ) {
159 foreach ( (array) $handles as $handle )
160 unset( $this->queue[$handle] );
163 function query( $handle, $list = 'registered' ) { // registered, queue, done, to_do
166 case 'scripts': // back compat
167 if ( isset($this->registered[$handle]) )
168 return $this->registered[$handle];
170 case 'to_print': // back compat
171 case 'printed': // back compat
172 if ( 'to_print' == $list )
177 if ( in_array($handle, $this->$list) )
186 class _WP_Dependency {
193 var $extra = array();
195 function _WP_Dependency() {
196 @list($this->handle, $this->src, $this->deps, $this->ver, $this->args) = func_get_args();
197 if ( !is_array($this->deps) )
198 $this->deps = array();
203 function add_data( $name, $data ) {
204 if ( !is_scalar($name) )
206 $this->extra[$name] = $data;