Make WordPress Core

Ticket #14162: 14162.3.patch

File 14162.3.patch, 16.4 KB (added by flixos90, 8 years ago)

Made $taxonomy argument optional in more functions

  • src/wp-includes/category.php

     
    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'];
  • src/wp-includes/class-wp-term.php

     
     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

     
    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
     736        // Ensure a taxonomy is set for the filters
     737        if ( ! $taxonomy ) {
     738                $taxonomy = $_term->taxonomy;
     739        }
     740
    722741        /**
    723742         * Filter a term.
    724743         *
     
    741760         * @param string     $taxonomy The taxonomy slug.
    742761         */
    743762        $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy );
    744         $_term = sanitize_term($_term, $taxonomy, $filter);
     763        $_term = sanitize_term( $_term, $taxonomy, $filter );
    745764
    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         }
     765        if ( $output == ARRAY_A )
     766                return $_term->to_array();
     767        elseif ( $output == ARRAY_N )
     768                return array_values( $_term->to_array() );
     769
     770        return $_term;
    757771}
    758772
    759773/**
     
    780794 * @param string     $taxonomy Taxonomy Name
    781795 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
    782796 * @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.
     797 * @return WP_Term|array|null|WP_Error|false Instance of WP_Term.
    784798 *                                          Will return false if $taxonomy does not exist or $term was not found.
    785799 */
    786 function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
     800function get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw') {
    787801        global $wpdb;
    788802
    789         if ( ! taxonomy_exists($taxonomy) )
     803        if ( $taxonomy && ! taxonomy_exists($taxonomy) )
    790804                return false;
    791805
    792806        if ( 'slug' == $field ) {
     
    808822                return $term;
    809823        }
    810824
    811         $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 ) );
     825        if ( $taxonomy ) {
     826                $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 ) );
     827        } else {
     828                $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 LIMIT 1", $value ) );
     829        }
     830
    812831        if ( ! $term )
    813832                return false;
    814833
    815         wp_cache_add( $term->term_id, $term, $taxonomy );
     834        wp_cache_add( $term->term_id, $term, 'terms' );
    816835
    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         }
     836        return get_term( $term, $taxonomy, $output, $filter );
    834837}
    835838
    836839/**
     
    887890 * @param string $context  Optional, default is display. Look at sanitize_term_field() for available options.
    888891 * @return string|int|null|WP_Error Will return an empty string if $term is not an object or if $field is not set in $term.
    889892 */
    890 function get_term_field( $field, $term, $taxonomy, $context = 'display' ) {
     893function get_term_field( $field, $term, $taxonomy = '', $context = 'display' ) {
    891894        $term = (int) $term;
    892895        $term = get_term( $term, $taxonomy );
    893896        if ( is_wp_error($term) )
     
    899902        if ( !isset($term->$field) )
    900903                return '';
    901904
     905        if ( ! $taxonomy ) {
     906                $taxonomy = $term->taxonomy;
     907        }
     908
    902909        return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context);
    903910}
    904911
     
    914921 * @param string     $taxonomy Taxonomy name.
    915922 * @return string|int|null|WP_Error Will return empty string if $term is not an object.
    916923 */
    917 function get_term_to_edit( $id, $taxonomy ) {
     924function get_term_to_edit( $id, $taxonomy = '' ) {
    918925        $term = get_term( $id, $taxonomy );
    919926
    920927        if ( is_wp_error($term) )
     
    10031010 *     @type string       $cache_domain      Unique cache key to be produced when this query is stored in an
    10041011 *                                           object cache. Default is 'core'.
    10051012 * }
    1006  * @return array|int|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies
     1013 * @return array|int|WP_Error List of WP_Term instances and their children. Will return WP_Error, if any of $taxonomies
    10071014 *                        do not exist.
    10081015 */
    10091016function get_terms( $taxonomies, $args = '' ) {
     
    14271434                foreach ( $terms as $term ) {
    14281435                        $_terms[ $term->term_id ] = $term->slug;
    14291436                }
     1437        } else {
     1438                foreach ( $terms as $term ) {
     1439                        $_terms[] = get_term( $term, $term->taxonomy );
     1440                }
    14301441        }
    14311442
    14321443        if ( ! empty( $_terms ) ) {
     
    15211532 * @param string     $taxonomy Taxonomy name that $term1 and `$term2` belong to.
    15221533 * @return bool Whether `$term2` is a child of `$term1`.
    15231534 */
    1524 function term_is_ancestor_of( $term1, $term2, $taxonomy ) {
     1535function term_is_ancestor_of( $term1, $term2, $taxonomy = '' ) {
    15251536        if ( ! isset( $term1->term_id ) )
    15261537                $term1 = get_term( $term1, $taxonomy );
    15271538        if ( ! isset( $term2->parent ) )
     
    15521563 *                               'display', 'attribute', or 'js'. Default 'display'.
    15531564 * @return array|object Term with all fields sanitized.
    15541565 */
    1555 function sanitize_term($term, $taxonomy, $context = 'display') {
     1566function sanitize_term($term, $taxonomy = '', $context = 'display') {
    15561567        $fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' );
    15571568
    15581569        $do_object = is_object( $term );
    15591570
    15601571        $term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0);
    15611572
     1573        if ( ! $taxonomy ) {
     1574                $taxonomy = $do_object ? $term->taxonomy : ( isset( $term['taxonomy'] ) ? $term['taxonomy'] : '' );
     1575        }
     1576
    15621577        foreach ( (array) $fields as $field ) {
    15631578                if ( $do_object ) {
    15641579                        if ( isset($term->$field) )
     
    31853200                foreach ( (array) $terms as $term ) {
    31863201                        $taxonomies[] = $term->taxonomy;
    31873202                        $ids[] = $term->term_id;
    3188                         wp_cache_delete($term->term_id, $term->taxonomy);
     3203                        wp_cache_delete( $term->term_id, 'terms' );
    31893204                }
    31903205                $taxonomies = array_unique($taxonomies);
    31913206        } else {
    31923207                $taxonomies = array($taxonomy);
    31933208                foreach ( $taxonomies as $taxonomy ) {
    31943209                        foreach ( $ids as $id ) {
    3195                                 wp_cache_delete($id, $taxonomy);
     3210                                wp_cache_delete( $id, 'terms' );
    31963211                        }
    31973212                }
    31983213        }
     
    33113326                if ( empty($term_taxonomy) )
    33123327                        $term_taxonomy = $term->taxonomy;
    33133328
    3314                 wp_cache_add( $term->term_id, $term, $term_taxonomy );
     3329                wp_cache_add( $term->term_id, $term, 'terms' );
    33153330        }
    33163331}
    33173332
  • src/wp-includes/taxonomy.php

     
    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

     
    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
     
    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' );
     
    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' );