Make WordPress Core


Ignore:
Timestamp:
08/14/2015 03:58:41 AM (9 years ago)
Author:
boonebgorges
Message:

Term splitting routine should be run in a separate process, triggered via wp-cron.

[32814] introduced a routine to split shared terms, which was run during the
regular WP database upgrade. This turned out to be problematic because plugins
are not loaded during the db upgrade (due to WP_INSTALLING), with the result
that plugins were not able to hook into the 'split_shared_term' action during
the bulk split. We work around this limitation by moving the term splitting
routine to a separate process, triggered by a wp-cron hook.

Props boonebgorges, Chouby, peterwilsoncc, pento, dd32.
Fixes #30261.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/upgrade.php

    r33609 r33615  
    15061506    }
    15071507
     1508    // Shared terms are split in a separate process.
    15081509    if ( $wp_current_db_version < 32814 ) {
    1509         split_all_shared_terms();
     1510        wp_schedule_single_event( time() + ( 1 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' );
    15101511    }
    15111512
     
    18791880
    18801881    return $wpdb->query( "ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci" );
    1881 }
    1882 
    1883 /**
    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 );
    19511882}
    19521883
Note: See TracChangeset for help on using the changeset viewer.