Prevent post and term hierarchy loops. Props mdawaffe. fixes #14662
git-svn-id: http://svn.automattic.com/wordpress/trunk@15806 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
@@ -4338,4 +4338,74 @@ function _wp_mysql_week( $column ) {
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
/**
|
||||
* Finds hierarchy loops using a callback function that maps objects to parents.
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param callback $callback function that accepts ( ID, callback_arg, ... ) and outputs parent_ID
|
||||
* @param $start The ID to start the loop check at
|
||||
* @param $start_parent the parent_ID of $start to use instead of calling $callback( $start ). Use null to always use $callback
|
||||
* @param array $override an array of ( ID => parent_ID, ... ) to use instead of $callback
|
||||
* @param array $callback_arg optional additional arguments to send to $callback
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @return array IDs of all members of loop
|
||||
*/
|
||||
function wp_find_hierarchy_loop( $callback, $start, $start_parent, $callback_args = array() ) {
|
||||
$override = is_null( $start_parent ) ? array() : array( $start => $start_parent );
|
||||
|
||||
echo "wp_find_hierarchy_loop: $callback, $start, $callback_args\n";
|
||||
if ( !$arbitrary_loop_member = wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override, $callback_args ) )
|
||||
return array();
|
||||
|
||||
return wp_find_hierarchy_loop_tortoise_hare( $callback, $arbitrary_loop_member, $override, $callback_args, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses 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.
|
||||
*
|
||||
* @since 3.1
|
||||
*
|
||||
* @param callback $callback function that accupts ( ID, callback_arg, ... ) and outputs parent_ID
|
||||
* @param $start The ID to start the loop check at
|
||||
* @param array $override an array of ( ID => parent_ID, ... ) to use instead of $callback
|
||||
* @param array $callback_args optional additional arguments to send to $callback
|
||||
* @param bool $_return_loop 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)
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @return mixed scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if $_return_loop
|
||||
*/
|
||||
function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) {
|
||||
$tortoise = $hare = $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] = $return[$evanescent_hare] = $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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user