Make WordPress Core

Changeset 29862


Ignore:
Timestamp:
10/09/2014 02:31:35 AM (10 years ago)
Author:
boonebgorges
Message:

Improve 'alias_of' handling in wp_insert_term() and wp_update_term().

Using get_term_by() rather than direct SQL queries to fetch the alias term
fixes a number of issues:

  • Object cache for aliased term is properly cleared after update.
  • If the aliased term is in the object cache, it's served from there, saving a database query.
  • Duplicate 'edit_terms' and 'edited_terms' hooks can be removed.
  • Fix a PHP notice when the 'alias_of' term is not found.
  • Prevent the incorrect creation of a new term group for the primary term when the 'alias_of' term is not found.

Adds unit tests for 'alias_of' functionality in both functions.

Fixes #29848.

Location:
trunk
Files:
2 edited

Legend:

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

    r29859 r29862  
    24492449    $term_group = 0;
    24502450    if ( $args['alias_of'] ) {
    2451         $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $args['alias_of'] ) );
    2452         if ( $alias->term_group ) {
     2451        $alias = get_term_by( 'slug', $args['alias_of'], $taxonomy );
     2452        if ( ! empty( $alias->term_group ) ) {
    24532453            // The alias we want is already in a group, so let's use that one.
    24542454            $term_group = $alias->term_group;
    2455         } else {
    2456             // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
     2455        } else if ( ! empty( $alias->term_id ) ) {
     2456            /*
     2457             * The alias is not in a group, so we create a new one
     2458             * and add the alias to it.
     2459             */
    24572460            $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
    24582461
    2459             /**
    2460              * Fires immediately before the given terms are edited.
    2461              *
    2462              * @since 2.9.0
    2463              *
    2464              * @param int    $term_id  Term ID.
    2465              * @param string $taxonomy Taxonomy slug.
    2466              */
    2467             do_action( 'edit_terms', $alias->term_id, $taxonomy );
    2468             $wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) );
    2469 
    2470             /**
    2471              * Fires immediately after the given terms are edited.
    2472              *
    2473              * @since 2.9.0
    2474              *
    2475              * @param int    $term_id  Term ID
    2476              * @param string $taxonomy Taxonomy slug.
    2477              */
    2478             do_action( 'edited_terms', $alias->term_id, $taxonomy );
     2462            wp_update_term( $alias->term_id, $taxonomy, array(
     2463                'term_group' => $term_group,
     2464            ) );
    24792465        }
    24802466    }
     
    29612947    $term_group = isset( $parsed_args['term_group'] ) ? $parsed_args['term_group'] : 0;
    29622948    if ( $args['alias_of'] ) {
    2963         $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $args['alias_of'] ) );
    2964         if ( $alias->term_group ) {
     2949        $alias = get_term_by( 'slug', $args['alias_of'], $taxonomy );
     2950        if ( ! empty( $alias->term_group ) ) {
    29652951            // The alias we want is already in a group, so let's use that one.
    29662952            $term_group = $alias->term_group;
    2967         } else {
    2968             // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
     2953        } else if ( ! empty( $alias->term_id ) ) {
     2954            /*
     2955             * The alias is not in a group, so we create a new one
     2956             * and add the alias to it.
     2957             */
    29692958            $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
    29702959
    2971             /** This action is documented in wp-includes/taxonomy.php */
    2972             do_action( 'edit_terms', $alias->term_id, $taxonomy );
    2973             $wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) );
    2974 
    2975             /** This action is documented in wp-includes/taxonomy.php */
    2976             do_action( 'edited_terms', $alias->term_id, $taxonomy );
     2960            wp_update_term( $alias->term_id, $taxonomy, array(
     2961                'term_group' => $term_group,
     2962            ) );
    29772963        }
    29782964
     
    30062992    }
    30072993
    3008     /** This action is documented in wp-includes/taxonomy.php */
     2994    /**
     2995     * Fires immediately before the given terms are edited.
     2996     *
     2997     * @since 2.9.0
     2998     *
     2999     * @param int    $term_id  Term ID.
     3000     * @param string $taxonomy Taxonomy slug.
     3001     */
    30093002    do_action( 'edit_terms', $term_id, $taxonomy );
    30103003    $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
     
    30143007    }
    30153008
    3016     /** This action is documented in wp-includes/taxonomy.php */
     3009    /**
     3010     * Fires immediately after the given terms are edited.
     3011     *
     3012     * @since 2.9.0
     3013     *
     3014     * @param int    $term_id  Term ID
     3015     * @param string $taxonomy Taxonomy slug.
     3016     */
    30173017    do_action( 'edited_terms', $term_id, $taxonomy );
    30183018
  • trunk/tests/phpunit/tests/term.php

    r29830 r29862  
    418418    }
    419419
     420    public function test_wp_insert_term_alias_of_no_term_group() {
     421        register_taxonomy( 'wptests_tax', 'post' );
     422        $t1 = $this->factory->term->create( array(
     423            'taxonomy' => 'wptests_tax',
     424        ) );
     425        $term_1 = get_term( $t1, 'wptests_tax' );
     426
     427        $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array(
     428            'alias_of' => $term_1->slug,
     429        ) );
     430        $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
     431
     432        $updated_term_1 = get_term( $term_1->term_id, 'wptests_tax' );
     433
     434        $term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
     435        _unregister_taxonomy( 'wptests_tax' );
     436
     437        $this->assertSame( 0, $term_1->term_group );
     438        $this->assertNotEmpty( $created_term->term_group );
     439        $this->assertSame( $created_term->term_group, $updated_term_1->term_group );
     440    }
     441
     442    public function test_wp_insert_term_alias_of_existing_term_group() {
     443        register_taxonomy( 'wptests_tax', 'post' );
     444        $t1 = $this->factory->term->create( array(
     445            'taxonomy' => 'wptests_tax',
     446        ) );
     447        $term_1 = get_term( $t1, 'wptests_tax' );
     448
     449        $t2 = $this->factory->term->create( array(
     450            'taxonomy' => 'wptests_tax',
     451            'alias_of' => $term_1->slug,
     452        ) );
     453        $term_2 = get_term( $t2, 'wptests_tax' );
     454
     455        $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array(
     456            'alias_of' => $term_2->slug,
     457        ) );
     458        $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
     459        _unregister_taxonomy( 'wptests_tax' );
     460
     461        $this->assertNotEmpty( $created_term->term_group );
     462        $this->assertSame( $created_term->term_group, $term_2->term_group );
     463    }
     464
     465    public function test_wp_insert_term_alias_of_nonexistent_term() {
     466        register_taxonomy( 'wptests_tax', 'post' );
     467        $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax', array(
     468            'alias_of' => 'foo',
     469        ) );
     470        $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
     471        _unregister_taxonomy( 'wptests_tax' );
     472
     473        $this->assertSame( 0, $created_term->term_group );
     474    }
     475
    420476    public function test_wp_insert_term_duplicate_name_slug_non_hierarchical() {
    421477        register_taxonomy( 'foo', 'post', array() );
     
    538594    }
    539595
     596    public function test_wp_update_term_alias_of_no_term_group() {
     597        register_taxonomy( 'wptests_tax', 'post' );
     598        $t1 = $this->factory->term->create( array(
     599            'taxonomy' => 'wptests_tax',
     600        ) );
     601        $term_1 = get_term( $t1, 'wptests_tax' );
     602
     603        $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );
     604        wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(
     605            'alias_of' => $term_1->slug,
     606        ) );
     607        $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
     608
     609        $updated_term_1 = get_term( $t1, 'wptests_tax' );
     610        _unregister_taxonomy( 'wptests_tax' );
     611
     612        $this->assertSame( 0, $term_1->term_group );
     613        $this->assertNotEmpty( $created_term->term_group );
     614        $this->assertSame( $created_term->term_group, $updated_term_1->term_group );
     615    }
     616
     617    public function test_wp_update_term_alias_of_existing_term_group() {
     618        register_taxonomy( 'wptests_tax', 'post' );
     619        $t1 = $this->factory->term->create( array(
     620            'taxonomy' => 'wptests_tax',
     621        ) );
     622        $term_1 = get_term( $t1, 'wptests_tax' );
     623
     624        $t2 = $this->factory->term->create( array(
     625            'taxonomy' => 'wptests_tax',
     626            'alias_of' => $term_1->slug,
     627        ) );
     628        $term_2 = get_term( $t2, 'wptests_tax' );
     629
     630        $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );
     631        wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(
     632            'alias_of' => $term_2->slug,
     633        ) );
     634        $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
     635        _unregister_taxonomy( 'wptests_tax' );
     636
     637        $this->assertNotEmpty( $created_term->term_group );
     638        $this->assertSame( $created_term->term_group, $term_2->term_group );
     639    }
     640
     641    public function test_wp_update_term_alias_of_nonexistent_term() {
     642        register_taxonomy( 'wptests_tax', 'post' );
     643        $created_term_ids = wp_insert_term( 'Foo', 'wptests_tax' );
     644        wp_update_term( $created_term_ids['term_id'], 'wptests_tax', array(
     645            'alias_of' => 'bar',
     646        ) );
     647        $created_term = get_term( $created_term_ids['term_id'], 'wptests_tax' );
     648        _unregister_taxonomy( 'wptests_tax' );
     649
     650        $this->assertSame( 0, $created_term->term_group );
     651    }
    540652    /**
    541653     * @ticket 5381
Note: See TracChangeset for help on using the changeset viewer.