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
This commit is contained in:
Peter Wilson
2022-09-20 04:29:11 +00:00
parent 4c173550a2
commit 4241aa348a
2 changed files with 32 additions and 1 deletions

View File

@@ -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.

View File

@@ -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(