WordPress.org

Make WordPress Core


Ignore:
Timestamp:
12/03/2016 04:04:05 AM (4 years ago)
Author:
dd32
Message:

Revert [38677] from the 4.7 branch.
This avoids fatal errors caused with recursive calling of term functions within the get_terms filter.

See #21760.

File:
1 edited

Legend:

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

    r39326 r39454  
    825825 */
    826826function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
     827    global $wpdb;
    827828
    828829    // 'term_taxonomy_id' lookups don't require taxonomy checks.
     
    831832    }
    832833
    833     if ( 'id' === $field || 'term_id' === $field ) {
     834    $tax_clause = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
     835
     836    if ( 'slug' == $field ) {
     837        $_field = 't.slug';
     838        $value = sanitize_title($value);
     839        if ( empty($value) )
     840            return false;
     841    } elseif ( 'name' == $field ) {
     842        // Assume already escaped
     843        $value = wp_unslash($value);
     844        $_field = 't.name';
     845    } elseif ( 'term_taxonomy_id' == $field ) {
     846        $value = (int) $value;
     847        $_field = 'tt.term_taxonomy_id';
     848
     849        // No `taxonomy` clause when searching by 'term_taxonomy_id'.
     850        $tax_clause = '';
     851    } else {
    834852        $term = get_term( (int) $value, $taxonomy, $output, $filter );
    835         if ( is_wp_error( $term ) || null === $term ) {
     853        if ( is_wp_error( $term ) || is_null( $term ) ) {
    836854            $term = false;
    837855        }
     
    839857    }
    840858
    841     $args = array(
    842         'get'                    => 'all',
    843         'number'                 => 1,
    844         'taxonomy'               => $taxonomy,
    845         'update_term_meta_cache' => false,
    846         'orderby'                => 'none',
    847     );
    848 
    849     switch ( $field ) {
    850         case 'slug' :
    851             $args['slug'] = $value;
    852             break;
    853         case 'name' :
    854             $args['name'] = $value;
    855             break;
    856         case 'term_taxonomy_id' :
    857             $args['term_taxonomy_id'] = $value;
    858             unset( $args[ 'taxonomy' ] );
    859             break;
    860         default :
    861             return false;
    862     }
    863 
    864     $terms = get_terms( $args );
    865     if ( is_wp_error( $terms ) || empty( $terms ) ) {
     859    $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" );
     860    if ( ! $term )
    866861        return false;
    867     }
    868 
    869     $term = array_shift( $terms );
    870862
    871863    // In the case of 'term_taxonomy_id', override the provided `$taxonomy` with whatever we find in the db.
     
    873865        $taxonomy = $term->taxonomy;
    874866    }
     867
     868    wp_cache_add( $term->term_id, $term, 'terms' );
    875869
    876870    return get_term( $term, $taxonomy, $output, $filter );
Note: See TracChangeset for help on using the changeset viewer.