Ticket #30261: 30261.2.diff
File 30261.2.diff, 7.1 KB (added by , 10 years ago) |
---|
-
src/wp-admin/includes/upgrade.php
diff --git src/wp-admin/includes/upgrade.php src/wp-admin/includes/upgrade.php index dde618a..ee8b3cb 100644
function upgrade_430() { 1489 1489 } 1490 1490 } 1491 1491 1492 function _upgrade_430_split_all_shared_terms() { 1493 global $wpdb; 1494 1495 // Get a list of shared term IDs, and the term-taxonomy pairs that correspond to them. 1496 $shared_terms = $wpdb->get_results( 1497 "SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {$wpdb->term_taxonomy} tt 1498 LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id 1499 GROUP BY t.term_id 1500 HAVING term_tt_count > 1" 1501 ); 1502 1503 // Rekey for easier lookups. 1504 $_shared_terms = array(); 1505 foreach ( $shared_terms as $shared_term ) { 1506 $term_id = intval( $shared_term->term_id ); 1507 $_shared_terms[ $term_id ] = $shared_term; 1508 } 1509 $shared_terms = $_shared_terms; 1510 1511 $shared_term_ids = implode( ',', array_keys( $shared_terms ) ); 1512 1513 $shared_tts = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" ); 1514 1515 // Split term data recording is slow, so we do it just once, outside the loop. 1516 $split_term_data = get_option( '_split_terms', array() ); 1517 $suspend = wp_suspend_cache_invalidation( true ); 1518 $skipped_first_term = array(); 1519 $taxonomies = array(); 1520 foreach ( $shared_tts as $shared_tt ) { 1521 $term_id = intval( $shared_tt->term_id ); 1522 1523 // Don't split the first tt belonging to a given term_id. 1524 if ( ! isset( $skipped_first_term[ $term_id ] ) ) { 1525 $skipped_first_term[ $term_id ] = 1; 1526 continue; 1527 } 1528 1529 if ( ! isset( $split_term_data[ $term_id ] ) ) { 1530 $split_term_data[ $term_id ] = array(); 1531 } 1532 1533 // Keep track of taxonomies whose hierarchies need flushing. 1534 if ( ! isset( $taxonomies[ $shared_tt->taxonomy ] ) ) { 1535 $taxonomies[ $shared_tt->taxonomy ] = 1; 1536 } 1537 1538 $split_term_data[ $term_id ][ $shared_tt->taxonomy ] = _split_shared_term( $shared_terms[ $term_id ], $shared_tt, false ); 1539 } 1540 1541 foreach ( array_keys( $taxonomies ) as $tax ) { 1542 delete_option( "{$tax}_children" ); 1543 _get_term_hierarchy( $tax ); 1544 } 1545 1546 wp_suspend_cache_invalidation( $suspend ); 1547 update_option( '_split_terms', $split_term_data ); 1548 } 1549 1492 1550 /** 1493 1551 * Executes network-level upgrade routines. 1494 1552 * -
src/wp-includes/taxonomy.php
diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php index 75873c3..a566ca5 100644
function _update_generic_term_count( $terms, $taxonomy ) { 4189 4189 * @ignore 4190 4190 * @since 4.2.0 4191 4191 * 4192 * @param int $term_id ID of the shared term. 4193 * @param int $term_taxonomy_id ID of the term_taxonomy item to receive a new term. 4192 * @param int|object $term_id ID of the shared term, or the shared term object. 4193 * @param int|object $term_taxonomy_id ID of the term_taxonomy item to receive a new term, or the term_taxonomy object 4194 * (corresponding to a row from the term_taxonomy table). 4195 * @param bool $record Whether to record data about the split term in the options table. The recording 4196 * process has the potential to be resource-intensive, so during batch operations 4197 * it can be beneficial to skip inline recording and do it just once, after the 4198 * batch is processed. Only set this to `false` if you know what you are doing. 4199 * Default: true. 4194 4200 * @return int|WP_Error When the current term does not need to be split (or cannot be split on the current 4195 4201 * database schema), `$term_id` is returned. When the term is successfully split, the 4196 4202 * new term_id is returned. A WP_Error is returned for miscellaneous errors. 4197 4203 */ 4198 function _split_shared_term( $term_id, $term_taxonomy_id ) {4204 function _split_shared_term( $term_id, $term_taxonomy_id, $record = true ) { 4199 4205 global $wpdb; 4200 4206 4207 if ( is_object( $term_id ) ) { 4208 $shared_term = $term_id; 4209 $term_id = intval( $shared_term->term_id ); 4210 } 4211 4212 if ( is_object( $term_taxonomy_id ) ) { 4213 $term_taxonomy = $term_taxonomy_id; 4214 $term_taxonomy_id = intval( $term_taxonomy->term_taxonomy_id ); 4215 } 4216 4201 4217 // Don't try to split terms if database schema does not support shared slugs. 4202 4218 $current_db_version = get_option( 'db_version' ); 4203 4219 if ( $current_db_version < 30133 ) { … … function _split_shared_term( $term_id, $term_taxonomy_id ) { 4206 4222 4207 4223 // If there are no shared term_taxonomy rows, there's nothing to do here. 4208 4224 $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 ) ); 4225 4209 4226 if ( ! $shared_tt_count ) { 4210 4227 return $term_id; 4211 4228 } 4212 4229 4213 4230 // Pull up data about the currently shared slug, which we'll use to populate the new one. 4214 $shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) ); 4231 if ( empty( $shared_term ) ) { 4232 $shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) ); 4233 } 4215 4234 4216 4235 $new_term_data = array( 4217 4236 'name' => $shared_term->name, … … function _split_shared_term( $term_id, $term_taxonomy_id ) { 4232 4251 ); 4233 4252 4234 4253 // Reassign child terms to the new parent. 4235 $term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) ); 4236 $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 ) ); 4254 if ( empty( $term_taxonomy ) ) { 4255 $term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) ); 4256 } 4237 4257 4258 $children_tt_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE parent = %d AND taxonomy = %s", $term_id, $term_taxonomy->taxonomy ) ); 4238 4259 if ( ! empty( $children_tt_ids ) ) { 4239 4260 foreach ( $children_tt_ids as $child_tt_id ) { 4240 4261 $wpdb->update( $wpdb->term_taxonomy, 4241 4262 array( 'parent' => $new_term_id ), 4242 4263 array( 'term_taxonomy_id' => $child_tt_id ) 4243 4264 ); 4265 4244 4266 clean_term_cache( $term_id, $term_taxonomy->taxonomy ); 4245 4267 } 4246 4268 } else { … … function _split_shared_term( $term_id, $term_taxonomy_id ) { 4257 4279 } 4258 4280 4259 4281 // Keep a record of term_ids that have been split, keyed by old term_id. See {@see wp_get_split_term()}. 4260 $split_term_data = get_option( '_split_terms', array() ); 4261 if ( ! isset( $split_term_data[ $term_id ] ) ) { 4262 $split_term_data[ $term_id ] = array(); 4263 } 4264 4265 $split_term_data[ $term_id ][ $term_taxonomy->taxonomy ] = $new_term_id; 4282 if ( $record ) { 4283 $split_term_data = get_option( '_split_terms', array() ); 4284 if ( ! isset( $split_term_data[ $term_id ] ) ) { 4285 $split_term_data[ $term_id ] = array(); 4286 } 4266 4287 4267 update_option( '_split_terms', $split_term_data ); 4288 $split_term_data[ $term_id ][ $term_taxonomy->taxonomy ] = $new_term_id; 4289 update_option( '_split_terms', $split_term_data ); 4290 } 4268 4291 4269 4292 /** 4270 4293 * Fires after a previously shared taxonomy term is split into two separate terms.