3 class WP_Dependencies {
4 var $registered = array();
10 function WP_Dependencies() {
11 $args = func_get_args();
12 call_user_func_array( array(&$this, '__construct'), $args );
15 function __construct() {}
20 * Process the items passed to it or the queue. Processes all dependencies.
22 * @param mixed handles (optional) items to be processed. (void) processes queue, (string) process that item, (array of strings) process those items
23 * @return array Items that have been processed
25 function do_items( $handles = false ) {
26 // Print the queue if nothing is passed. If a string is passed, print that script. If an array is passed, print those scripts.
27 $handles = false === $handles ? $this->queue : (array) $handles;
28 $this->all_deps( $handles );
30 foreach( $this->to_do as $handle ) {
31 if ( !in_array($handle, $this->done) && isset($this->registered[$handle]) ) {
32 if ( $this->registered[$handle]->src ) { // Else it defines a group.
33 $this->do_item( $handle );
35 $this->done[] = $handle;
39 $this->to_do = array();
43 function do_item( $handle ) {
44 return isset($this->registered[$handle]);
48 * Determines dependencies
50 * Recursively builds array of items to process taking dependencies into account. Does NOT catch infinite loops.
53 * @param mixed handles Accepts (string) dep name or (array of strings) dep names
54 * @param bool recursion Used internally when function calls itself
56 function all_deps( $handles, $recursion = false ) {
57 if ( !$handles = (array) $handles )
60 foreach ( $handles as $handle ) {
61 $handle = explode('?', $handle);
62 if ( isset($handle[1]) )
63 $this->args[$handle[0]] = $handle[1];
66 if ( isset($this->to_do[$handle]) ) // Already grobbed it and its deps
70 if ( !isset($this->registered[$handle]) )
71 $keep_going = false; // Script doesn't exist
72 elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
73 $keep_going = false; // Script requires deps which don't exist (not a necessary check. efficiency?)
74 elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true ) )
75 $keep_going = false; // Script requires deps which don't exist
77 if ( !$keep_going ) { // Either script or its deps don't exist.
79 return false; // Abort this branch.
81 continue; // We're at the top level. Move on to the next one.
84 $this->to_do[$handle] = true;
87 if ( !$recursion ) // at the end
88 $this->to_do = array_keys( $this->to_do );
95 * Adds the item only if no item of that name already exists
97 * @param string handle Script name
98 * @param string src Script url
99 * @param array deps (optional) Array of script names on which this script depends
100 * @param string ver (optional) Script version (used for cache busting)
101 * @return array Hierarchical array of dependencies
103 function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
104 if ( isset($this->registered[$handle]) )
106 $this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
113 * Adds data only if script has already been added
115 * @param string handle Script name
116 * @param string data_name Name of object in which to store extra data
117 * @param array data Array of extra data
118 * @return bool success
120 function add_data( $handle, $data_name, $data ) {
121 if ( !isset($this->registered[$handle]) )
123 return $this->registered[$handle]->add_data( $data_name, $data );
126 function remove( $handles ) {
127 foreach ( (array) $handles as $handle )
128 unset($this->registered[$handle]);
131 function enqueue( $handles ) {
132 foreach ( (array) $handles as $handle ) {
133 $handle = explode('?', $handle);
134 if ( !in_array($handle[0], $this->queue) && isset($this->registered[$handle[0]]) ) {
135 $this->queue[] = $handle[0];
136 if ( isset($handle[1]) )
137 $this->args[$handle[0]] = $handle[1];
142 function dequeue( $handles ) {
143 foreach ( (array) $handles as $handle )
144 unset( $this->queue[$handle] );
147 function query( $handle, $list = 'registered' ) { // registered, queue, done, to_do
150 case 'scripts': // back compat
151 if ( isset($this->registered[$handle]) )
152 return $this->registered[$handle];
154 case 'to_print': // back compat
155 case 'printed': // back compat
156 if ( 'to_print' == $list )
161 if ( in_array($handle, $this->$list) )
170 class _WP_Dependency {
177 var $extra = array();
179 function _WP_Dependency() {
180 @list($this->handle, $this->src, $this->deps, $this->ver, $this->args) = func_get_args();
181 if ( !is_array($this->deps) )
182 $this->deps = array();
187 function add_data( $name, $data ) {
188 if ( !is_scalar($name) )
190 $this->extra[$name] = $data;