[wp-trac] [WordPress Trac] #34533: Term splitting in `get_term()` can have unexpected effects

WordPress Trac noreply at wordpress.org
Sun Nov 1 01:39:50 UTC 2015


#34533: Term splitting in `get_term()` can have unexpected effects
--------------------------+-----------------------------
 Reporter:  dlh           |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Taxonomy      |    Version:  trunk
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 As of [34997], `get_term()` attempts to split a shared term if it finds a
 term in a different taxonomy than the one requested. This new feature can
 cause a slight change to previous behavior and have unexpected effects in
 some edge cases.

 1. Previously, `wp_update_term()` would split the term being updated if
 that term was shared. `wp_update_term()` calls  `get_term()`, but
 `get_term()` splits the term found, not the term requested. If the
 splitting in `get_term()` leaves no more shared terms for the updated term
 ID, then the updated term is no longer split.

 2. If you collect several shared term IDs in advance and try to update
 them, then after calling `wp_update_term()` on the first term, the
 remaining terms might have been split and no longer exist. The first test
 below tries to show this case.

 3. Calling `get_term()` with an invalid term ID and taxonomy pairing will
 cause the term ID to be split after `get_term()` detects the mismatch. The
 second test below attempts to show this case. In itself, the behavior
 might not be "bad," just surprising.

 Changing `get_term()` so that it splits the requested term, rather than
 the found term, could be a workaround. But in that scenario, the requested
 term couldn't be split unless its term taxonomy ID was available.

 {{{#!php
 function test_wp_update_term_splits_other_term() {
         global $wpdb;

         register_taxonomy( 'test_tax', 'post' );
         register_taxonomy( 'test_tax_2', 'post' );

         $t1 = wp_insert_term( 'Bar', 'test_tax' );
         $t2 = wp_insert_term( 'Bar', 'test_tax_2' );

         $wpdb->update( $wpdb->term_taxonomy,
                 array( 'term_id' => $t1['term_id'] ),
                 array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
                 array( '%d' ),
                 array( '%d' )
         );

         $t2 = wp_update_term( $t1['term_id'], 'test_tax_2', array( 'name'
 => 'New Bar' ) );
         $this->assertNotInstanceOf( 'WP_Error', $t2, 'Error from first
 wp_update_term()' );

         $t1 = wp_update_term( $t1['term_id'], 'test_tax', array( 'name' =>
 'New Bar' ) );
         $this->assertNotInstanceOf( 'WP_Error', $t1, 'Error from second
 wp_update_term()' );
 }

 function test_get_invalid_term_splits_shared_term() {
         global $wpdb;

         register_taxonomy( 'test_tax', 'post' );
         register_taxonomy( 'test_tax_2', 'post' );

         $t1 = wp_insert_term( 'Bar', 'test_tax' );
         $t2 = wp_insert_term( 'Bar', 'test_tax_2' );

         $wpdb->update( $wpdb->term_taxonomy,
                 array( 'term_id' => $t1['term_id'] ),
                 array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
                 array( '%d' ),
                 array( '%d' )
         );

         $this->assertInstanceOf( 'WP_Term', get_term( $t1['term_id'],
 'test_tax' ), 'Error before get_term()' );
         get_term( $t1['term_id'], 'category' );
         $this->assertInstanceOf( 'WP_Term', get_term( $t1['term_id'],
 'test_tax' ), 'Error after get_term()' );
 }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/34533>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list