Make WordPress Core

Changeset 32813


Ignore:
Timestamp:
06/17/2015 01:47:52 AM (11 years ago)
Author:
boonebgorges
Message:

Performance enhancements for _split_shared_term().

  • Introduce a $record parameter, which defaults to true. When set to false, _split_shared_term() will not keep a record of split term data in wp_options. The judicious use of this flag can greatly improve performance when processing shared terms in batches.
  • Allow term/tt objects to be passed to the $term_id and $term_taxonomy_id parameters. This has the potential to save database queries when the objects are already available.

See #30261.

File:
1 edited

Legend:

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

    r32709 r32813  
    41894189 * @ignore
    41904190 * @since 4.2.0
     4191 * @since 4.3.0 Introduced `$record` parameter. `$term_id` and `$term_taxonomy_id` can now accept objects.
    41914192 *
    41924193 * @global wpdb $wpdb
    41934194 *
    4194  * @param int  $term_id          ID of the shared term.
    4195  * @param int  $term_taxonomy_id ID of the term_taxonomy item to receive a new term.
     4195 * @param int|object $term_id          ID of the shared term, or the shared term object.
     4196 * @param int|object $term_taxonomy_id ID of the term_taxonomy item to receive a new term, or the term_taxonomy object
     4197 *                                     (corresponding to a row from the term_taxonomy table).
     4198 * @param bool       $record           Whether to record data about the split term in the options table. The recording
     4199 *                                     process has the potential to be resource-intensive, so during batch operations
     4200 *                                     it can be beneficial to skip inline recording and do it just once, after the
     4201 *                                     batch is processed. Only set this to `false` if you know what you are doing.
     4202 *                                     Default: true.
    41964203 * @return int|WP_Error When the current term does not need to be split (or cannot be split on the current
    41974204 *                      database schema), `$term_id` is returned. When the term is successfully split, the
    41984205 *                      new term_id is returned. A WP_Error is returned for miscellaneous errors.
    41994206 */
    4200 function _split_shared_term( $term_id, $term_taxonomy_id ) {
     4207function _split_shared_term( $term_id, $term_taxonomy_id, $record = true ) {
    42014208        global $wpdb;
     4209
     4210        if ( is_object( $term_id ) ) {
     4211                $shared_term = $term_id;
     4212                $term_id = intval( $shared_term->term_id );
     4213        }
     4214
     4215        if ( is_object( $term_taxonomy_id ) ) {
     4216                $term_taxonomy = $term_taxonomy_id;
     4217                $term_taxonomy_id = intval( $term_taxonomy->term_taxonomy_id );
     4218        }
    42024219
    42034220        // Don't try to split terms if database schema does not support shared slugs.
    … …  
    42094226        // If there are no shared term_taxonomy rows, there's nothing to do here.
    42104227        $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 ) );
     4228
    42114229        if ( ! $shared_tt_count ) {
    42124230                return $term_id;
    … …  
    42144232
    42154233        // Pull up data about the currently shared slug, which we'll use to populate the new one.
    4216         $shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) );
     4234        if ( empty( $shared_term ) ) {
     4235                $shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) );
     4236        }
    42174237
    42184238        $new_term_data = array(
    … …  
    42354255
    42364256        // Reassign child terms to the new parent.
    4237         $term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) );
    4238         $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 ) );
    4239 
     4257        if ( empty( $term_taxonomy ) ) {
     4258                $term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) );
     4259        }
     4260
     4261        $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 ) );
    42404262        if ( ! empty( $children_tt_ids ) ) {
    42414263                foreach ( $children_tt_ids as $child_tt_id ) {
    … …  
    42604282
    42614283        // Keep a record of term_ids that have been split, keyed by old term_id. See {@see wp_get_split_term()}.
    4262         $split_term_data = get_option( '_split_terms', array() );
    4263         if ( ! isset( $split_term_data[ $term_id ] ) ) {
    4264                 $split_term_data[ $term_id ] = array();
    4265         }
    4266 
    4267         $split_term_data[ $term_id ][ $term_taxonomy->taxonomy ] = $new_term_id;
    4268 
    4269         update_option( '_split_terms', $split_term_data );
     4284        if ( $record ) {
     4285                $split_term_data = get_option( '_split_terms', array() );
     4286                if ( ! isset( $split_term_data[ $term_id ] ) ) {
     4287                        $split_term_data[ $term_id ] = array();
     4288                }
     4289
     4290                $split_term_data[ $term_id ][ $term_taxonomy->taxonomy ] = $new_term_id;
     4291                update_option( '_split_terms', $split_term_data );
     4292        }
    42704293
    42714294        /**
Note: See TracChangeset for help on using the changeset viewer.