WordPress.org

Make WordPress Core

Ticket #21760: 21760.patch

File 21760.patch, 7.7 KB (added by johnjamesjacoby, 8 years ago)
  • taxonomy.php

     
    873873 * @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not
    874874 * exist then WP_Error will be returned.
    875875 */
    876 function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
     876function get_term( $term, $taxonomy, $output = OBJECT, $filter = 'raw' ) {
    877877        global $wpdb;
    878         $null = null;
    879878
    880879        if ( empty($term) ) {
    881                 $error = new WP_Error('invalid_term', __('Empty Term'));
    882                 return $error;
     880                return new WP_Error( 'invalid_term', __( 'Empty Term' ) );
    883881        }
    884882
    885         if ( ! taxonomy_exists($taxonomy) ) {
    886                 $error = new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
    887                 return $error;
     883        if ( ! taxonomy_exists( $taxonomy ) ) {
     884                return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
    888885        }
    889886
    890         if ( is_object($term) && empty($term->filter) ) {
    891                 wp_cache_add($term->term_id, $term, $taxonomy);
     887        if ( is_object( $term ) && empty( $term->filter ) ) {
     888                wp_cache_add( $term->term_id, $term, $taxonomy );
    892889                $_term = $term;
    893890        } else {
    894                 if ( is_object($term) )
     891                if ( is_object( $term ) ) {
    895892                        $term = $term->term_id;
    896                 if ( !$term = (int) $term )
    897                         return $null;
    898                 if ( ! $_term = wp_cache_get($term, $taxonomy) ) {
    899                         $_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 t.term_id = %d LIMIT 1", $taxonomy, $term) );
    900                         if ( ! $_term )
    901                                 return $null;
    902                         wp_cache_add($term, $_term, $taxonomy);
    903893                }
     894
     895                if ( empty( $term ) ) {
     896                        return null;
     897                }
     898
     899                if ( ! $_term = wp_cache_get( $term, $taxonomy ) ) {
     900                        $_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 t.term_id = %d LIMIT 1", $taxonomy, (int) $term ) );
     901                        if ( empty( $_term ) ) {
     902                                return null;
     903                        }
     904                        wp_cache_add( $term, $_term, $taxonomy );
     905                }
    904906        }
    905907
    906         $_term = apply_filters('get_term', $_term, $taxonomy);
    907         $_term = apply_filters("get_$taxonomy", $_term, $taxonomy);
    908         $_term = sanitize_term($_term, $taxonomy, $filter);
     908        $_term = apply_filters( 'get_term',      $_term,    $taxonomy );
     909        $_term = apply_filters( "get_$taxonomy", $_term,    $taxonomy );
     910        $_term = sanitize_term( $_term,          $taxonomy, $filter   );
    909911
    910         if ( $output == OBJECT ) {
     912        if ( $output === OBJECT ) {
    911913                return $_term;
    912         } elseif ( $output == ARRAY_A ) {
    913                 $__term = get_object_vars($_term);
     914        } elseif ( $output === ARRAY_A ) {
     915                $__term = get_object_vars( $_term );
    914916                return $__term;
    915         } elseif ( $output == ARRAY_N ) {
    916                 $__term = array_values(get_object_vars($_term));
     917        } elseif ( $output === ARRAY_N ) {
     918                $__term = array_values( get_object_vars( $_term ) );
    917919                return $__term;
    918920        } else {
    919921                return $_term;
     
    937939 * @since 2.3.0
    938940 *
    939941 * @uses $wpdb
     942 * @uses _get_term_by() To query the database directly if no cached data exists
     943 * @uses get_term() To get the term if it's already cached
     944 *
     945 * @param string $field Either 'slug', 'name', or 'id'
     946 * @param string|int $value Search for this term value
     947 * @param string $taxonomy Taxonomy Name
     948 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
     949 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
     950 * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
     951 */
     952function get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filter = 'raw' ) {
     953
     954        // Allow plugins to short-circuit get_term_by()
     955        $pre = apply_filters( 'pre_get_term_by_' . $field . '_' . $value . '_' . $taxonomy, false );
     956        if ( false !== $pre )
     957                return $pre;
     958
     959        // ID lookups are already cached
     960        if ( 'id' == $field ) {
     961                return _get_term_by( $field, $value, $taxonomy, $output, $filter );
     962        }
     963
     964        // Build the key and check the cache
     965        $cache_key = $field . '|' . $taxonomy . '|' . md5( $value );
     966        $term_id   = wp_cache_get( $cache_key, 'get_term_by' );
     967
     968        // Not cached so hit the DB
     969        if ( false === $term_id ) {
     970                $term    = _get_term_by( $field, $value, $taxonomy );
     971                $term_id = !empty( $term ) && ! is_wp_error( $term ) ? $term->term_id : 0;
     972
     973                wp_cache_set( $cache_key, $term_id, 'get_term_by' );
     974
     975        // Cached, so call get_term() to get the term
     976        } else {
     977                $term = get_term( $term_id, $taxonomy, $output, $filter );
     978        }
     979
     980        // Set the return value to false if an error occurred
     981        if ( is_wp_error( $term ) ) {
     982                $term = false;
     983        }
     984
     985        return $term;
     986}
     987
     988/**
     989 * Used internally by get_term_by(), this function is responsible for querying
     990 * the database directly and priming the cache if it's needed.
     991 *
     992 * @package WordPress
     993 * @subpackage Taxonomy
     994 * @since 3.7.0
     995 * @internal Used by get_term_by()
     996 *
     997 * @uses $wpdb
    940998 * @uses sanitize_term() Cleanses the term based on $filter context before returning.
    941999 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
    9421000 *
     
    9471005 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
    9481006 * @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
    9491007 */
    950 function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
     1008function _get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filter = 'raw' ) {
    9511009        global $wpdb;
    9521010
    953         if ( ! taxonomy_exists($taxonomy) )
     1011        // Taxonomy is currently a required part of the query below, so bail if it's
     1012        // missing, not registered, etc...
     1013        if ( ! taxonomy_exists( $taxonomy ) )
    9541014                return false;
    9551015
     1016        // Querying by slug
    9561017        if ( 'slug' == $field ) {
    9571018                $field = 't.slug';
    958                 $value = sanitize_title($value);
    959                 if ( empty($value) )
     1019                $value = sanitize_title( $value );
     1020
     1021                // Bail if no value to check
     1022                if ( empty( $value ) ) {
    9601023                        return false;
     1024                }
    9611025        } else if ( 'name' == $field ) {
    962                 // Assume already escaped
    963                 $value = wp_unslash($value);
     1026                $value = wp_unslash( $value ); // Assume already escaped
    9641027                $field = 't.name';
    9651028        } else {
    966                 $term = get_term( (int) $value, $taxonomy, $output, $filter);
    967                 if ( is_wp_error( $term ) )
     1029                $term = get_term( (int) $value, $taxonomy, $output, $filter );
     1030                if ( is_wp_error( $term ) ) {
    9681031                        $term = false;
     1032                }
    9691033                return $term;
    9701034        }
    9711035
    972         $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) );
    973         if ( !$term )
     1036        // Query for the term
     1037        $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 ) );
     1038        if ( empty( $term ) ) {
    9741039                return false;
     1040        }
    9751041
    976         wp_cache_add($term->term_id, $term, $taxonomy);
     1042        // Prime the term cache in case it's used elsewhere (like a loop of posts)
     1043        wp_cache_add( $term->term_id, $term, $taxonomy );
    9771044
    978         $term = apply_filters('get_term', $term, $taxonomy);
    979         $term = apply_filters("get_$taxonomy", $term, $taxonomy);
    980         $term = sanitize_term($term, $taxonomy, $filter);
     1045        // Filter and sanitize the term
     1046        $term = apply_filters( 'get_term',       $term,     $taxonomy );
     1047        $term = apply_filters( "get_$taxonomy",  $term,     $taxonomy );
     1048        $term = sanitize_term( $term,            $taxonomy, $filter   );
    9811049
     1050        // Adjust the return value based on the $output type
    9821051        if ( $output == OBJECT ) {
    9831052                return $term;
    9841053        } elseif ( $output == ARRAY_A ) {
    985                 return get_object_vars($term);
     1054                return get_object_vars( $term );
    9861055        } elseif ( $output == ARRAY_N ) {
    987                 return array_values(get_object_vars($term));
     1056                return array_values( get_object_vars( $term ) );
    9881057        } else {
    9891058                return $term;
    9901059        }