WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/05/2015 04:44:59 PM (5 years ago)
Author:
boonebgorges
Message:

Make get_term() behave more consistently in the context of shared terms.

When WP_Term was introduced in [34997], the $taxonomy parameter for
get_term() was made optional. This meant that, when the optional param was
omitted, get_term() had no way of determining which term was intended when
the term_id was shared between multiple taxonomies. As a (somewhat sneaky) way
of fixing things, get_term() split any shared terms it found. But this could
cause problems with developer expectations: it's not clear why requesting a
term should result in a database update, much less a potential change in the
ID of a term.

In place of this technique, this changeset introduces a number of changes that
make the handling of shared terms a bit less insane:

  • When a taxonomy is provided to get_term(), and a cached term is found matching the term_id, make sure the taxonomy also matches before returning it.
  • When a taxonomy is not provided, ensure that the term is not shared before adding it to the cache.
  • When a term is shared between taxonomies and no taxonomy is provided, return a WP_Error rather than splitting the term.
  • When a term is shared between taxonomies, only one of which is valid, return the term from that taxonomy.

Props boonebgorges, dlh.
Fixes #34533.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/getTerm.php

    r35242 r35537  
    1010    }
    1111
     12    /**
     13     * Utility function for generating two shared terms, in the 'wptests_tax' and 'wptests_tax_2' taxonomies.
     14     *
     15     * @return array Array of term_id/old_term_id/term_taxonomy_id triplets.
     16     */
     17    protected function generate_shared_terms() {
     18        global $wpdb;
     19
     20        register_taxonomy( 'wptests_tax_2', 'post' );
     21
     22        $t1 = wp_insert_term( 'Foo', 'wptests_tax' );
     23        $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
     24
     25        // Manually modify because shared terms shouldn't naturally occur.
     26        $wpdb->update( $wpdb->term_taxonomy,
     27            array( 'term_id' => $t1['term_id'] ),
     28            array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
     29            array( '%d' ),
     30            array( '%d' )
     31        );
     32
     33        return array(
     34            array(
     35                'term_id' => $t1['term_id'],
     36                'old_term_id' => $t1['term_id'],
     37                'term_taxonomy_id' => $t1['term_taxonomy_id'],
     38            ),
     39            array(
     40                'term_id' => $t1['term_id'],
     41                'old_term_id' => $t2['term_id'],
     42                'term_taxonomy_id' => $t2['term_taxonomy_id'],
     43            ),
     44        );
     45    }
     46
    1247    public function test_should_return_error_for_empty_term() {
    1348        $found = get_term( '', 'wptests_tax' );
     
    115150        $this->assertNull( get_term( $term_id, 'category' ) );
    116151    }
     152
     153    /**
     154     * @ticket 34533
     155     */
     156    public function test_should_return_wp_error_when_term_is_shared_and_no_taxonomy_is_specified() {
     157        $terms = $this->generate_shared_terms();
     158
     159        $found = get_term( $terms[0]['term_id'] );
     160
     161        $this->assertWPError( $found );
     162    }
     163
     164    /**
     165     * @ticket 34533
     166     */
     167    public function test_should_return_term_when_term_is_shared_and_correct_taxonomy_is_specified() {
     168        $terms = $this->generate_shared_terms();
     169
     170        $found = get_term( $terms[0]['term_id'], 'wptests_tax' );
     171
     172        $this->assertInstanceOf( 'WP_Term', $found );
     173        $this->assertSame( $terms[0]['term_id'], $found->term_id );
     174    }
     175
     176    /**
     177     * @ticket 34533
     178     */
     179    public function test_should_return_null_when_term_is_shared_and_incorrect_taxonomy_is_specified() {
     180        $terms = $this->generate_shared_terms();
     181
     182        $found = get_term( $terms[0]['term_id'], 'post_tag' );
     183
     184        $this->assertNull( $found );
     185    }
     186
     187    /**
     188     * @ticket 34533
     189     */
     190    public function test_shared_term_in_cache_should_be_ignored_when_specifying_a_different_taxonomy() {
     191        global $wpdb;
     192
     193        $terms = $this->generate_shared_terms();
     194
     195        // Prime cache for 'wptests_tax'.
     196        get_term( $terms[0]['term_id'], 'wptests_tax' );
     197        $num_queries = $wpdb->num_queries;
     198
     199        // Database should be hit again.
     200        $found = get_term( $terms[1]['term_id'], 'wptests_tax_2' );
     201        $num_queries++;
     202
     203        $this->assertSame( $num_queries, $wpdb->num_queries );
     204        $this->assertInstanceOf( 'WP_Term', $found );
     205        $this->assertSame( 'wptests_tax_2', $found->taxonomy );
     206    }
     207
     208    /**
     209     * @ticket 34533
     210     */
     211    public function test_should_return_error_when_only_matching_term_is_in_an_invalid_taxonomy() {
     212        $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     213
     214        _unregister_taxonomy( 'wptests_tax' );
     215
     216        $found = get_term( $t );
     217        $this->assertWPError( $found );
     218        $this->assertSame( 'invalid_taxonomy', $found->get_error_code() );
     219    }
     220
     221    /**
     222     * @ticket 34533
     223     */
     224    public function test_term_should_be_returned_when_id_is_shared_only_with_invalid_taxonomies() {
     225        $terms = $this->generate_shared_terms();
     226
     227        _unregister_taxonomy( 'wptests_tax' );
     228
     229        $found = get_term( $terms[1]['term_id'] );
     230        $this->assertInstanceOf( 'WP_Term', $found );
     231        $this->assertSame( 'wptests_tax_2', $found->taxonomy );
     232        $this->assertSame( $terms[1]['term_id'], $found->term_id );
     233    }
    117234}
Note: See TracChangeset for help on using the changeset viewer.