Make WordPress Core

Ticket #30261: 30261.2.diff

File 30261.2.diff, 7.1 KB (added by boonebgorges, 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() { 
    14891489        }
    14901490}
    14911491
     1492function _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
    14921550/**
    14931551 * Executes network-level upgrade routines.
    14941552 *
  • 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 ) { 
    41894189 * @ignore
    41904190 * @since 4.2.0
    41914191 *
    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.
    41944200 * @return int|WP_Error When the current term does not need to be split (or cannot be split on the current
    41954201 *                      database schema), `$term_id` is returned. When the term is successfully split, the
    41964202 *                      new term_id is returned. A WP_Error is returned for miscellaneous errors.
    41974203 */
    4198 function _split_shared_term( $term_id, $term_taxonomy_id ) {
     4204function _split_shared_term( $term_id, $term_taxonomy_id, $record = true ) {
    41994205        global $wpdb;
    42004206
     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
    42014217        // Don't try to split terms if database schema does not support shared slugs.
    42024218        $current_db_version = get_option( 'db_version' );
    42034219        if ( $current_db_version < 30133 ) {
    function _split_shared_term( $term_id, $term_taxonomy_id ) { 
    42064222
    42074223        // If there are no shared term_taxonomy rows, there's nothing to do here.
    42084224        $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
    42094226        if ( ! $shared_tt_count ) {
    42104227                return $term_id;
    42114228        }
    42124229
    42134230        // 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        }
    42154234
    42164235        $new_term_data = array(
    42174236                'name' => $shared_term->name,
    function _split_shared_term( $term_id, $term_taxonomy_id ) { 
    42324251        );
    42334252
    42344253        // 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        }
    42374257
     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 ) );
    42384259        if ( ! empty( $children_tt_ids ) ) {
    42394260                foreach ( $children_tt_ids as $child_tt_id ) {
    42404261                        $wpdb->update( $wpdb->term_taxonomy,
    42414262                                array( 'parent' => $new_term_id ),
    42424263                                array( 'term_taxonomy_id' => $child_tt_id )
    42434264                        );
     4265
    42444266                        clean_term_cache( $term_id, $term_taxonomy->taxonomy );
    42454267                }
    42464268        } else {
    function _split_shared_term( $term_id, $term_taxonomy_id ) { 
    42574279        }
    42584280
    42594281        // 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                }
    42664287
    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        }
    42684291
    42694292        /**
    42704293         * Fires after a previously shared taxonomy term is split into two separate terms.