Skip to content

Commit

Permalink
Posts: Ensure get_sample_permalink returns the right permalink for au…
Browse files Browse the repository at this point in the history
…to-draft posts.

The post editor uses the permalink_template property from the posts REST API to render the post slug input or not. For auto-drafts or newly created posts, this property was not set properly. This PR solves by fixing the get_sample_permalink function used to compute this property.

Props youknowriad, mcsf, antonvlasenko, azaozz, peterwilsoncc, andrewserong, hellofromTonya, spacedmonkey.
Fixes #59283.

git-svn-id: https://develop.svn.wordpress.org/trunk@58174 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
youknowriad committed May 20, 2024
1 parent a557bae commit ce84e1f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ function get_sample_permalink( $post, $title = null, $name = null ) {
$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 ) ) {
if ( in_array( $post->post_status, array( 'auto-draft', 'draft', 'pending', 'future' ), true ) ) {
$post->post_status = 'publish';
$post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
}
Expand Down
6 changes: 5 additions & 1 deletion src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,11 @@ function get_post_mime_type( $post = null ) {
* @return string|false Post status on success, false on failure.
*/
function get_post_status( $post = null ) {
$post = get_post( $post );
if ( $post instanceof WP_Post && isset( $post->filter ) && 'sample' === $post->filter ) {
// Skip normalization
} else {
$post = get_post( $post );
}

if ( ! is_object( $post ) ) {
return false;
Expand Down
22 changes: 22 additions & 0 deletions tests/phpunit/tests/admin/includesPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,28 @@ function ( $permalink, $post_id, $title, $name, $post ) use ( $post_original ) {
$this->assertEquals( $post_original, $post, 'get_sample_permalink() modifies the post object.' );
}

/**
* @ticket 59283
*/
public function test_get_sample_permalink_should_return_pretty_permalink_for_posts_with_post_status_auto_draft() {
$permalink_structure = '%postname%';
$this->set_permalink_structure( "/$permalink_structure/" );

$future_date = gmdate( 'Y-m-d H:i:s', time() + 100 );
$p = self::factory()->post->create(
array(
'post_status' => 'auto-draft',
'post_name' => 'foo',
'post_date' => $future_date,
)
);

$found = get_sample_permalink( $p );
$expected = trailingslashit( home_url( $permalink_structure ) );

$this->assertSame( $expected, $found[0] );
}

public function test_post_exists_should_match_title() {
$p = self::factory()->post->create(
array(
Expand Down

0 comments on commit ce84e1f

Please sign in to comment.