WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/13/2014 08:04:02 PM (5 years ago)
Author:
boonebgorges
Message:

Don't split shared terms on term update.

Splitting shared terms means assigning a new term_id to a given term_taxonomy_id.
It was uncovered that this change could cause problems for sites that have
cached the original term_id somehow - say, in postmeta - since future lookups
using that term_id will now fail.

Removing for 4.1-beta1. We'll look at improvements to backward compatibility
to try to get this back into a later beta.

Props mboynes.
See #30335.

File:
1 edited

Legend:

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

    r30265 r30336  
    33873387    $tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id) );
    33883388
    3389     // Check whether this is a shared term that needs splitting.
    3390     $_term_id = _split_shared_term( $term_id, $tt_id );
    3391     if ( ! is_wp_error( $_term_id ) ) {
    3392         $term_id = $_term_id;
    3393     }
    3394 
    33953389    /**
    33963390     * Fires immediately before the given terms are edited.
     
    40444038        do_action( 'edited_term_taxonomy', $term, $taxonomy );
    40454039    }
    4046 }
    4047 
    4048 /**
    4049  * Create a new term for a term_taxonomy item that currently shares its term.
    4050  *
    4051  * @since 4.1.0
    4052  * @access private
    4053  *
    4054  * @param int   $term_id          ID of the shared term.
    4055  * @param int   $term_taxonomy_id ID of the term taxonomy item to receive a new term.
    4056  * @param array $shared_tts       Sibling term taxonomies, used for busting caches.
    4057  * @return int  Term ID.
    4058  */
    4059 function _split_shared_term( $term_id, $term_taxonomy_id ) {
    4060     global $wpdb;
    4061 
    4062     // Don't try to split terms if database schema does not support shared slugs.
    4063     $current_db_version = get_option( 'db_version' );
    4064     if ( $current_db_version < 30133 ) {
    4065         return $term_id;
    4066     }
    4067 
    4068     // If there are no shared term_taxonomy rows, there's nothing to do here.
    4069     $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 ) );
    4070     if ( ! $shared_tt_count ) {
    4071         return $term_id;
    4072     }
    4073 
    4074     // Pull up data about the currently shared slug, which we'll use to populate the new one.
    4075     $shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) );
    4076 
    4077     $new_term_data = array(
    4078         'name' => $shared_term->name,
    4079         'slug' => $shared_term->slug,
    4080         'term_group' => $shared_term->term_group,
    4081     );
    4082 
    4083     if ( false === $wpdb->insert( $wpdb->terms, $new_term_data ) ) {
    4084         return new WP_Error( 'db_insert_error', __( 'Could not split shared term.' ), $wpdb->last_error );
    4085     }
    4086 
    4087     $new_term_id = (int) $wpdb->insert_id;
    4088 
    4089     // Update the existing term_taxonomy to point to the newly created term.
    4090     $wpdb->update( $wpdb->term_taxonomy,
    4091         array( 'term_id' => $new_term_id ),
    4092         array( 'term_taxonomy_id' => $term_taxonomy_id )
    4093     );
    4094 
    4095     // Clean the cache for term taxonomies formerly shared with the current term.
    4096     $shared_term_taxonomies = $wpdb->get_row( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) );
    4097     foreach ( (array) $shared_term_taxonomies as $shared_term_taxonomy ) {
    4098         clean_term_cache( $term_id, $shared_term_taxonomy );
    4099     }
    4100 
    4101     return $new_term_id;
    41024040}
    41034041
Note: See TracChangeset for help on using the changeset viewer.