Ticket #31383: 31383.diff
File 31383.diff, 9.1 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-user-query.php
53 53 public $meta_query = false; 54 54 55 55 /** 56 * Taxonomy query, as passed to get_tax_sql() 57 * 58 * @since 4.8.0 59 * @access public 60 * @var object WP_Tax_Query 61 */ 62 public $tax_query; 63 64 /** 56 65 * The SQL query used to fetch matching users. 57 66 * 58 67 * @since 4.4.0 … … 102 111 'meta_key' => '', 103 112 'meta_value' => '', 104 113 'meta_compare' => '', 114 'tax_query' => array(), 105 115 'include' => array(), 106 116 'exclude' => array(), 107 117 'search' => '', … … 326 336 $this->meta_query = new WP_Meta_Query(); 327 337 $this->meta_query->parse_query_vars( $qv ); 328 338 339 // Taxonomy query. 340 $this->parse_tax_query( $qv ); 341 329 342 if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) { 330 343 $who_query = array( 331 344 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'user_level', … … 444 457 } 445 458 } 446 459 460 if ( ! empty( $this->tax_query->queries ) ) { 461 $taxonomy_clauses = $this->tax_query->get_sql( $wpdb->users, 'ID' ); 462 $this->query_from .= $taxonomy_clauses['join']; 463 $this->query_where .= $taxonomy_clauses['where']; 464 } 465 447 466 // sorting 448 467 $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; 449 468 $order = $this->parse_order( $qv['order'] ); … … 583 602 } 584 603 585 604 /** 605 * Parses various taxonomy related query vars. 606 * 607 * @access protected 608 * @since 4.8.0 609 * 610 * @param array $q The query variables. Passed by reference. 611 */ 612 protected function parse_tax_query( &$q ) { 613 if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) { 614 $tax_query = $q['tax_query']; 615 } else { 616 $tax_query = array(); 617 } 618 619 if ( ! empty( $q['taxonomy'] ) && ! empty( $q['term'] ) ) { 620 $tax_query[] = array( 621 'taxonomy' => $q['taxonomy'], 622 'terms' => array( $q['term'] ), 623 'field' => 'slug', 624 ); 625 } 626 627 foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) { 628 if ( 'post_tag' == $taxonomy ) { 629 continue; // Handled further down in the $q['tag'] block. 630 } 631 632 if ( $t->query_var && ! empty( $q[ $t->query_var ] ) ) { 633 $tax_query_defaults = array( 634 'taxonomy' => $taxonomy, 635 'field' => 'slug', 636 ); 637 638 if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) { 639 $q[ $t->query_var ] = wp_basename( $q[ $t->query_var ] ); 640 } 641 642 $term = $q[ $t->query_var ]; 643 644 if ( is_array( $term ) ) { 645 $term = implode( ',', $term ); 646 } 647 648 if ( strpos( $term, '+' ) !== false ) { 649 $terms = preg_split( '/[+]+/', $term ); 650 foreach ( $terms as $term ) { 651 $tax_query[] = array_merge( $tax_query_defaults, array( 652 'terms' => array( $term ) 653 ) ); 654 } 655 } else { 656 $tax_query[] = array_merge( $tax_query_defaults, array( 657 'terms' => preg_split( '/[,]+/', $term ) 658 ) ); 659 } 660 } 661 } 662 663 $this->tax_query = new WP_Tax_Query( $tax_query ); 664 665 /** 666 * Fires after taxonomy-related query vars have been parsed. 667 * 668 * @since 4.8.0 669 * 670 * @param WP_User_Query $this The WP_User_Query instance. 671 */ 672 do_action( 'parse_user_tax_query', $this ); 673 } 674 675 676 /** 586 677 * Execute the query, with the current variables. 587 678 * 588 679 * @since 3.1.0 -
src/wp-includes/taxonomy.php
545 545 if ( !isset($wp_taxonomies[$taxonomy]) ) 546 546 return false; 547 547 548 if ( ! get_post_type_object($object_type) )548 if ( 'user' != $object_type && ! get_post_type_object( $object_type ) ) { 549 549 return false; 550 } 550 551 551 552 if ( ! in_array( $object_type, $wp_taxonomies[$taxonomy]->object_type ) ) 552 553 $wp_taxonomies[$taxonomy]->object_type[] = $object_type; … … 574 575 if ( ! isset( $wp_taxonomies[ $taxonomy ] ) ) 575 576 return false; 576 577 577 if ( ! get_post_type_object( $object_type ) )578 if ( 'user' != $object_type && ! get_post_type_object( $object_type ) ) { 578 579 return false; 580 } 579 581 580 582 $key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true ); 581 583 if ( false === $key ) -
tests/phpunit/tests/taxonomy.php
33 33 } 34 34 } 35 35 36 function test_get_user_taxonomy() { 37 foreach ( get_object_taxonomies('user') as $taxonomy ) { 38 $tax = get_taxonomy($taxonomy); 39 // should return an object with the correct taxonomy object type 40 $this->assertTrue( is_object( $tax ) ); 41 $this->assertTrue( is_array( $tax->object_type ) ); 42 $this->assertEquals( array( 'user' ), $tax->object_type ); 43 } 44 } 45 36 46 function test_get_the_taxonomies() { 37 47 $post_id = self::factory()->post->create(); 38 48 … … 139 149 unset($GLOBALS['wp_taxonomies'][$tax]); 140 150 } 141 151 152 /** 153 * @ticket 31383 154 */ 155 function test_register_user_taxonomy() { 156 157 // make up a new taxonomy name, and ensure it's unused 158 $tax = rand_str(); 159 $this->assertFalse( taxonomy_exists($tax) ); 160 161 register_taxonomy( $tax, 'user' ); 162 $this->assertTrue( taxonomy_exists($tax) ); 163 $this->assertFalse( is_taxonomy_hierarchical($tax) ); 164 165 // clean up 166 unset($GLOBALS['wp_taxonomies'][$tax]); 167 } 168 142 169 function test_register_hierarchical_taxonomy() { 143 170 144 171 // make up a new taxonomy name, and ensure it's unused … … 209 236 $this->assertFalse( unregister_taxonomy_for_object_type( 'category', 'post' ) ); 210 237 $this->assertTrue( register_taxonomy_for_object_type( 'category', 'post' ) ); 211 238 239 // Core taxonomy, users 240 $this->assertTrue( register_taxonomy_for_object_type( 'category', 'user' ) ); 241 $this->assertTrue( unregister_taxonomy_for_object_type( 'category', 'user' ) ); 242 $this->assertFalse( unregister_taxonomy_for_object_type( 'category', 'user' ) ); 243 $this->assertTrue( register_taxonomy_for_object_type( 'category', 'user' ) ); 244 212 245 // Core taxonomy, non-core post type 213 246 $this->assertTrue( register_taxonomy_for_object_type( 'category', $post_type ) ); 214 247 $this->assertTrue( unregister_taxonomy_for_object_type( 'category', $post_type ) ); … … 215 248 $this->assertFalse( unregister_taxonomy_for_object_type( 'category', $post_type ) ); 216 249 $this->assertTrue( register_taxonomy_for_object_type( 'category', $post_type ) ); 217 250 218 // Core taxonomies, non-post object types219 $this->assertFalse( register_taxonomy_for_object_type( 'category', 'user' ) );220 $this->assertFalse( unregister_taxonomy_for_object_type( 'category', 'user' ) );221 222 251 // Non-core taxonomy, core post type 223 252 $this->assertTrue( unregister_taxonomy_for_object_type( $tax, 'post' ) ); 224 253 $this->assertFalse( unregister_taxonomy_for_object_type( $tax, 'post' ) ); 225 254 $this->assertTrue( register_taxonomy_for_object_type( $tax, 'post' ) ); 226 255 256 // Non-core taxonomy, users 257 $this->assertTrue( register_taxonomy_for_object_type( $tax, 'user' ) ); 258 $this->assertTrue( unregister_taxonomy_for_object_type( $tax, 'user' ) ); 259 $this->assertFalse( unregister_taxonomy_for_object_type( $tax, 'user' ) ); 260 $this->assertTrue( register_taxonomy_for_object_type( $tax, 'user' ) ); 261 227 262 // Non-core taxonomy, non-core post type 228 263 $this->assertTrue( register_taxonomy_for_object_type( $tax, $post_type ) ); 229 264 $this->assertTrue( unregister_taxonomy_for_object_type( $tax, $post_type ) ); … … 230 265 $this->assertFalse( unregister_taxonomy_for_object_type( $tax, $post_type ) ); 231 266 $this->assertTrue( register_taxonomy_for_object_type( $tax, $post_type ) ); 232 267 233 // Non-core taxonomies, non-post object types234 $this->assertFalse( register_taxonomy_for_object_type( $tax, 'user' ) );235 $this->assertFalse( unregister_taxonomy_for_object_type( $tax, 'user' ) );236 237 268 unset($GLOBALS['wp_taxonomies'][$tax]); 238 269 _unregister_post_type( $post_type ); 239 270 … … 240 271 } 241 272 242 273 /** 274 * @ticket 31383 275 */ 276 function test_registering_taxonomies_to_users() { 277 // Create a taxonomy to test with 278 $tax = 'test_tax'; 279 $this->assertFalse( taxonomy_exists( $tax ) ); 280 register_taxonomy( $tax, '', array( 'hierarchical' => true ) ); 281 282 // Core taxonomy, users 283 $this->assertTrue( register_taxonomy_for_object_type( 'category', 'user' ) ); 284 $this->assertTrue( unregister_taxonomy_for_object_type( 'category', 'user' ) ); 285 $this->assertFalse( unregister_taxonomy_for_object_type( 'category', 'user' ) ); 286 $this->assertTrue( register_taxonomy_for_object_type( 'category', 'user' ) ); 287 288 // Non-core taxonomy, users 289 $this->assertTrue( register_taxonomy_for_object_type( $tax, 'user' ) ); 290 $this->assertTrue( unregister_taxonomy_for_object_type( $tax, 'user' ) ); 291 $this->assertFalse( unregister_taxonomy_for_object_type( $tax, 'user' ) ); 292 $this->assertTrue( register_taxonomy_for_object_type( $tax, 'user' ) ); 293 294 unset( $GLOBALS['wp_taxonomies'][ $tax ] ); 295 } 296 297 /** 243 298 * @ticket 32590 244 299 */ 245 300 public function test_register_taxonomy_for_post_type_for_taxonomy_with_no_object_type_should_filter_out_empty_object_types() {