WordPress.org

Make WordPress Core

Ticket #30261: 30261.5.diff

File 30261.5.diff, 7.2 KB (added by boonebgorges, 4 years ago)
  • src/wp-admin/includes/admin-filters.php

    diff --git src/wp-admin/includes/admin-filters.php src/wp-admin/includes/admin-filters.php
    index d0709f4..2787736 100644
    add_action( 'profile_update', 'default_password_nag_edit_user', 10, 2 ); 
    100100// Update hooks.
    101101add_action( 'admin_init', 'wp_plugin_update_rows' );
    102102add_action( 'admin_init', 'wp_theme_update_rows'  );
     103add_action( 'admin_init', 'maybe_split_shared_terms' );
    103104
    104105add_action( 'admin_notices', 'update_nag',      3  );
    105106add_action( 'admin_notices', 'maintenance_nag', 10 );
  • src/wp-admin/includes/schema.php

    diff --git src/wp-admin/includes/schema.php src/wp-admin/includes/schema.php
    index 95ee375..e8f3037 100644
    function populate_options() { 
    496496
    497497        // 3.5
    498498        'link_manager_enabled' => 0,
     499
     500        // 4.3
     501        'shared_terms_split' => 0,
    499502        );
    500503
    501504        // 3.3
  • src/wp-admin/includes/taxonomy.php

    diff --git src/wp-admin/includes/taxonomy.php src/wp-admin/includes/taxonomy.php
    index 4f11bdb..783125e 100644
    function wp_create_term($tag_name, $taxonomy = 'post_tag') { 
    288288
    289289        return wp_insert_term($tag_name, $taxonomy);
    290290}
     291
     292/**
     293 * Split existing shared taxonomy terms, if necessary.
     294 *
     295 * @since 4.3.0
     296 */
     297function maybe_split_shared_terms() {
     298        if ( ! current_user_can( 'update_core' ) ) {
     299                return;
     300        }
     301
     302        if ( get_option( 'shared_terms_split' ) ) {
     303                return;
     304        }
     305
     306        update_option( 'shared_terms_split', 1 );
     307        split_all_shared_terms();
     308}
     309
     310/**
     311 * Splits all shared taxonomy terms.
     312 *
     313 * @since 4.3.0
     314 *
     315 * @global wpdb $wpdb WordPress database abstraction object.
     316 */
     317function split_all_shared_terms() {
     318        global $wpdb;
     319
     320        // Get a list of shared terms (those with more than one associated row in term_taxonomy).
     321        $shared_terms = $wpdb->get_results(
     322                "SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {$wpdb->term_taxonomy} tt
     323                 LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id
     324                 GROUP BY t.term_id
     325                 HAVING term_tt_count > 1"
     326        );
     327
     328        if ( empty( $shared_terms ) ) {
     329                return;
     330        }
     331
     332        // Rekey shared term array for faster lookups.
     333        $_shared_terms = array();
     334        foreach ( $shared_terms as $shared_term ) {
     335                $term_id = intval( $shared_term->term_id );
     336                $_shared_terms[ $term_id ] = $shared_term;
     337        }
     338        $shared_terms = $_shared_terms;
     339
     340        // Get term taxonomy data for all shared terms.
     341        $shared_term_ids = implode( ',', array_keys( $shared_terms ) );
     342        $shared_tts = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" );
     343
     344        // Split term data recording is slow, so we do it just once, outside the loop.
     345        $suspend = wp_suspend_cache_invalidation( true );
     346        $split_term_data = get_option( '_split_terms', array() );
     347        $skipped_first_term = $taxonomies = array();
     348        foreach ( $shared_tts as $shared_tt ) {
     349                $term_id = intval( $shared_tt->term_id );
     350
     351                // Don't split the first tt belonging to a given term_id.
     352                if ( ! isset( $skipped_first_term[ $term_id ] ) ) {
     353                        $skipped_first_term[ $term_id ] = 1;
     354                        continue;
     355                }
     356
     357                if ( ! isset( $split_term_data[ $term_id ] ) ) {
     358                        $split_term_data[ $term_id ] = array();
     359                }
     360
     361                // Keep track of taxonomies whose hierarchies need flushing.
     362                if ( ! isset( $taxonomies[ $shared_tt->taxonomy ] ) ) {
     363                        $taxonomies[ $shared_tt->taxonomy ] = 1;
     364                }
     365
     366                // Split the term.
     367                $split_term_data[ $term_id ][ $shared_tt->taxonomy ] = _split_shared_term( $shared_terms[ $term_id ], $shared_tt, false );
     368        }
     369
     370        // Rebuild the cached hierarchy for each affected taxonomy.
     371        foreach ( array_keys( $taxonomies ) as $tax ) {
     372                delete_option( "{$tax}_children" );
     373                _get_term_hierarchy( $tax );
     374        }
     375
     376        wp_suspend_cache_invalidation( $suspend );
     377        update_option( '_split_terms', $split_term_data );
     378        update_option( 'shared_terms_split', 1 );
     379}
  • src/wp-admin/includes/upgrade.php

    diff --git src/wp-admin/includes/upgrade.php src/wp-admin/includes/upgrade.php
    index 86c6f9a..221c7e8 100644
    function upgrade_430() { 
    15051505                upgrade_430_fix_comments();
    15061506        }
    15071507
    1508         if ( $wp_current_db_version < 32814 ) {
    1509                 split_all_shared_terms();
    1510         }
    1511 
    15121508        if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) {
    15131509                if ( is_multisite() ) {
    15141510                        $tables = $wpdb->tables( 'blog' );
    function upgrade_430() { 
    15191515                                $tables = array_diff_assoc( $tables, $global_tables );
    15201516                        }
    15211517                }
    1522        
     1518
    15231519                foreach ( $tables as $table ) {
    15241520                        maybe_convert_table_to_utf8mb4( $table );
    15251521                }
    function maybe_convert_table_to_utf8mb4( $table ) { 
    18811877}
    18821878
    18831879/**
    1884  * Splits all shared taxonomy terms.
    1885  *
    1886  * @since 4.3.0
    1887  *
    1888  * @global wpdb $wpdb WordPress database abstraction object.
    1889  */
    1890 function split_all_shared_terms() {
    1891         global $wpdb;
    1892 
    1893         // Get a list of shared terms (those with more than one associated row in term_taxonomy).
    1894         $shared_terms = $wpdb->get_results(
    1895                 "SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {$wpdb->term_taxonomy} tt
    1896                  LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id
    1897                  GROUP BY t.term_id
    1898                  HAVING term_tt_count > 1"
    1899         );
    1900 
    1901         if ( empty( $shared_terms ) ) {
    1902                 return;
    1903         }
    1904 
    1905         // Rekey shared term array for faster lookups.
    1906         $_shared_terms = array();
    1907         foreach ( $shared_terms as $shared_term ) {
    1908                 $term_id = intval( $shared_term->term_id );
    1909                 $_shared_terms[ $term_id ] = $shared_term;
    1910         }
    1911         $shared_terms = $_shared_terms;
    1912 
    1913         // Get term taxonomy data for all shared terms.
    1914         $shared_term_ids = implode( ',', array_keys( $shared_terms ) );
    1915         $shared_tts = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" );
    1916 
    1917         // Split term data recording is slow, so we do it just once, outside the loop.
    1918         $suspend = wp_suspend_cache_invalidation( true );
    1919         $split_term_data = get_option( '_split_terms', array() );
    1920         $skipped_first_term = $taxonomies = array();
    1921         foreach ( $shared_tts as $shared_tt ) {
    1922                 $term_id = intval( $shared_tt->term_id );
    1923 
    1924                 // Don't split the first tt belonging to a given term_id.
    1925                 if ( ! isset( $skipped_first_term[ $term_id ] ) ) {
    1926                         $skipped_first_term[ $term_id ] = 1;
    1927                         continue;
    1928                 }
    1929 
    1930                 if ( ! isset( $split_term_data[ $term_id ] ) ) {
    1931                         $split_term_data[ $term_id ] = array();
    1932                 }
    1933 
    1934                 // Keep track of taxonomies whose hierarchies need flushing.
    1935                 if ( ! isset( $taxonomies[ $shared_tt->taxonomy ] ) ) {
    1936                         $taxonomies[ $shared_tt->taxonomy ] = 1;
    1937                 }
    1938 
    1939                 // Split the term.
    1940                 $split_term_data[ $term_id ][ $shared_tt->taxonomy ] = _split_shared_term( $shared_terms[ $term_id ], $shared_tt, false );
    1941         }
    1942 
    1943         // Rebuild the cached hierarchy for each affected taxonomy.
    1944         foreach ( array_keys( $taxonomies ) as $tax ) {
    1945                 delete_option( "{$tax}_children" );
    1946                 _get_term_hierarchy( $tax );
    1947         }
    1948 
    1949         wp_suspend_cache_invalidation( $suspend );
    1950         update_option( '_split_terms', $split_term_data );
    1951 }
    1952 
    1953 /**
    19541880 * Retrieve all options as it was for 1.2.
    19551881 *
    19561882 * @since 1.2.0
    endif; 
    26902616
    26912617/**
    26922618 * Determine if global tables should be upgraded.
    2693  * 
     2619 *
    26942620 * This function performs a series of checks to ensure the environment allows
    26952621 * for the safe upgrading of global WordPress database tables. It is necessary
    26962622 * because global tables will commonly grow to millions of rows on large