Make WordPress Core

Changeset 30347


Ignore:
Timestamp:
11/14/2014 09:52:23 PM (12 years ago)
Author:
boonebgorges
Message:

Flush cache for newly created term in _split_shared_term().

The term itself does not have any cached values yet, but in some cases the new
term's taxonomy may need its cached hierarchy to be refreshed as a result of
the term splitting.

Props jorbin.
See #30335.

Location:
trunk
Files:
2 edited

Legend:

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

    r30344 r30347  
    40974097        $children_tt_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND parent = %d", $term_taxonomy->taxonomy, $term_id ) );
    40984098
    4099         foreach ( $children_tt_ids as $child_tt_id ) {
    4100                 $wpdb->update( $wpdb->term_taxonomy,
    4101                         array( 'parent' => $new_term_id ),
    4102                         array( 'term_taxonomy_id' => $child_tt_id )
    4103                 );
    4104                 clean_term_cache( $term_id, $term_taxonomy->taxonomy );
     4099        if ( ! empty( $children_tt_ids ) ) {
     4100                foreach ( $children_tt_ids as $child_tt_id ) {
     4101                        $wpdb->update( $wpdb->term_taxonomy,
     4102                                array( 'parent' => $new_term_id ),
     4103                                array( 'term_taxonomy_id' => $child_tt_id )
     4104                        );
     4105                        clean_term_cache( $term_id, $term_taxonomy->taxonomy );
     4106                }
     4107        } else {
     4108                // If the term has no children, we must force its taxonomy cache to be rebuilt separately.
     4109                clean_term_cache( $new_term_id, $term_taxonomy->taxonomy );
    41054110        }
    41064111
  • trunk/tests/phpunit/tests/term/splitSharedTerm.php

    r30344 r30347  
    9090                $this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
    9191        }
     92
     93        /**
     94         * @ticket 30335
     95         */
     96        public function test_should_rebuild_split_term_taxonomy_hierarchy() {
     97                global $wpdb;
     98
     99                register_taxonomy( 'wptests_tax_3', 'post' );
     100                register_taxonomy( 'wptests_tax_4', 'post', array(
     101                        'hierarchical' => true,
     102                ) );
     103
     104                $t1 = wp_insert_term( 'Foo1', 'wptests_tax_3' );
     105                $t2 = wp_insert_term( 'Foo1 Parent', 'wptests_tax_4' );
     106                $t3 = wp_insert_term( 'Foo1', 'wptests_tax_4', array(
     107                        'parent' => $t2['term_id'],
     108                ) );
     109
     110                // Manually modify because split terms shouldn't naturally occur.
     111                $wpdb->update( $wpdb->term_taxonomy,
     112                        array( 'term_id' => $t1['term_id'] ),
     113                        array( 'term_taxonomy_id' => $t3['term_taxonomy_id'] ),
     114                        array( '%d' ),
     115                        array( '%d' )
     116                );
     117                $th = _get_term_hierarchy( 'wptests_tax_4' );
     118
     119                $new_term_id = _split_shared_term( $t1['term_id'], $t3['term_taxonomy_id'] );
     120
     121                $t2_children = get_term_children( $t2['term_id'], 'wptests_tax_4' );
     122                $this->assertEquals( array( $new_term_id ), $t2_children );
     123        }
    92124}
Note: See TracChangeset for help on using the changeset viewer.