WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/14/2010 03:09:04 PM (11 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/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.