Used By
Used By | Description |
---|---|
wp-includes/functions.php: wp_find_hierarchy_loop() | Find hierarchy loops using a callback function that maps object IDs to parent IDs. |
This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.
Use the “The Tortoise and the Hare” algorithm to detect loops.
For every step of the algorithm, the hare takes two steps and the tortoise one. If the hare ever laps the tortoise, there must be a loop.
(callable) (Required) Function that accepts ( ID, callback_arg, ... ) and outputs parent_ID.
(int) (Required) The ID to start the loop check at.
(array) (Optional) An array of ( ID => parent_ID, ... ) to use instead of $callback.
Default value: array()
(array) (Optional) Additional arguments to send to $callback.
Default value: array()
(bool) (Optional) Return loop members or just detect presence of loop? Only set to true if you already know the given $start is part of a loop (otherwise the returned array might include branches).
Default value: false
(mixed) Scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if $_return_loop
File: wp-includes/functions.php
function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) { $tortoise = $start; $hare = $start; $evanescent_hare = $start; $return = array(); // Set evanescent_hare to one past hare. // Increment hare two steps. while ( $tortoise && ( $evanescent_hare = isset( $override[ $hare ] ) ? $override[ $hare ] : call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) ) && ( $hare = isset( $override[ $evanescent_hare ] ) ? $override[ $evanescent_hare ] : call_user_func_array( $callback, array_merge( array( $evanescent_hare ), $callback_args ) ) ) ) { if ( $_return_loop ) { $return[ $tortoise ] = true; $return[ $evanescent_hare ] = true; $return[ $hare ] = true; } // Tortoise got lapped - must be a loop. if ( $tortoise == $evanescent_hare || $tortoise == $hare ) { return $_return_loop ? $return : $tortoise; } // Increment tortoise by one step. $tortoise = isset( $override[ $tortoise ] ) ? $override[ $tortoise ] : call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) ); } return false; }
Version | Description |
---|---|
3.1.0 | Introduced. |
© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_find_hierarchy_loop_tortoise_hare