WordPress.org

Make WordPress Core

Ticket #30261: 30261.6.diff

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

    diff --git src/wp-admin/includes/upgrade.php src/wp-admin/includes/upgrade.php
    index 86c6f9a..24c2d69 100644
    function upgrade_430() { 
    15061506        }
    15071507
    15081508        if ( $wp_current_db_version < 32814 ) {
    1509                 split_all_shared_terms();
     1509                // Terms are split in an external process, so that all plugins are loaded.
     1510                $url = admin_url( 'upgrade.php?step=split_shared_terms' );
     1511                wp_remote_get( $url, array( 'timeout' => 5 ) );
    15101512        }
    15111513
    15121514        if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) {
    function upgrade_network() { 
    16861688                        $tables = $wpdb->tables( 'global' );
    16871689
    16881690                        // sitecategories may not exist.
    1689                         if ( ! $this->get_var( "SHOW TABLES LIKE '{$tables['sitecategories']}'" ) ) {
     1691                        if ( ! $wpdb->get_var( "SHOW TABLES LIKE '{$tables['sitecategories']}'" ) ) {
    16901692                                unset( $tables['sitecategories'] );
    16911693                        }
    16921694
    function upgrade_network() { 
    17151717                        $tables = $wpdb->tables( 'global' );
    17161718
    17171719                        // sitecategories may not exist.
    1718                         if ( ! $this->get_var( "SHOW TABLES LIKE '{$tables['sitecategories']}'" ) ) {
     1720                        if ( ! $wpdb->get_var( "SHOW TABLES LIKE '{$tables['sitecategories']}'" ) ) {
    17191721                                unset( $tables['sitecategories'] );
    17201722                        }
    17211723
    function maybe_convert_table_to_utf8mb4( $table ) { 
    18811883}
    18821884
    18831885/**
    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 /**
    19541886 * Retrieve all options as it was for 1.2.
    19551887 *
    19561888 * @since 1.2.0
  • src/wp-admin/upgrade.php

    diff --git src/wp-admin/upgrade.php src/wp-admin/upgrade.php
    index 53c1d17..3e4d021 100644
     
    66 * @subpackage Administration
    77 */
    88
    9 /**
    10  * We are upgrading WordPress.
    11  *
    12  * @since 1.5.1
    13  * @var bool
    14  */
    15 define( 'WP_INSTALLING', true );
     9if ( isset( $_GET['step'] ) )
     10        $step = $_GET['step'];
     11else
     12        $step = 0;
     13
     14if ( 'split_shared_terms' !== $step ) {
     15        /**
     16         * We are upgrading WordPress.
     17         *
     18         * @since 1.5.1
     19         * @var bool
     20         */
     21        define( 'WP_INSTALLING', true );
     22}
    1623
    1724/** Load WordPress Bootstrap */
    1825require( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); 
    2431
    2532delete_site_transient('update_core');
    2633
    27 if ( isset( $_GET['step'] ) )
    28         $step = $_GET['step'];
    29 else
    30         $step = 0;
    31 
    3234// Do it. No output.
    3335if ( 'upgrade_db' === $step ) {
    3436        wp_upgrade();
    3537        die( '0' );
     38} elseif ( 'split_shared_terms' === $step ) {
     39        split_all_shared_terms();
     40        die( '0' );
    3641}
    3742
    3843/**
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index c2d0acc..2afa998 100644
    function _split_shared_term( $term_id, $term_taxonomy_id, $record = true ) { 
    42514251
    42524252        // Don't try to split terms if database schema does not support shared slugs.
    42534253        $current_db_version = get_option( 'db_version' );
    4254         if ( $current_db_version < 30133 ) {
     4254        if ( $current_db_version < 30133 && ! defined( 'WP_INSTALLING' ) ) {
    42554255                return $term_id;
    42564256        }
    42574257