wppaste
WordPress

wp_find_hierarchy_loop_tortoise_hare( callable $callback, int $start, array $override = array(), array $callback_args = array(), bool $_return_loop = false ): mixed

Since
3.1.0
Source
wp-includes/functions.php:7114
Uses the "The Tortoise and the Hare" algorithm to detect loops.

Description

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.

Parameters

$callbackcallable
Function that accepts ( ID, callback_arg, ... ) and outputs parent_ID.
$startint
The ID to start the loop check at.
$overridearrayoptional
An array of ( ID => parent_ID, ... ) to use instead of $callback. Default empty array.Default: array()
$callback_argsarrayoptional
Additional arguments to send to $callback. Default empty array.Default: array()
$_return_loopbooloptional
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 false.Default: false

Return

mixed
Scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if $_return_loop

Used by · 1

Source

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;}

History

Introduced in 3.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.