Make WordPress Core


Ignore:
Timestamp:
09/20/2022 04:29:11 AM (3 years ago)
Author:
peterwilsoncc
Message:

Posts, Post types: Prevent get_sample_permalink() modifying the post object.

get_sample_permalink() (ab)uses the $post->filter property to indicate a sample permalink is being generated for the post. This change ensures the property is restored to its original value.

Props herregroen, hellofromTonya, peterwilsoncc, Rahmohn, costdev.
Fixes #54736.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r54122 r54244  
    685685        $this->assertSame( home_url() . '/parent-page/%pagename%/', $actual[0] );
    686686        $this->assertSame( 'child-page', $actual[1] );
     687    }
     688
     689    /**
     690     * Tests that get_sample_permalink() preserves the original WP_Post properties.
     691     *
     692     * @ticket 54736
     693     *
     694     * @covers ::get_sample_permalink
     695     */
     696    public function test_get_sample_permalink_should_preserve_the_original_post_properties() {
     697        $post = self::factory()->post->create_and_get(
     698            array(
     699                'post_status' => 'draft',
     700            )
     701        );
     702
     703        $post_original = clone $post;
     704
     705        add_filter(
     706            'get_sample_permalink',
     707            function( $permalink, $post_id, $title, $name, $post ) use ( $post_original ) {
     708                $this->assertEquals( $post_original, $post, 'Modified post object passed to get_sample_permalink filter.' );
     709                return $permalink;
     710            },
     711            10,
     712            5
     713        );
     714
     715        get_sample_permalink( $post );
     716        $this->assertEquals( $post_original, $post, 'get_sample_permalink() modifies the post object.' );
    687717    }
    688718
Note: See TracChangeset for help on using the changeset viewer.