Ticket #35495: 35495.3.diff
File 35495.3.diff, 6.7 KB (added by , 9 years ago) |
---|
-
src/wp-includes/taxonomy.php
1077 1077 * @since 4.4.0 Introduced the ability to pass 'term_id' as an alias of 'id' for the `orderby` parameter. 1078 1078 * Introduced the 'meta_query' and 'update_term_meta_cache' parameters. Converted to return 1079 1079 * a list of WP_Term objects. 1080 * @since 4.5.0 Introduced 'meta_key' and 'meta_value' parameters. Introduced the ability to order results by metadata. 1080 * @since 4.5.0 Changed the function signature so that the `$args` array can be provided as the first parameter. 1081 * Introduced 'meta_key' and 'meta_value' parameters. Introduced the ability to order results by metadata. 1081 1082 * 1082 1083 * @global wpdb $wpdb WordPress database abstraction object. 1083 1084 * @global array $wp_filter 1084 1085 * 1085 * @param string|array $taxonomies Taxonomy name or list of Taxonomy names.1086 1086 * @param array|string $args { 1087 1087 * Optional. Array or string of arguments to get terms. 1088 1088 * 1089 * @type string|array $taxonomy Taxonomy name, or array of taxonomies, to which results should 1090 * be limited. 1089 1091 * @type string $orderby Field(s) to order terms by. Accepts term fields ('name', 'slug', 1090 1092 * 'term_group', 'term_id', 'id', 'description'), 'count' for term 1091 1093 * taxonomy count, 'include' to match the 'order' of the $include param, … … 1144 1146 * @type string $meta_value Limit terms to those matching a specific metadata value. Usually used 1145 1147 * in conjunction with `$meta_key`. 1146 1148 * } 1149 * @param array $legacy_args Argument array, when using the legacy function parameter format. If present, this 1150 * parameter will be interpreted as `$args`, and the first function parameter will 1151 * be parsed as a taxonomy or array of taxonomies. 1147 1152 * @return array|int|WP_Error List of WP_Term instances and their children. Will return WP_Error, if any of $taxonomies 1148 1153 * do not exist. 1149 1154 */ 1150 function get_terms( $ taxonomies, $args = '' ) {1155 function get_terms( $args = array(), $legacy_args = '' ) { 1151 1156 global $wpdb; 1152 $empty_array = array();1153 1157 1154 if ( ! is_array( $taxonomies ) ) {1155 $taxonomies = array( $taxonomies );1156 }1157 1158 foreach ( $taxonomies as $taxonomy ) {1159 if ( ! taxonomy_exists($taxonomy) ) {1160 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );1161 }1162 }1163 1164 1158 $defaults = array( 1159 'taxonomy' => null, 1165 1160 'orderby' => 'name', 1166 1161 'order' => 'ASC', 1167 1162 'hide_empty' => true, … … 1187 1182 'meta_query' => '' 1188 1183 ); 1189 1184 1185 /* 1186 * Legacy argument format ($taxonomy, $args) takes precedence. 1187 * 1188 * We detect legacy argument format by checking if 1189 * (a) a second non-empty parameter is passed, or 1190 * (b) the first parameter shares no keys with the default array (ie, it's a list of taxonomies) 1191 */ 1192 $do_legacy_args = $legacy_args || empty( array_intersect_key( $defaults, (array) $args ) );; 1193 1194 $taxonomies = null; 1195 if ( $do_legacy_args ) { 1196 $taxonomies = (array) $args; 1197 $args = $legacy_args; 1198 } elseif ( isset( $args['taxonomy'] ) && null !== $args['taxonomy'] ) { 1199 $taxonomies = (array) $args['taxonomy']; 1200 unset( $args['taxonomy'] ); 1201 } 1202 1203 $empty_array = array(); 1204 1205 if ( $taxonomies ) { 1206 foreach ( $taxonomies as $taxonomy ) { 1207 if ( ! taxonomy_exists($taxonomy) ) { 1208 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) ); 1209 } 1210 } 1211 } 1212 1190 1213 /** 1191 1214 * Filter the terms query default arguments. 1192 1215 * … … 1204 1227 1205 1228 // Save queries by not crawling the tree in the case of multiple taxes or a flat tax. 1206 1229 $has_hierarchical_tax = false; 1207 foreach ( $taxonomies as $_tax ) { 1208 if ( is_taxonomy_hierarchical( $_tax ) ) { 1209 $has_hierarchical_tax = true; 1230 if ( $taxonomies ) { 1231 foreach ( $taxonomies as $_tax ) { 1232 if ( is_taxonomy_hierarchical( $_tax ) ) { 1233 $has_hierarchical_tax = true; 1234 } 1210 1235 } 1211 1236 } 1212 1237 … … 1309 1334 $order = 'ASC'; 1310 1335 } 1311 1336 1312 $where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')"; 1337 $where = ''; 1338 if ( $taxonomies ) { 1339 $where .= "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')"; 1340 } 1313 1341 1314 1342 $exclude = $args['exclude']; 1315 1343 $exclude_tree = $args['exclude_tree']; … … 1554 1582 $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : ''; 1555 1583 $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; 1556 1584 1557 $query = "SELECT $distinct $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits"; 1585 if ( $where ) { 1586 if ( 0 === strpos( $where, ' AND ' ) ) { 1587 $where = substr( $where, 5 ); 1588 } 1589 $where = ' WHERE ' . $where; 1590 } 1558 1591 1592 $query = "SELECT $distinct $fields FROM $wpdb->terms AS t $join $where $orderby $order $limits"; 1593 1559 1594 // $args can be anything. Only use the args defined in defaults to compute the key. 1560 1595 $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $query ); 1561 1596 $last_changed = wp_cache_get( 'last_changed', 'terms' ); -
tests/phpunit/tests/term/getTerms.php
12 12 } 13 13 14 14 /** 15 * @ticket 35495 16 */ 17 public function test_should_accept_an_args_array_containing_taxonomy_for_first_parameter() { 18 register_taxonomy( 'wptests_tax', 'post' ); 19 $term = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); 20 21 $found = get_terms( array( 22 'taxonomy' => 'wptests_tax', 23 'hide_empty' => false, 24 'fields' => 'ids', 25 'update_term_meta_cache' => false, 26 ) ); 27 28 $this->assertEqualSets( array( $term ), $found ); 29 } 30 31 /** 32 * @ticket 35495 33 */ 34 public function test_excluding_taxonomy_arg_should_return_terms_from_all_taxonomies() { 35 register_taxonomy( 'wptests_tax1', 'post' ); 36 register_taxonomy( 'wptests_tax2', 'post' ); 37 $t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) ); 38 $t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) ); 39 40 $found = get_terms( array( 41 'hide_empty' => false, 42 'fields' => 'ids', 43 'update_term_meta_cache' => false, 44 ) ); 45 46 // There may be other terms lying around, so don't do an exact match. 47 $this->assertContains( $t1, $found ); 48 $this->assertContains( $t2, $found ); 49 } 50 51 /** 15 52 * @ticket 23326 16 53 */ 17 54 public function test_get_terms_cache() {