Ticket #14162: 14162.2.patch
| File 14162.2.patch, 13.7 KB (added by , 11 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 ) { 323 323 */ 324 324 function _make_cat_compat( &$category ) { 325 325 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; 332 332 } elseif ( is_array( $category ) && isset( $category['term_id'] ) ) { 333 333 $category['cat_ID'] = &$category['term_id']; 334 334 $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 */ 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 ) { 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 ) { 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 function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') { 696 global $wpdb; 697 695 function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { 698 696 if ( empty( $term ) ) { 699 697 return new WP_Error( 'invalid_term', __( 'Empty Term' ) ); 700 698 } 701 699 702 if ( ! taxonomy_exists( $taxonomy ) ) {700 if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) { 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 ); 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 ); 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 ); 719 729 } 720 730 } 721 731 732 if ( ! $_term ) { 733 return null; 734 } 735 722 736 /** 723 737 * Filter a term. 724 738 * … … function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') { 741 755 * @param string $taxonomy The taxonomy slug. 742 756 */ 743 757 $_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; 757 766 } 758 767 759 768 /** … … function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') { 780 789 * @param string $taxonomy Taxonomy Name 781 790 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N 782 791 * @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. 784 793 * Will return false if $taxonomy does not exist or $term was not found. 785 794 */ 786 795 function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') { … … function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw 814 823 815 824 wp_cache_add( $term->term_id, $term, $taxonomy ); 816 825 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 ); 834 827 } 835 828 836 829 /** … … function get_term_to_edit( $id, $taxonomy ) { 1003 996 * @type string $cache_domain Unique cache key to be produced when this query is stored in an 1004 997 * object cache. Default is 'core'. 1005 998 * } 1006 * @return array|int|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies999 * @return array|int|WP_Error List of WP_Term instances and their children. Will return WP_Error, if any of $taxonomies 1007 1000 * do not exist. 1008 1001 */ 1009 1002 function get_terms( $taxonomies, $args = '' ) { … … function get_terms( $taxonomies, $args = '' ) { 1427 1420 foreach ( $terms as $term ) { 1428 1421 $_terms[ $term->term_id ] = $term->slug; 1429 1422 } 1423 } else { 1424 foreach ( $terms as $term ) { 1425 $_terms[] = get_term( $term, $term->taxonomy ); 1426 } 1430 1427 } 1431 1428 1432 1429 if ( ! empty( $_terms ) ) { … … function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) { 3185 3182 foreach ( (array) $terms as $term ) { 3186 3183 $taxonomies[] = $term->taxonomy; 3187 3184 $ids[] = $term->term_id; 3188 wp_cache_delete( $term->term_id, $term->taxonomy);3185 wp_cache_delete( $term->term_id, 'terms' ); 3189 3186 } 3190 3187 $taxonomies = array_unique($taxonomies); 3191 3188 } else { 3192 3189 $taxonomies = array($taxonomy); 3193 3190 foreach ( $taxonomies as $taxonomy ) { 3194 3191 foreach ( $ids as $id ) { 3195 wp_cache_delete( $id, $taxonomy);3192 wp_cache_delete( $id, 'terms' ); 3196 3193 } 3197 3194 } 3198 3195 } … … function update_term_cache( $terms, $taxonomy = '' ) { 3311 3308 if ( empty($term_taxonomy) ) 3312 3309 $term_taxonomy = $term->taxonomy; 3313 3310 3314 wp_cache_add( $term->term_id, $term, $term_taxonomy);3311 wp_cache_add( $term->term_id, $term, 'terms' ); 3315 3312 } 3316 3313 } 3317 3314 -
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 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/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 { 103 103 ) ); 104 104 105 105 $term_object = get_term( $term, 'wptests_tax' ); 106 wp_cache_delete( $term, ' wptests_tax' );106 wp_cache_delete( $term, 'terms' ); 107 107 108 108 // Affirm that the cache is empty. 109 $this->assertEmpty( wp_cache_get( $term, ' wptests_tax' ) );109 $this->assertEmpty( wp_cache_get( $term, 'terms' ) ); 110 110 111 111 $num_queries = $wpdb->num_queries; 112 112 … … class Tests_Term_Cache extends WP_UnitTestCase { 128 128 'taxonomy' => 'wptests_tax', 129 129 ) ); 130 130 131 wp_cache_delete( $term, ' wptests_tax' );131 wp_cache_delete( $term, 'terms' ); 132 132 133 133 // Affirm that the cache is empty. 134 $this->assertEmpty( wp_cache_get( $term, ' wptests_tax' ) );134 $this->assertEmpty( wp_cache_get( $term, 'terms' ) ); 135 135 136 136 $num_queries = $wpdb->num_queries; 137 137 138 138 // Prime cache. 139 139 $term_object = get_term( $term, 'wptests_tax' ); 140 $this->assertNotEmpty( wp_cache_get( $term, ' wptests_tax' ) );140 $this->assertNotEmpty( wp_cache_get( $term, 'terms' ) ); 141 141 $this->assertSame( $num_queries + 1, $wpdb->num_queries ); 142 142 143 143 $term_object_2 = get_term( $term, 'wptests_tax' ); … … class Tests_Term_Cache extends WP_UnitTestCase { 155 155 'taxonomy' => 'wptests_tax', 156 156 ) ); 157 157 158 wp_cache_delete( $term, ' wptests_tax' );158 wp_cache_delete( $term, 'terms' ); 159 159 160 160 // Affirm that the cache is empty. 161 $this->assertEmpty( wp_cache_get( $term, ' wptests_tax' ) );161 $this->assertEmpty( wp_cache_get( $term, 'terms' ) ); 162 162 163 163 $num_queries = $wpdb->num_queries; 164 164 165 165 // Prime cache. 166 166 $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' ) ); 168 168 $this->assertSame( $num_queries + 1, $wpdb->num_queries ); 169 169 170 170 $term_object_2 = get_term( $term, 'wptests_tax' );