Make WordPress Core


Ignore:
Timestamp:
09/22/2007 06:01:08 PM (18 years ago)
Author:
ryan
Message:

Term with slug that conflicts with existing term with different parent gets a new term entry with a unique slug. see #5034

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/taxonomy.php

    r6125 r6157  
    708708        $value = apply_filters("pre_term_$field", $value, $taxonomy);
    709709        $value = apply_filters("pre_${taxonomy}_$field", $value);
     710        // Back compat filters
     711        if ( 'slug' == $field )
     712            $value = apply_filters('pre_category_nicename', $value);
     713           
    710714    } else if ( 'rss' == $context ) {
    711715        $value = apply_filters("term_${field}_rss", $value, $taxonomy);
     
    915919    $args = wp_parse_args($args, $defaults);
    916920    $args['name'] = $term;
     921    $args['taxonomy'] = $taxonomy;
    917922    $args = sanitize_term($args, $taxonomy, 'db');
    918923    extract($args, EXTR_SKIP);
     
    937942        $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$name', '$slug', '$term_group')");
    938943        $term_id = (int) $wpdb->insert_id;
     944    } else if ( is_taxonomy_hierarchical($taxonomy) && !empty($parent) ) {
     945        // If the taxonomy supports hierarchy and the term has a parent, make the slug unique
     946        // by incorporating parent slugs.
     947        $slug = wp_unique_term_slug($slug, (object) $args);
     948        $wpdb->query("INSERT INTO $wpdb->terms (name, slug, term_group) VALUES ('$name', '$slug', '$term_group')");
     949        $term_id = (int) $wpdb->insert_id;
    939950    }
    940951
     
    10201031}
    10211032
     1033function wp_unique_term_slug($slug, $term) {
     1034    global $wpdb;
     1035
     1036    // If the taxonomy supports hierarchy and the term has a parent, make the slug unique
     1037    // by incorporating parent slugs.
     1038    if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) {
     1039        $the_parent = $term->parent;
     1040        while ( ! empty($the_parent) ) {
     1041            $parent_term = get_term($the_parent, $term->taxonomy);
     1042            if ( is_wp_error($parent_term) || empty($parent_term) )
     1043                break;
     1044                $slug .= '-' . $parent_term->slug;
     1045            if ( empty($parent_term->parent) )
     1046                break;
     1047            $the_parent = $parent_term->parent;
     1048        }
     1049    }
     1050
     1051    // If we didn't get a unique slug, try appending a number to make it unique.
     1052    if ( $wpdb->get_var("SELECT slug FROM $wpdb->terms WHERE slug = '$slug'") ) {
     1053        $num = 2;
     1054        do {
     1055            $alt_slug = $slug . "-$num";
     1056            $num++;
     1057            $slug_check = $wpdb->get_var("SELECT slug FROM $wpdb->terms WHERE slug = '$alt_slug'");
     1058        } while ( $slug_check );
     1059        $slug = $alt_slug;
     1060    }
     1061
     1062    return $slug;
     1063}
     1064
    10221065function wp_update_term( $term, $taxonomy, $args = array() ) {
    10231066    global $wpdb;
     
    10421085    extract($args, EXTR_SKIP);
    10431086
    1044     if ( empty($slug) )
     1087    $empty_slug = false;
     1088    if ( empty($slug) ) {
     1089        $empty_slug = true;
    10451090        $slug = sanitize_title($name);
     1091    }
    10461092
    10471093    if ( $alias_of ) {
     
    10571103    }
    10581104
     1105    // Check for duplicate slug
     1106    $id = $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE slug = '$slug'");
     1107    if ( $id && ($id != $term_id) ) {
     1108        // If an empty slug was passed, reset the slug to something unique.
     1109        // Otherwise, bail.
     1110        if ( $empty_slug )
     1111            $slug = wp_unique_term_slug($slug, (object) $args);
     1112        else
     1113            return new WP_Error('duplicate_term_slug', sprintf(__('The slug "%s" is already in use by another term'), $slug));
     1114    }
     1115
    10591116    $wpdb->query("UPDATE $wpdb->terms SET name = '$name', slug = '$slug', term_group = '$term_group' WHERE term_id = '$term_id'");
    10601117
Note: See TracChangeset for help on using the changeset viewer.