Make WordPress Core

Ticket #24612: 24612.2.patch

File 24612.2.patch, 2.4 KB (added by SergeyBiryukov, 8 years ago)
  • src/wp-includes/query.php

     
    18331833                }
    18341834
    18351835                if ( '' != $qv['pagename'] ) {
    1836                         $this->queried_object = get_page_by_path($qv['pagename']);
    1837                         if ( !empty($this->queried_object) )
     1836                        $this->queried_object = get_page_by_path( $qv['pagename'] );
     1837
     1838                        if ( $this->queried_object && 'attachment' == $this->queried_object->post_type ) {
     1839                                if ( preg_match( "/^[^%]*%(?:postname)%/", get_option( 'permalink_structure' ) ) ) {
     1840                                        // See if we also have a post with the same slug
     1841                                        $post = get_page_by_path( $qv['pagename'], OBJECT, 'post' );
     1842                                        if ( $post ) {
     1843                                                $this->queried_object = $post;
     1844                                                $this->is_page = false;
     1845                                                $this->is_single = true;
     1846                                        }
     1847                                }
     1848                        }
     1849
     1850                        if ( ! empty( $this->queried_object ) ) {
    18381851                                $this->queried_object_id = (int) $this->queried_object->ID;
    1839                         else
    1840                                 unset($this->queried_object);
     1852                        } else {
     1853                                unset( $this->queried_object );
     1854                        }
    18411855
    18421856                        if  ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
    18431857                                $this->is_page = false;
  • tests/phpunit/tests/query/conditionals.php

     
    767767                $this->assertFalse( $q->is_single( $p2 ) );
    768768        }
    769769
     770        /**
     771         * @ticket 24612
     772         */
     773        public function test_is_single_with_slug_that_clashes_with_attachment() {
     774                $this->set_permalink_structure( '/%postname%/' );
     775
     776                $attachment_id = $this->factory->post->create( array(
     777                        'post_type'  => 'attachment',
     778                ) );
     779
     780                $post_id = $this->factory->post->create( array(
     781                        'post_title' => get_post( $attachment_id )->post_title
     782                ) );
     783
     784                $this->go_to( get_permalink( $post_id ) );
     785
     786                $q = $GLOBALS['wp_query'];
     787
     788                $this->assertTrue( $q->is_single() );
     789                $this->assertTrue( $q->is_single( $post_id ) );
     790                $this->assertFalse( $q->is_attachment() );
     791                $this->assertFalse( $q->is_404() );
     792
     793                $this->set_permalink_structure();
     794        }
     795
    770796        function test_is_page() {
    771797                $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
    772798                $this->go_to( "/?page_id=$post_id" );