Make WordPress Core

Ticket #14162: 14162.patch

File 14162.patch, 10.8 KB (added by flixos90, 9 years ago)

A starting point for the WP_Term class and its integration

  • src/wp-includes/category.php

     
    322322 * @param array|object $category Category Row object or array
    323323 */
    324324function _make_cat_compat( &$category ) {
     325        // fix for "Indirect modification of overloaded property" notice
     326        if ( $category instanceof WP_Term )
     327                $category = (object) $category->to_array();
     328
    325329        if ( is_object( $category ) && ! is_wp_error( $category ) ) {
    326330                $category->cat_ID = &$category->term_id;
    327331                $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, $taxonomy ) {
     91                global $wpdb;
     92
     93                $term_id = (int) $term_id;
     94                if ( ! $term_id )
     95                        return false;
     96
     97                $_term = wp_cache_get( $term_id, $taxonomy );
     98
     99                if ( ! $_term ) {
     100                        $_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_id) );
     101                        if ( ! $_term )
     102                                return false;
     103
     104                        $_term = sanitize_term( $_term, $taxonomy, 'raw' );
     105                        wp_cache_add( $term_id, $_term, $taxonomy );
     106                } elseif ( empty( $_term->filter ) ) {
     107                        $_term = sanitize_term( $_term, $taxonomy, 'raw' );
     108                }
     109
     110                return new WP_Term( $_term );
     111        }
     112
     113        /**
     114         * Constructor.
     115         *
     116         * @param WP_Term|object $term Term object.
     117         */
     118        public function __construct( $term ) {
     119                foreach ( get_object_vars( $term ) as $key => $value )
     120                        $this->$key = $value;
     121        }
     122
     123        /**
     124         * Isset-er.
     125         *
     126         * @param string $key Property to check if set.
     127         * @return bool
     128         */
     129        public function __isset( $key ) {
     130                if ( 'ancestors' == $key )
     131                        return true;
     132
     133                if ( 'children' == $key )
     134                        return true;
     135
     136                if ( 'link' == $key )
     137                        return true;
     138
     139                if ( 'objects' == $key )
     140                        return true;
     141
     142                return false;
     143        }
     144
     145        /**
     146         * Getter.
     147         *
     148         * @param string $key Key to get.
     149         * @return mixed
     150         */
     151        public function __get( $key ) {
     152                if ( 'ancestors' == $key ) {
     153                        if ( empty( $this->parent ) || $this->parent == $this->term_id )
     154                                return array();
     155
     156                        $ancestors = array();
     157
     158                        $id = $ancestors[] = $this->parent;
     159
     160                        while ( $ancestor = get_term( $id, $this->taxonomy ) ) {
     161                                // Loop detection: If the ancestor has been seen before, break.
     162                                if ( empty( $ancestor->parent ) || ( $ancestor->parent == $this->term_id ) || in_array( $ancestor->parent, $ancestors ) )
     163                                        break;
     164
     165                                $id = $ancestors[] = $ancestor->parent;
     166                        }
     167
     168                        return $ancestors;
     169                }
     170
     171                if ( 'children' == $key )
     172                        return get_term_children( $this->term_id, $this->taxonomy );
     173
     174                if ( 'link' == $key )
     175                        return get_term_link( $this->term_id, $this->taxonomy );
     176
     177                if ( 'objects' == $key )
     178                        return get_objects_in_term( $this->term_id, $this->taxonomy );
     179
     180                return false;
     181        }
     182
     183        /**
     184         * {@Missing Summary}
     185         *
     186         * @param string $filter Filter.
     187         * @return self|array|bool|object|WP_Term
     188         */
     189        public function filter( $filter ) {
     190                if ( $this->filter == $filter )
     191                        return $this;
     192
     193                if ( $filter == 'raw' )
     194                        return self::get_instance( $this->term_id, $this->taxonomy );
     195
     196                return sanitize_term( $this, $this->taxonomy, $filter );
     197        }
     198
     199        /**
     200         * Convert object to array.
     201         *
     202         * @return array Object as array.
     203         */
     204        public function to_array() {
     205                $term = get_object_vars( $this );
     206
     207                foreach ( array( 'ancestors', 'children', 'link', 'objects' ) as $key ) {
     208                        if ( $this->__isset( $key ) )
     209                                $term[ $key ] = $this->__get( $key );
     210                }
     211
     212                return $term;
     213        }
     214}
  • 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 */
    695695function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
    696         global $wpdb;
    697 
    698696        if ( empty( $term ) ) {
    699697                return new WP_Error( 'invalid_term', __( 'Empty Term' ) );
    700698        }
     
    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, $taxonomy );
     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 );
    719                 }
     716                $_term = WP_Term::get_instance( $term, $taxonomy );
    720717        }
    721718
     719        if ( ! $_term )
     720                return null;
     721
    722722        /**
    723723         * Filter a term.
    724724         *
     
    743743        $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy );
    744744        $_term = sanitize_term($_term, $taxonomy, $filter);
    745745
    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         }
     746        if ( $output == ARRAY_A )
     747                return $_term->to_array();
     748        elseif ( $output == ARRAY_N )
     749                return array_values( $_term->to_array() );
     750
     751        return $_term;
    757752}
    758753
    759754/**
     
    780775 * @param string     $taxonomy Taxonomy Name
    781776 * @param string     $output   Constant OBJECT, ARRAY_A, or ARRAY_N
    782777 * @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.
     778 * @return WP_Term|array|null|WP_Error|false Instance of WP_Term.
    784779 *                                          Will return false if $taxonomy does not exist or $term was not found.
    785780 */
    786781function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
     
    814809
    815810        wp_cache_add( $term->term_id, $term, $taxonomy );
    816811
    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         }
     812        return get_term( $term, $taxonomy, $output, $filter );
    834813}
    835814
    836815/**
     
    1003982 *     @type string       $cache_domain      Unique cache key to be produced when this query is stored in an
    1004983 *                                           object cache. Default is 'core'.
    1005984 * }
    1006  * @return array|int|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies
     985 * @return array|int|WP_Error List of WP_Term instances and their children. Will return WP_Error, if any of $taxonomies
    1007986 *                        do not exist.
    1008987 */
    1009988function get_terms( $taxonomies, $args = '' ) {
     
    14271406                foreach ( $terms as $term ) {
    14281407                        $_terms[ $term->term_id ] = $term->slug;
    14291408                }
     1409        } else {
     1410                foreach ( $terms as $term ) {
     1411                        $_terms[ $term->term_id ] = get_term( $term, $term->taxonomy );
     1412                }
    14301413        }
    14311414
    14321415        if ( ! empty( $_terms ) ) {
  • 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' );