Make WordPress Core

Changeset 32837


Ignore:
Timestamp:
06/18/2015 01:19:12 PM (9 years ago)
Author:
boonebgorges
Message:

Add filters to wp_unique_term_slug().

This changeset adds two new filters:

  • 'wp_unique_term_slug_is_bad_slug' lets developers control whether a test slug needs to be made unique, before the queries required to build a suffix are performed.
  • 'wp_unique_term_slug' filters the output of the function.

These changes introduce parity with the filters in wp_unique_post_slug().

Props coffee2code, bolo1988, boonebgorges.
Fixes #20783.

File:
1 edited

Legend:

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

    r32813 r32837  
    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 */
     
    33313331    global $wpdb;
    33323332
    3333     if ( ! term_exists( $slug ) )
    3334         return $slug;
     3333    $needs_suffix = 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        $needs_suffix = false;
    33393339    }
    33403340
     
    33433343     * by incorporating parent slugs.
    33443344     */
    3345     if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) {
     3345    $parent_suffix = '';
     3346    if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) {
    33463347        $the_parent = $term->parent;
    33473348        while ( ! empty($the_parent) ) {
     
    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) )
     
    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;
    3375     }
    3376 
    3377     return $slug;
     3364    /**
     3365     * Filter whether the proposed unique term slug is bad.
     3366     *
     3367     * @since 4.3.0
     3368     *
     3369     * @param bool   $needs_suffix Whether the slug needs to be made unique with a suffix.
     3370     * @param string $slug         The slug.
     3371     * @param object $term         Term object.
     3372     */
     3373    if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix, $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        }
     3392    }
     3393
     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
Note: See TracChangeset for help on using the changeset viewer.