Changeset 30585
- Timestamp:
- 11/27/2014 12:04:59 AM (10 years ago)
- Location:
- trunk
- Files:
-
- 1 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/default-filters.php
r30499 r30585 307 307 add_filter( 'determine_current_user', 'wp_validate_logged_in_cookie', 20 ); 308 308 309 // Split term updates310 add_action( 'split_shared_term', '_wp_check_split_default_terms', 10, 4 );311 add_action( 'split_shared_term', '_wp_check_split_terms_in_menus', 10, 4 );312 313 309 unset($filter, $action); -
trunk/src/wp-includes/taxonomy.php
r30546 r30585 3388 3388 $tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id) ); 3389 3389 3390 // Check whether this is a shared term that needs splitting.3391 $_term_id = _split_shared_term( $term_id, $tt_id );3392 if ( ! is_wp_error( $_term_id ) ) {3393 $term_id = $_term_id;3394 }3395 3396 3390 /** 3397 3391 * Fires immediately before the given terms are edited. … … 4044 4038 /** This action is documented in wp-includes/taxonomy.php */ 4045 4039 do_action( 'edited_term_taxonomy', $term, $taxonomy ); 4046 }4047 }4048 4049 /**4050 * Create a new term for a term_taxonomy item that currently shares its term.4051 *4052 * @since 4.1.04053 * @access private4054 *4055 * @param int $term_id ID of the shared term.4056 * @param int $term_taxonomy_id ID of the term taxonomy item to receive a new term.4057 * @param array $shared_tts Sibling term taxonomies, used for busting caches.4058 * @return int Term ID.4059 */4060 function _split_shared_term( $term_id, $term_taxonomy_id ) {4061 global $wpdb;4062 4063 // Don't try to split terms if database schema does not support shared slugs.4064 $current_db_version = get_option( 'db_version' );4065 if ( $current_db_version < 30133 ) {4066 return $term_id;4067 }4068 4069 // If there are no shared term_taxonomy rows, there's nothing to do here.4070 $shared_tt_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy tt WHERE tt.term_id = %d AND tt.term_taxonomy_id != %d", $term_id, $term_taxonomy_id ) );4071 if ( ! $shared_tt_count ) {4072 return $term_id;4073 }4074 4075 // Pull up data about the currently shared slug, which we'll use to populate the new one.4076 $shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) );4077 4078 $new_term_data = array(4079 'name' => $shared_term->name,4080 'slug' => $shared_term->slug,4081 'term_group' => $shared_term->term_group,4082 );4083 4084 if ( false === $wpdb->insert( $wpdb->terms, $new_term_data ) ) {4085 return new WP_Error( 'db_insert_error', __( 'Could not split shared term.' ), $wpdb->last_error );4086 }4087 4088 $new_term_id = (int) $wpdb->insert_id;4089 4090 // Update the existing term_taxonomy to point to the newly created term.4091 $wpdb->update( $wpdb->term_taxonomy,4092 array( 'term_id' => $new_term_id ),4093 array( 'term_taxonomy_id' => $term_taxonomy_id )4094 );4095 4096 // Reassign child terms to the new parent.4097 $term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) );4098 $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 ) );4099 4100 if ( ! empty( $children_tt_ids ) ) {4101 foreach ( $children_tt_ids as $child_tt_id ) {4102 $wpdb->update( $wpdb->term_taxonomy,4103 array( 'parent' => $new_term_id ),4104 array( 'term_taxonomy_id' => $child_tt_id )4105 );4106 clean_term_cache( $term_id, $term_taxonomy->taxonomy );4107 }4108 } else {4109 // If the term has no children, we must force its taxonomy cache to be rebuilt separately.4110 clean_term_cache( $new_term_id, $term_taxonomy->taxonomy );4111 }4112 4113 // Clean the cache for term taxonomies formerly shared with the current term.4114 $shared_term_taxonomies = $wpdb->get_row( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) );4115 foreach ( (array) $shared_term_taxonomies as $shared_term_taxonomy ) {4116 clean_term_cache( $term_id, $shared_term_taxonomy );4117 }4118 4119 // Keep a record of term_ids that have been split, keyed by old term_id.4120 $split_term_data = get_option( '_split_terms_' . $term_taxonomy->taxonomy, array() );4121 $split_term_data[ $term_id ] = $new_term_id;4122 update_option( '_split_terms_' . $term_taxonomy->taxonomy, $split_term_data );4123 4124 /**4125 * Fires after a previously shared taxonomy term is split into two separate terms.4126 *4127 * @since 4.1.04128 *4129 * @param int $term_id ID of the formerly shared term.4130 * @param int $new_term_id ID of the new term created for the $term_taxonomy_id.4131 * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.4132 * @param string $taxonomy Taxonomy for the split term.4133 */4134 do_action( 'split_shared_term', $term_id, $new_term_id, $term_taxonomy_id, $term_taxonomy->taxonomy );4135 4136 return $new_term_id;4137 }4138 4139 /**4140 * Check default categories when a term gets split to see if any of them need4141 * to be updated.4142 *4143 * @since 4.1.04144 * @access private4145 *4146 * @param int $term_id ID of the formerly shared term.4147 * @param int $new_term_id ID of the new term created for the $term_taxonomy_id.4148 * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.4149 * @param string $taxonomy Taxonomy for the split term.4150 */4151 function _wp_check_split_default_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {4152 if ( 'category' == $taxonomy ) {4153 foreach ( array( 'default_category', 'default_link_category', 'default_email_category' ) as $option ) {4154 if ( $term_id == get_option( $option, -1 ) ) {4155 update_option( $option, $new_term_id );4156 }4157 }4158 }4159 }4160 4161 /**4162 * Check menu items when a term gets split to see if any of them need to be4163 * updated.4164 *4165 * @since 4.1.04166 * @access private4167 *4168 * @param int $term_id ID of the formerly shared term.4169 * @param int $new_term_id ID of the new term created for the $term_taxonomy_id.4170 * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.4171 * @param string $taxonomy Taxonomy for the split term.4172 */4173 function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {4174 global $wpdb;4175 $post_ids = $wpdb->get_col( $wpdb->prepare(4176 "SELECT m1.post_id4177 FROM {$wpdb->postmeta} AS m14178 INNER JOIN {$wpdb->postmeta} AS m2 ON m2.post_id=m1.post_id4179 INNER JOIN {$wpdb->postmeta} AS m3 ON m3.post_id=m1.post_id4180 WHERE ( m1.meta_key = '_menu_item_type' AND m1.meta_value = 'taxonomy' )4181 AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = '%s' )4182 AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )",4183 $taxonomy,4184 $term_id4185 ) );4186 4187 if ( $post_ids ) {4188 foreach ( $post_ids as $post_id ) {4189 update_post_meta( $post_id, '_menu_item_object_id', $new_term_id, $term_id );4190 }4191 4040 } 4192 4041 } -
trunk/tests/phpunit/tests/term.php
r30344 r30585 691 691 } 692 692 693 /**694 * @ticket 5809695 */696 public function test_wp_update_term_should_split_shared_term() {697 global $wpdb;698 699 register_taxonomy( 'wptests_tax', 'post' );700 register_taxonomy( 'wptests_tax_2', 'post' );701 702 $t1 = wp_insert_term( 'Foo', 'wptests_tax' );703 $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );704 705 // Manually modify because split terms shouldn't naturally occur.706 $wpdb->update( $wpdb->term_taxonomy,707 array( 'term_id' => $t1['term_id'] ),708 array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),709 array( '%d' ),710 array( '%d' )711 );712 713 $posts = $this->factory->post->create_many( 2 );714 wp_set_object_terms( $posts[0], array( 'Foo' ), 'wptests_tax' );715 wp_set_object_terms( $posts[1], array( 'Foo' ), 'wptests_tax_2' );716 717 // Verify that the terms are shared.718 $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );719 $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );720 $this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );721 722 wp_update_term( $t2_terms[0]->term_id, 'wptests_tax_2', array(723 'name' => 'New Foo',724 ) );725 726 $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );727 $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );728 $this->assertNotEquals( $t1_terms[0]->term_id, $t2_terms[0]->term_id );729 }730 731 /**732 * @ticket 5809733 */734 public function test_wp_update_term_should_not_split_shared_term_before_410_schema_change() {735 global $wpdb;736 737 $db_version = get_option( 'db_version' );738 update_option( 'db_version', 30055 );739 740 register_taxonomy( 'wptests_tax', 'post' );741 register_taxonomy( 'wptests_tax_2', 'post' );742 743 $t1 = wp_insert_term( 'Foo', 'wptests_tax' );744 $t2 = wp_insert_term( 'Foo', 'wptests_tax_2' );745 746 // Manually modify because split terms shouldn't naturally occur.747 $wpdb->update( $wpdb->term_taxonomy,748 array( 'term_id' => $t1['term_id'] ),749 array( 'term_taxonomy_id' => $t2['term_taxonomy_id'] ),750 array( '%d' ),751 array( '%d' )752 );753 754 $posts = $this->factory->post->create_many( 2 );755 wp_set_object_terms( $posts[0], array( 'Foo' ), 'wptests_tax' );756 wp_set_object_terms( $posts[1], array( 'Foo' ), 'wptests_tax_2' );757 758 // Verify that the term is shared.759 $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );760 $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );761 $this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );762 763 wp_update_term( $t2_terms[0]->term_id, 'wptests_tax_2', array(764 'name' => 'New Foo',765 ) );766 767 // Term should still be shared.768 $t1_terms = wp_get_object_terms( $posts[0], 'wptests_tax' );769 $t2_terms = wp_get_object_terms( $posts[1], 'wptests_tax_2' );770 $this->assertSame( $t1_terms[0]->term_id, $t2_terms[0]->term_id );771 772 update_option( 'db_version', $db_version );773 }774 775 693 public function test_wp_update_term_alias_of_no_term_group() { 776 694 register_taxonomy( 'wptests_tax', 'post' );
Note: See TracChangeset
for help on using the changeset viewer.