Ticket #5358: 5358.3.diff
File 5358.3.diff, 9.8 KB (added by , 11 years ago) |
---|
-
src/wp-includes/query.php
58 58 } 59 59 60 60 /** 61 * Retrieve the currently-queried objects. Wrapper for $wp_query->get_queried_objects() 62 * 63 * @uses WP_Query::get_queried_objects 64 * 65 * @since 3.7.0 66 * @access public 67 * 68 * @return object|null 69 */ 70 function get_queried_objects() { 71 global $wp_query; 72 return $wp_query->get_queried_objects(); 73 } 74 75 /** 76 * Retrieve IDs of the current queried objects. Wrapper for $wp_query->get_queried_object_ids() 77 * 78 * @uses WP_Query::get_queried_object_ids() 79 * 80 * @since 3.7.0 81 * @access public 82 * 83 * @return int 84 */ 85 function get_queried_object_ids() { 86 global $wp_query; 87 return $wp_query->get_queried_object_ids(); 88 } 89 90 /** 61 91 * Set query variable. 62 92 * 63 93 * @see WP_Query::set() … … 896 926 var $queried_object_id; 897 927 898 928 /** 929 * Holds the data for the objects that are queried. 930 * 931 * @since 3.7.0 932 * @access public 933 * @var array 934 */ 935 var $queried_objects; 936 937 /** 938 * The IDs of the queried objects. 939 * 940 * @since 3.7.0 941 * @access public 942 * @var array 943 */ 944 var $queried_object_ids; 945 946 /** 899 947 * Get post database query. 900 948 * 901 949 * @since 2.0.1 … … 3036 3084 * @return object 3037 3085 */ 3038 3086 function get_queried_object() { 3039 if ( isset($this->queried_object) ) 3040 return $this->queried_object; 3087 $this->get_queried_objects(); 3088 return $this->queried_object; 3089 } 3041 3090 3042 $this->queried_object = null; 3043 $this->queried_object_id = 0; 3091 /** 3092 * Retrieve ID of the current queried object. 3093 * 3094 * @since 1.5.0 3095 * @access public 3096 * 3097 * @return int 3098 */ 3099 function get_queried_object_id() { 3100 $this->get_queried_objects(); 3101 return $this->queried_object_id; 3102 } 3044 3103 3104 /** 3105 * Retrieve queried objects. 3106 * 3107 * If queried objects is not set, then the queried object will be set from 3108 * the categories, tags, taxonomies, posts page, single post, page, or author 3109 * query variable. After it is set up, it will be returned. 3110 * 3111 * @since 3.7.0 3112 * @access public 3113 * 3114 * @return array 3115 */ 3116 public function get_queried_objects() { 3117 if ( isset( $this->queried_objects ) ) 3118 return $this->queried_objects; 3119 3120 $this->queried_objects = array(); 3121 $this->queried_object_ids = array(); 3122 3045 3123 if ( $this->is_category || $this->is_tag || $this->is_tax ) { 3046 3124 $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' ); 3047 3125 3126 $terms = array(); 3048 3127 $query = reset( $tax_query_in_and ); 3049 3128 3050 if ( 'term_id' == $query['field'] ) 3051 $term = get_term( reset( $query['terms'] ), $query['taxonomy'] ); 3052 elseif ( $query['terms'] ) 3053 $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] ); 3129 if ( $query['terms'] ) { 3130 foreach ( $query['terms'] as $term ) { 3131 $term = get_term_by( $query['field'], $term, $query['taxonomy'] ); 3132 if ( $term && ! is_wp_error( $term ) ) { 3133 $terms[] = $term; 3134 } 3135 } 3136 } 3054 3137 3055 if ( ! empty( $term ) && ! is_wp_error( $term ) ) { 3056 $this->queried_object = $term; 3057 $this->queried_object_id = (int) $term->term_id; 3138 if ( ! empty( $terms ) ) { 3139 if ( $this->is_category ) { 3140 foreach ( $terms as &$category ) { 3141 _make_cat_compat( $category ); 3142 } 3143 unset( $category ); 3144 } 3058 3145 3059 if ( $this->is_category )3060 _make_cat_compat( $this->queried_object);3146 $this->queried_objects = $terms; 3147 $this->queried_object_ids = wp_list_pluck( $this->queried_objects, 'term_id' ); 3061 3148 } 3062 3149 } elseif ( $this->is_post_type_archive ) { 3063 $this->queried_object = get_post_type_object( $this->get('post_type') ); 3150 $post_types = (array) $this->get( 'post_type' ); 3151 foreach ( $post_types as $post_type ) { 3152 $this->queried_objects[] = get_post_type_object( $post_type ); 3153 } 3064 3154 } elseif ( $this->is_posts_page ) { 3065 $page_for_posts = get_option( 'page_for_posts');3066 $this->queried_object = get_post( $page_for_posts );3067 $this->queried_object_id = (int) $this->queried_object->ID;3068 } elseif ( $this->is_singular && ! is_null($this->post) ) {3069 $this->queried_object = $this->post;3070 $this->queried_object_id = (int) $this->post->ID;3155 $page_for_posts = get_option( 'page_for_posts' ); 3156 $this->queried_objects[] = get_post( $page_for_posts ); 3157 $this->queried_object_ids = wp_list_pluck( $this->queried_objects, 'ID' ); 3158 } elseif ( $this->is_singular && ! is_null( $this->post ) ) { 3159 $this->queried_objects[] = $this->post; 3160 $this->queried_object_ids = wp_list_pluck( $this->queried_objects, 'ID' ); 3071 3161 } elseif ( $this->is_author ) { 3072 $this->queried_object_id = (int) $this->get('author'); 3073 $this->queried_object = get_userdata( $this->queried_object_id ); 3162 $this->queried_object_ids = array_map( 'absint', (array) $this->get( 'author' ) ); 3163 foreach ( $this->queried_object_ids as $user_id ) { 3164 $this->queried_objects[] = get_userdata( $user_id ); 3165 } 3074 3166 } 3075 3167 3076 return $this->queried_object; 3168 // back-compat for get_queried_object and get_queried_object_id 3169 $this->queried_object = ( ! empty( $this->queried_objects ) ) ? reset( $this->queried_objects ) : null; 3170 $this->queried_object_id = ( ! empty( $this->queried_object_ids ) ) ? reset( $this->queried_object_ids ) : 0; 3171 3172 return $this->queried_objects; 3077 3173 } 3078 3174 3079 3175 /** 3080 * Retrieve ID of the current queried object.3176 * Retrieve IDs of the current queried objects. 3081 3177 * 3082 * @since 1.5.03178 * @since 3.7.0 3083 3179 * @access public 3084 3180 * 3085 * @return int3181 * @return array 3086 3182 */ 3087 function get_queried_object_id() { 3088 $this->get_queried_object(); 3089 3090 if ( isset($this->queried_object_id) ) { 3091 return $this->queried_object_id; 3092 } 3093 3094 return 0; 3183 public function get_queried_object_ids() { 3184 $this->get_queried_objects(); 3185 return $this->queried_object_ids; 3095 3186 } 3096 3187 3097 3188 /** -
tests/phpunit/tests/query/results.php
Cannot display: file marked as a binary type. svn:mime-type = application/octet-stream
13 13 function setUp() { 14 14 parent::setUp(); 15 15 16 update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' ); 17 $GLOBALS['wp_rewrite']->init(); 18 create_initial_taxonomies(); 19 register_post_type( 'cpt', array( 'public' => true, 'has_archive' => true ) ); 20 flush_rewrite_rules(); 21 16 22 $cat_a = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-a' ) ); 17 23 $cat_b = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-b' ) ); 18 24 $cat_c = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'cat-c' ) ); … … 486 492 $author_ids = array_unique( wp_list_pluck( $posts, 'post_author' ) ); 487 493 $this->assertEqualSets( array( $author_1 ), $author_ids ); 488 494 } 495 496 /** 497 * Tag archives 498 * @ticket 5358 499 */ 500 function test_queried_objects_tags() { 501 // Single tag archive 502 $tag_one_id = $this->factory->tag->create(); 503 $tag_one = get_term( $tag_one_id, 'post_tag' ); 504 $this->go_to( get_term_link( $tag_one, 'post_tag' ) ); 505 506 $this->assertTrue( is_archive() && is_tag() ); 507 $this->assertEquals( get_queried_object_id(), $tag_one_id ); 508 $this->assertEquals( get_queried_object(), $tag_one ); 509 $this->assertEquals( get_queried_object_ids(), array( $tag_one_id ) ); 510 $this->assertEquals( get_queried_objects(), array( $tag_one ) ); 511 512 // Multiple tags archive 513 $tag_two_id = $this->factory->tag->create(); 514 $tag_two = get_term( $tag_two_id, 'post_tag' ); 515 $this->go_to( "/tag/{$tag_one->slug}+{$tag_two->slug}/" ); 516 517 $this->assertTrue( is_archive() && is_tag() ); 518 $this->assertEquals( get_queried_object_id(), $tag_one_id ); 519 $this->assertEquals( get_queried_object(), $tag_one ); 520 $this->assertEquals( get_queried_object_ids(), array( $tag_one_id, $tag_two_id ) ); 521 $this->assertEquals( get_queried_objects(), array( $tag_one, $tag_two ) ); 522 } 523 524 /** 525 * Post type archives 526 * @ticket 5358 527 */ 528 function test_queried_objects_post_types() { 529 $post_type = get_post_type_object( 'cpt' ); 530 $this->factory->post->create( array( 'post_type' => 'cpt' ) ); 531 $this->go_to( get_post_type_archive_link( 'cpt' ) ); 532 533 $this->assertTrue( is_archive() && is_post_type_archive() ); 534 $this->assertEquals( get_queried_object_id(), null ); 535 $this->assertEquals( get_queried_object(), $post_type ); 536 $this->assertEquals( get_queried_object_ids(), array() ); 537 $this->assertEquals( get_queried_objects(), array( $post_type ) ); 538 } 539 540 /** 541 * Posts 542 * @ticket 5358 543 */ 544 function test_queried_objects_posts() { 545 $post_id = $this->factory->post->create(); 546 $post = get_post( $post_id ); 547 $this->go_to( get_permalink( $post->ID ) ); 548 549 $this->assertTrue( is_single() && is_singular() ); 550 $this->assertEquals( get_queried_object_id(), $post->ID ); 551 $this->assertEquals( get_queried_object(), $post ); 552 $this->assertEquals( get_queried_object_ids(), array( $post->ID ) ); 553 $this->assertEquals( get_queried_objects(), array( $post ) ); 554 } 555 556 /** 557 * Authors 558 * @ticket 5358 559 */ 560 function test_queried_objects_authors() { 561 $user_id = $this->factory->user->create(); 562 $user = get_userdata( $user_id ); 563 $this->factory->post->create( array( 'author' => $user->ID ) ); 564 $this->go_to( get_author_posts_url( $user->ID ) ); 565 566 $this->assertTrue( is_archive() && is_author() ); 567 $this->assertEquals( get_queried_object_id(), $user->ID ); 568 $this->assertEquals( get_queried_object(), $user ); 569 $this->assertEquals( get_queried_object_ids(), array( $user->ID ) ); 570 $this->assertEquals( get_queried_objects(), array( $user ) ); 571 } 489 572 }