|
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 var $groups = array(); |
|
26 var $group = 0; |
|
27 |
|
28 function WP_Dependencies() { |
|
29 $args = func_get_args(); |
|
30 call_user_func_array( array(&$this, '__construct'), $args ); |
|
31 } |
|
32 |
|
33 function __construct() {} |
|
34 |
|
35 /** |
|
36 * Do the dependencies |
|
37 * |
|
38 * Process the items passed to it or the queue. Processes all dependencies. |
|
39 * |
|
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 |
|
42 */ |
|
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 ); |
|
47 |
|
48 foreach( $this->to_do as $key => $handle ) { |
|
49 if ( !in_array($handle, $this->done) && isset($this->registered[$handle]) ) { |
|
50 |
|
51 if ( ! $this->registered[$handle]->src ) { // Defines a group. |
|
52 $this->done[] = $handle; |
|
53 continue; |
|
54 } |
|
55 |
|
56 if ( $this->do_item( $handle, $group ) ) |
|
57 $this->done[] = $handle; |
|
58 |
|
59 unset( $this->to_do[$key] ); |
|
60 } |
|
61 } |
|
62 |
|
63 return $this->done; |
|
64 } |
|
65 |
|
66 function do_item( $handle ) { |
|
67 return isset($this->registered[$handle]); |
|
68 } |
|
69 |
|
70 /** |
|
71 * Determines dependencies |
|
72 * |
|
73 * Recursively builds array of items to process taking dependencies into account. Does NOT catch infinite loops. |
|
74 * |
|
75 |
|
76 * @param mixed handles Accepts (string) dep name or (array of strings) dep names |
|
77 * @param bool recursion Used internally when function calls itself |
|
78 */ |
|
79 function all_deps( $handles, $recursion = false, $group = false ) { |
|
80 if ( !$handles = (array) $handles ) |
|
81 return false; |
|
82 |
|
83 foreach ( $handles as $handle ) { |
|
84 $handle_parts = explode('?', $handle); |
|
85 $handle = $handle_parts[0]; |
|
86 $queued = in_array($handle, $this->to_do, true); |
|
87 |
|
88 if ( in_array($handle, $this->done, true) ) // Already done |
|
89 continue; |
|
90 |
|
91 $moved = $this->set_group( $handle, $recursion, $group ); |
|
92 |
|
93 if ( $queued && !$moved ) // already queued and in the right group |
|
94 continue; |
|
95 |
|
96 $keep_going = true; |
|
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 |
|
103 |
|
104 if ( !$keep_going ) { // Either script or its deps don't exist. |
|
105 if ( $recursion ) |
|
106 return false; // Abort this branch. |
|
107 else |
|
108 continue; // We're at the top level. Move on to the next one. |
|
109 } |
|
110 |
|
111 if ( $queued ) // Already grobbed it and its deps |
|
112 continue; |
|
113 |
|
114 if ( isset($handle_parts[1]) ) |
|
115 $this->args[$handle] = $handle_parts[1]; |
|
116 |
|
117 $this->to_do[] = $handle; |
|
118 } |
|
119 |
|
120 return true; |
|
121 } |
|
122 |
|
123 /** |
|
124 * Adds item |
|
125 * |
|
126 * Adds the item only if no item of that name already exists |
|
127 * |
|
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 |
|
133 */ |
|
134 function add( $handle, $src, $deps = array(), $ver = false, $args = null ) { |
|
135 if ( isset($this->registered[$handle]) ) |
|
136 return false; |
|
137 $this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args ); |
|
138 return true; |
|
139 } |
|
140 |
|
141 /** |
|
142 * Adds extra data |
|
143 * |
|
144 * Adds data only if script has already been added |
|
145 * |
|
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 |
|
150 */ |
|
151 function add_data( $handle, $data_name, $data ) { |
|
152 if ( !isset($this->registered[$handle]) ) |
|
153 return false; |
|
154 return $this->registered[$handle]->add_data( $data_name, $data ); |
|
155 } |
|
156 |
|
157 function remove( $handles ) { |
|
158 foreach ( (array) $handles as $handle ) |
|
159 unset($this->registered[$handle]); |
|
160 } |
|
161 |
|
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]; |
|
169 } |
|
170 } |
|
171 } |
|
172 |
|
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]]); |
|
180 } |
|
181 } |
|
182 } |
|
183 |
|
184 function query( $handle, $list = 'registered' ) { // registered, queue, done, to_do |
|
185 switch ( $list ) : |
|
186 case 'registered': |
|
187 case 'scripts': // back compat |
|
188 if ( isset($this->registered[$handle]) ) |
|
189 return $this->registered[$handle]; |
|
190 break; |
|
191 case 'to_print': // back compat |
|
192 case 'printed': // back compat |
|
193 if ( 'to_print' == $list ) |
|
194 $list = 'to_do'; |
|
195 else |
|
196 $list = 'printed'; |
|
197 default: |
|
198 if ( in_array($handle, $this->$list) ) |
|
199 return true; |
|
200 break; |
|
201 endswitch; |
|
202 return false; |
|
203 } |
|
204 |
|
205 function set_group( $handle, $recursion, $group ) { |
|
206 $group = (int) $group; |
|
207 |
|
208 if ( $recursion ) |
|
209 $group = min($this->group, $group); |
|
210 else |
|
211 $this->group = $group; |
|
212 |
|
213 if ( isset($this->groups[$handle]) && $this->groups[$handle] <= $group ) |
|
214 return false; |
|
215 |
|
216 $this->groups[$handle] = $group; |
|
217 return true; |
|
218 } |
|
219 |
|
220 } |
|
221 |
|
222 class _WP_Dependency { |
|
223 var $handle; |
|
224 var $src; |
|
225 var $deps = array(); |
|
226 var $ver = false; |
|
227 var $args = null; |
|
228 |
|
229 var $extra = array(); |
|
230 |
|
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(); |
|
235 if ( !$this->ver ) |
|
236 $this->ver = false; |
|
237 } |
|
238 |
|
239 function add_data( $name, $data ) { |
|
240 if ( !is_scalar($name) ) |
|
241 return false; |
|
242 $this->extra[$name] = $data; |
|
243 return true; |
|
244 } |
|
245 } |