74 * @var int |
74 * @var int |
75 */ |
75 */ |
76 public $group = 0; |
76 public $group = 0; |
77 |
77 |
78 /** |
78 /** |
|
79 * Cached lookup array of flattened queued items and dependencies. |
|
80 * |
|
81 * @since 5.4.0 |
|
82 * @var array |
|
83 */ |
|
84 private $all_queued_deps; |
|
85 |
|
86 /** |
79 * Processes the items and dependencies. |
87 * Processes the items and dependencies. |
80 * |
88 * |
81 * Processes the items passed to it or the queue, and their dependencies. |
89 * Processes the items passed to it or the queue, and their dependencies. |
82 * |
90 * |
83 * @since 2.6.0 |
91 * @since 2.6.0 |
84 * @since 2.8.0 Added the `$group` parameter. |
92 * @since 2.8.0 Added the `$group` parameter. |
85 * |
93 * |
86 * @param mixed $handles Optional. Items to be processed: Process queue (false), process item (string), process items (array of strings). |
94 * @param string|string[]|false $handles Optional. Items to be processed: queue (false), |
87 * @param mixed $group Group level: level (int), no groups (false). |
95 * single item (string), or multiple items (array of strings). |
88 * @return array Handles of items that have been processed. |
96 * Default false. |
|
97 * @param int|false $group Optional. Group level: level (int), no groups (false). |
|
98 * @return string[] Array of handles of items that have been processed. |
89 */ |
99 */ |
90 public function do_items( $handles = false, $group = false ) { |
100 public function do_items( $handles = false, $group = false ) { |
91 /* |
101 /* |
92 * If nothing is passed, print the queue. If a string is passed, |
102 * If nothing is passed, print the queue. If a string is passed, |
93 * print that item. If an array is passed, print those items. |
103 * print that item. If an array is passed, print those items. |
134 * |
147 * |
135 * @since 2.1.0 |
148 * @since 2.1.0 |
136 * @since 2.6.0 Moved from `WP_Scripts`. |
149 * @since 2.6.0 Moved from `WP_Scripts`. |
137 * @since 2.8.0 Added the `$group` parameter. |
150 * @since 2.8.0 Added the `$group` parameter. |
138 * |
151 * |
139 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
152 * @param string|string[] $handles Item handle (string) or item handles (array of strings). |
140 * @param bool $recursion Internal flag that function is calling itself. |
153 * @param bool $recursion Optional. Internal flag that function is calling itself. |
141 * @param int|false $group Group level: (int) level, (false) no groups. |
154 * Default false. |
|
155 * @param int|false $group Optional. Group level: level (int), no groups (false). |
|
156 * Default false. |
142 * @return bool True on success, false on failure. |
157 * @return bool True on success, false on failure. |
143 */ |
158 */ |
144 public function all_deps( $handles, $recursion = false, $group = false ) { |
159 public function all_deps( $handles, $recursion = false, $group = false ) { |
145 if ( ! $handles = (array) $handles ) { |
160 $handles = (array) $handles; |
|
161 if ( ! $handles ) { |
146 return false; |
162 return false; |
147 } |
163 } |
148 |
164 |
149 foreach ( $handles as $handle ) { |
165 foreach ( $handles as $handle ) { |
150 $handle_parts = explode( '?', $handle ); |
166 $handle_parts = explode( '?', $handle ); |
151 $handle = $handle_parts[0]; |
167 $handle = $handle_parts[0]; |
152 $queued = in_array( $handle, $this->to_do, true ); |
168 $queued = in_array( $handle, $this->to_do, true ); |
153 |
169 |
154 if ( in_array( $handle, $this->done, true ) ) { // Already done |
170 if ( in_array( $handle, $this->done, true ) ) { // Already done. |
155 continue; |
171 continue; |
156 } |
172 } |
157 |
173 |
158 $moved = $this->set_group( $handle, $recursion, $group ); |
174 $moved = $this->set_group( $handle, $recursion, $group ); |
159 $new_group = $this->groups[ $handle ]; |
175 $new_group = $this->groups[ $handle ]; |
160 |
176 |
161 if ( $queued && ! $moved ) { // already queued and in the right group |
177 if ( $queued && ! $moved ) { // Already queued and in the right group. |
162 continue; |
178 continue; |
163 } |
179 } |
164 |
180 |
165 $keep_going = true; |
181 $keep_going = true; |
166 if ( ! isset( $this->registered[ $handle ] ) ) { |
182 if ( ! isset( $this->registered[ $handle ] ) ) { |
200 * |
216 * |
201 * @since 2.1.0 |
217 * @since 2.1.0 |
202 * @since 2.6.0 Moved from `WP_Scripts`. |
218 * @since 2.6.0 Moved from `WP_Scripts`. |
203 * |
219 * |
204 * @param string $handle Name of the item. Should be unique. |
220 * @param string $handle Name of the item. Should be unique. |
205 * @param string|bool $src Full URL of the item, or path of the item relative to the WordPress root directory. |
221 * @param string|bool $src Full URL of the item, or path of the item relative |
206 * If source is set to false, item is an alias of other items it depends on. |
222 * to the WordPress root directory. If source is set to false, |
207 * @param string[] $deps Optional. An array of registered item handles this item depends on. Default empty array. |
223 * item is an alias of other items it depends on. |
208 * @param string|bool|null $ver Optional. String specifying item version number, if it has one, which is added to the URL |
224 * @param string[] $deps Optional. An array of registered item handles this item depends on. |
209 * as a query string for cache busting purposes. If version is set to false, a version |
225 * Default empty array. |
210 * number is automatically added equal to current installed WordPress version. |
226 * @param string|bool|null $ver Optional. String specifying item version number, if it has one, |
|
227 * which is added to the URL as a query string for cache busting purposes. |
|
228 * If version is set to false, a version number is automatically added |
|
229 * equal to current installed WordPress version. |
211 * If set to null, no version is added. |
230 * If set to null, no version is added. |
212 * @param mixed $args Optional. Custom property of the item. NOT the class property $args. Examples: $media, $in_footer. |
231 * @param mixed $args Optional. Custom property of the item. NOT the class property $args. |
|
232 * Examples: $media, $in_footer. |
213 * @return bool Whether the item has been registered. True on success, false on failure. |
233 * @return bool Whether the item has been registered. True on success, false on failure. |
214 */ |
234 */ |
215 public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) { |
235 public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) { |
216 if ( isset( $this->registered[ $handle ] ) ) { |
236 if ( isset( $this->registered[ $handle ] ) ) { |
217 return false; |
237 return false; |
287 * Note $args is NOT the $args property of items in the $registered array. |
306 * Note $args is NOT the $args property of items in the $registered array. |
288 * |
307 * |
289 * @since 2.1.0 |
308 * @since 2.1.0 |
290 * @since 2.6.0 Moved from `WP_Scripts`. |
309 * @since 2.6.0 Moved from `WP_Scripts`. |
291 * |
310 * |
292 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
311 * @param string|string[] $handles Item handle (string) or item handles (array of strings). |
293 */ |
312 */ |
294 public function enqueue( $handles ) { |
313 public function enqueue( $handles ) { |
295 foreach ( (array) $handles as $handle ) { |
314 foreach ( (array) $handles as $handle ) { |
296 $handle = explode( '?', $handle ); |
315 $handle = explode( '?', $handle ); |
297 if ( ! in_array( $handle[0], $this->queue ) && isset( $this->registered[ $handle[0] ] ) ) { |
316 |
|
317 if ( ! in_array( $handle[0], $this->queue, true ) && isset( $this->registered[ $handle[0] ] ) ) { |
298 $this->queue[] = $handle[0]; |
318 $this->queue[] = $handle[0]; |
|
319 |
|
320 // Reset all dependencies so they must be recalculated in recurse_deps(). |
|
321 $this->all_queued_deps = null; |
|
322 |
299 if ( isset( $handle[1] ) ) { |
323 if ( isset( $handle[1] ) ) { |
300 $this->args[ $handle[0] ] = $handle[1]; |
324 $this->args[ $handle[0] ] = $handle[1]; |
301 } |
325 } |
302 } |
326 } |
303 } |
327 } |
310 * and removes arguments from the class property $args. |
334 * and removes arguments from the class property $args. |
311 * |
335 * |
312 * @since 2.1.0 |
336 * @since 2.1.0 |
313 * @since 2.6.0 Moved from `WP_Scripts`. |
337 * @since 2.6.0 Moved from `WP_Scripts`. |
314 * |
338 * |
315 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings). |
339 * @param string|string[] $handles Item handle (string) or item handles (array of strings). |
316 */ |
340 */ |
317 public function dequeue( $handles ) { |
341 public function dequeue( $handles ) { |
318 foreach ( (array) $handles as $handle ) { |
342 foreach ( (array) $handles as $handle ) { |
319 $handle = explode( '?', $handle ); |
343 $handle = explode( '?', $handle ); |
320 $key = array_search( $handle[0], $this->queue ); |
344 $key = array_search( $handle[0], $this->queue, true ); |
|
345 |
321 if ( false !== $key ) { |
346 if ( false !== $key ) { |
|
347 // Reset all dependencies so they must be recalculated in recurse_deps(). |
|
348 $this->all_queued_deps = null; |
|
349 |
322 unset( $this->queue[ $key ] ); |
350 unset( $this->queue[ $key ] ); |
323 unset( $this->args[ $handle[0] ] ); |
351 unset( $this->args[ $handle[0] ] ); |
324 } |
352 } |
325 } |
353 } |
326 } |
354 } |
327 |
355 |
328 /** |
356 /** |
329 * Recursively search the passed dependency tree for $handle |
357 * Recursively search the passed dependency tree for $handle. |
330 * |
358 * |
331 * @since 4.0.0 |
359 * @since 4.0.0 |
332 * |
360 * |
333 * @param string[] $queue An array of queued _WP_Dependency handles. |
361 * @param string[] $queue An array of queued _WP_Dependency handles. |
334 * @param string $handle Name of the item. Should be unique. |
362 * @param string $handle Name of the item. Should be unique. |
335 * @return bool Whether the handle is found after recursively searching the dependency tree. |
363 * @return bool Whether the handle is found after recursively searching the dependency tree. |
336 */ |
364 */ |
337 protected function recurse_deps( $queue, $handle ) { |
365 protected function recurse_deps( $queue, $handle ) { |
338 foreach ( $queue as $queued ) { |
366 if ( isset( $this->all_queued_deps ) ) { |
339 if ( ! isset( $this->registered[ $queued ] ) ) { |
367 return isset( $this->all_queued_deps[ $handle ] ); |
340 continue; |
368 } |
341 } |
369 |
342 |
370 $all_deps = array_fill_keys( $queue, true ); |
343 if ( in_array( $handle, $this->registered[ $queued ]->deps ) ) { |
371 $queues = array(); |
344 return true; |
372 $done = array(); |
345 } elseif ( $this->recurse_deps( $this->registered[ $queued ]->deps, $handle ) ) { |
373 |
346 return true; |
374 while ( $queue ) { |
347 } |
375 foreach ( $queue as $queued ) { |
348 } |
376 if ( ! isset( $done[ $queued ] ) && isset( $this->registered[ $queued ] ) ) { |
349 |
377 $deps = $this->registered[ $queued ]->deps; |
350 return false; |
378 if ( $deps ) { |
|
379 $all_deps += array_fill_keys( $deps, true ); |
|
380 array_push( $queues, $deps ); |
|
381 } |
|
382 $done[ $queued ] = true; |
|
383 } |
|
384 } |
|
385 $queue = array_pop( $queues ); |
|
386 } |
|
387 |
|
388 $this->all_queued_deps = $all_deps; |
|
389 |
|
390 return isset( $this->all_queued_deps[ $handle ] ); |
351 } |
391 } |
352 |
392 |
353 /** |
393 /** |
354 * Query list for an item. |
394 * Query list for an item. |
355 * |
395 * |
356 * @since 2.1.0 |
396 * @since 2.1.0 |
357 * @since 2.6.0 Moved from `WP_Scripts`. |
397 * @since 2.6.0 Moved from `WP_Scripts`. |
358 * |
398 * |
359 * @param string $handle Name of the item. Should be unique. |
399 * @param string $handle Name of the item. Should be unique. |
360 * @param string $list Property name of list array. |
400 * @param string $list Optional. Property name of list array. Default 'registered'. |
361 * @return bool|_WP_Dependency Found, or object Item data. |
401 * @return bool|_WP_Dependency Found, or object Item data. |
362 */ |
402 */ |
363 public function query( $handle, $list = 'registered' ) { |
403 public function query( $handle, $list = 'registered' ) { |
364 switch ( $list ) { |
404 switch ( $list ) { |
365 case 'registered': |
405 case 'registered': |
366 case 'scripts': // back compat |
406 case 'scripts': // Back compat. |
367 if ( isset( $this->registered[ $handle ] ) ) { |
407 if ( isset( $this->registered[ $handle ] ) ) { |
368 return $this->registered[ $handle ]; |
408 return $this->registered[ $handle ]; |
369 } |
409 } |
370 return false; |
410 return false; |
371 |
411 |
372 case 'enqueued': |
412 case 'enqueued': |
373 case 'queue': |
413 case 'queue': |
374 if ( in_array( $handle, $this->queue ) ) { |
414 if ( in_array( $handle, $this->queue, true ) ) { |
375 return true; |
415 return true; |
376 } |
416 } |
377 return $this->recurse_deps( $this->queue, $handle ); |
417 return $this->recurse_deps( $this->queue, $handle ); |
378 |
418 |
379 case 'to_do': |
419 case 'to_do': |
380 case 'to_print': // back compat |
420 case 'to_print': // Back compat. |
381 return in_array( $handle, $this->to_do ); |
421 return in_array( $handle, $this->to_do, true ); |
382 |
422 |
383 case 'done': |
423 case 'done': |
384 case 'printed': // back compat |
424 case 'printed': // Back compat. |
385 return in_array( $handle, $this->done ); |
425 return in_array( $handle, $this->done, true ); |
386 } |
426 } |
387 return false; |
427 return false; |
388 } |
428 } |
389 |
429 |
390 /** |
430 /** |
391 * Set item group, unless already in a lower group. |
431 * Set item group, unless already in a lower group. |
392 * |
432 * |
393 * @since 2.8.0 |
433 * @since 2.8.0 |
394 * |
434 * |
395 * @param string $handle Name of the item. Should be unique. |
435 * @param string $handle Name of the item. Should be unique. |
396 * @param bool $recursion Internal flag that calling function was called recursively. |
436 * @param bool $recursion Internal flag that calling function was called recursively. |
397 * @param mixed $group Group level. |
437 * @param int|false $group Group level: level (int), no groups (false). |
398 * @return bool Not already in the group or a lower group |
438 * @return bool Not already in the group or a lower group. |
399 */ |
439 */ |
400 public function set_group( $handle, $recursion, $group ) { |
440 public function set_group( $handle, $recursion, $group ) { |
401 $group = (int) $group; |
441 $group = (int) $group; |
402 |
442 |
403 if ( isset( $this->groups[ $handle ] ) && $this->groups[ $handle ] <= $group ) { |
443 if ( isset( $this->groups[ $handle ] ) && $this->groups[ $handle ] <= $group ) { |