Make WordPress Core

Changeset 30494


Ignore:
Timestamp:
11/21/2014 03:16:08 AM (12 years ago)
Author:
boonebgorges
Message:

Improve cleanup of cached term_ids after shared terms are split.

  • If the split term ID is stored as 'default_category', 'default_link_category', or 'default_email_category', update it to the new ID.
  • If the split term ID is associated with a nav menu item, update that piece of postmeta to the new ID.

Props mboynes.
See #30335.

Location:
trunk
Files:
3 edited

Legend:

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

    r30155 r30494  
    307307add_filter( 'determine_current_user', 'wp_validate_logged_in_cookie', 20 );
    308308
     309// Split term updates
     310add_filter( 'split_shared_term', '_wp_check_split_default_terms',  10, 4 );
     311add_filter( 'split_shared_term', '_wp_check_split_terms_in_menus', 10, 4 );
     312
    309313unset($filter, $action);
  • trunk/src/wp-includes/taxonomy.php

    r30493 r30494  
    41374137
    41384138/**
     4139 * Check default categories when a term gets split to see if any of them need
     4140 * to be updated.
     4141 *
     4142 * @since 4.1.0
     4143 * @access private
     4144 *
     4145 * @param int    $term_id          ID of the formerly shared term.
     4146 * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
     4147 * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
     4148 * @param string $taxonomy         Taxonomy for the split term.
     4149 */
     4150function _wp_check_split_default_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
     4151        if ( 'category' == $taxonomy ) {
     4152                foreach ( array( 'default_category', 'default_link_category', 'default_email_category' ) as $option ) {
     4153                        if ( $term_id == get_option( $option, -1 ) ) {
     4154                                update_option( $option, $new_term_id );
     4155                        }
     4156                }
     4157        }
     4158}
     4159
     4160/**
     4161 * Check menu items when a term gets split to see if any of them need to be
     4162 * updated.
     4163 *
     4164 * @since 4.1.0
     4165 * @access private
     4166 *
     4167 * @param int    $term_id          ID of the formerly shared term.
     4168 * @param int    $new_term_id      ID of the new term created for the $term_taxonomy_id.
     4169 * @param int    $term_taxonomy_id ID for the term_taxonomy row affected by the split.
     4170 * @param string $taxonomy         Taxonomy for the split term.
     4171 */
     4172function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
     4173        global $wpdb;
     4174        $post_ids = $wpdb->get_col( $wpdb->prepare(
     4175                "SELECT m1.post_id
     4176                FROM {$wpdb->postmeta} AS m1
     4177                        INNER JOIN {$wpdb->postmeta} AS m2 ON m2.post_id=m1.post_id
     4178                        INNER JOIN {$wpdb->postmeta} AS m3 ON m3.post_id=m1.post_id
     4179                WHERE ( m1.meta_key = '_menu_item_type' AND m1.meta_value = 'taxonomy' )
     4180                        AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = '%s' )
     4181                        AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )",
     4182                $taxonomy,
     4183                $term_id
     4184        ) );
     4185
     4186        if ( $post_ids ) {
     4187                foreach ( $post_ids as $post_id ) {
     4188                        update_post_meta( $post_id, '_menu_item_object_id', $new_term_id, $term_id );
     4189                }
     4190        }
     4191}
     4192
     4193/**
    41394194 * Generate a permalink for a taxonomy term archive.
    41404195 *
  • trunk/tests/phpunit/tests/term/splitSharedTerm.php

    r30347 r30494  
    55 */
    66class Tests_Term_SplitSharedTerm extends WP_UnitTestCase {
    7         protected $tt_ids = array();
     7        protected $terms = array();
    88
    99        public function setUp() {
     
    4141                ) );
    4242
    43                 $this->tt_ids = array(
    44                         't1' => $t1['term_taxonomy_id'],
    45                         't2' => $t2['term_taxonomy_id'],
    46                         't3' => $t3['term_taxonomy_id'],
    47                         't2_child' => $t2_child['term_taxonomy_id'],
     43                $this->terms = array(
     44                        't1' => $t1,
     45                        't2' => $t2,
     46                        't3' => $t3,
     47                        't2_child' => $t2_child,
    4848                );
    4949
     
    5656         */
    5757        public function test_should_create_new_term_ids() {
    58                 $t1_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t1'], 'wptests_tax' );
    59                 $t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
    60                 $t3_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t3'], 'wptests_tax_3' );
     58                $t1_term = get_term_by( 'term_taxonomy_id', $this->terms['t1']['term_taxonomy_id'], 'wptests_tax' );
     59                $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
     60                $t3_term = get_term_by( 'term_taxonomy_id', $this->terms['t3']['term_taxonomy_id'], 'wptests_tax_3' );
    6161
    6262                $this->assertNotEquals( $t1_term->term_id, $t2_term->term_id );
     
    6969         */
    7070        public function test_should_retain_child_terms_when_using_get_terms_parent() {
    71                 $t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
     71                $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
    7272                $children = get_terms( 'wptests_tax_2', array(
    7373                        'parent' => $t2_term->term_id,
     
    7575                ) );
    7676
    77                 $this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
     77                $this->assertEquals( $this->terms['t2_child']['term_taxonomy_id'], $children[0]->term_taxonomy_id );
    7878        }
    7979
     
    8282         */
    8383        public function test_should_retain_child_terms_when_using_get_terms_child_of() {
    84                 $t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
     84                $t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
    8585                $children = get_terms( 'wptests_tax_2', array(
    8686                        'child_of' => $t2_term->term_id,
     
    8888                ) );
    8989
    90                 $this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
     90                $this->assertEquals( $this->terms['t2_child']['term_taxonomy_id'], $children[0]->term_taxonomy_id );
    9191        }
    9292
     
    122122                $this->assertEquals( array( $new_term_id ), $t2_children );
    123123        }
     124
     125        /**
     126         * @ticket 30335
     127         */
     128        public function test_should_update_default_category_on_term_split() {
     129                global $wpdb;
     130                $t1 = wp_insert_term( 'Foo Default', 'category' );
     131
     132                update_option( 'default_category', $t1['term_id'] );
     133
     134                register_taxonomy( 'wptests_tax_5', 'post' );
     135                $t2 = wp_insert_term( 'Foo Default', 'wptests_tax_5' );
     136
     137                // Manually modify because split terms shouldn't naturally occur.
     138                $wpdb->update( $wpdb->term_taxonomy,
     139                        array( 'term_id' => $t1['term_id'] ),
     140                        array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
     141                        array( '%d' ),
     142                        array( '%d' )
     143                );
     144
     145                $this->assertEquals( $t1['term_id'], get_option( 'default_category', -1 ) );
     146
     147                $new_term_id = _split_shared_term( $t1['term_id'], $t1['term_taxonomy_id'] );
     148
     149                $this->assertNotEquals( $new_term_id, $t1['term_id'] );
     150                $this->assertEquals( $new_term_id, get_option( 'default_category', -1 ) );
     151        }
     152
     153        /**
     154         * @ticket 30335
     155         */
     156        public function test_should_update_menus_on_term_split() {
     157                global $wpdb;
     158
     159                $t1 = wp_insert_term( 'Foo Menu', 'category' );
     160
     161                register_taxonomy( 'wptests_tax_6', 'post' );
     162                $t2 = wp_insert_term( 'Foo Menu', 'wptests_tax_6' );
     163
     164                // Manually modify because split terms shouldn't naturally occur.
     165                $wpdb->update( $wpdb->term_taxonomy,
     166                        array( 'term_id' => $t1['term_id'] ),
     167                        array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
     168                        array( '%d' ),
     169                        array( '%d' )
     170                );
     171
     172                $menu_id = wp_create_nav_menu( rand_str() );
     173                $cat_menu_item = wp_update_nav_menu_item( $menu_id, 0, array(
     174                        'menu-item-type' => 'taxonomy',
     175                        'menu-item-object' => 'category',
     176                        'menu-item-object-id' => $t1['term_id'],
     177                        'menu-item-status' => 'publish'
     178                ) );
     179                $this->assertEquals( $t1['term_id'], get_post_meta( $cat_menu_item, '_menu_item_object_id', true ) );
     180
     181                $new_term_id = _split_shared_term( $t1['term_id'], $t1['term_taxonomy_id'] );
     182                $this->assertNotEquals( $new_term_id, $t1['term_id'] );
     183                $this->assertEquals( $new_term_id, get_post_meta( $cat_menu_item, '_menu_item_object_id', true ) );
     184        }
    124185}
Note: See TracChangeset for help on using the changeset viewer.