Make WordPress Core

Changeset 54244


Ignore:
Timestamp:
09/20/2022 04:29:11 AM (2 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.

Location:
trunk
Files:
2 edited

Legend:

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

    r54122 r54244  
    14001400    $original_date   = $post->post_date;
    14011401    $original_name   = $post->post_name;
     1402    $original_filter = $post->filter;
    14021403
    14031404    // Hack: get_permalink() would return plain permalink for drafts, so we will fake that our post is published.
     
    14441445    $post->post_date   = $original_date;
    14451446    $post->post_name   = $original_name;
    1446     unset( $post->filter );
     1447    $post->filter      = $original_filter;
    14471448
    14481449    /**
  • 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.