WordPress.org

Make WordPress Core

Ticket #14162: 14162.2.patch

File 14162.2.patch, 13.7 KB (added by boonebgorges, 6 years ago)
  • src/wp-includes/category.php

    diff --git a/src/wp-includes/category.php b/src/wp-includes/category.php
    index a94baa7..df1f91a 100644
    a b function clean_category_cache( $id ) { 
    323323 */
    324324function _make_cat_compat( &$category ) {
    325325        if ( is_object( $category ) && ! is_wp_error( $category ) ) {
    326                 $category->cat_ID = &$category->term_id;
    327                 $category->category_count = &$category->count;
    328                 $category->category_description = &$category->description;
    329                 $category->cat_name = &$category->name;
    330                 $category->category_nicename = &$category->slug;
    331                 $category->category_parent = &$category->parent;
     326                $category->cat_ID = $category->term_id;
     327                $category->category_count = $category->count;
     328                $category->category_description = $category->description;
     329                $category->cat_name = $category->name;
     330                $category->category_nicename = $category->slug;
     331                $category->category_parent = $category->parent;
    332332        } elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
    333333                $category['cat_ID'] = &$category['term_id'];
    334334                $category['category_count'] = &$category['count'];
  • new file src/wp-includes/class-wp-term.php

    diff --git a/src/wp-includes/class-wp-term.php b/src/wp-includes/class-wp-term.php
    new file mode 100644
    index 0000000..18afedd
    - +  
     1<?php
     2/**
     3 * WordPress Term class.
     4 *
     5 * @since 4.4.0
     6 * @package WordPress
     7 * @subpackage Taxonomy
     8 *
     9 */
     10final class WP_Term {
     11
     12        /**
     13         * Term ID.
     14         *
     15         * @var int
     16         */
     17        public $term_id;
     18
     19        /**
     20         * The term's name.
     21         *
     22         * @var string
     23         */
     24        public $name = '';
     25
     26        /**
     27         * The term's slug.
     28         *
     29         * @var string
     30         */
     31        public $slug = '';
     32
     33        public $term_group = '';
     34
     35        /**
     36         * Term Taxonomy ID.
     37         *
     38         * @var int
     39         */
     40        public $term_taxonomy_id = 0;
     41
     42        /**
     43         * The term's taxonomy name.
     44         *
     45         * @var string
     46         */
     47        public $taxonomy = '';
     48
     49        /**
     50         * The term's description.
     51         *
     52         * @var string
     53         */
     54        public $description = '';
     55
     56        /**
     57         * ID of a term's parent term.
     58         *
     59         * @var int
     60         */
     61        public $parent = 0;
     62
     63        /**
     64         * Cached object count for this term.
     65         *
     66         * @var int
     67         */
     68        public $count = 0;
     69
     70        /**
     71         * Stores the term object's sanitization level.
     72         *
     73         * Does not correspond to a DB field.
     74         *
     75         * @var string
     76         */
     77        public $filter;
     78
     79        /**
     80         * Retrieve WP_Term instance.
     81         *
     82         * @static
     83         * @access public
     84         *
     85         * @global wpdb $wpdb
     86         *
     87         * @param int $term_id Term ID.
     88         * @return WP_Term|false Term object, false otherwise.
     89         */
     90        public static function get_instance( $term_id ) {
     91                global $wpdb;
     92
     93                $term_id = (int) $term_id;
     94                if ( ! $term_id ) {
     95                        return false;
     96                }
     97
     98                $_term = wp_cache_get( $term_id, 'terms' );
     99
     100                if ( ! $_term ) {
     101                        $_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 t.term_id = %d LIMIT 1", $term_id ) );
     102                        if ( ! $_term ) {
     103                                return false;
     104                        }
     105
     106                        $_term = sanitize_term( $_term, $_term->taxonomy, 'raw' );
     107                        wp_cache_add( $term_id, $_term, 'terms' );
     108                } elseif ( empty( $_term->filter ) ) {
     109                        $_term = sanitize_term( $_term, $_term->taxonomy, 'raw' );
     110                }
     111
     112                return new WP_Term( $_term );
     113        }
     114
     115        /**
     116         * Constructor.
     117         *
     118         * @param WP_Term|object $term Term object.
     119         */
     120        public function __construct( $term ) {
     121                foreach ( get_object_vars( $term ) as $key => $value ) {
     122                        $this->$key = $value;
     123                }
     124        }
     125
     126        /**
     127         * {@Missing Summary}
     128         *
     129         * @param string $filter Filter.
     130         * @return self|array|bool|object|WP_Term
     131         */
     132        public function filter( $filter ) {
     133                if ( $this->filter == $filter )
     134                        return $this;
     135
     136                if ( $filter == 'raw' )
     137                        return self::get_instance( $this->term_id );
     138
     139                return sanitize_term( $this, $this->taxonomy, $filter );
     140        }
     141
     142        /**
     143         * Convert object to array.
     144         *
     145         * @return array Object as array.
     146         */
     147        public function to_array() {
     148                return get_object_vars( $this );
     149        }
     150}
  • src/wp-includes/taxonomy-functions.php

    diff --git a/src/wp-includes/taxonomy-functions.php b/src/wp-includes/taxonomy-functions.php
    index 4f6bbf9..d51e130 100644
    a b function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) { 
    685685 * @global wpdb $wpdb WordPress database abstraction object.
    686686 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
    687687 *
    688  * @param int|object $term     If integer, will get from database. If object will apply filters and return $term.
    689  * @param string     $taxonomy Taxonomy name that $term is part of.
    690  * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
    691  * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
    692  * @return object|array|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not
    693  * exist then WP_Error will be returned.
     688 * @param int|WP_Term $term     If integer, will get from database. If object will apply filters and return $term.
     689 * @param string      $taxonomy Taxonomy name that $term is part of.
     690 * @param string      $output   Constant OBJECT, ARRAY_A, or ARRAY_N
     691 * @param string      $filter   Optional, default is raw or no WordPress defined filter will applied.
     692 * @return WP_Term|array|null|WP_Error Type corresponding to $output on success or null on failure. When $output is OBJECT,
     693 *                                     a `WP_Term` instance is returned. If taxonomy does not exist then WP_Error will be returned.
    694694 */
    695 function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
    696         global $wpdb;
    697 
     695function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) {
    698696        if ( empty( $term ) ) {
    699697                return new WP_Error( 'invalid_term', __( 'Empty Term' ) );
    700698        }
    701699
    702         if ( ! taxonomy_exists( $taxonomy ) ) {
     700        if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) {
    703701                return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
    704702        }
    705703
    706         if ( is_object($term) && empty($term->filter) ) {
    707                 wp_cache_add( $term->term_id, $term, $taxonomy );
     704        if ( $term instanceof WP_Term ) {
    708705                $_term = $term;
     706        } elseif ( is_object( $term ) ) {
     707                if ( empty( $term->filter ) ) {
     708                        $_term = sanitize_term( $term, $taxonomy, 'raw' );
     709                        $_term = new WP_Term( $_term );
     710                } elseif ( 'raw' == $term->filter ) {
     711                        $_term = new WP_Term( $term );
     712                } else {
     713                        $_term = WP_Term::get_instance( $term->term_id );
     714                }
    709715        } else {
    710                 if ( is_object($term) )
    711                         $term = $term->term_id;
    712                 if ( !$term = (int) $term )
    713                         return null;
    714                 if ( ! $_term = wp_cache_get( $term, $taxonomy ) ) {
    715                         $_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) );
    716                         if ( ! $_term )
    717                                 return null;
    718                         wp_cache_add( $term, $_term, $taxonomy );
     716                $_term = WP_Term::get_instance( $term );
     717        }
     718
     719        // If a taxonomy was passed, make sure it matches the taxonomy of the located term.
     720        if ( $_term && $taxonomy && $taxonomy !== $_term->taxonomy ) {
     721                // If there are two terms with the same ID, split them.
     722                $new_term_id = _split_shared_term( $_term->term_id, $_term->term_taxonomy_id );
     723
     724                // If no split occurred, this is an invalid request.
     725                if ( $new_term_id === $_term->term_id ) {
     726                        return new WP_Error( 'invalid_term', __( 'Empty Term' ) );
     727                } else {
     728                        $_term = WP_Term::get_instance( $new_term_id );
    719729                }
    720730        }
    721731
     732        if ( ! $_term ) {
     733                return null;
     734        }
     735
    722736        /**
    723737         * Filter a term.
    724738         *
    function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') { 
    741755         * @param string     $taxonomy The taxonomy slug.
    742756         */
    743757        $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy );
    744         $_term = sanitize_term($_term, $taxonomy, $filter);
    745 
    746         if ( $output == OBJECT ) {
    747                 return $_term;
    748         } elseif ( $output == ARRAY_A ) {
    749                 $__term = get_object_vars($_term);
    750                 return $__term;
    751         } elseif ( $output == ARRAY_N ) {
    752                 $__term = array_values(get_object_vars($_term));
    753                 return $__term;
    754         } else {
    755                 return $_term;
    756         }
     758        $_term = sanitize_term( $_term, $taxonomy, $filter );
     759
     760        if ( $output == ARRAY_A )
     761                return $_term->to_array();
     762        elseif ( $output == ARRAY_N )
     763                return array_values( $_term->to_array() );
     764
     765        return $_term;
    757766}
    758767
    759768/**
    function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') { 
    780789 * @param string     $taxonomy Taxonomy Name
    781790 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
    782791 * @param string     $filter   Optional, default is raw or no WordPress defined filter will applied.
    783  * @return object|array|null|WP_Error|false Term Row from database.
     792 * @return WP_Term|array|null|WP_Error|false Instance of WP_Term.
    784793 *                                          Will return false if $taxonomy does not exist or $term was not found.
    785794 */
    786795function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
    function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw 
    814823
    815824        wp_cache_add( $term->term_id, $term, $taxonomy );
    816825
    817         /** This filter is documented in wp-includes/taxonomy-functions.php */
    818         $term = apply_filters( 'get_term', $term, $taxonomy );
    819 
    820         /** This filter is documented in wp-includes/taxonomy-functions.php */
    821         $term = apply_filters( "get_$taxonomy", $term, $taxonomy );
    822 
    823         $term = sanitize_term($term, $taxonomy, $filter);
    824 
    825         if ( $output == OBJECT ) {
    826                 return $term;
    827         } elseif ( $output == ARRAY_A ) {
    828                 return get_object_vars($term);
    829         } elseif ( $output == ARRAY_N ) {
    830                 return array_values(get_object_vars($term));
    831         } else {
    832                 return $term;
    833         }
     826        return get_term( $term, $taxonomy, $output, $filter );
    834827}
    835828
    836829/**
    function get_term_to_edit( $id, $taxonomy ) { 
    1003996 *     @type string       $cache_domain      Unique cache key to be produced when this query is stored in an
    1004997 *                                           object cache. Default is 'core'.
    1005998 * }
    1006  * @return array|int|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies
     999 * @return array|int|WP_Error List of WP_Term instances and their children. Will return WP_Error, if any of $taxonomies
    10071000 *                        do not exist.
    10081001 */
    10091002function get_terms( $taxonomies, $args = '' ) {
    function get_terms( $taxonomies, $args = '' ) { 
    14271420                foreach ( $terms as $term ) {
    14281421                        $_terms[ $term->term_id ] = $term->slug;
    14291422                }
     1423        } else {
     1424                foreach ( $terms as $term ) {
     1425                        $_terms[] = get_term( $term, $term->taxonomy );
     1426                }
    14301427        }
    14311428
    14321429        if ( ! empty( $_terms ) ) {
    function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) { 
    31853182                foreach ( (array) $terms as $term ) {
    31863183                        $taxonomies[] = $term->taxonomy;
    31873184                        $ids[] = $term->term_id;
    3188                         wp_cache_delete($term->term_id, $term->taxonomy);
     3185                        wp_cache_delete( $term->term_id, 'terms' );
    31893186                }
    31903187                $taxonomies = array_unique($taxonomies);
    31913188        } else {
    31923189                $taxonomies = array($taxonomy);
    31933190                foreach ( $taxonomies as $taxonomy ) {
    31943191                        foreach ( $ids as $id ) {
    3195                                 wp_cache_delete($id, $taxonomy);
     3192                                wp_cache_delete( $id, 'terms' );
    31963193                        }
    31973194                }
    31983195        }
    function update_term_cache( $terms, $taxonomy = '' ) { 
    33113308                if ( empty($term_taxonomy) )
    33123309                        $term_taxonomy = $term->taxonomy;
    33133310
    3314                 wp_cache_add( $term->term_id, $term, $term_taxonomy );
     3311                wp_cache_add( $term->term_id, $term, 'terms' );
    33153312        }
    33163313}
    33173314
  • src/wp-includes/taxonomy.php

    diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php
    index 3382aa0..794eea3 100644
    a b  
    99 */
    1010
    1111require_once( ABSPATH . WPINC . '/taxonomy-functions.php' );
     12require_once( ABSPATH . WPINC . '/class-wp-term.php' );
    1213require_once( ABSPATH . WPINC . '/class-wp-tax-query.php' );
  • tests/phpunit/tests/term/cache.php

    diff --git a/tests/phpunit/tests/term/cache.php b/tests/phpunit/tests/term/cache.php
    index 1a31df0..a620986 100644
    a b class Tests_Term_Cache extends WP_UnitTestCase { 
    103103                ) );
    104104
    105105                $term_object = get_term( $term, 'wptests_tax' );
    106                 wp_cache_delete( $term, 'wptests_tax' );
     106                wp_cache_delete( $term, 'terms' );
    107107
    108108                // Affirm that the cache is empty.
    109                 $this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     109                $this->assertEmpty( wp_cache_get( $term, 'terms' ) );
    110110
    111111                $num_queries = $wpdb->num_queries;
    112112
    class Tests_Term_Cache extends WP_UnitTestCase { 
    128128                        'taxonomy' => 'wptests_tax',
    129129                ) );
    130130
    131                 wp_cache_delete( $term, 'wptests_tax' );
     131                wp_cache_delete( $term, 'terms' );
    132132
    133133                // Affirm that the cache is empty.
    134                 $this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     134                $this->assertEmpty( wp_cache_get( $term, 'terms' ) );
    135135
    136136                $num_queries = $wpdb->num_queries;
    137137
    138138                // Prime cache.
    139139                $term_object = get_term( $term, 'wptests_tax' );
    140                 $this->assertNotEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     140                $this->assertNotEmpty( wp_cache_get( $term, 'terms' ) );
    141141                $this->assertSame( $num_queries + 1, $wpdb->num_queries );
    142142
    143143                $term_object_2 = get_term( $term, 'wptests_tax' );
    class Tests_Term_Cache extends WP_UnitTestCase { 
    155155                        'taxonomy' => 'wptests_tax',
    156156                ) );
    157157
    158                 wp_cache_delete( $term, 'wptests_tax' );
     158                wp_cache_delete( $term, 'terms' );
    159159
    160160                // Affirm that the cache is empty.
    161                 $this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     161                $this->assertEmpty( wp_cache_get( $term, 'terms' ) );
    162162
    163163                $num_queries = $wpdb->num_queries;
    164164
    165165                // Prime cache.
    166166                $term_object = get_term_by( 'id', $term, 'wptests_tax' );
    167                 $this->assertNotEmpty( wp_cache_get( $term, 'wptests_tax' ) );
     167                $this->assertNotEmpty( wp_cache_get( $term, 'terms' ) );
    168168                $this->assertSame( $num_queries + 1, $wpdb->num_queries );
    169169
    170170                $term_object_2 = get_term( $term, 'wptests_tax' );