diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index ff60783ae5..909c1f8082 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -1308,7 +1308,6 @@ function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) { $post_name_html = '' . $post_name_abridged . ''; $display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, urldecode( $permalink ) ); - $pretty_permalink = str_replace( array( '%pagename%', '%postname%' ), $post_name, urldecode( $permalink ) ); $return = '' . __( 'Permalink:' ) . "\n"; $return .= '' . $display_link . "\n"; @@ -1324,8 +1323,12 @@ function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) { $preview_link = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post ); $return .= "$view_post\n"; } else { - if ( empty( $pretty_permalink ) ) { - $pretty_permalink = $permalink; + if ( 'publish' === $post->post_status ) { + // View Post button should always go to the saved permalink. + $pretty_permalink = get_permalink( $post ); + } else { + // Allow non-published (private, future) to be viewed at a pretty permalink. + $pretty_permalink = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, urldecode( $permalink ) ); } $return .= "$view_post\n"; diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index e25a9171f3..75677baf3b 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -314,6 +314,48 @@ class Tests_Admin_includesPost extends WP_UnitTestCase { flush_rewrite_rules(); } + /** + * @ticket 32954 + */ + public function test_get_sample_permalink_html_should_use_correct_permalink_for_view_post_button_when_changing_slug() { + global $wp_rewrite; + $old_permalink_structure = get_option( 'permalink_structure' ); + $permalink_structure = '%postname%'; + $wp_rewrite->set_permalink_structure( "/$permalink_structure/" ); + flush_rewrite_rules(); + + wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) ); + + // Published posts should use published permalink + $p = $this->factory->post->create( array( 'post_status' => 'publish', 'post_name' => 'foo' ) ); + + $found = get_sample_permalink_html( $p, null, 'new_slug' ); + $post = get_post( $p ); + $this->assertContains( "span id='view-post-btn'>post_name . "/'", $found ); + + // Scheduled posts should use published permalink + $future_date = date( 'Y-m-d H:i:s', time() + 100 ); + $p = $this->factory->post->create( array( 'post_status' => 'future', 'post_name' => 'bar', 'post_date' => $future_date ) ); + + $found = get_sample_permalink_html( $p, null, 'new_slug' ); + $post = get_post( $p ); + $this->assertContains( "span id='view-post-btn'>post_name . "/'", $found ); + + // Draft posts should use preview link + $p = $this->factory->post->create( array( 'post_status' => 'draft', 'post_name' => 'baz' ) ); + + $found = get_sample_permalink_html( $p, null, 'new_slug' ); + $post = get_post( $p ); + + $preview_link = get_permalink( $post->ID ); + $preview_link = add_query_arg( 'preview', 'true', $preview_link ); + + $this->assertContains( "span id='view-post-btn'>set_permalink_structure( $old_permalink_structure ); + flush_rewrite_rules(); + } + /** * @ticket 5305 */