Make WordPress Core


Ignore:
Timestamp:
10/10/2015 01:58:37 AM (9 years ago)
Author:
boonebgorges
Message:

Introduce WP_Term.

get_term() now returns a WP_Term object, instead of a stdClass object.
Cache support and sanitization filters for individual terms are now more
centralized. For example, get_term_by() is able to cast results of its query
to a WP_Term object by passing it through get_term().

The $taxonomy parameter for get_term() is now optional, as terms ought to
be unique to a taxonomy (ie, shared terms no longer exist). In cases where
get_term() detects that the term matching the specified term_id is from the
wrong taxonomy, it checks to see if you've requested a shared term, and if so,
it splits the term. This is used only for fallback purposes.

The elimination of shared terms allows the caching strategy for terms to be
simplified. Individual terms are now cached in a single 'terms' bucket.

Props flixos90, boonebgorges, scribu, dipesh.kakadiya.
See #14162.

File:
1 edited

Legend:

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

    r34891 r34997  
    709709 *
    710710 * @since 2.3.0
     711 * @since 4.4.0 Converted to return a WP_Term object if `$output` is `OBJECT`.
     712 *              The `$taxonomy` parameter was made optional.
    711713 *
    712714 * @global wpdb $wpdb WordPress database abstraction object.
    713715 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
    714716 *
    715  * @param int|object $term     If integer, will get from database. If object will apply filters and return $term.
    716  * @param string     $taxonomy Taxonomy name that $term is part of.
     717 * @param int|WP_Term|object $term If integer, term data will be fetched from the database, or from the cache if
     718 *                                 available. If stdClass object (as in the results of a database query), will apply
     719 *                                 filters and return a `WP_Term` object corresponding to the `$term` data. If `WP_Term`,
     720 *                                 will return `$term`.
     721 * @param string     $taxonomy Optional. Taxonomy name that $term is part of.
    717722 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
    718723 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
    719  * @return object|array|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not
    720  * exist then WP_Error will be returned.
    721  */
    722 function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
    723     global $wpdb;
    724 
     724 * @return mixed Type corresponding to `$output` on success or null on failure. When `$output` is `OBJECT`,
     725 *               a WP_Term instance is returned. If taxonomy does not exist then WP_Error will be returned.
     726 */
     727function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
    725728    if ( empty( $term ) ) {
    726729        return new WP_Error( 'invalid_term', __( 'Empty Term' ) );
    727730    }
    728731
    729     if ( ! taxonomy_exists( $taxonomy ) ) {
     732    if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) {
    730733        return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
    731734    }
    732735
    733     if ( is_object($term) && empty($term->filter) ) {
    734         wp_cache_add( $term->term_id, $term, $taxonomy );
     736    if ( $term instanceof WP_Term ) {
    735737        $_term = $term;
     738    } elseif ( is_object( $term ) ) {
     739        if ( empty( $term->filter ) || 'raw' === $term->filter ) {
     740            $_term = sanitize_term( $term, $taxonomy, 'raw' );
     741            $_term = new WP_Term( $_term );
     742        } else {
     743            $_term = WP_Term::get_instance( $term->term_id );
     744        }
    736745    } else {
    737         if ( is_object($term) )
    738             $term = $term->term_id;
    739         if ( !$term = (int) $term )
    740             return null;
    741         if ( ! $_term = wp_cache_get( $term, $taxonomy ) ) {
    742             $_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) );
    743             if ( ! $_term )
    744                 return null;
    745             wp_cache_add( $term, $_term, $taxonomy );
    746         }
     746        $_term = WP_Term::get_instance( $term );
     747    }
     748
     749    // If `$taxonomy` was provided, make sure it matches the taxonomy of the located term.
     750    if ( $_term && $taxonomy && $taxonomy !== $_term->taxonomy ) {
     751        // If there are two terms with the same ID, split the other one to a new term.
     752        $new_term_id = _split_shared_term( $_term->term_id, $_term->term_taxonomy_id );
     753
     754        // If no split occurred, this is an invalid request.
     755        if ( $new_term_id === $_term->term_id ) {
     756            return new WP_Error( 'invalid_term', __( 'Empty Term' ) );
     757
     758        // The term has been split. Refetch the term from the proper taxonomy.
     759        } else {
     760            return get_term( $_term->term_id, $taxonomy, $output, $filter );
     761        }
     762    }
     763
     764    if ( ! $_term ) {
     765        return null;
    747766    }
    748767
     
    751770     *
    752771     * @since 2.3.0
    753      *
    754      * @param int|object $_term    Term object or ID.
    755      * @param string     $taxonomy The taxonomy slug.
     772     * @since 4.4.0 `$_term` can now also be a WP_Term object.
     773     *
     774     * @param int|WP_Term $_term    Term object or ID.
     775     * @param string      $taxonomy The taxonomy slug.
    756776     */
    757777    $_term = apply_filters( 'get_term', $_term, $taxonomy );
     
    764784     *
    765785     * @since 2.3.0
    766      *
    767      * @param int|object $_term    Term object or ID.
    768      * @param string     $taxonomy The taxonomy slug.
     786     * @since 4.4.0 `$_term` can now also be a WP_Term object.
     787     *
     788     * @param int|WP_Term $_term    Term object or ID.
     789     * @param string      $taxonomy The taxonomy slug.
    769790     */
    770791    $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy );
    771     $_term = sanitize_term($_term, $taxonomy, $filter);
    772 
    773     if ( $output == OBJECT ) {
    774         return $_term;
    775     } elseif ( $output == ARRAY_A ) {
    776         $__term = get_object_vars($_term);
    777         return $__term;
     792
     793    // Sanitize term, according to the specified filter.
     794    $_term->filter( $filter );
     795
     796    if ( $output == ARRAY_A ) {
     797        return $_term->to_array();
    778798    } elseif ( $output == ARRAY_N ) {
    779         $__term = array_values(get_object_vars($_term));
    780         return $__term;
    781     } else {
    782         return $_term;
    783     }
     799        return array_values( $_term->to_array() );
     800    }
     801
     802    return $_term;
    784803}
    785804
     
    799818 *
    800819 * @since 2.3.0
    801  * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'.
     820 * @since 4.4.0 `$taxonomy` is optional if `$field` is 'term_taxonomy_id'. Converted to return
     821 *              a WP_Term object if `$output` is `OBJECT`.
    802822 *
    803823 * @global wpdb $wpdb WordPress database abstraction object.
     
    809829 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
    810830 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
    811  * @return object|array|null|WP_Error|false Term Row from database.
    812  *                                          Will return false if $taxonomy does not exist or $term was not found.
     831 * @return WP_Term|bool WP_Term instance on success. Will return false if `$taxonomy` does not exist
     832 *                      or `$term` was not found.
    813833 */
    814834function get_term_by( $field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
     
    823843
    824844    if ( 'slug' == $field ) {
    825         $field = 't.slug';
     845        $_field = 't.slug';
    826846        $value = sanitize_title($value);
    827847        if ( empty($value) )
     
    830850        // Assume already escaped
    831851        $value = wp_unslash($value);
    832         $field = 't.name';
     852        $_field = 't.name';
    833853    } elseif ( 'term_taxonomy_id' == $field ) {
    834854        $value = (int) $value;
    835         $field = 'tt.term_taxonomy_id';
     855        $_field = 'tt.term_taxonomy_id';
    836856
    837857        // No `taxonomy` clause when searching by 'term_taxonomy_id'.
     
    845865    }
    846866
    847     $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 ) );
     867    $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 ) );
    848868    if ( ! $term )
    849869        return false;
     
    854874    }
    855875
    856     wp_cache_add( $term->term_id, $term, $taxonomy );
    857 
    858     /** This filter is documented in wp-includes/taxonomy-functions.php */
    859     $term = apply_filters( 'get_term', $term, $taxonomy );
    860 
    861     /** This filter is documented in wp-includes/taxonomy-functions.php */
    862     $term = apply_filters( "get_$taxonomy", $term, $taxonomy );
    863 
    864     $term = sanitize_term($term, $taxonomy, $filter);
    865 
    866     if ( $output == OBJECT ) {
    867         return $term;
    868     } elseif ( $output == ARRAY_A ) {
    869         return get_object_vars($term);
    870     } elseif ( $output == ARRAY_N ) {
    871         return array_values(get_object_vars($term));
    872     } else {
    873         return $term;
    874     }
     876    wp_cache_add( $term->term_id, $term, 'terms' );
     877
     878    return get_term( $term, $taxonomy, $output, $filter );
    875879}
    876880
     
    34233427            $taxonomies[] = $term->taxonomy;
    34243428            $ids[] = $term->term_id;
    3425             wp_cache_delete($term->term_id, $term->taxonomy);
     3429            wp_cache_delete( $term->term_id, 'terms' );
    34263430        }
    34273431        $taxonomies = array_unique($taxonomies);
     
    34303434        foreach ( $taxonomies as $taxonomy ) {
    34313435            foreach ( $ids as $id ) {
    3432                 wp_cache_delete($id, $taxonomy);
     3436                wp_cache_delete( $id, 'terms' );
    34333437            }
    34343438        }
     
    35533557            $term_taxonomy = $term->taxonomy;
    35543558
    3555         wp_cache_add( $term->term_id, $term, $term_taxonomy );
     3559        wp_cache_add( $term->term_id, $term, 'terms' );
    35563560    }
    35573561}
Note: See TracChangeset for help on using the changeset viewer.