Ticket #37198: 37198.2.diff
File 37198.2.diff, 15.2 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-term-query.php
diff --git src/wp-includes/class-wp-term-query.php src/wp-includes/class-wp-term-query.php index 338c940..15631a1 100644
class WP_Term_Query { 507 507 $this->sql_clauses['where']['description__like'] = $this->db->prepare( "tt.description LIKE %s", '%' . $this->db->esc_like( $args['description__like'] ) . '%' ); 508 508 } 509 509 510 if ( ! empty( $args['object_ids'] ) ) { 511 $object_ids = implode( ', ', $args['object_ids'] ); 512 $this->sql_clauses['where']['object_ids'] = "tr.object_id IN ($object_ids)"; 513 } 514 515 // temp 516 if ( ! empty( $args['object_ids'] ) ) { 517 $args['hide_empty'] = false; 518 } 519 510 520 if ( '' !== $parent ) { 511 521 $parent = (int) $parent; 512 522 $this->sql_clauses['where']['parent'] = "tt.parent = '$parent'"; … … class WP_Term_Query { 558 568 $selects = array(); 559 569 switch ( $args['fields'] ) { 560 570 case 'all': 571 case 'all_with_object_id' : 572 case 'tt_ids' : 573 case 'slugs' : 561 574 $selects = array( 't.*', 'tt.*' ); 575 if ( 'all_with_object_id' === $args['fields'] && ! empty( $args['object_ids'] ) ) { 576 $selects[] = 'tr.object_id'; 577 } 562 578 break; 563 579 case 'ids': 564 580 case 'id=>parent': … … class WP_Term_Query { 602 618 603 619 $join .= " INNER JOIN {$this->db->term_taxonomy} AS tt ON t.term_id = tt.term_id"; 604 620 621 if ( ! empty( $this->query_vars['object_ids'] ) ) { 622 $join .= " INNER JOIN {$this->db->term_relationships} AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id"; 623 } 624 605 625 $where = implode( ' AND ', $this->sql_clauses['where'] ); 606 626 607 627 /** … … class WP_Term_Query { 657 677 } 658 678 659 679 $terms = $this->db->get_results( $this->request ); 660 if ( 'all' == $_fields ) {680 if ( 'all' == $_fields || 'all_with_object_id' === $_fields ) { 661 681 update_term_cache( $terms ); 662 682 } 663 683 … … class WP_Term_Query { 715 735 } 716 736 } elseif ( 'ids' == $_fields ) { 717 737 foreach ( $terms as $term ) { 718 $_terms[] = $term->term_id; 738 $_terms[] = (int) $term->term_id; 739 } 740 } elseif ( 'tt_ids' == $_fields ) { 741 foreach ( $terms as $term ) { 742 $_terms[] = (int) $term->term_taxonomy_id; 719 743 } 720 744 } elseif ( 'names' == $_fields ) { 721 745 foreach ( $terms as $term ) { 722 746 $_terms[] = $term->name; 723 747 } 748 } elseif ( 'slugs' == $_fields ) { 749 foreach ( $terms as $term ) { 750 $_terms[] = $term->slug; 751 } 724 752 } elseif ( 'id=>name' == $_fields ) { 725 753 foreach ( $terms as $term ) { 726 754 $_terms[ $term->term_id ] = $term->name; … … class WP_Term_Query { 731 759 } 732 760 } 733 761 762 if ( ! empty( $args['object_ids'] ) && 'all' == $_fields ) { 763 $_tt_ids = array(); 764 foreach ( $terms as $term ) { 765 if ( isset( $_tt_ids[ $term->term_taxonomy_id ] ) ) { 766 continue; 767 } 768 769 $_tt_ids[ $term->term_taxonomy_id ] = 1; 770 $_terms[] = $term; 771 } 772 773 $terms = $_terms; 774 } 775 734 776 if ( ! empty( $_terms ) ) { 735 777 $terms = $_terms; 736 778 } … … class WP_Term_Query { 746 788 747 789 wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS ); 748 790 749 if ( 'all' === $_fields ) {791 if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) { 750 792 $terms = array_map( 'get_term', $terms ); 751 793 } 752 794 … … class WP_Term_Query { 766 808 protected function parse_orderby( $orderby_raw ) { 767 809 $_orderby = strtolower( $orderby_raw ); 768 810 $maybe_orderby_meta = false; 769 if ( 'count' == $_orderby ) { 770 $orderby = 'tt.count'; 771 } elseif ( 'name' == $_orderby ) { 772 $orderby = 't.name'; 773 } elseif ( 'slug' == $_orderby ) { 774 $orderby = 't.slug'; 811 812 if ( in_array( $_orderby, array( 'term_id', 'name', 'slug', 'term_group' ), true ) ) { 813 $orderby = "t.$_orderby"; 814 } elseif ( in_array( $_orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id', 'description' ), true ) ) { 815 $orderby = "tt.$_orderby"; 816 } elseif ( 'term_order' === $_orderby ) { 817 $orderby = 'tr.term_order'; 775 818 } elseif ( 'include' == $_orderby && ! empty( $this->query_vars['include'] ) ) { 776 819 $include = implode( ',', wp_parse_id_list( $this->query_vars['include'] ) ); 777 820 $orderby = "FIELD( t.term_id, $include )"; 778 } elseif ( 'term_group' == $_orderby ) {779 $orderby = 't.term_group';780 } elseif ( 'description' == $_orderby ) {781 $orderby = 'tt.description';782 821 } elseif ( 'none' == $_orderby ) { 783 822 $orderby = ''; 784 823 } elseif ( empty( $_orderby ) || 'id' == $_orderby || 'term_id' === $_orderby ) { -
src/wp-includes/taxonomy.php
diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php index 80e1f84..d1d5510 100644
function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { 1983 1983 $object_ids = array($object_ids); 1984 1984 $object_ids = array_map('intval', $object_ids); 1985 1985 1986 $defaults = array( 1987 'orderby' => 'name', 1988 'order' => 'ASC', 1989 'fields' => 'all', 1990 'parent' => '', 1991 'update_term_meta_cache' => true, 1992 'meta_query' => '', 1993 ); 1994 $args = wp_parse_args( $args, $defaults ); 1995 1996 $terms = array(); 1997 if ( count($taxonomies) > 1 ) { 1998 foreach ( $taxonomies as $index => $taxonomy ) { 1999 $t = get_taxonomy($taxonomy); 2000 if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) { 2001 unset($taxonomies[$index]); 2002 $terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args))); 2003 } 2004 } 2005 } else { 2006 $t = get_taxonomy($taxonomies[0]); 2007 if ( isset($t->args) && is_array($t->args) ) 2008 $args = array_merge($args, $t->args); 2009 } 2010 2011 $orderby = $args['orderby']; 2012 $order = $args['order']; 2013 $fields = $args['fields']; 2014 2015 if ( in_array( $orderby, array( 'term_id', 'name', 'slug', 'term_group' ) ) ) { 2016 $orderby = "t.$orderby"; 2017 } elseif ( in_array( $orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id' ) ) ) { 2018 $orderby = "tt.$orderby"; 2019 } elseif ( 'term_order' === $orderby ) { 2020 $orderby = 'tr.term_order'; 2021 } elseif ( 'none' === $orderby ) { 2022 $orderby = ''; 2023 $order = ''; 2024 } else { 2025 $orderby = 't.term_id'; 2026 } 2027 2028 // tt_ids queries can only be none or tr.term_taxonomy_id 2029 if ( ('tt_ids' == $fields) && !empty($orderby) ) 2030 $orderby = 'tr.term_taxonomy_id'; 2031 2032 if ( !empty($orderby) ) 2033 $orderby = "ORDER BY $orderby"; 2034 2035 $order = strtoupper( $order ); 2036 if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) 2037 $order = 'ASC'; 2038 2039 $taxonomy_array = $taxonomies; 2040 $object_id_array = $object_ids; 2041 $taxonomies = "'" . implode("', '", array_map( 'esc_sql', $taxonomies ) ) . "'"; 2042 $object_ids = implode(', ', $object_ids); 2043 2044 $select_this = ''; 2045 if ( 'all' == $fields ) { 2046 $select_this = 't.*, tt.*'; 2047 } elseif ( 'ids' == $fields ) { 2048 $select_this = 't.term_id'; 2049 } elseif ( 'names' == $fields ) { 2050 $select_this = 't.name'; 2051 } elseif ( 'slugs' == $fields ) { 2052 $select_this = 't.slug'; 2053 } elseif ( 'all_with_object_id' == $fields ) { 2054 $select_this = 't.*, tt.*, tr.object_id'; 2055 } 2056 2057 $where = array( 2058 "tt.taxonomy IN ($taxonomies)", 2059 "tr.object_id IN ($object_ids)", 2060 ); 2061 2062 if ( '' !== $args['parent'] ) { 2063 $where[] = $wpdb->prepare( 'tt.parent = %d', $args['parent'] ); 2064 } 2065 2066 // Meta query support. 2067 $meta_query_join = ''; 2068 if ( ! empty( $args['meta_query'] ) ) { 2069 $mquery = new WP_Meta_Query( $args['meta_query'] ); 2070 $mq_sql = $mquery->get_sql( 'term', 't', 'term_id' ); 2071 2072 $meta_query_join .= $mq_sql['join']; 2073 2074 // Strip leading AND. 2075 $where[] = preg_replace( '/^\s*AND/', '', $mq_sql['where'] ); 2076 } 2077 2078 $where = implode( ' AND ', $where ); 2079 2080 $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id $meta_query_join WHERE $where $orderby $order"; 1986 $args['taxonomy'] = $taxonomies; 1987 $args['object_ids'] = $object_ids; 2081 1988 2082 $objects = false; 2083 if ( 'all' == $fields || 'all_with_object_id' == $fields ) { 2084 $_terms = $wpdb->get_results( $query ); 2085 $object_id_index = array(); 2086 foreach ( $_terms as $key => $term ) { 2087 $term = sanitize_term( $term, $taxonomy, 'raw' ); 2088 $_terms[ $key ] = $term; 2089 2090 if ( isset( $term->object_id ) ) { 2091 $object_id_index[ $key ] = $term->object_id; 2092 } 2093 } 2094 2095 update_term_cache( $_terms ); 2096 $_terms = array_map( 'get_term', $_terms ); 2097 2098 // Re-add the object_id data, which is lost when fetching terms from cache. 2099 if ( 'all_with_object_id' === $fields ) { 2100 foreach ( $_terms as $key => $_term ) { 2101 if ( isset( $object_id_index[ $key ] ) ) { 2102 $_term->object_id = $object_id_index[ $key ]; 2103 } 2104 } 2105 } 2106 2107 $terms = array_merge( $terms, $_terms ); 2108 $objects = true; 2109 2110 } elseif ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) { 2111 $_terms = $wpdb->get_col( $query ); 2112 $_field = ( 'ids' == $fields ) ? 'term_id' : 'name'; 2113 foreach ( $_terms as $key => $term ) { 2114 $_terms[$key] = sanitize_term_field( $_field, $term, $term, $taxonomy, 'raw' ); 2115 } 2116 $terms = array_merge( $terms, $_terms ); 2117 } elseif ( 'tt_ids' == $fields ) { 2118 $terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order"); 2119 foreach ( $terms as $key => $tt_id ) { 2120 $terms[$key] = sanitize_term_field( 'term_taxonomy_id', $tt_id, 0, $taxonomy, 'raw' ); // 0 should be the term id, however is not needed when using raw context. 2121 } 2122 } 2123 2124 // Update termmeta cache, if necessary. 2125 if ( $args['update_term_meta_cache'] && ( 'all' === $fields || 'all_with_object_id' === $fields || 'ids' === $fields ) ) { 2126 if ( 'ids' === $fields ) { 2127 $term_ids = $terms; 2128 } else { 2129 $term_ids = wp_list_pluck( $terms, 'term_id' ); 2130 } 2131 2132 update_termmeta_cache( $term_ids ); 2133 } 2134 2135 if ( ! $terms ) { 2136 $terms = array(); 2137 } elseif ( $objects && 'all_with_object_id' !== $fields ) { 2138 $_tt_ids = array(); 2139 $_terms = array(); 2140 foreach ( $terms as $term ) { 2141 if ( in_array( $term->term_taxonomy_id, $_tt_ids ) ) { 2142 continue; 2143 } 2144 2145 $_tt_ids[] = $term->term_taxonomy_id; 2146 $_terms[] = $term; 2147 } 2148 $terms = $_terms; 2149 } elseif ( ! $objects ) { 2150 $terms = array_values( array_unique( $terms ) ); 2151 } 1989 $terms = get_terms( $args ); 2152 1990 2153 1991 /** 2154 1992 * Filters the terms for a given object or objects. 2155 1993 * 2156 1994 * @since 4.2.0 2157 1995 * 2158 * @param array $terms 2159 * @param array $object_id _arrayArray of object IDs for which `$terms` were retrieved.2160 * @param array $taxonom y_arrayArray of taxonomies from which `$terms` were retrieved.2161 * @param array $args 2162 * 1996 * @param array $terms An array of terms for the given object or objects. 1997 * @param array $object_ids Array of object IDs for which `$terms` were retrieved. 1998 * @param array $taxonomies Array of taxonomies from which `$terms` were retrieved. 1999 * @param array $args An array of arguments for retrieving terms for the given 2000 * object(s). See wp_get_object_terms() for details. 2163 2001 */ 2164 $terms = apply_filters( 'get_object_terms', $terms, $object_id_array, $taxonomy_array, $args ); 2002 $terms = apply_filters( 'get_object_terms', $terms, $object_ids, $taxonomies, $args ); 2003 2004 $object_ids = implode( ',', $object_ids ); 2005 $taxonomies = implode( ',', $taxonomies ); 2165 2006 2166 2007 /** 2167 2008 * Filters the terms for a given object or objects. -
tests/phpunit/tests/term/wpGetObjectTerms.php
diff --git tests/phpunit/tests/term/wpGetObjectTerms.php tests/phpunit/tests/term/wpGetObjectTerms.php index 892e7a1..a035501 100644
class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase { 23 23 $this->assertEquals( 3, count($tt_1) ); 24 24 25 25 // make sure they're correct 26 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'slugs', 'orderby' => 't .term_id'));26 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'slugs', 'orderby' => 'term_id')); 27 27 $this->assertEquals( $terms_1_slugs, $terms ); 28 28 } 29 29 … … class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase { 360 360 * @ticket 15675 361 361 */ 362 362 public function test_parent() { 363 register_taxonomy( 'wptests_tax2', 'post', array( 364 'hierarchical' => true, 365 ) ); 366 363 367 $t1 = self::factory()->term->create( array( 364 'taxonomy' => $this->taxonomy,368 'taxonomy' => 'wptests_tax2', 365 369 ) ); 366 370 $t2 = self::factory()->term->create( array( 367 'taxonomy' => $this->taxonomy,371 'taxonomy' => 'wptests_tax2', 368 372 ) ); 369 373 $t3 = self::factory()->term->create( array( 370 'taxonomy' => $this->taxonomy,374 'taxonomy' => 'wptests_tax2', 371 375 'parent' => $t1, 372 376 ) ); 373 377 $t4 = self::factory()->term->create( array( 374 'taxonomy' => $this->taxonomy,378 'taxonomy' => 'wptests_tax2', 375 379 'parent' => $t2, 376 380 ) ); 377 381 378 382 $p = self::factory()->post->create(); 379 383 380 wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), $this->taxonomy);384 wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), 'wptests_tax2' ); 381 385 382 $found = wp_get_object_terms( $p, $this->taxonomy, array(386 $found = wp_get_object_terms( $p, 'wptests_tax2', array( 383 387 'parent' => $t1, 384 388 'fields' => 'ids', 385 389 ) ); -
tests/phpunit/tests/term/wpSetObjectTerms.php
diff --git tests/phpunit/tests/term/wpSetObjectTerms.php tests/phpunit/tests/term/wpSetObjectTerms.php index 279f448..6f126b3 100644
class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase { 259 259 $this->assertEquals( 3, count($tt_1) ); 260 260 261 261 // make sure they're correct 262 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't .term_id'));262 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 'term_id')); 263 263 $this->assertEquals( $terms_1, $terms ); 264 264 265 265 // change the terms … … class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase { 267 267 $this->assertEquals( 2, count($tt_2) ); 268 268 269 269 // make sure they're correct 270 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 't .term_id'));270 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'ids', 'orderby' => 'term_id')); 271 271 $this->assertEquals( $terms_2, $terms ); 272 272 273 273 // make sure the tt id for 'bar' matches … … class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase { 288 288 $this->assertEquals( 3, count($tt_1) ); 289 289 290 290 // make sure they're correct 291 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't .term_id'));291 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 'term_id')); 292 292 $this->assertEquals( $terms_1, $terms ); 293 293 294 294 // change the terms … … class Tests_Term_WpSetObjectTerms extends WP_UnitTestCase { 296 296 $this->assertEquals( 2, count($tt_2) ); 297 297 298 298 // make sure they're correct 299 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 't .term_id'));299 $terms = wp_get_object_terms($post_id, $this->taxonomy, array('fields' => 'names', 'orderby' => 'term_id')); 300 300 $this->assertEquals( $terms_2, $terms ); 301 301 302 302 // make sure the tt id for 'bar' matches