1 <?php |
1 <?php |
2 /** |
2 /** |
3 * BackPress Scripts enqueue |
3 * Dependencies API: WP_Dependencies base class |
4 * |
4 * |
5 * Classes were refactored from the WP_Scripts and WordPress script enqueue API. |
5 * @since 2.6.0 |
6 * |
6 * |
7 * @since BackPress r74 |
7 * @package WordPress |
|
8 * @subpackage Dependencies |
|
9 */ |
|
10 |
|
11 /** |
|
12 * Core base class extended to register items. |
8 * |
13 * |
9 * @package BackPress |
14 * @since 2.6.0 |
10 * @uses _WP_Dependency |
15 * |
11 * @since r74 |
16 * @see _WP_Dependency |
12 */ |
17 */ |
13 class WP_Dependencies { |
18 class WP_Dependencies { |
14 /** |
19 /** |
15 * An array of registered handle objects. |
20 * An array of registered handle objects. |
16 * |
21 * |
17 * @access public |
|
18 * @since 2.6.8 |
22 * @since 2.6.8 |
19 * @var array |
23 * @var array |
20 */ |
24 */ |
21 public $registered = array(); |
25 public $registered = array(); |
22 |
26 |
23 /** |
27 /** |
24 * An array of queued _WP_Dependency handle objects. |
28 * An array of queued _WP_Dependency handle objects. |
25 * |
29 * |
26 * @access public |
|
27 * @since 2.6.8 |
30 * @since 2.6.8 |
28 * @var array |
31 * @var array |
29 */ |
32 */ |
30 public $queue = array(); |
33 public $queue = array(); |
31 |
34 |
32 /** |
35 /** |
33 * An array of _WP_Dependency handle objects to queue. |
36 * An array of _WP_Dependency handle objects to queue. |
34 * |
37 * |
35 * @access public |
|
36 * @since 2.6.0 |
38 * @since 2.6.0 |
37 * @var array |
39 * @var array |
38 */ |
40 */ |
39 public $to_do = array(); |
41 public $to_do = array(); |
40 |
42 |
41 /** |
43 /** |
42 * An array of _WP_Dependency handle objects already queued. |
44 * An array of _WP_Dependency handle objects already queued. |
43 * |
45 * |
44 * @access public |
|
45 * @since 2.6.0 |
46 * @since 2.6.0 |
46 * @var array |
47 * @var array |
47 */ |
48 */ |
48 public $done = array(); |
49 public $done = array(); |
49 |
50 |
50 /** |
51 /** |
51 * An array of additional arguments passed when a handle is registered. |
52 * An array of additional arguments passed when a handle is registered. |
52 * |
53 * |
53 * Arguments are appended to the item query string. |
54 * Arguments are appended to the item query string. |
54 * |
55 * |
55 * @access public |
|
56 * @since 2.6.0 |
56 * @since 2.6.0 |
57 * @var array |
57 * @var array |
58 */ |
58 */ |
59 public $args = array(); |
59 public $args = array(); |
60 |
60 |
61 /** |
61 /** |
62 * An array of handle groups to enqueue. |
62 * An array of handle groups to enqueue. |
63 * |
63 * |
64 * @access public |
|
65 * @since 2.8.0 |
64 * @since 2.8.0 |
66 * @var array |
65 * @var array |
67 */ |
66 */ |
68 public $groups = array(); |
67 public $groups = array(); |
69 |
68 |
70 /** |
69 /** |
71 * A handle group to enqueue. |
70 * A handle group to enqueue. |
72 * |
71 * |
73 * @access public |
|
74 * @since 2.8.0 |
72 * @since 2.8.0 |
|
73 * @deprecated 4.5.0 |
75 * @var int |
74 * @var int |
76 */ |
75 */ |
77 public $group = 0; |
76 public $group = 0; |
78 |
77 |
79 /** |
78 /** |
80 * Process the items and dependencies. |
79 * Processes the items and dependencies. |
81 * |
80 * |
82 * Processes the items passed to it or the queue, and their dependencies. |
81 * Processes the items passed to it or the queue, and their dependencies. |
83 * |
82 * |
84 * @access public |
83 * @since 2.6.0 |
85 * @since 2.1.0 |
84 * @since 2.8.0 Added the `$group` parameter. |
86 * |
85 * |
87 * @param mixed $handles Optional. Items to be processed: Process queue (false), process item (string), process items (array of strings). |
86 * @param mixed $handles Optional. Items to be processed: Process queue (false), process item (string), process items (array of strings). |
88 * @param mixed $group Group level: level (int), no groups (false). |
87 * @param mixed $group Group level: level (int), no groups (false). |
89 * @return array Handles of items that have been processed. |
88 * @return array Handles of items that have been processed. |
90 */ |
89 */ |
94 * print that item. If an array is passed, print those items. |
93 * print that item. If an array is passed, print those items. |
95 */ |
94 */ |
96 $handles = false === $handles ? $this->queue : (array) $handles; |
95 $handles = false === $handles ? $this->queue : (array) $handles; |
97 $this->all_deps( $handles ); |
96 $this->all_deps( $handles ); |
98 |
97 |
99 foreach( $this->to_do as $key => $handle ) { |
98 foreach ( $this->to_do as $key => $handle ) { |
100 if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) { |
99 if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) { |
101 |
|
102 /* |
|
103 * A single item may alias a set of items, by having dependencies, |
|
104 * but no source. Queuing the item queues the dependencies. |
|
105 * |
|
106 * Example: The extending class WP_Scripts is used to register 'scriptaculous' as a set of registered handles: |
|
107 * <code>add( 'scriptaculous', false, array( 'scriptaculous-dragdrop', 'scriptaculous-slider', 'scriptaculous-controls' ) );</code> |
|
108 * |
|
109 * The src property is false. |
|
110 */ |
|
111 if ( ! $this->registered[$handle]->src ) { |
|
112 $this->done[] = $handle; |
|
113 continue; |
|
114 } |
|
115 |
|
116 /* |
100 /* |
117 * Attempt to process the item. If successful, |
101 * Attempt to process the item. If successful, |
118 * add the handle to the done array. |
102 * add the handle to the done array. |
119 * |
103 * |
120 * Unset the item from the to_do array. |
104 * Unset the item from the to_do array. |
128 |
112 |
129 return $this->done; |
113 return $this->done; |
130 } |
114 } |
131 |
115 |
132 /** |
116 /** |
133 * Process a dependency. |
117 * Processes a dependency. |
134 * |
118 * |
135 * @access public |
|
136 * @since 2.6.0 |
119 * @since 2.6.0 |
137 * |
120 * |
138 * @param string $handle Name of the item. Should be unique. |
121 * @param string $handle Name of the item. Should be unique. |
139 * @return bool True on success, false if not set. |
122 * @return bool True on success, false if not set. |
140 */ |
123 */ |
141 public function do_item( $handle ) { |
124 public function do_item( $handle ) { |
142 return isset($this->registered[$handle]); |
125 return isset($this->registered[$handle]); |
143 } |
126 } |
144 |
127 |
145 /** |
128 /** |
146 * Determine dependencies. |
129 * Determines dependencies. |
147 * |
130 * |
148 * Recursively builds an array of items to process taking |
131 * Recursively builds an array of items to process taking |
149 * dependencies into account. Does NOT catch infinite loops. |
132 * dependencies into account. Does NOT catch infinite loops. |
150 * |
133 * |
151 * @access public |
134 * @since 2.1.0 |
152 * @since 2.1.0 |
135 * @since 2.6.0 Moved from `WP_Scripts`. |
153 * |
136 * @since 2.8.0 Added the `$group` parameter. |
154 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
137 * |
155 * @param bool $recursion Internal flag that function is calling itself. |
138 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
156 * @param mixed $group Group level: (int) level, (false) no groups. |
139 * @param bool $recursion Internal flag that function is calling itself. |
|
140 * @param int|false $group Group level: (int) level, (false) no groups. |
157 * @return bool True on success, false on failure. |
141 * @return bool True on success, false on failure. |
158 */ |
142 */ |
159 public function all_deps( $handles, $recursion = false, $group = false ) { |
143 public function all_deps( $handles, $recursion = false, $group = false ) { |
160 if ( !$handles = (array) $handles ) |
144 if ( !$handles = (array) $handles ) |
161 return false; |
145 return false; |
166 $queued = in_array($handle, $this->to_do, true); |
150 $queued = in_array($handle, $this->to_do, true); |
167 |
151 |
168 if ( in_array($handle, $this->done, true) ) // Already done |
152 if ( in_array($handle, $this->done, true) ) // Already done |
169 continue; |
153 continue; |
170 |
154 |
171 $moved = $this->set_group( $handle, $recursion, $group ); |
155 $moved = $this->set_group( $handle, $recursion, $group ); |
|
156 $new_group = $this->groups[ $handle ]; |
172 |
157 |
173 if ( $queued && !$moved ) // already queued and in the right group |
158 if ( $queued && !$moved ) // already queued and in the right group |
174 continue; |
159 continue; |
175 |
160 |
176 $keep_going = true; |
161 $keep_going = true; |
177 if ( !isset($this->registered[$handle]) ) |
162 if ( !isset($this->registered[$handle]) ) |
178 $keep_going = false; // Item doesn't exist. |
163 $keep_going = false; // Item doesn't exist. |
179 elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) ) |
164 elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) ) |
180 $keep_going = false; // Item requires dependencies that don't exist. |
165 $keep_going = false; // Item requires dependencies that don't exist. |
181 elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $group ) ) |
166 elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $new_group ) ) |
182 $keep_going = false; // Item requires dependencies that don't exist. |
167 $keep_going = false; // Item requires dependencies that don't exist. |
183 |
168 |
184 if ( ! $keep_going ) { // Either item or its dependencies don't exist. |
169 if ( ! $keep_going ) { // Either item or its dependencies don't exist. |
185 if ( $recursion ) |
170 if ( $recursion ) |
186 return false; // Abort this branch. |
171 return false; // Abort this branch. |
203 /** |
188 /** |
204 * Register an item. |
189 * Register an item. |
205 * |
190 * |
206 * Registers the item if no item of that name already exists. |
191 * Registers the item if no item of that name already exists. |
207 * |
192 * |
208 * @access public |
193 * @since 2.1.0 |
209 * @since 2.1.0 |
194 * @since 2.6.0 Moved from `WP_Scripts`. |
210 * |
195 * |
211 * @param string $handle Unique item name. |
196 * @param string $handle Name of the item. Should be unique. |
212 * @param string $src The item url. |
197 * @param string $src Full URL of the item, or path of the item relative to the WordPress root directory. |
213 * @param array $deps Optional. An array of item handle strings on which this item depends. |
198 * @param array $deps Optional. An array of registered item handles this item depends on. Default empty array. |
214 * @param string $ver Optional. Version (used for cache busting). |
199 * @param string|bool|null $ver Optional. String specifying item version number, if it has one, which is added to the URL |
215 * @param mixed $args Optional. Custom property of the item. NOT the class property $args. Examples: $media, $in_footer. |
200 * as a query string for cache busting purposes. If version is set to false, a version |
216 * @return bool True on success, false on failure. |
201 * number is automatically added equal to current installed WordPress version. |
|
202 * If set to null, no version is added. |
|
203 * @param mixed $args Optional. Custom property of the item. NOT the class property $args. Examples: $media, $in_footer. |
|
204 * @return bool Whether the item has been registered. True on success, false on failure. |
217 */ |
205 */ |
218 public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) { |
206 public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) { |
219 if ( isset($this->registered[$handle]) ) |
207 if ( isset($this->registered[$handle]) ) |
220 return false; |
208 return false; |
221 $this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args ); |
209 $this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args ); |
245 /** |
232 /** |
246 * Get extra item data. |
233 * Get extra item data. |
247 * |
234 * |
248 * Gets data associated with a registered item. |
235 * Gets data associated with a registered item. |
249 * |
236 * |
250 * @access public |
|
251 * @since 3.3.0 |
237 * @since 3.3.0 |
252 * |
238 * |
253 * @param string $handle Name of the item. Should be unique. |
239 * @param string $handle Name of the item. Should be unique. |
254 * @param string $key The data key. |
240 * @param string $key The data key. |
255 * @return mixed Extra item data (string), false otherwise. |
241 * @return mixed Extra item data (string), false otherwise. |
284 * Decodes handles and arguments, then queues handles and stores |
270 * Decodes handles and arguments, then queues handles and stores |
285 * arguments in the class property $args. For example in extending |
271 * arguments in the class property $args. For example in extending |
286 * classes, $args is appended to the item url as a query string. |
272 * classes, $args is appended to the item url as a query string. |
287 * Note $args is NOT the $args property of items in the $registered array. |
273 * Note $args is NOT the $args property of items in the $registered array. |
288 * |
274 * |
289 * @access public |
275 * @since 2.1.0 |
290 * @since 2.1.0 |
276 * @since 2.6.0 Moved from `WP_Scripts`. |
291 * |
277 * |
292 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
278 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
293 */ |
279 */ |
294 public function enqueue( $handles ) { |
280 public function enqueue( $handles ) { |
295 foreach ( (array) $handles as $handle ) { |
281 foreach ( (array) $handles as $handle ) { |
306 * Dequeue an item or items. |
292 * Dequeue an item or items. |
307 * |
293 * |
308 * Decodes handles and arguments, then dequeues handles |
294 * Decodes handles and arguments, then dequeues handles |
309 * and removes arguments from the class property $args. |
295 * and removes arguments from the class property $args. |
310 * |
296 * |
311 * @access public |
297 * @since 2.1.0 |
312 * @since 2.1.0 |
298 * @since 2.6.0 Moved from `WP_Scripts`. |
313 * |
299 * |
314 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
300 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
315 */ |
301 */ |
316 public function dequeue( $handles ) { |
302 public function dequeue( $handles ) { |
317 foreach ( (array) $handles as $handle ) { |
303 foreach ( (array) $handles as $handle ) { |
329 * |
315 * |
330 * @since 4.0.0 |
316 * @since 4.0.0 |
331 * |
317 * |
332 * @param array $queue An array of queued _WP_Dependency handle objects. |
318 * @param array $queue An array of queued _WP_Dependency handle objects. |
333 * @param string $handle Name of the item. Should be unique. |
319 * @param string $handle Name of the item. Should be unique. |
334 * @return boolean Whether the handle is found after recursively searching the dependency tree. |
320 * @return bool Whether the handle is found after recursively searching the dependency tree. |
335 */ |
321 */ |
336 protected function recurse_deps( $queue, $handle ) { |
322 protected function recurse_deps( $queue, $handle ) { |
337 foreach ( $queue as $queued ) { |
323 foreach ( $queue as $queued ) { |
338 if ( ! isset( $this->registered[ $queued ] ) ) { |
324 if ( ! isset( $this->registered[ $queued ] ) ) { |
339 continue; |
325 continue; |
350 } |
336 } |
351 |
337 |
352 /** |
338 /** |
353 * Query list for an item. |
339 * Query list for an item. |
354 * |
340 * |
355 * @access public |
341 * @since 2.1.0 |
356 * @since 2.1.0 |
342 * @since 2.6.0 Moved from `WP_Scripts`. |
357 * |
343 * |
358 * @param string $handle Name of the item. Should be unique. |
344 * @param string $handle Name of the item. Should be unique. |
359 * @param string $list Property name of list array. |
345 * @param string $list Property name of list array. |
360 * @return bool Found, or object Item data. |
346 * @return bool|_WP_Dependency Found, or object Item data. |
361 */ |
347 */ |
362 public function query( $handle, $list = 'registered' ) { |
348 public function query( $handle, $list = 'registered' ) { |
363 switch ( $list ) { |
349 switch ( $list ) { |
364 case 'registered' : |
350 case 'registered' : |
365 case 'scripts': // back compat |
351 case 'scripts': // back compat |
386 } |
372 } |
387 |
373 |
388 /** |
374 /** |
389 * Set item group, unless already in a lower group. |
375 * Set item group, unless already in a lower group. |
390 * |
376 * |
391 * @access public |
|
392 * @since 2.8.0 |
377 * @since 2.8.0 |
393 * |
378 * |
394 * @param string $handle Name of the item. Should be unique. |
379 * @param string $handle Name of the item. Should be unique. |
395 * @param bool $recursion Internal flag that calling function was called recursively. |
380 * @param bool $recursion Internal flag that calling function was called recursively. |
396 * @param mixed $group Group level. |
381 * @param mixed $group Group level. |
397 * @return bool Not already in the group or a lower group |
382 * @return bool Not already in the group or a lower group |
398 */ |
383 */ |
399 public function set_group( $handle, $recursion, $group ) { |
384 public function set_group( $handle, $recursion, $group ) { |
400 $group = (int) $group; |
385 $group = (int) $group; |
401 |
386 |
402 if ( $recursion ) |
387 if ( isset( $this->groups[ $handle ] ) && $this->groups[ $handle ] <= $group ) { |
403 $group = min($this->group, $group); |
388 return false; |
404 else |
389 } |
405 $this->group = $group; |
390 |
406 |
391 $this->groups[ $handle ] = $group; |
407 if ( isset($this->groups[$handle]) && $this->groups[$handle] <= $group ) |
392 |
408 return false; |
|
409 |
|
410 $this->groups[$handle] = $group; |
|
411 return true; |
393 return true; |
412 } |
394 } |
413 |
395 |
414 } // WP_Dependencies |
396 } |
415 |
|
416 /** |
|
417 * Class _WP_Dependency |
|
418 * |
|
419 * Helper class to register a handle and associated data. |
|
420 * |
|
421 * @access private |
|
422 * @since 2.6.0 |
|
423 */ |
|
424 class _WP_Dependency { |
|
425 /** |
|
426 * The handle name. |
|
427 * |
|
428 * @access public |
|
429 * @since 2.6.0 |
|
430 * @var null |
|
431 */ |
|
432 public $handle; |
|
433 |
|
434 /** |
|
435 * The handle source. |
|
436 * |
|
437 * @access public |
|
438 * @since 2.6.0 |
|
439 * @var null |
|
440 */ |
|
441 public $src; |
|
442 |
|
443 /** |
|
444 * An array of handle dependencies. |
|
445 * |
|
446 * @access public |
|
447 * @since 2.6.0 |
|
448 * @var array |
|
449 */ |
|
450 public $deps = array(); |
|
451 |
|
452 /** |
|
453 * The handle version. |
|
454 * |
|
455 * Used for cache-busting. |
|
456 * |
|
457 * @access public |
|
458 * @since 2.6.0 |
|
459 * @var bool|string |
|
460 */ |
|
461 public $ver = false; |
|
462 |
|
463 /** |
|
464 * Additional arguments for the handle. |
|
465 * |
|
466 * @access public |
|
467 * @since 2.6.0 |
|
468 * @var null |
|
469 */ |
|
470 public $args = null; // Custom property, such as $in_footer or $media. |
|
471 |
|
472 /** |
|
473 * Extra data to supply to the handle. |
|
474 * |
|
475 * @access public |
|
476 * @since 2.6.0 |
|
477 * @var array |
|
478 */ |
|
479 public $extra = array(); |
|
480 |
|
481 /** |
|
482 * Setup dependencies. |
|
483 * |
|
484 * @since 2.6.0 |
|
485 */ |
|
486 public function __construct() { |
|
487 @list( $this->handle, $this->src, $this->deps, $this->ver, $this->args ) = func_get_args(); |
|
488 if ( ! is_array($this->deps) ) |
|
489 $this->deps = array(); |
|
490 } |
|
491 |
|
492 /** |
|
493 * Add handle data. |
|
494 * |
|
495 * @access public |
|
496 * @since 2.6.0 |
|
497 * |
|
498 * @param string $name The data key to add. |
|
499 * @param mixed $data The data value to add. |
|
500 * @return bool False if not scalar, true otherwise. |
|
501 */ |
|
502 public function add_data( $name, $data ) { |
|
503 if ( !is_scalar($name) ) |
|
504 return false; |
|
505 $this->extra[$name] = $data; |
|
506 return true; |
|
507 } |
|
508 |
|
509 } // _WP_Dependencies |
|