Skip to content

Commit 4241aa3

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@54244 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4c17355 commit 4241aa3

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/wp-admin/includes/post.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,7 @@ function get_sample_permalink( $post, $title = null, $name = null ) {
13991399
$original_status = $post->post_status;
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.
14041405
if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ), true ) ) {
@@ -1443,7 +1444,7 @@ function get_sample_permalink( $post, $title = null, $name = null ) {
14431444
$post->post_status = $original_status;
14441445
$post->post_date = $original_date;
14451446
$post->post_name = $original_name;
1446-
unset( $post->filter );
1447+
$post->filter = $original_filter;
14471448

14481449
/**
14491450
* Filters the sample permalink.

tests/phpunit/tests/admin/includesPost.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,36 @@ public function test_get_sample_permalink_should_respect_hierarchy_of_draft_page
686686
$this->assertSame( 'child-page', $actual[1] );
687687
}
688688

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.' );
717+
}
718+
689719
public function test_post_exists_should_match_title() {
690720
$p = self::factory()->post->create(
691721
array(

0 commit comments

Comments
 (0)