Make WordPress Core


Ignore:
Timestamp:
11/05/2015 04:44:59 PM (9 years ago)
Author:
boonebgorges
Message:

Make get_term() behave more consistently in the context of shared terms.

When WP_Term was introduced in [34997], the $taxonomy parameter for
get_term() was made optional. This meant that, when the optional param was
omitted, get_term() had no way of determining which term was intended when
the term_id was shared between multiple taxonomies. As a (somewhat sneaky) way
of fixing things, get_term() split any shared terms it found. But this could
cause problems with developer expectations: it's not clear why requesting a
term should result in a database update, much less a potential change in the
ID of a term.

In place of this technique, this changeset introduces a number of changes that
make the handling of shared terms a bit less insane:

  • When a taxonomy is provided to get_term(), and a cached term is found matching the term_id, make sure the taxonomy also matches before returning it.
  • When a taxonomy is not provided, ensure that the term is not shared before adding it to the cache.
  • When a term is shared between taxonomies and no taxonomy is provided, return a WP_Error rather than splitting the term.
  • When a term is shared between taxonomies, only one of which is valid, return the term from that taxonomy.

Props boonebgorges, dlh.
Fixes #34533.

File:
1 edited

Legend:

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

    r35515 r35537  
    765765        }
    766766    } else {
    767         $_term = WP_Term::get_instance( $term );
    768     }
    769 
    770     // If `$taxonomy` was provided, make sure it matches the taxonomy of the located term.
    771     if ( $_term && $taxonomy && $taxonomy !== $_term->taxonomy ) {
    772         // If there are two terms with the same ID, split the other one to a new term.
    773         $new_term_id = _split_shared_term( $_term->term_id, $_term->term_taxonomy_id );
    774 
    775         // If no split occurred, this is an invalid request. Return null (not WP_Error) for back compat.
    776         if ( $new_term_id === $_term->term_id ) {
    777             return null;
    778 
    779         // The term has been split. Refetch the term from the proper taxonomy.
    780         } else {
    781             return get_term( $_term->term_id, $taxonomy, $output, $filter );
    782         }
    783     }
    784 
    785     if ( ! $_term ) {
     767        $_term = WP_Term::get_instance( $term, $taxonomy );
     768    }
     769
     770    if ( is_wp_error( $_term ) ) {
     771        return $_term;
     772    } elseif ( ! $_term ) {
    786773        return null;
    787774    }
Note: See TracChangeset for help on using the changeset viewer.