WordPress.org

Make WordPress Core


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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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/**
Note: See TracChangeset for help on using the changeset viewer.