Ticket #14162: 14162_3.diff
File 14162_3.diff, 12.2 KB (added by , 9 years ago) |
---|
-
src/wp-includes/category.php
322 322 * @param array|object $category Category Row object or array 323 323 */ 324 324 function _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 325 329 if ( is_object( $category ) && ! is_wp_error( $category ) ) { 326 330 $category->cat_ID = &$category->term_id; 327 331 $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 */ 10 final 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
685 685 * @global wpdb $wpdb WordPress database abstraction object. 686 686 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param. 687 687 * 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_N691 * @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 not693 * 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. 694 694 */ 695 695 function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') { 696 global $wpdb;697 698 696 if ( empty( $term ) ) { 699 697 return new WP_Error( 'invalid_term', __( 'Empty Term' ) ); 700 698 } … … 703 701 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) ); 704 702 } 705 703 706 if ( is_object($term) && empty($term->filter) ) { 707 wp_cache_add( $term->term_id, $term, $taxonomy ); 704 if ( $term instanceof WP_Term ) { 708 705 $_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 } 709 715 } 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 ); 720 717 } 721 718 719 if ( ! $_term ) 720 return null; 721 722 722 /** 723 723 * Filter a term. 724 724 * … … 743 743 $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy ); 744 744 $_term = sanitize_term($_term, $taxonomy, $filter); 745 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 } 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; 757 752 } 758 753 759 754 /** … … 780 775 * @param string $taxonomy Taxonomy Name 781 776 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N 782 777 * @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. 784 779 * Will return false if $taxonomy does not exist or $term was not found. 785 780 */ 786 781 function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') { … … 814 809 815 810 wp_cache_add( $term->term_id, $term, $taxonomy ); 816 811 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 ); 834 813 } 835 814 836 815 /** … … 1003 982 * @type string $cache_domain Unique cache key to be produced when this query is stored in an 1004 983 * object cache. Default is 'core'. 1005 984 * } 1006 * @return array|int|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies985 * @return array|int|WP_Error List of WP_Term instances and their children. Will return WP_Error, if any of $taxonomies 1007 986 * do not exist. 1008 987 */ 1009 988 function get_terms( $taxonomies, $args = '' ) { … … 1427 1406 foreach ( $terms as $term ) { 1428 1407 $_terms[ $term->term_id ] = $term->slug; 1429 1408 } 1409 } else { 1410 foreach ( $terms as $term ) { 1411 $_terms[ $term->term_id ] = get_term( $term, $term->taxonomy ); 1412 } 1430 1413 } 1431 1414 1432 1415 if ( ! empty( $_terms ) ) { -
src/wp-includes/taxonomy.php
9 9 */ 10 10 11 11 require_once( ABSPATH . WPINC . '/taxonomy-functions.php' ); 12 require_once( ABSPATH . WPINC . '/class-wp-term.php' ); 12 13 require_once( ABSPATH . WPINC . '/class-wp-tax-query.php' ); -
tests/phpunit/tests/xmlrpc/wp/getTerm.php
95 95 $this->assertEquals( 'category', $result['taxonomy'] ); 96 96 $this->assertEquals( $term['description'], $result['description'] ); 97 97 } 98 99 /** 100 * @ticket 14162 101 */ 102 function test_get_term_object() { 103 $this->make_user_by_role( 'editor' ); 104 105 $term = get_term( $this->term['term_id'], 'category', OBJECT ); 106 107 $this->assertNotInstanceOf( 'IXR_Error', $term ); 108 $this->assertInstanceOf( 'WP_Term', $term ); 109 110 // Check DataTypes 111 $this->assertInternalType( 'string', $term->name ); 112 $this->assertInternalType( 'string',$term->slug ); 113 $this->assertInternalType( 'string', $term->taxonomy ); 114 $this->assertInternalType( 'string', $term->description ); 115 $this->assertInternalType( 'int', $term->count ); 116 117 // We expect all ID's to be strings not integers so we don't return something larger than an XMLRPC integer can describe. 118 var_dump( $term->term_id ); 119 $this->assertInternalType( 'int', $term->term_id ); 120 $this->assertInternalType( 'int', $term->term_group ); 121 $this->assertInternalType( 'int', $term->term_taxonomy_id ); 122 $this->assertInternalType( 'int', $term->parent ); 123 124 // Check Data 125 $this->assertEquals( 0, $term->count ); 126 } 98 127 }