WordPress.org

Make WordPress Core

Ticket #14662: 14662.c.diff

File 14662.c.diff, 8.6 KB (added by mdawaffe, 9 years ago)

PHP4 compatible version

  • wp-includes/default-filters.php

     
    117117        add_filter( $filter, 'convert_chars' );
    118118}
    119119
     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 );
     123
    120124// Display filters
    121125add_filter( 'the_title', 'wptexturize'   );
    122126add_filter( 'the_title', 'convert_chars' );
  • wp-includes/taxonomy.php

     
    20392039                }
    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 ) );
    20442047        if ( $id && ($id != $term_id) ) {
     
    28792882
    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}
  • wp-includes/post.php

     
    23202320        else
    23212321                $post_parent = 0;
    23222322
    2323         if ( !empty($post_ID) ) {
    2324                 if ( $post_parent == $post_ID ) {
    2325                         // Post can't be its own parent
    2326                         $post_parent = 0;
    2327                 } elseif ( !empty($post_parent) ) {
    2328                         $parent_post = get_post($post_parent);
    2329                         // Check for circular dependency
    2330                         if ( isset( $parent_post->post_parent ) && $parent_post->post_parent == $post_ID )
    2331                                 $post_parent = 0;
    2332                 }
    2333         }
     2323        // Check the post_parent to see if it will cause a hierarchy loop
     2324        $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );
    23342325
    23352326        if ( isset($menu_order) )
    23362327                $menu_order = (int) $menu_order;
     
    47874778        }
    47884779}
    47894780
     4781/**
     4782 * Returns the post's parent's post_ID
     4783 *
     4784 * @since 3.1
     4785 *
     4786 * @param int $post_id
     4787 *
     4788 * @return int|bool false on error
     4789 */
     4790function wp_get_post_parent_id( $post_ID ) {
     4791        $post = get_post( $post_ID );
     4792        if ( !$post || is_wp_error( $post ) )
     4793                return false;
     4794        return (int) $post->post_parent;
     4795}
    47904796
    47914797/**
     4798 * Checks the given subset of the post hierarchy for hierarchy loops.
     4799 * Prevents loops from forming and breaks those that it finds.
     4800 *
     4801 * Attached to the wp_insert_post_parent filter.
     4802 *
     4803 * @since 3.1
     4804 * @uses wp_find_hierarchy_loop()
     4805 *
     4806 * @param int $post_parent ID of the parent for the post we're checking.
     4807 * @parem int $post_ID ID of the post we're checking.
     4808 *
     4809 * @return int The new post_parent for the post.
     4810 */
     4811function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) {
     4812        // Nothing fancy here - bail
     4813        if ( !$post_parent )
     4814                return 0;
     4815
     4816        // New post can't cause a loop
     4817        if ( empty( $post_ID ) )
     4818                return $post_parent;
     4819
     4820        // Can't be its own parent
     4821        if ( $post_parent == $post_ID )
     4822                return 0;
     4823
     4824        // Now look for larger loops
     4825
     4826        if ( !$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) )
     4827                return $post_parent; // No loop
     4828
     4829        // Setting $post_parent to the given value causes a loop
     4830        if ( isset( $loop[$post_ID] ) )
     4831                return 0;
     4832
     4833        // There's a loop, but it doesn't contain $post_ID.  Break the loop.
     4834        foreach ( array_keys( $loop ) as $loop_member )
     4835                wp_update_post( array( 'ID' => $loop_member, 'post_parent' => 0 ) );
     4836
     4837        return $post_parent;
     4838}
     4839
     4840/**
    47924841 * Default post information to use when populating the "Write Post" form.
    47934842 *
    47944843 * @since unknown
  • wp-includes/functions.php

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