diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 1abe542316..ff3c8d269a 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -1399,6 +1399,7 @@ function get_sample_permalink( $post, $title = null, $name = null ) { $original_status = $post->post_status; $original_date = $post->post_date; $original_name = $post->post_name; + $original_filter = $post->filter; // Hack: get_permalink() would return plain permalink for drafts, so we will fake that our post is published. if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ), true ) ) { @@ -1443,7 +1444,7 @@ function get_sample_permalink( $post, $title = null, $name = null ) { $post->post_status = $original_status; $post->post_date = $original_date; $post->post_name = $original_name; - unset( $post->filter ); + $post->filter = $original_filter; /** * Filters the sample permalink. diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index 2a41131a92..799d927dfa 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -686,6 +686,36 @@ class Tests_Admin_IncludesPost extends WP_UnitTestCase { $this->assertSame( 'child-page', $actual[1] ); } + /** + * Tests that get_sample_permalink() preserves the original WP_Post properties. + * + * @ticket 54736 + * + * @covers ::get_sample_permalink + */ + public function test_get_sample_permalink_should_preserve_the_original_post_properties() { + $post = self::factory()->post->create_and_get( + array( + 'post_status' => 'draft', + ) + ); + + $post_original = clone $post; + + add_filter( + 'get_sample_permalink', + function( $permalink, $post_id, $title, $name, $post ) use ( $post_original ) { + $this->assertEquals( $post_original, $post, 'Modified post object passed to get_sample_permalink filter.' ); + return $permalink; + }, + 10, + 5 + ); + + get_sample_permalink( $post ); + $this->assertEquals( $post_original, $post, 'get_sample_permalink() modifies the post object.' ); + } + public function test_post_exists_should_match_title() { $p = self::factory()->post->create( array(