WordPress.org

Make WordPress Core

Changeset 28733


Ignore:
Timestamp:
06/11/14 02:27:36 (3 years ago)
Author:
wonderboymusic
Message:

In wp_insert_term(), when no slug is provided, check for an existing term by name. If it exists, use that slug instead of calling sanitize_title( $name ).

Prevents creating an endless number of terms like A+ or $$$$ in any given taxonomy.

Props wonderboymusic, SergeyBiryukov, aaroncampbell.
Fixes #17689.

Location:
trunk
Files:
2 edited

Legend:

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

    r28712 r28733  
    24372437    $slug_provided = ! empty( $args['slug'] ); 
    24382438    if ( ! $slug_provided ) { 
    2439         $slug = sanitize_title($name); 
     2439        $_name = trim( $name ); 
     2440        $existing_term = get_term_by( 'name', $_name, $taxonomy ); 
     2441        if ( $existing_term ) { 
     2442            $slug = $existing_term->slug; 
     2443        } else { 
     2444            $slug = sanitize_title( $name ); 
     2445        } 
    24402446    } else { 
    24412447        $slug = $args['slug']; 
  • trunk/tests/phpunit/tests/taxonomy.php

    r28421 r28733  
    233233        $this->assertEquals( 0, wp_insert_category( $cat, false ) ); 
    234234    } 
     235 
     236    /** 
     237     * @ticket 17689 
     238     */ 
     239    function test_duplicate_name() { 
     240        $term = $this->factory->tag->create_and_get( array( 'name' => 'Bozo' ) ); 
     241        $this->assertFalse( is_wp_error( $term ) ); 
     242        $this->assertTrue( empty( $term->errors ) ); 
     243 
     244        // Test existing term name with unique slug 
     245        $term1 = $this->factory->tag->create( array( 'name' => 'Bozo', 'slug' => 'bozo1' ) ); 
     246        $this->assertFalse( is_wp_error( $term1 ) ); 
     247        $this->assertTrue( empty($term1->errors ) ); 
     248 
     249        // Test an existing term name 
     250        $term2 = $this->factory->tag->create( array( 'name' => 'Bozo' ) ); 
     251        $this->assertTrue( is_wp_error( $term2 ) ); 
     252        $this->assertNotEmpty( $term2->errors ); 
     253 
     254        // Test named terms ending in special characters 
     255        $term3 = $this->factory->tag->create( array( 'name' => 'T$' ) ); 
     256        $term4 = $this->factory->tag->create( array( 'name' => 'T$$' ) ); 
     257        $term5 = $this->factory->tag->create( array( 'name' => 'T$$$' ) ); 
     258        $term6 = $this->factory->tag->create( array( 'name' => 'T$$$$' ) ); 
     259        $term7 = $this->factory->tag->create( array( 'name' => 'T$$$$' ) ); 
     260        $this->assertTrue( is_wp_error( $term7 ) ); 
     261        $this->assertNotEmpty( $term7->errors ); 
     262        $this->assertEquals( $term6, $term7->error_data['term_exists'] ); 
     263 
     264        $terms = array_map( 'get_tag', array( $term3, $term4, $term5, $term6 ) ); 
     265        $this->assertCount( 4, array_unique( wp_list_pluck( $terms, 'slug' ) ) ); 
     266 
     267        // Test named terms with only special characters 
     268        $term8 = $this->factory->tag->create( array( 'name' => '$' ) ); 
     269        $term9 = $this->factory->tag->create( array( 'name' => '$$' ) ); 
     270        $term10 = $this->factory->tag->create( array( 'name' => '$$$' ) ); 
     271        $term11 = $this->factory->tag->create( array( 'name' => '$$$$' ) ); 
     272        $term12 = $this->factory->tag->create( array( 'name' => '$$$$' ) ); 
     273        $this->assertTrue( is_wp_error( $term12 ) ); 
     274        $this->assertNotEmpty( $term12->errors ); 
     275        $this->assertEquals( $term11, $term12->error_data['term_exists'] ); 
     276 
     277        $terms = array_map( 'get_tag', array( $term8, $term9, $term10, $term11 ) ); 
     278        $this->assertCount( 4, array_unique( wp_list_pluck( $terms, 'slug' ) ) ); 
     279 
     280        $term13 = $this->factory->tag->create( array( 'name' => 'A' ) ); 
     281        $this->assertFalse( is_wp_error( $term13 ) ); 
     282        $term14 = $this->factory->tag->create( array( 'name' => 'A' ) ); 
     283        $this->assertTrue( is_wp_error( $term14 ) ); 
     284        $term15 = $this->factory->tag->create( array( 'name' => 'A+', 'slug' => 'a' ) ); 
     285        $this->assertFalse( is_wp_error( $term15 ) ); 
     286        $term16 = $this->factory->tag->create( array( 'name' => 'A+' ) ); 
     287        $this->assertTrue( is_wp_error( $term16 ) ); 
     288        $term17 = $this->factory->tag->create( array( 'name' => 'A++' ) ); 
     289        $this->assertFalse( is_wp_error( $term17 ) ); 
     290        $term18 = $this->factory->tag->create( array( 'name' => 'A-', 'slug' => 'a' ) ); 
     291        $this->assertFalse( is_wp_error( $term18 ) ); 
     292        $term19 = $this->factory->tag->create( array( 'name' => 'A-' ) ); 
     293        $this->assertTrue( is_wp_error( $term19 ) ); 
     294        $term20 = $this->factory->tag->create( array( 'name' => 'A--' ) ); 
     295        $this->assertFalse( is_wp_error( $term20 ) ); 
     296    } 
    235297} 
Note: See TracChangeset for help on using the changeset viewer.