Make WordPress Core


Ignore:
Timestamp:
11/04/2015 09:23:28 PM (10 years ago)
Author:
boonebgorges
Message:

Don't allow term meta to be added to shared taxonomy terms.

add_term_meta() and update_term_meta() identify terms by $term_id. In
cases where a term is shared between taxonomies, $term_id is insufficient to
distinguish where the metadata belongs.

When attempting to add/update termmeta on a shared term, a WP_Error object
is returned. This gives developers enough information to decide whether they'd
like to force the term to be split and retry the save, or show an error in the
UI, or whatever.

Props boonebgorges, mboynes, DH-Shredder, jorbin, aaroncampbell.
Fixes #34544.

File:
1 edited

Legend:

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

    r35242 r35515  
    308308    }
    309309
     310    /**
     311     * @ticket 34544
     312     */
     313    public function test_add_term_meta_should_return_error_when_term_id_is_shared() {
     314        global $wpdb;
     315
     316        update_option( 'finished_splitting_shared_terms', false );
     317
     318        register_taxonomy( 'wptests_tax', 'post' );
     319        register_taxonomy( 'wptests_tax_2', 'post' );
     320        register_taxonomy( 'wptests_tax_3', 'post' );
     321
     322        $t1 = wp_insert_term( 'Foo', 'wptests_tax' );
     323        $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
     324        $t3 = wp_insert_term( 'Foo', 'wptests_tax_3' );
     325
     326        // Manually modify because shared terms shouldn't naturally occur.
     327        $wpdb->update( $wpdb->term_taxonomy,
     328            array( 'term_id' => $t1['term_id'] ),
     329            array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
     330            array( '%d' ),
     331            array( '%d' )
     332        );
     333
     334        $wpdb->update( $wpdb->term_taxonomy,
     335            array( 'term_id' => $t1['term_id'] ),
     336            array( 'term_taxonomy_id' => $t3['term_taxonomy_id'] ),
     337            array( '%d' ),
     338            array( '%d' )
     339        );
     340
     341        $found = add_term_meta( $t1['term_id'], 'bar', 'baz' );
     342        $this->assertWPError( $found );
     343        $this->assertSame( 'ambiguous_term_id', $found->get_error_code() );
     344    }
     345
     346    /**
     347     * @ticket 34544
     348     */
     349    public function test_update_term_meta_should_return_error_when_term_id_is_shared() {
     350        global $wpdb;
     351
     352        update_option( 'finished_splitting_shared_terms', false );
     353
     354        register_taxonomy( 'wptests_tax', 'post' );
     355        $t1 = wp_insert_term( 'Foo', 'wptests_tax' );
     356        add_term_meta( $t1, 'foo', 'bar' );
     357
     358        register_taxonomy( 'wptests_tax_2', 'post' );
     359        register_taxonomy( 'wptests_tax_3', 'post' );
     360
     361        $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );
     362        $t3 = wp_insert_term( 'Foo', 'wptests_tax_3' );
     363
     364        // Manually modify because shared terms shouldn't naturally occur.
     365        $wpdb->update( $wpdb->term_taxonomy,
     366            array( 'term_id' => $t1['term_id'] ),
     367            array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
     368            array( '%d' ),
     369            array( '%d' )
     370        );
     371
     372        $wpdb->update( $wpdb->term_taxonomy,
     373            array( 'term_id' => $t1['term_id'] ),
     374            array( 'term_taxonomy_id' => $t3['term_taxonomy_id'] ),
     375            array( '%d' ),
     376            array( '%d' )
     377        );
     378
     379        $found = update_term_meta( $t1['term_id'], 'foo', 'baz' );
     380        $this->assertWPError( $found );
     381        $this->assertSame( 'ambiguous_term_id', $found->get_error_code() );
     382    }
     383
    310384    public static function set_cache_results( $q ) {
    311385        $q->set( 'cache_results', true );
Note: See TracChangeset for help on using the changeset viewer.