Make WordPress Core

Changeset 25292


Ignore:
Timestamp:
09/06/2013 11:38:51 PM (13 years ago)
Author:
wonderboymusic
Message:

Check the value passed to get_post_type_object(). If it's an array, use the first item. get_query_var( 'post_type' ) can be an array if the query has been altered via filters/actions. There are several places in core that pass the query var. Adds unit tests.

In template-loader.php, move is_post_type_archive() and is_tax() directly below is_home().

See #18614, [25291].

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r25271 r25292  
    10581058function get_post_type_object( $post_type ) {
    10591059        global $wp_post_types;
     1060
     1061        if ( is_array( $post_type ) )
     1062                $post_type = reset( $post_type );
    10601063
    10611064        if ( empty($wp_post_types[$post_type]) )
  • trunk/src/wp-includes/template-loader.php

    r25291 r25292  
    2727        if     ( is_404()            && $template = get_404_template()            ) :
    2828        elseif ( is_search()         && $template = get_search_template()         ) :
    29         elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
    30         elseif ( is_tax()            && $template = get_taxonomy_template()       ) :
    3129        elseif ( is_front_page()     && $template = get_front_page_template()     ) :
    3230        elseif ( is_home()           && $template = get_home_template()           ) :
     31        elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
     32        elseif ( is_tax()            && $template = get_taxonomy_template()       ) :
    3333        elseif ( is_attachment()     && $template = get_attachment_template()     ) :
    3434                remove_filter('the_content', 'prepend_attachment');
  • trunk/tests/phpunit/tests/query/conditionals.php

    r25291 r25292  
    684684                ) );
    685685        }
     686
     687        function test_post_type_array() {
     688                delete_option( 'rewrite_rules' );
     689
     690                $cpt_name = 'thearray';
     691                register_post_type( $cpt_name, array(
     692                        'taxonomies' => array( 'post_tag', 'category' ),
     693                        'rewrite' => true,
     694                        'has_archive' => true,
     695                        'public' => true
     696                ) );
     697                $this->factory->post->create( array( 'post_type' => $cpt_name ) );
     698
     699                $this->go_to( "/$cpt_name/" );
     700                $this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
     701                $this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
     702                $this->assertEquals( get_queried_object(), get_post_type_object( array( $cpt_name ) ) );
     703                $this->assertEquals( get_queried_object(), get_post_type_object( array( $cpt_name, 'post' ) ) );
     704
     705                add_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_type_array' ) );
     706
     707                $this->go_to( "/$cpt_name/" );
     708                $this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
     709                $this->assertEquals( get_queried_object(), get_post_type_object( 'post' ) );
     710                $this->assertEquals( get_queried_object(), get_post_type_object( array( 'post' ) ) );
     711                $this->assertEquals( get_queried_object(), get_post_type_object( array( 'post', $cpt_name ) ) );
     712
     713                remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_type_array' ) );
     714        }
     715
     716        function pre_get_posts_with_type_array( &$query ) {
     717                $query->set( 'post_type', array( 'post', 'thearray' ) );
     718        }
    686719}
Note: See TracChangeset for help on using the changeset viewer.