Make WordPress Core


Ignore:
Timestamp:
10/09/2014 02:31:35 AM (11 years ago)
Author:
boonebgorges
Message:

Improve 'alias_of' handling in wp_insert_term() and wp_update_term().

Using get_term_by() rather than direct SQL queries to fetch the alias term
fixes a number of issues:

  • Object cache for aliased term is properly cleared after update.
  • If the aliased term is in the object cache, it's served from there, saving a database query.
  • Duplicate 'edit_terms' and 'edited_terms' hooks can be removed.
  • Fix a PHP notice when the 'alias_of' term is not found.
  • Prevent the incorrect creation of a new term group for the primary term when the 'alias_of' term is not found.

Adds unit tests for 'alias_of' functionality in both functions.

Fixes #29848.

File:
1 edited

Legend:

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

    r29859 r29862  
    24492449    $term_group = 0;
    24502450    if ( $args['alias_of'] ) {
    2451         $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $args['alias_of'] ) );
    2452         if ( $alias->term_group ) {
     2451        $alias = get_term_by( 'slug', $args['alias_of'], $taxonomy );
     2452        if ( ! empty( $alias->term_group ) ) {
    24532453            // The alias we want is already in a group, so let's use that one.
    24542454            $term_group = $alias->term_group;
    2455         } else {
    2456             // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
     2455        } else if ( ! empty( $alias->term_id ) ) {
     2456            /*
     2457             * The alias is not in a group, so we create a new one
     2458             * and add the alias to it.
     2459             */
    24572460            $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
    24582461
    2459             /**
    2460              * Fires immediately before the given terms are edited.
    2461              *
    2462              * @since 2.9.0
    2463              *
    2464              * @param int    $term_id  Term ID.
    2465              * @param string $taxonomy Taxonomy slug.
    2466              */
    2467             do_action( 'edit_terms', $alias->term_id, $taxonomy );
    2468             $wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id) );
    2469 
    2470             /**
    2471              * Fires immediately after the given terms are edited.
    2472              *
    2473              * @since 2.9.0
    2474              *
    2475              * @param int    $term_id  Term ID
    2476              * @param string $taxonomy Taxonomy slug.
    2477              */
    2478             do_action( 'edited_terms', $alias->term_id, $taxonomy );
     2462            wp_update_term( $alias->term_id, $taxonomy, array(
     2463                'term_group' => $term_group,
     2464            ) );
    24792465        }
    24802466    }
     
    29612947    $term_group = isset( $parsed_args['term_group'] ) ? $parsed_args['term_group'] : 0;
    29622948    if ( $args['alias_of'] ) {
    2963         $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $args['alias_of'] ) );
    2964         if ( $alias->term_group ) {
     2949        $alias = get_term_by( 'slug', $args['alias_of'], $taxonomy );
     2950        if ( ! empty( $alias->term_group ) ) {
    29652951            // The alias we want is already in a group, so let's use that one.
    29662952            $term_group = $alias->term_group;
    2967         } else {
    2968             // The alias isn't in a group, so let's create a new one and firstly add the alias term to it.
     2953        } else if ( ! empty( $alias->term_id ) ) {
     2954            /*
     2955             * The alias is not in a group, so we create a new one
     2956             * and add the alias to it.
     2957             */
    29692958            $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
    29702959
    2971             /** This action is documented in wp-includes/taxonomy.php */
    2972             do_action( 'edit_terms', $alias->term_id, $taxonomy );
    2973             $wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) );
    2974 
    2975             /** This action is documented in wp-includes/taxonomy.php */
    2976             do_action( 'edited_terms', $alias->term_id, $taxonomy );
     2960            wp_update_term( $alias->term_id, $taxonomy, array(
     2961                'term_group' => $term_group,
     2962            ) );
    29772963        }
    29782964
     
    30062992    }
    30072993
    3008     /** This action is documented in wp-includes/taxonomy.php */
     2994    /**
     2995     * Fires immediately before the given terms are edited.
     2996     *
     2997     * @since 2.9.0
     2998     *
     2999     * @param int    $term_id  Term ID.
     3000     * @param string $taxonomy Taxonomy slug.
     3001     */
    30093002    do_action( 'edit_terms', $term_id, $taxonomy );
    30103003    $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
     
    30143007    }
    30153008
    3016     /** This action is documented in wp-includes/taxonomy.php */
     3009    /**
     3010     * Fires immediately after the given terms are edited.
     3011     *
     3012     * @since 2.9.0
     3013     *
     3014     * @param int    $term_id  Term ID
     3015     * @param string $taxonomy Taxonomy slug.
     3016     */
    30173017    do_action( 'edited_terms', $term_id, $taxonomy );
    30183018
Note: See TracChangeset for help on using the changeset viewer.