Make WordPress Core

Ticket #30261: 30261.13.diff

File 30261.13.diff, 8.4 KB (added by pento, 9 years ago)
  • src/wp-admin/includes/schema.php

     
    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/upgrade.php

     
    15061506        }
    15071507
    15081508        if ( $wp_current_db_version < 32814 ) {
    1509                 split_all_shared_terms();
     1509                _wp_batch_split_terms();
    15101510        }
    15111511
     1512
    15121513        if ( $wp_current_db_version < 33055 && 'utf8mb4' === $wpdb->charset ) {
    15131514                if ( is_multisite() ) {
    15141515                        $tables = $wpdb->tables( 'blog' );
     
    18811882}
    18821883
    18831884/**
    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 /**
    19541885 * Retrieve all options as it was for 1.2.
    19551886 *
    19561887 * @since 1.2.0
  • src/wp-includes/default-filters.php

     
    331331add_action( 'split_shared_term', '_wp_check_split_default_terms',  10, 4 );
    332332add_action( 'split_shared_term', '_wp_check_split_terms_in_menus', 10, 4 );
    333333add_action( 'split_shared_term', '_wp_check_split_nav_menu_terms', 10, 4 );
     334add_action( 'split_shared_term_batch', '_wp_batch_split_terms' );
    334335
    335336/**
    336337 * Filters formerly mixed into wp-includes
  • src/wp-includes/taxonomy.php

     
    42494249                $term_taxonomy_id = intval( $term_taxonomy->term_taxonomy_id );
    42504250        }
    42514251
    4252         // Don't try to split terms if database schema does not support shared slugs.
    4253         $current_db_version = get_option( 'db_version' );
    4254         if ( $current_db_version < 30133 ) {
    4255                 return $term_id;
    4256         }
    4257 
    42584252        // If there are no shared term_taxonomy rows, there's nothing to do here.
    42594253        $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 ) );
    42604254
     
    42624256                return $term_id;
    42634257        }
    42644258
     4259        // Check if the term has not already been split by a plugin hooked to 'split_shared_term'
     4260        $check_term_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) );
     4261        if ( $check_term_id != $term_id ) {
     4262                return $check_term_id;
     4263        }
     4264
    42654265        // Pull up data about the currently shared slug, which we'll use to populate the new one.
    42664266        if ( empty( $shared_term ) ) {
    42674267                $shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) );
     
    43394339}
    43404340
    43414341/**
     4342 * Splits a batch of shared taxonomy terms.
     4343 *
     4344 * @since 4.3.0
     4345 *
     4346 * @global wpdb $wpdb WordPress database abstraction object.
     4347 */
     4348function _wp_batch_split_terms() {
     4349        global $wpdb;
     4350
     4351        $lock_name = 'term_split.lock';
     4352
     4353        // Try to lock
     4354        $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_name, time() ) );
     4355
     4356        if ( ! $lock_result ) {
     4357                $lock_result = get_option( $lock_name );
     4358
     4359                // If we couldn't create a lock, and there isn't a lock, bail
     4360                if ( ! $lock_result )
     4361                        return;
     4362
     4363                // Check to see if the lock is still valid
     4364                if ( $lock_result > ( time() - HOUR_IN_SECONDS ) )
     4365                        return;
     4366        }
     4367
     4368        // Update the lock, as by this point we've definitely got a lock, just need to fire the actions
     4369        update_option( $lock_name, time() );
     4370
     4371        // Get a list of shared terms (those with more than one associated row in term_taxonomy).
     4372        $shared_terms = $wpdb->get_results(
     4373                "SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {$wpdb->term_taxonomy} tt
     4374                 LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id
     4375                 GROUP BY t.term_id
     4376                 HAVING term_tt_count > 1
     4377                 LIMIT 20"
     4378        );
     4379
     4380        // No more terms, we're done here.
     4381        if ( ! $shared_terms ) {
     4382                delete_option( $lock_name );
     4383                return;
     4384        }
     4385
     4386        // Shared terms found? We'll need to run this script again.
     4387        wp_schedule_single_event( time() + ( 5 * MINUTE_IN_SECONDS ), 'split_shared_term_batch' );
     4388
     4389        // Rekey shared term array for faster lookups.
     4390        $_shared_terms = array();
     4391        foreach ( $shared_terms as $shared_term ) {
     4392                $term_id = intval( $shared_term->term_id );
     4393                $_shared_terms[ $term_id ] = $shared_term;
     4394        }
     4395        $shared_terms = $_shared_terms;
     4396
     4397        // Get term taxonomy data for all shared terms.
     4398        $shared_term_ids = implode( ',', array_keys( $shared_terms ) );
     4399        $shared_tts = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" );
     4400
     4401        // Split term data recording is slow, so we do it just once, outside the loop.
     4402        $suspend = wp_suspend_cache_invalidation( true );
     4403        $split_term_data = get_option( '_split_terms', array() );
     4404        $skipped_first_term = $taxonomies = array();
     4405        foreach ( $shared_tts as $shared_tt ) {
     4406                $term_id = intval( $shared_tt->term_id );
     4407
     4408                // Don't split the first tt belonging to a given term_id.
     4409                if ( ! isset( $skipped_first_term[ $term_id ] ) ) {
     4410                        $skipped_first_term[ $term_id ] = 1;
     4411                        continue;
     4412                }
     4413
     4414                if ( ! isset( $split_term_data[ $term_id ] ) ) {
     4415                        $split_term_data[ $term_id ] = array();
     4416                }
     4417
     4418                // Keep track of taxonomies whose hierarchies need flushing.
     4419                if ( ! isset( $taxonomies[ $shared_tt->taxonomy ] ) ) {
     4420                        $taxonomies[ $shared_tt->taxonomy ] = 1;
     4421                }
     4422
     4423                // Split the term.
     4424                $split_term_data[ $term_id ][ $shared_tt->taxonomy ] = _split_shared_term( $shared_terms[ $term_id ], $shared_tt, false );
     4425        }
     4426
     4427        // Rebuild the cached hierarchy for each affected taxonomy.
     4428        foreach ( array_keys( $taxonomies ) as $tax ) {
     4429                delete_option( "{$tax}_children" );
     4430                _get_term_hierarchy( $tax );
     4431        }
     4432
     4433        wp_suspend_cache_invalidation( $suspend );
     4434        update_option( '_split_terms', $split_term_data );
     4435
     4436        delete_option( $lock_name );
     4437}
     4438
     4439/**
    43424440 * Check default categories when a term gets split to see if any of them need to be updated.
    43434441 *
    43444442 * @ignore