Make WordPress Core

Ticket #20783: 20783.3.diff

File 20783.3.diff, 3.8 KB (added by boonebgorges, 11 years ago)
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index 75873c3..23125e5 100644
    function wp_remove_object_terms( $object_id, $terms, $taxonomy ) { 
    33243324 * @global wpdb $wpdb WordPress database abstraction object.
    33253325 *
    33263326 * @param string $slug The string that will be tried for a unique slug.
    3327  * @param object $term The term object that the $slug will belong too.
     3327 * @param object $term The term object that the `$slug` will belong to.
    33283328 * @return string Will return a true unique slug.
    33293329 */
    33303330function wp_unique_term_slug( $slug, $term ) {
    33313331        global $wpdb;
    33323332
    3333         if ( ! term_exists( $slug ) )
    3334                 return $slug;
     3333        $is_bad = true;
     3334        $original_slug = $slug;
    33353335
    33363336        // As of 4.1, duplicate slugs are allowed as long as they're in different taxonomies.
    3337         if ( get_option( 'db_version' ) >= 30133 && ! get_term_by( 'slug', $slug, $term->taxonomy ) ) {
    3338                 return $slug;
     3337        if ( ! term_exists( $slug ) || get_option( 'db_version' ) >= 30133 && ! get_term_by( 'slug', $slug, $term->taxonomy ) ) {
     3338                $is_bad = false;
    33393339        }
    33403340
    33413341        /*
    33423342         * If the taxonomy supports hierarchy and the term has a parent, make the slug unique
    33433343         * by incorporating parent slugs.
    33443344         */
    3345         if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) {
     3345        $parent_suffix = '';
     3346        if ( $is_bad && is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) {
    33463347                $the_parent = $term->parent;
    33473348                while ( ! empty($the_parent) ) {
    33483349                        $parent_term = get_term($the_parent, $term->taxonomy);
    33493350                        if ( is_wp_error($parent_term) || empty($parent_term) )
    33503351                                break;
    3351                         $slug .= '-' . $parent_term->slug;
    3352                         if ( ! term_exists( $slug ) )
    3353                                 return $slug;
     3352                        $parent_suffix .= '-' . $parent_term->slug;
     3353                        if ( ! term_exists( $slug . $parent_suffix ) ) {
     3354                                break;
     3355                        }
    33543356
    33553357                        if ( empty($parent_term->parent) )
    33563358                                break;
    function wp_unique_term_slug( $slug, $term ) { 
    33593361        }
    33603362
    33613363        // If we didn't get a unique slug, try appending a number to make it unique.
    3362         if ( ! empty( $term->term_id ) )
    3363                 $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id );
    3364         else
    3365                 $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
    3366 
    3367         if ( $wpdb->get_var( $query ) ) {
    3368                 $num = 2;
    3369                 do {
    3370                         $alt_slug = $slug . "-$num";
    3371                         $num++;
    3372                         $slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );
    3373                 } while ( $slug_check );
    3374                 $slug = $alt_slug;
     3364        /**
     3365         * Filter whether the proposed unique term slug is bad.
     3366         *
     3367         * @since 4.3.0
     3368         *
     3369         * @param bool   $is_bad Whether the slug is bad.
     3370         * @param string $slug   The slug.
     3371         * @param object $term   Term object.
     3372         */
     3373        if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $is_bad, $slug, $term ) ) {
     3374                if ( $parent_suffix ) {
     3375                        $slug .= $parent_suffix;
     3376                } else {
     3377                        if ( ! empty( $term->term_id ) )
     3378                                $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id );
     3379                        else
     3380                                $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
     3381
     3382                        if ( $wpdb->get_var( $query ) ) {
     3383                                $num = 2;
     3384                                do {
     3385                                        $alt_slug = $slug . "-$num";
     3386                                        $num++;
     3387                                        $slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );
     3388                                } while ( $slug_check );
     3389                                $slug = $alt_slug;
     3390                        }
     3391                }
    33753392        }
    33763393
    3377         return $slug;
     3394        /**
     3395         * Filter the unique term slug.
     3396         *
     3397         * @since 4.3.0
     3398         *
     3399         * @param string $slug          Unique term slug.
     3400         * @param object $term          Term object.
     3401         * @param string $original_slug Slug originally passed to the function for testing.
     3402         */
     3403        return apply_filters( 'wp_unique_term_slug', $slug, $term, $original_slug );
    33783404}
    33793405
    33803406/**