diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index b609ee725f954..e10a06947e1c7 100644 --- a/src/wp-includes/post-template.php +++ b/src/wp-includes/post-template.php @@ -1078,7 +1078,7 @@ function _wp_link_page( $i ) { if ( 1 === $i ) { $url = get_permalink(); } else { - if ( ! get_option( 'permalink_structure' ) || in_array( $post->post_status, array( 'draft', 'pending' ), true ) ) { + if ( ! get_option( 'permalink_structure' ) || in_array( $post->post_status, array( 'draft', 'pending', 'future' ), true ) ) { $url = add_query_arg( 'page', $i, get_permalink() ); } elseif ( 'page' === get_option( 'show_on_front' ) && (int) get_option( 'page_on_front' ) === $post->ID ) { $url = trailingslashit( get_permalink() ) . user_trailingslashit( "$wp_rewrite->pagination_base/" . $i, 'single_paged' ); diff --git a/tests/phpunit/tests/post/wpLinkPages.php b/tests/phpunit/tests/post/wpLinkPages.php index 861ffb9fa8cc6..96dc671bd0285 100644 --- a/tests/phpunit/tests/post/wpLinkPages.php +++ b/tests/phpunit/tests/post/wpLinkPages.php @@ -133,4 +133,63 @@ public function test_wp_link_pages() { $this->assertSame( $pagelink, $output ); } + + /** + * Tests the URL of a subpage of a post that is split with the nextpage tag. + * + * The permalink of a post that is not published is a plain `?p=ID` URL, + * so the page number must be added as a query argument as well. + * + * @ticket 32295 + * + * @dataProvider data_post_statuses + * + * @param string $post_status Post status. + * @param bool $uses_query_arg Whether the page number is expected as a query argument. + */ + public function test_subpage_url_with_pretty_permalinks( $post_status, $uses_query_arg ) { + $this->set_permalink_structure( '/%postname%/' ); + + $post_args = array( + 'post_status' => $post_status, + 'post_content' => 'OneTwoThree', + ); + + // A post is only scheduled when its date is in the future. + if ( 'future' === $post_status ) { + $post_args['post_date'] = gmdate( 'Y-m-d H:i:s', strtotime( '+1 day' ) ); + $post_args['post_date_gmt'] = $post_args['post_date']; + } + + $post_id = self::factory()->post->create( $post_args ); + + $this->assertSame( $post_status, get_post_status( $post_id ), 'The post should have the expected status.' ); + + $GLOBALS['post'] = get_post( $post_id ); + setup_postdata( $GLOBALS['post'] ); + + $permalink = get_permalink( $post_id ); + + if ( $uses_query_arg ) { + $expected_url = add_query_arg( 'page', 2, $permalink ); + } else { + $expected_url = trailingslashit( $permalink ) . user_trailingslashit( 2, 'single_paged' ); + } + + $this->assertSame( '', _wp_link_page( 2 ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_post_statuses() { + return array( + 'draft' => array( 'draft', true ), + 'pending' => array( 'pending', true ), + 'scheduled' => array( 'future', true ), + 'published' => array( 'publish', false ), + ); + } }