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.

git-svn-id: https://develop.svn.wordpress.org/trunk@30494 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-11-21 03:16:08 +00:00
parent f14649feb7
commit 5e71d5f21e
3 changed files with 133 additions and 13 deletions

View File

@ -306,4 +306,8 @@ add_filter( 'authenticate', 'wp_authenticate_spam_check', 99 );
add_filter( 'determine_current_user', 'wp_validate_auth_cookie' );
add_filter( 'determine_current_user', 'wp_validate_logged_in_cookie', 20 );
// Split term updates
add_filter( 'split_shared_term', '_wp_check_split_default_terms', 10, 4 );
add_filter( 'split_shared_term', '_wp_check_split_terms_in_menus', 10, 4 );
unset($filter, $action);

View File

@ -4135,6 +4135,61 @@ function _split_shared_term( $term_id, $term_taxonomy_id ) {
return $new_term_id;
}
/**
* Check default categories when a term gets split to see if any of them need
* to be updated.
*
* @since 4.1.0
* @access private
*
* @param int $term_id ID of the formerly shared term.
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
* @param string $taxonomy Taxonomy for the split term.
*/
function _wp_check_split_default_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
if ( 'category' == $taxonomy ) {
foreach ( array( 'default_category', 'default_link_category', 'default_email_category' ) as $option ) {
if ( $term_id == get_option( $option, -1 ) ) {
update_option( $option, $new_term_id );
}
}
}
}
/**
* Check menu items when a term gets split to see if any of them need to be
* updated.
*
* @since 4.1.0
* @access private
*
* @param int $term_id ID of the formerly shared term.
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
* @param string $taxonomy Taxonomy for the split term.
*/
function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
global $wpdb;
$post_ids = $wpdb->get_col( $wpdb->prepare(
"SELECT m1.post_id
FROM {$wpdb->postmeta} AS m1
INNER JOIN {$wpdb->postmeta} AS m2 ON m2.post_id=m1.post_id
INNER JOIN {$wpdb->postmeta} AS m3 ON m3.post_id=m1.post_id
WHERE ( m1.meta_key = '_menu_item_type' AND m1.meta_value = 'taxonomy' )
AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = '%s' )
AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )",
$taxonomy,
$term_id
) );
if ( $post_ids ) {
foreach ( $post_ids as $post_id ) {
update_post_meta( $post_id, '_menu_item_object_id', $new_term_id, $term_id );
}
}
}
/**
* Generate a permalink for a taxonomy term archive.
*

View File

@ -4,7 +4,7 @@
* @group taxonomy
*/
class Tests_Term_SplitSharedTerm extends WP_UnitTestCase {
protected $tt_ids = array();
protected $terms = array();
public function setUp() {
global $wpdb;
@ -40,11 +40,11 @@ class Tests_Term_SplitSharedTerm extends WP_UnitTestCase {
'parent' => $t1['term_id'],
) );
$this->tt_ids = array(
't1' => $t1['term_taxonomy_id'],
't2' => $t2['term_taxonomy_id'],
't3' => $t3['term_taxonomy_id'],
't2_child' => $t2_child['term_taxonomy_id'],
$this->terms = array(
't1' => $t1,
't2' => $t2,
't3' => $t3,
't2_child' => $t2_child,
);
_split_shared_term( $t1['term_id'], $t2['term_taxonomy_id'] );
@ -55,9 +55,9 @@ class Tests_Term_SplitSharedTerm extends WP_UnitTestCase {
* @ticket 5809
*/
public function test_should_create_new_term_ids() {
$t1_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t1'], 'wptests_tax' );
$t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
$t3_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t3'], 'wptests_tax_3' );
$t1_term = get_term_by( 'term_taxonomy_id', $this->terms['t1']['term_taxonomy_id'], 'wptests_tax' );
$t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
$t3_term = get_term_by( 'term_taxonomy_id', $this->terms['t3']['term_taxonomy_id'], 'wptests_tax_3' );
$this->assertNotEquals( $t1_term->term_id, $t2_term->term_id );
$this->assertNotEquals( $t1_term->term_id, $t3_term->term_id );
@ -68,26 +68,26 @@ class Tests_Term_SplitSharedTerm extends WP_UnitTestCase {
* @ticket 5809
*/
public function test_should_retain_child_terms_when_using_get_terms_parent() {
$t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
$t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
$children = get_terms( 'wptests_tax_2', array(
'parent' => $t2_term->term_id,
'hide_empty' => false,
) );
$this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
$this->assertEquals( $this->terms['t2_child']['term_taxonomy_id'], $children[0]->term_taxonomy_id );
}
/**
* @ticket 5809
*/
public function test_should_retain_child_terms_when_using_get_terms_child_of() {
$t2_term = get_term_by( 'term_taxonomy_id', $this->tt_ids['t2'], 'wptests_tax_2' );
$t2_term = get_term_by( 'term_taxonomy_id', $this->terms['t2']['term_taxonomy_id'], 'wptests_tax_2' );
$children = get_terms( 'wptests_tax_2', array(
'child_of' => $t2_term->term_id,
'hide_empty' => false,
) );
$this->assertEquals( $this->tt_ids['t2_child'], $children[0]->term_taxonomy_id );
$this->assertEquals( $this->terms['t2_child']['term_taxonomy_id'], $children[0]->term_taxonomy_id );
}
/**
@ -121,4 +121,65 @@ class Tests_Term_SplitSharedTerm extends WP_UnitTestCase {
$t2_children = get_term_children( $t2['term_id'], 'wptests_tax_4' );
$this->assertEquals( array( $new_term_id ), $t2_children );
}
/**
* @ticket 30335
*/
public function test_should_update_default_category_on_term_split() {
global $wpdb;
$t1 = wp_insert_term( 'Foo Default', 'category' );
update_option( 'default_category', $t1['term_id'] );
register_taxonomy( 'wptests_tax_5', 'post' );
$t2 = wp_insert_term( 'Foo Default', 'wptests_tax_5' );
// Manually modify because split terms shouldn't naturally occur.
$wpdb->update( $wpdb->term_taxonomy,
array( 'term_id' => $t1['term_id'] ),
array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
array( '%d' ),
array( '%d' )
);
$this->assertEquals( $t1['term_id'], get_option( 'default_category', -1 ) );
$new_term_id = _split_shared_term( $t1['term_id'], $t1['term_taxonomy_id'] );
$this->assertNotEquals( $new_term_id, $t1['term_id'] );
$this->assertEquals( $new_term_id, get_option( 'default_category', -1 ) );
}
/**
* @ticket 30335
*/
public function test_should_update_menus_on_term_split() {
global $wpdb;
$t1 = wp_insert_term( 'Foo Menu', 'category' );
register_taxonomy( 'wptests_tax_6', 'post' );
$t2 = wp_insert_term( 'Foo Menu', 'wptests_tax_6' );
// Manually modify because split terms shouldn't naturally occur.
$wpdb->update( $wpdb->term_taxonomy,
array( 'term_id' => $t1['term_id'] ),
array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),
array( '%d' ),
array( '%d' )
);
$menu_id = wp_create_nav_menu( rand_str() );
$cat_menu_item = wp_update_nav_menu_item( $menu_id, 0, array(
'menu-item-type' => 'taxonomy',
'menu-item-object' => 'category',
'menu-item-object-id' => $t1['term_id'],
'menu-item-status' => 'publish'
) );
$this->assertEquals( $t1['term_id'], get_post_meta( $cat_menu_item, '_menu_item_object_id', true ) );
$new_term_id = _split_shared_term( $t1['term_id'], $t1['term_taxonomy_id'] );
$this->assertNotEquals( $new_term_id, $t1['term_id'] );
$this->assertEquals( $new_term_id, get_post_meta( $cat_menu_item, '_menu_item_object_id', true ) );
}
}