Make WordPress Core

Changeset 15806


Ignore:
Timestamp:
10/14/2010 03:09:04 PM (14 years ago)
Author:
ryan
Message:

Prevent post and term hierarchy loops. Props mdawaffe. fixes #14662

Location:
trunk/wp-includes
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/default-filters.php

    r15747 r15806  
    117117    add_filter( $filter, 'convert_chars' );
    118118}
     119
     120// Pre save hierarchy
     121add_filter( 'wp_insert_post_parent', 'wp_check_post_hierarchy_for_loops', 10, 2 );
     122add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 );
    119123
    120124// Display filters
  • trunk/wp-includes/functions.php

    r15746 r15806  
    43394339}
    43404340
    4341 ?>
     4341/**
     4342 * Finds hierarchy loops using a callback function that maps objects to parents.
     4343 *
     4344 * @since 3.1
     4345 *
     4346 * @param callback $callback function that accepts ( ID, callback_arg, ... ) and outputs parent_ID
     4347 * @param $start The ID to start the loop check at
     4348 * @param $start_parent the parent_ID of $start to use instead of calling $callback( $start ).  Use null to always use $callback
     4349 * @param array $override an array of ( ID => parent_ID, ... ) to use instead of $callback
     4350 * @param array $callback_arg optional additional arguments to send to $callback
     4351 *
     4352 * @internal
     4353 *
     4354 * @return array IDs of all members of loop
     4355 */
     4356function wp_find_hierarchy_loop( $callback, $start, $start_parent, $callback_args = array() ) {
     4357    $override = is_null( $start_parent ) ? array() : array( $start => $start_parent );
     4358
     4359    echo "wp_find_hierarchy_loop: $callback, $start, $callback_args\n";
     4360    if ( !$arbitrary_loop_member = wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override, $callback_args ) )
     4361        return array();
     4362
     4363    return wp_find_hierarchy_loop_tortoise_hare( $callback, $arbitrary_loop_member, $override, $callback_args, true );
     4364}
     4365
     4366/**
     4367 * Uses the "The Tortoise and the Hare" algorithm to detect loops.
     4368 *
     4369 * For every step of the algorithm, the hare takes two steps and the tortoise one.
     4370 * If the hare ever laps the tortoise, there must be a loop.
     4371 *
     4372 * @since 3.1
     4373 *
     4374 * @param callback $callback function that accupts ( ID, callback_arg, ... ) and outputs parent_ID
     4375 * @param $start The ID to start the loop check at
     4376 * @param array $override an array of ( ID => parent_ID, ... ) to use instead of $callback
     4377 * @param array $callback_args optional additional arguments to send to $callback
     4378 * @param bool $_return_loop Return loop members or just detect presence of loop?
     4379 *             Only set to true if you already know the given $start is part of a loop
     4380 *             (otherwise the returned array might include branches)
     4381 *
     4382 * @internal
     4383 *
     4384 * @return mixed scalar ID of some arbitrary member of the loop, or array of IDs of all members of loop if $_return_loop
     4385 */
     4386function wp_find_hierarchy_loop_tortoise_hare( $callback, $start, $override = array(), $callback_args = array(), $_return_loop = false ) {
     4387    $tortoise = $hare = $evanescent_hare = $start;
     4388    $return = array();
     4389
     4390    // Set evanescent_hare to one past hare
     4391    // Increment hare two steps
     4392    while (
     4393        $tortoise
     4394    &&
     4395        ( $evanescent_hare = isset( $override[$hare] ) ? $override[$hare] : call_user_func_array( $callback, array_merge( array( $hare ), $callback_args ) ) )
     4396    &&
     4397        ( $hare = isset( $override[$evanescent_hare] ) ? $override[$evanescent_hare] : call_user_func_array( $callback, array_merge( array( $evanescent_hare ), $callback_args ) ) )
     4398    ) {
     4399        if ( $_return_loop )
     4400            $return[$tortoise] = $return[$evanescent_hare] = $return[$hare] = true;
     4401
     4402        // tortoise got lapped - must be a loop
     4403        if ( $tortoise == $evanescent_hare || $tortoise == $hare )
     4404            return $_return_loop ? $return : $tortoise;
     4405
     4406        // Increment tortoise by one step
     4407        $tortoise = isset( $override[$tortoise] ) ? $override[$tortoise] : call_user_func_array( $callback, array_merge( array( $tortoise ), $callback_args ) );
     4408    }
     4409
     4410    return false;
     4411}
  • trunk/wp-includes/post.php

    r15804 r15806  
    23382338        $post_parent = 0;
    23392339
    2340     if ( !empty($post_ID) ) {
    2341         if ( $post_parent == $post_ID ) {
    2342             // Post can't be its own parent
    2343             $post_parent = 0;
    2344         } elseif ( !empty($post_parent) ) {
    2345             $parent_post = get_post($post_parent);
    2346             // Check for circular dependency
    2347             if ( isset( $parent_post->post_parent ) && $parent_post->post_parent == $post_ID )
    2348                 $post_parent = 0;
    2349         }
    2350     }
     2340    // Check the post_parent to see if it will cause a hierarchy loop
     2341    $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );
    23512342
    23522343    if ( isset($menu_order) )
     
    48054796}
    48064797
     4798/**
     4799 * Returns the post's parent's post_ID
     4800 *
     4801 * @since 3.1
     4802 *
     4803 * @param int $post_id
     4804 *
     4805 * @return int|bool false on error
     4806 */
     4807function wp_get_post_parent_id( $post_ID ) {
     4808    $post = get_post( $post_ID );
     4809    if ( !$post || is_wp_error( $post ) )
     4810        return false;
     4811    return (int) $post->post_parent;
     4812}
     4813
     4814/**
     4815 * Checks the given subset of the post hierarchy for hierarchy loops.
     4816 * Prevents loops from forming and breaks those that it finds.
     4817 *
     4818 * Attached to the wp_insert_post_parent filter.
     4819 *
     4820 * @since 3.1
     4821 * @uses wp_find_hierarchy_loop()
     4822 *
     4823 * @param int $post_parent ID of the parent for the post we're checking.
     4824 * @parem int $post_ID ID of the post we're checking.
     4825 *
     4826 * @return int The new post_parent for the post.
     4827 */
     4828function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) {
     4829    // Nothing fancy here - bail
     4830    if ( !$post_parent )
     4831        return 0;
     4832
     4833    // New post can't cause a loop
     4834    if ( empty( $post_ID ) )
     4835        return $post_parent;
     4836
     4837    // Can't be its own parent
     4838    if ( $post_parent == $post_ID )
     4839        return 0;
     4840
     4841    // Now look for larger loops
     4842
     4843    if ( !$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) )
     4844        return $post_parent; // No loop
     4845
     4846    // Setting $post_parent to the given value causes a loop
     4847    if ( isset( $loop[$post_ID] ) )
     4848        return 0;
     4849
     4850    // There's a loop, but it doesn't contain $post_ID.  Break the loop.
     4851    foreach ( array_keys( $loop ) as $loop_member )
     4852        wp_update_post( array( 'ID' => $loop_member, 'post_parent' => 0 ) );
     4853
     4854    return $post_parent;
     4855}
    48074856
    48084857/**
  • trunk/wp-includes/taxonomy.php

    r15797 r15806  
    20402040    }
    20412041
     2042    // Check $parent to see if it will cause a hierarchy loop
     2043    $parent = apply_filters( 'wp_update_term_parent', $parent, $term_id, $taxonomy, compact( array_keys( $args ) ), $args );
     2044
    20422045    // Check for duplicate slug
    20432046    $id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) );
     
    28802883    return $tags_to_edit;
    28812884}
     2885
     2886/**
     2887 * Returns the term's parent's term_ID
     2888 *
     2889 * @since 3.1
     2890 *
     2891 * @param int $term_id
     2892 * @param string $taxonomy
     2893 *
     2894 * @return int|bool false on error
     2895 */
     2896function wp_get_term_taxonomy_parent_id( $term_id, $taxonomy ) {
     2897    $term = get_term( $term_id, $taxonomy );
     2898    if ( !$term || is_wp_error( $term ) )
     2899        return false;
     2900    return (int) $term->parent;
     2901}
     2902
     2903/**
     2904 * Checks the given subset of the term hierarchy for hierarchy loops.
     2905 * Prevents loops from forming and breaks those that it finds.
     2906 *
     2907 * Attached to the wp_update_term_parent filter.
     2908 *
     2909 * @since 3.1
     2910 * @uses wp_find_hierarchy_loop()
     2911 *
     2912 * @param int $parent term_id of the parent for the term we're checking.
     2913 * @param int $term_id The term we're checking.
     2914 * @param string $taxonomy The taxonomy of the term we're checking.
     2915 *
     2916 * @return int The new parent for the term.
     2917 */
     2918function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) {
     2919    // Nothing fancy here - bail
     2920    if ( !$parent )
     2921        return 0;
     2922
     2923    // Can't be its own parent
     2924    if ( $parent == $term_id )
     2925        return 0;
     2926
     2927    echo "larger loops\n";
     2928
     2929    // Now look for larger loops
     2930
     2931    if ( !$loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) )
     2932        return $parent; // No loop
     2933
     2934    // Setting $parent to the given value causes a loop
     2935    if ( isset( $loop[$term_id] ) )
     2936        return 0;
     2937
     2938    // There's a loop, but it doesn't contain $term_id.  Break the loop.
     2939    foreach ( array_keys( $loop ) as $loop_member )
     2940        wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );
     2941
     2942    return $parent;
     2943}
Note: See TracChangeset for help on using the changeset viewer.