root/trunk/wp-includes/class.wp-dependencies.php

Revision 1519, 5.6 kB (checked in by donncha, 6 days ago)

WP Merge

Line 
1 <?php
2 /**
3  * BackPress Scripts enqueue.
4  *
5  * These classes were refactored from the WordPress WP_Scripts and WordPress
6  * script enqueue API.
7  *
8  * @package BackPress
9  * @since r74
10  */
11
12 /**
13  * BackPress enqueued dependiences class.
14  *
15  * @package BackPress
16  * @uses _WP_Dependency
17  * @since r74
18  */
19 class WP_Dependencies {
20     var $registered = array();
21     var $queue = array();
22     var $to_do = array();
23     var $done = array();
24     var $args = array();
25
26     function WP_Dependencies() {
27         $args = func_get_args();
28         call_user_func_array( array(&$this, '__construct'), $args );
29     }
30
31     function __construct() {}
32
33     /**
34      * Do the dependencies
35      *
36      * Process the items passed to it or the queue.  Processes all dependencies.
37      *
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
40      */
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 );
45
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 );
50                 }
51                 $this->done[] = $handle;
52             }
53         }
54
55         $this->to_do = array();
56         return $this->done;
57     }
58
59     function do_item( $handle ) {
60         return isset($this->registered[$handle]);
61     }
62
63     /**
64      * Determines dependencies
65      *
66      * Recursively builds array of items to process taking dependencies into account.  Does NOT catch infinite loops.
67      *
68
69      * @param mixed handles Accepts (string) dep name or (array of strings) dep names
70      * @param bool recursion Used internally when function calls itself
71      */
72     function all_deps( $handles, $recursion = false ) {
73         if ( !$handles = (array) $handles )
74             return false;
75
76         foreach ( $handles as $handle ) {
77             $handle = explode('?', $handle);
78             if ( isset($handle[1]) )
79                 $this->args[$handle[0]] = $handle[1];
80             $handle = $handle[0];
81
82             if ( isset($this->to_do[$handle]) ) // Already grobbed it and its deps
83                 continue;
84
85             $keep_going = true;
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
92
93             if ( !$keep_going ) { // Either script or its deps don't exist.
94                 if ( $recursion )
95                     return false; // Abort this branch.
96                 else
97                     continue; // We're at the top level.  Move on to the next one.
98             }
99
100             $this->to_do[$handle] = true;
101         }
102
103         if ( !$recursion ) // at the end
104             $this->to_do = array_keys( $this->to_do );
105         return true;
106     }
107
108     /**
109      * Adds item
110      *
111      * Adds the item only if no item of that name already exists
112      *
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
118      */
119     function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
120         if ( isset($this->registered[$handle]) )
121             return false;
122         $this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
123         return true;
124     }
125
126     /**
127      * Adds extra data
128      *
129      * Adds data only if script has already been added
130      *
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
135      */
136     function add_data( $handle, $data_name, $data ) {
137         if ( !isset($this->registered[$handle]) )
138             return false;
139         return $this->registered[$handle]->add_data( $data_name, $data );
140     }
141
142     function remove( $handles ) {
143         foreach ( (array) $handles as $handle )
144             unset($this->registered[$handle]);
145     }
146
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];
154             }
155         }
156     }
157
158     function dequeue( $handles ) {
159         foreach ( (array) $handles as $handle )
160             unset( $this->queue[$handle] );
161     }
162
163     function query( $handle, $list = 'registered' ) { // registered, queue, done, to_do
164         switch ( $list ) :
165         case 'registered':
166         case 'scripts': // back compat
167             if ( isset($this->registered[$handle]) )
168                 return $this->registered[$handle];
169             break;
170         case 'to_print': // back compat
171         case 'printed': // back compat
172             if ( 'to_print' == $list )
173                 $list = 'to_do';
174             else
175                 $list = 'printed';
176         default:
177             if ( in_array($handle, $this->$list) )
178                 return true;
179             break;
180         endswitch;
181         return false;
182     }
183
184 }
185
186 class _WP_Dependency {
187     var $handle;
188     var $src;
189     var $deps = array();
190     var $ver = false;
191     var $args = null;
192
193     var $extra = array();
194
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();
199         if ( !$this->ver )
200             $this->ver = false;
201     }
202
203     function add_data( $name, $data ) {
204         if ( !is_scalar($name) )
205             return false;
206         $this->extra[$name] = $data;
207         return true;
208     }
209 }
210
Note: See TracBrowser for help on using the browser.