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

Props wonderboymusic.
Fixes #30620.

git-svn-id: https://develop.svn.wordpress.org/trunk@34679 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-09-29 03:51:11 +00:00
parent 58d7f89bf7
commit 36af857315
2 changed files with 40 additions and 4 deletions

View File

@ -781,23 +781,28 @@ function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
* @todo Better formatting for DocBlock. * @todo Better formatting for DocBlock.
* *
* @since 2.3.0 * @since 2.3.0
* @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'.
* *
* @global wpdb $wpdb WordPress database abstraction object. * @global wpdb $wpdb WordPress database abstraction object.
* @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
* *
* @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id' * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
* @param string|int $value Search for this term value * @param string|int $value Search for this term value
* @param string $taxonomy Taxonomy Name * @param string $taxonomy Taxonomy name. Optional, if `$field` is 'term_taxonomy_id'.
* @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
* @param string $filter Optional, default is raw or no WordPress defined filter will applied. * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
* @return object|array|null|WP_Error|false Term Row from database. * @return object|array|null|WP_Error|false Term Row from database.
* Will return false if $taxonomy does not exist or $term was not found. * Will return false if $taxonomy does not exist or $term was not found.
*/ */
function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') { function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
global $wpdb; global $wpdb;
if ( ! taxonomy_exists($taxonomy) ) // 'term_taxonomy_id' lookups don't require taxonomy checks.
if ( 'term_taxonomy_id' !== $field && ! taxonomy_exists( $taxonomy ) ) {
return false; return false;
}
$tax_clause = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
if ( 'slug' == $field ) { if ( 'slug' == $field ) {
$field = 't.slug'; $field = 't.slug';
@ -811,6 +816,9 @@ function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw
} elseif ( 'term_taxonomy_id' == $field ) { } elseif ( 'term_taxonomy_id' == $field ) {
$value = (int) $value; $value = (int) $value;
$field = 'tt.term_taxonomy_id'; $field = 'tt.term_taxonomy_id';
// No `taxonomy` clause when searching by 'term_taxonomy_id'.
$tax_clause = '';
} else { } else {
$term = get_term( (int) $value, $taxonomy, $output, $filter ); $term = get_term( (int) $value, $taxonomy, $output, $filter );
if ( is_wp_error( $term ) || is_null( $term ) ) { if ( is_wp_error( $term ) || is_null( $term ) ) {
@ -819,10 +827,15 @@ function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw
return $term; return $term;
} }
$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 ) ); $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 ) );
if ( ! $term ) if ( ! $term )
return false; return false;
// In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
if ( 'term_taxonomy_id' === $field ) {
$taxonomy = $term->taxonomy;
}
wp_cache_add( $term->term_id, $term, $taxonomy ); wp_cache_add( $term->term_id, $term, $taxonomy );
/** This filter is documented in wp-includes/taxonomy-functions.php */ /** This filter is documented in wp-includes/taxonomy-functions.php */

View File

@ -36,4 +36,27 @@ class Tests_Term_GetTermBy extends WP_UnitTestCase {
$found = get_term_by( 'slug', 'nas', 'wptests_tax' ); $found = get_term_by( 'slug', 'nas', 'wptests_tax' );
$this->assertSame( $t, $found->term_id ); $this->assertSame( $t, $found->term_id );
} }
/**
* @ticket 30620
*/
public function test_taxonomy_should_be_ignored_if_matching_by_term_taxonomy_id() {
global $wpdb;
register_taxonomy( 'wptests_tax', 'post' );
$t = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax' ) );
$term = get_term( $t, 'wptests_tax' );
$new_ttid = $term->term_taxonomy_id + 1;
// Offset just to be sure.
$wpdb->update(
$wpdb->term_taxonomy,
array( 'term_taxonomy_id' => $new_ttid ),
array( 'term_id' => $t )
);
$found = get_term_by( 'term_taxonomy_id', $new_ttid, 'foo' );
$this->assertSame( $t, $found->term_id );
}
} }