Ticket #14162: 14162.3.patch
File 14162.3.patch, 16.4 KB (added by , 8 years ago) |
---|
-
src/wp-includes/category.php
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']; -
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 ) { 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
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 736 // Ensure a taxonomy is set for the filters 737 if ( ! $taxonomy ) { 738 $taxonomy = $_term->taxonomy; 739 } 740 722 741 /** 723 742 * Filter a term. 724 743 * … … 741 760 * @param string $taxonomy The taxonomy slug. 742 761 */ 743 762 $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy ); 744 $_term = sanitize_term( $_term, $taxonomy, $filter);763 $_term = sanitize_term( $_term, $taxonomy, $filter ); 745 764 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; 757 771 } 758 772 759 773 /** … … 780 794 * @param string $taxonomy Taxonomy Name 781 795 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N 782 796 * @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. 784 798 * Will return false if $taxonomy does not exist or $term was not found. 785 799 */ 786 function get_term_by($field, $value, $taxonomy , $output = OBJECT, $filter = 'raw') {800 function get_term_by($field, $value, $taxonomy = '', $output = OBJECT, $filter = 'raw') { 787 801 global $wpdb; 788 802 789 if ( ! taxonomy_exists($taxonomy) )803 if ( $taxonomy && ! taxonomy_exists($taxonomy) ) 790 804 return false; 791 805 792 806 if ( 'slug' == $field ) { … … 808 822 return $term; 809 823 } 810 824 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 812 831 if ( ! $term ) 813 832 return false; 814 833 815 wp_cache_add( $term->term_id, $term, $taxonomy);834 wp_cache_add( $term->term_id, $term, 'terms' ); 816 835 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 ); 834 837 } 835 838 836 839 /** … … 887 890 * @param string $context Optional, default is display. Look at sanitize_term_field() for available options. 888 891 * @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. 889 892 */ 890 function get_term_field( $field, $term, $taxonomy , $context = 'display' ) {893 function get_term_field( $field, $term, $taxonomy = '', $context = 'display' ) { 891 894 $term = (int) $term; 892 895 $term = get_term( $term, $taxonomy ); 893 896 if ( is_wp_error($term) ) … … 899 902 if ( !isset($term->$field) ) 900 903 return ''; 901 904 905 if ( ! $taxonomy ) { 906 $taxonomy = $term->taxonomy; 907 } 908 902 909 return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context); 903 910 } 904 911 … … 914 921 * @param string $taxonomy Taxonomy name. 915 922 * @return string|int|null|WP_Error Will return empty string if $term is not an object. 916 923 */ 917 function get_term_to_edit( $id, $taxonomy ) {924 function get_term_to_edit( $id, $taxonomy = '' ) { 918 925 $term = get_term( $id, $taxonomy ); 919 926 920 927 if ( is_wp_error($term) ) … … 1003 1010 * @type string $cache_domain Unique cache key to be produced when this query is stored in an 1004 1011 * object cache. Default is 'core'. 1005 1012 * } 1006 * @return array|int|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies1013 * @return array|int|WP_Error List of WP_Term instances and their children. Will return WP_Error, if any of $taxonomies 1007 1014 * do not exist. 1008 1015 */ 1009 1016 function get_terms( $taxonomies, $args = '' ) { … … 1427 1434 foreach ( $terms as $term ) { 1428 1435 $_terms[ $term->term_id ] = $term->slug; 1429 1436 } 1437 } else { 1438 foreach ( $terms as $term ) { 1439 $_terms[] = get_term( $term, $term->taxonomy ); 1440 } 1430 1441 } 1431 1442 1432 1443 if ( ! empty( $_terms ) ) { … … 1521 1532 * @param string $taxonomy Taxonomy name that $term1 and `$term2` belong to. 1522 1533 * @return bool Whether `$term2` is a child of `$term1`. 1523 1534 */ 1524 function term_is_ancestor_of( $term1, $term2, $taxonomy ) {1535 function term_is_ancestor_of( $term1, $term2, $taxonomy = '' ) { 1525 1536 if ( ! isset( $term1->term_id ) ) 1526 1537 $term1 = get_term( $term1, $taxonomy ); 1527 1538 if ( ! isset( $term2->parent ) ) … … 1552 1563 * 'display', 'attribute', or 'js'. Default 'display'. 1553 1564 * @return array|object Term with all fields sanitized. 1554 1565 */ 1555 function sanitize_term($term, $taxonomy , $context = 'display') {1566 function sanitize_term($term, $taxonomy = '', $context = 'display') { 1556 1567 $fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' ); 1557 1568 1558 1569 $do_object = is_object( $term ); 1559 1570 1560 1571 $term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0); 1561 1572 1573 if ( ! $taxonomy ) { 1574 $taxonomy = $do_object ? $term->taxonomy : ( isset( $term['taxonomy'] ) ? $term['taxonomy'] : '' ); 1575 } 1576 1562 1577 foreach ( (array) $fields as $field ) { 1563 1578 if ( $do_object ) { 1564 1579 if ( isset($term->$field) ) … … 3185 3200 foreach ( (array) $terms as $term ) { 3186 3201 $taxonomies[] = $term->taxonomy; 3187 3202 $ids[] = $term->term_id; 3188 wp_cache_delete( $term->term_id, $term->taxonomy);3203 wp_cache_delete( $term->term_id, 'terms' ); 3189 3204 } 3190 3205 $taxonomies = array_unique($taxonomies); 3191 3206 } else { 3192 3207 $taxonomies = array($taxonomy); 3193 3208 foreach ( $taxonomies as $taxonomy ) { 3194 3209 foreach ( $ids as $id ) { 3195 wp_cache_delete( $id, $taxonomy);3210 wp_cache_delete( $id, 'terms' ); 3196 3211 } 3197 3212 } 3198 3213 } … … 3311 3326 if ( empty($term_taxonomy) ) 3312 3327 $term_taxonomy = $term->taxonomy; 3313 3328 3314 wp_cache_add( $term->term_id, $term, $term_taxonomy);3329 wp_cache_add( $term->term_id, $term, 'terms' ); 3315 3330 } 3316 3331 } 3317 3332 -
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/term/cache.php
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 … … 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' ); … … 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' );