Make WordPress Core

Changeset 34679


Ignore:
Timestamp:
09/29/2015 03:51:11 AM (9 years ago)
Author:
boonebgorges
Message:

Don't require explicit taxonomy when getting terms by term_taxonomy_id.

Props wonderboymusic.
Fixes #30620.

Location:
trunk
Files:
2 edited

Legend:

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

    r34538 r34679  
    782782 *
    783783 * @since 2.3.0
     784 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'.
    784785 *
    785786 * @global wpdb $wpdb WordPress database abstraction object.
     
    788789 * @param string     $field    Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
    789790 * @param string|int $value    Search for this term value
    790  * @param string     $taxonomy Taxonomy Name
     791 * @param string     $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
    791792 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
    792793 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
     
    794795 *                                          Will return false if $taxonomy does not exist or $term was not found.
    795796 */
    796 function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
     797function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
    797798    global $wpdb;
    798799
    799     if ( ! taxonomy_exists($taxonomy) )
     800    // 'term_taxonomy_id' lookups don't require taxonomy checks.
     801    if ( 'term_taxonomy_id' !== $field && ! taxonomy_exists( $taxonomy ) ) {
    800802        return false;
     803    }
     804
     805    $tax_clause = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
    801806
    802807    if ( 'slug' == $field ) {
     
    812817        $value = (int) $value;
    813818        $field = 'tt.term_taxonomy_id';
     819
     820        // No `taxonomy` clause when searching by 'term_taxonomy_id'.
     821        $tax_clause = '';
    814822    } else {
    815823        $term = get_term( (int) $value, $taxonomy, $output, $filter );
     
    820828    }
    821829
    822     $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value ) );
     830    $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE $field = %s $tax_clause LIMIT 1", $value ) );
    823831    if ( ! $term )
    824832        return false;
     833
     834    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
     835    if ( 'term_taxonomy_id' === $field ) {
     836        $taxonomy = $term->taxonomy;
     837    }
    825838
    826839    wp_cache_add( $term->term_id, $term, $taxonomy );
  • trunk/tests/phpunit/tests/term/getTermBy.php

    r34628 r34679  
    3737        $this->assertSame( $t, $found->term_id );
    3838    }
     39
     40    /**
     41     * @ticket 30620
     42     */
     43    public function test_taxonomy_should_be_ignored_if_matching_by_term_taxonomy_id() {
     44        global $wpdb;
     45
     46        register_taxonomy( 'wptests_tax', 'post' );
     47        $t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
     48        $term = get_term( $t, 'wptests_tax' );
     49
     50        $new_ttid = $term->term_taxonomy_id + 1;
     51
     52        // Offset just to be sure.
     53        $wpdb->update(
     54            $wpdb->term_taxonomy,
     55            array( 'term_taxonomy_id' => $new_ttid ),
     56            array( 'term_id' => $t )
     57        );
     58
     59        $found = get_term_by( 'term_taxonomy_id', $new_ttid, 'foo' );
     60        $this->assertSame( $t, $found->term_id );
     61    }
    3962}
Note: See TracChangeset for help on using the changeset viewer.