Make WordPress Core

Changeset 58174


Ignore:
Timestamp:
05/20/2024 09:47:41 AM (5 weeks ago)
Author:
youknowriad
Message:

Posts: Ensure get_sample_permalink returns the right permalink for auto-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.

Location:
trunk
Files:
3 edited

Legend:

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

    r58129 r58174  
    14641464
    14651465    // Hack: get_permalink() would return plain permalink for drafts, so we will fake that our post is published.
    1466     if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ), true ) ) {
     1466    if ( in_array( $post->post_status, array( 'auto-draft', 'draft', 'pending', 'future' ), true ) ) {
    14671467        $post->post_status = 'publish';
    14681468        $post->post_name   = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
  • trunk/src/wp-includes/post.php

    r58055 r58174  
    12201220 */
    12211221function get_post_status( $post = null ) {
    1222     $post = get_post( $post );
     1222    if ( $post instanceof WP_Post && isset( $post->filter ) && 'sample' === $post->filter ) {
     1223        // Skip normalization
     1224    } else {
     1225        $post = get_post( $post );
     1226    }
    12231227
    12241228    if ( ! is_object( $post ) ) {
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r57580 r58174  
    931931        get_sample_permalink( $post );
    932932        $this->assertEquals( $post_original, $post, 'get_sample_permalink() modifies the post object.' );
     933    }
     934
     935    /**
     936     * @ticket 59283
     937     */
     938    public function test_get_sample_permalink_should_return_pretty_permalink_for_posts_with_post_status_auto_draft() {
     939        $permalink_structure = '%postname%';
     940        $this->set_permalink_structure( "/$permalink_structure/" );
     941
     942        $future_date = gmdate( 'Y-m-d H:i:s', time() + 100 );
     943        $p           = self::factory()->post->create(
     944            array(
     945                'post_status' => 'auto-draft',
     946                'post_name'   => 'foo',
     947                'post_date'   => $future_date,
     948            )
     949        );
     950
     951        $found    = get_sample_permalink( $p );
     952        $expected = trailingslashit( home_url( $permalink_structure ) );
     953
     954        $this->assertSame( $expected, $found[0] );
    933955    }
    934956
Note: See TracChangeset for help on using the changeset viewer.