From 324748a854d5d52d4a19a0a30671a22fa02fd7ff Mon Sep 17 00:00:00 2001 From: Dominik Schilling Date: Thu, 29 Sep 2016 22:36:30 +0000 Subject: [PATCH] Taxonomy: Use `WP_Term_Query` in `get_term_by()`. `WP_Term_Query` already supports querying terms by 'slug', 'name', and 'term_taxonomy_id'. Its additional arguments allow us to generate nearly the same SQL queries as before. This change has one yuge benefit: the term queries are now cached. Add tests to increase coverage of `get_term_by()`. Props spacedmonkey, boonebgorges, johnjamesjacoby, pento, ocean90. Fixes #21760. Built from https://develop.svn.wordpress.org/trunk@38677 git-svn-id: http://core.svn.wordpress.org/trunk@38620 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/taxonomy.php | 54 ++++++++++++++++++++++------------------ wp-includes/version.php | 2 +- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 5767ebf7a3..a39fa47518 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -927,49 +927,55 @@ function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { * or `$term` was not found. */ function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { - global $wpdb; // 'term_taxonomy_id' lookups don't require taxonomy checks. if ( 'term_taxonomy_id' !== $field && ! taxonomy_exists( $taxonomy ) ) { return false; } - $tax_clause = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy ); - - if ( 'slug' == $field ) { - $_field = 't.slug'; - $value = sanitize_title($value); - if ( empty($value) ) - return false; - } elseif ( 'name' == $field ) { - // Assume already escaped - $value = wp_unslash($value); - $_field = 't.name'; - } elseif ( 'term_taxonomy_id' == $field ) { - $value = (int) $value; - $_field = 'tt.term_taxonomy_id'; - - // No `taxonomy` clause when searching by 'term_taxonomy_id'. - $tax_clause = ''; - } else { + if ( 'id' === $field || 'term_id' === $field ) { $term = get_term( (int) $value, $taxonomy, $output, $filter ); - if ( is_wp_error( $term ) || is_null( $term ) ) { + if ( is_wp_error( $term ) || null === $term ) { $term = false; } 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 $_field = %s", $value ) . " $tax_clause LIMIT 1" ); - if ( ! $term ) + $args = array( + 'get' => 'all', + 'number' => 1, + 'taxonomy' => $taxonomy, + 'update_term_meta_cache' => false, + 'orderby' => 'none', + ); + + switch ( $field ) { + case 'slug' : + $args['slug'] = $value; + break; + case 'name' : + $args['name'] = $value; + break; + case 'term_taxonomy_id' : + $args['term_taxonomy_id'] = $value; + unset( $args[ 'taxonomy' ] ); + break; + default : + return false; + } + + $terms = get_terms( $args ); + if ( is_wp_error( $terms ) || empty( $terms ) ) { return false; + } + + $term = array_shift( $terms ); // 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, 'terms' ); - return get_term( $term, $taxonomy, $output, $filter ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 9d1e3dcf2f..2d06feb5e6 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7-alpha-38676'; +$wp_version = '4.7-alpha-38677'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.