Make WordPress Core

Changeset 35679


Ignore:
Timestamp:
11/18/2015 07:58:01 PM (8 years ago)
Author:
wonderboymusic
Message:

Rewrite: alleviate conflicts between image attachment pages and posts when permalink structure is /%postname%/.

Adds unit test.

Props SergeyBiryukov.
Fixes #24612.

Location:
trunk
Files:
2 edited

Legend:

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

    r35285 r35679  
    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') ) {
  • trunk/tests/phpunit/tests/query/conditionals.php

    r35244 r35679  
    761761    }
    762762
     763    /**
     764     * @ticket 24612
     765     */
     766    public function test_is_single_with_slug_that_clashes_with_attachment() {
     767        $this->set_permalink_structure( '/%postname%/' );
     768
     769        $attachment_id = $this->factory->post->create( array(
     770            'post_type'  => 'attachment',
     771        ) );
     772
     773        $post_id = $this->factory->post->create( array(
     774            'post_title' => get_post( $attachment_id )->post_title
     775        ) );
     776
     777        $this->go_to( get_permalink( $post_id ) );
     778
     779        $q = $GLOBALS['wp_query'];
     780
     781        $this->assertTrue( $q->is_single() );
     782        $this->assertTrue( $q->is_single( $post_id ) );
     783        $this->assertFalse( $q->is_attachment() );
     784        $this->assertFalse( $q->is_404() );
     785
     786        $this->set_permalink_structure();
     787    }
     788
    763789    function test_is_page() {
    764790        $post_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
Note: See TracChangeset for help on using the changeset viewer.