diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php
index 3765e9873c..dd9e22d920 100644
--- a/src/wp-includes/post-template.php
+++ b/src/wp-includes/post-template.php
@@ -314,7 +314,13 @@ function get_the_content( $more_link_text = null, $strip_teaser = false, $post =
$page_no = $elements['page'];
$content = $elements['pages'][ $page_no - 1 ];
if ( preg_match( '//', $content, $matches ) ) {
+ if ( has_block( 'more', $content ) ) {
+ // Remove the core/more block delimiters. They will be left over after $content is split up.
+ $content = preg_replace( '//', '', $content );
+ }
+
$content = explode( $matches[0], $content, 2 );
+
if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) ) {
$more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) );
}
diff --git a/tests/phpunit/tests/post/output.php b/tests/phpunit/tests/post/output.php
index b6c402ba89..02be7ffa6e 100644
--- a/tests/phpunit/tests/post/output.php
+++ b/tests/phpunit/tests/post/output.php
@@ -175,4 +175,183 @@ EOF;
kses_remove_filters();
}
+
+ /**
+ * Ensure the_content handles a More block on a singular page.
+ *
+ * @ticket 46471
+ *
+ * @group blocks
+ */
+ public function test_the_content_should_handle_more_block_on_singular() {
+ $post_content = << Teaser part. Second block. Second block.
Second block.
+EOF; + + $this->go_to( get_permalink( $post_id ) ); + $this->assertTrue( is_singular() ); + $this->assertTrue( have_posts() ); + $this->assertNull( the_post() ); + + // Without the teaser. + $actual = get_echo( 'the_content', array( null, true ) ); + $this->assertSame( strip_ws( $expected_without_teaser ), strip_ws( $actual ) ); + + // With the teaser. + $actual = get_echo( 'the_content', array( null, false ) ); + $this->assertSame( strip_ws( $expected_with_teaser ), strip_ws( $actual ) ); + } + + /** + * Ensure the_content handles a More block when using the noteaser text tag on a singular page. + * + * @ticket 46471 + * + * @group blocks + */ + public function test_the_content_should_handle_more_block_when_noteaser_on_singular() { + $post_content = <<Teaser part.
+ + + + + + + + +Second block.
+ +EOF; + + $post_id = self::factory()->post->create( compact( 'post_content' ) ); + + $expected = <<Second block.
+EOF; + + $this->go_to( get_permalink( $post_id ) ); + $this->assertTrue( is_singular() ); + $this->assertTrue( have_posts() ); + $this->assertNull( the_post() ); + + $actual = get_echo( 'the_content', array( null, true ) ); + $this->assertSame( strip_ws( $expected ), strip_ws( $actual ) ); + + $actual = get_echo( 'the_content', array( null, false ) ); + $this->assertSame( strip_ws( $expected ), strip_ws( $actual ) ); + } + + /** + * Ensure the_content displays the teaser part with a read more link + * for a More block on a non-singular page. + * + * @ticket 46471 + * + * @group blocks + */ + public function test_the_content_should_handle_more_block_when_non_singular() { + $post_content = <<Teaser part.
+ + + + + + + +Second block.
+ +EOF; + + $post_id = self::factory()->post->create( compact( 'post_content' ) ); + + $expected = <<Second block.
+EOF; + + $this->go_to( home_url() ); + $this->assertFalse( is_singular() ); + $this->assertTrue( have_posts() ); + $this->assertNull( the_post() ); + + foreach ( array( true, false ) as $strip_teaser ) { + $actual = get_echo( 'the_content', array( null, $strip_teaser ) ); + $this->assertContains( 'Teaser part', $actual ); + $this->assertContains( 'Read More', $actual ); + $this->assertNotContains( '', $actual ); + $this->assertNotContains( 'wp:more', $actual ); + $this->assertNotContains( 'wp:paragraph', $actual ); + } + } + + /** + * Ensure the_content displays the teaser part with a read more link for a More block + * when using the noteaser text tag on a non-singular page. + * + * @ticket 46471 + * + * @group blocks + */ + public function test_the_content_should_handle_more_block_when_noteaser_on_non_singular() { + $post_content = <<Teaser part.
+ + + + + + + + +Second block.
+ +EOF; + + $post_id = self::factory()->post->create( compact( 'post_content' ) ); + + $this->go_to( home_url() ); + $this->assertFalse( is_singular() ); + $this->assertTrue( have_posts() ); + $this->assertNull( the_post() ); + + foreach ( array( true, false ) as $strip_teaser ) { + $actual = get_echo( 'the_content', array( null, $strip_teaser ) ); + $this->assertContains( 'Teaser part', $actual ); + $this->assertContains( '(more…)', $actual ); + $this->assertNotContains( '', $actual ); + $this->assertNotContains( '', $actual ); // We placed the noteaser tag below the more tag. + $this->assertNotContains( 'wp:more', $actual ); + $this->assertNotContains( 'wp:paragraph', $actual ); + } + } }