diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index cde99d840e..dfcb3ec440 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -1300,14 +1300,14 @@ function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) { $preview_target = ''; if ( current_user_can( 'read_post', $post->ID ) ) { - if ( 'draft' === $post->post_status ) { + if ( 'draft' === $post->post_status || empty( $post->post_name ) ) { $view_link = get_preview_post_link( $post ); $preview_target = " target='wp-preview-{$post->ID}'"; } else { if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) { $view_link = get_permalink( $post ); } else { - // Allow non-published (private, future) to be viewed at a pretty permalink. + // Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set $view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink ); } } diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index ca0e6f3051..60efbb2e34 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -356,6 +356,23 @@ class Tests_Admin_Includes_Post extends WP_UnitTestCase { $this->assertContains( '>new_slug-صورة<', $found, $message ); } + /** + * @ticket 30910 + * @ticket 18306 + */ + public function test_get_sample_permalink_html_should_use_preview_links_for_draft_and_pending_posts_with_no_post_name() { + $this->set_permalink_structure( '/%postname%/' ); + + wp_set_current_user( self::$admin_id ); + + $future_date = date( 'Y-m-d H:i:s', time() + 100 ); + $p = self::factory()->post->create( array( 'post_status' => 'pending', 'post_name' => '', 'post_date' => $future_date ) ); + + $found = get_sample_permalink_html( $p ); + $post = get_post( $p ); + $this->assertContains( 'href="' . esc_url( get_preview_post_link( $post ) ), $found ); + } + /** * @ticket 5305 */