<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[45265] trunk: Fix parsing of inner blocks when auto-generating an excerpt.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/45265">45265</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/45265","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>azaozz</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-04-24 21:38:21 +0000 (Wed, 24 Apr 2019)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Fix parsing of inner blocks when auto-generating an excerpt. Helps to prevent cases where dynamic inner blocks may cause an infinite loop if trying to auto-generate an excerpt.

Props desrosj, pento, gziolo, azaozz.
Fixes <a href="https://core.trac.wordpress.org/ticket/46133">#46133</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocksphp">trunk/src/wp-includes/blocks.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsformattingExcerptRemoveBlocksphp">trunk/tests/phpunit/tests/formatting/ExcerptRemoveBlocks.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesblocksphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/blocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/blocks.php  2019-04-24 18:36:58 UTC (rev 45264)
+++ trunk/src/wp-includes/blocks.php    2019-04-24 21:38:21 UTC (rev 45265)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -125,10 +125,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @return string The parsed and filtered content.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function excerpt_remove_blocks( $content ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $allowed_blocks = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $allowed_inner_blocks = array(
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Classic blocks have their blockName set to null.
</span><span class="cx" style="display: block; padding: 0 10px">                null,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                'core/columns',
</del><span class="cx" style="display: block; padding: 0 10px">                 'core/freeform',
</span><span class="cx" style="display: block; padding: 0 10px">                'core/heading',
</span><span class="cx" style="display: block; padding: 0 10px">                'core/html',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -141,6 +140,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'core/table',
</span><span class="cx" style="display: block; padding: 0 10px">                'core/verse',
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       $allowed_blocks = array_merge( $allowed_inner_blocks, array( 'core/columns' ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Filters the list of blocks that can contribute to the excerpt.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -154,15 +156,58 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $allowed_blocks = apply_filters( 'excerpt_allowed_blocks', $allowed_blocks );
</span><span class="cx" style="display: block; padding: 0 10px">        $blocks         = parse_blocks( $content );
</span><span class="cx" style="display: block; padding: 0 10px">        $output         = '';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">         foreach ( $blocks as $block ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( in_array( $block['blockName'], $allowed_blocks, true ) ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        if ( ! empty( $block['innerBlocks'] ) ) {
+                               if ( 'core/columns' === $block['blockName'] ) {
+                                       $output .= _excerpt_render_inner_columns_blocks( $block, $allowed_inner_blocks );
+                                       continue;
+                               }
+
+                               // Skip the block if it has disallowed or nested inner blocks.
+                               foreach ( $block['innerBlocks'] as $inner_block ) {
+                                       if (
+                                               ! in_array( $inner_block['blockName'], $allowed_inner_blocks, true ) ||
+                                               ! empty( $inner_block['innerBlocks'] )
+                                       ) {
+                                               continue 2;
+                                       }
+                               }
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         $output .= render_block( $block );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         return $output;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       return $output;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Render inner blocks from the `core/columns` block for generating an excerpt.
+ *
+ * @since 5.2.0
+ * @access private
+ *
+ * @param array $columns        The parsed columns block.
+ * @param array $allowed_blocks The list of allowed inner blocks.
+ * @return string The rendered inner blocks.
+ */
+function _excerpt_render_inner_columns_blocks( $columns, $allowed_blocks ) {
+       $output = '';
+
+       foreach ( $columns['innerBlocks'] as $column ) {
+               foreach ( $column['innerBlocks'] as $inner_block ) {
+                       if ( in_array( $inner_block['blockName'], $allowed_blocks, true ) && empty( $inner_block['innerBlocks'] ) ) {
+                               $output .= render_block( $inner_block );
+                       }
+               }
+       }
+
+       return $output;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Renders a single block into a HTML string.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.0.0
</span></span></pre></div>
<a id="trunktestsphpunittestsformattingExcerptRemoveBlocksphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/formatting/ExcerptRemoveBlocks.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/formatting/ExcerptRemoveBlocks.php                              (rev 0)
+++ trunk/tests/phpunit/tests/formatting/ExcerptRemoveBlocks.php        2019-04-24 21:38:21 UTC (rev 45265)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,129 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group formatting
+ * @covers ::excerpt_remove_blocks
+ * @ticket 46133
+ */
+class Tests_Formatting_ExcerptRemoveBlocks extends WP_UnitTestCase {
+
+       public static $post_id;
+
+       public $content = '
+<!-- wp:paragraph -->
+<p>paragraph</p>
+<!-- /wp:paragraph -->
+<!-- wp:latest-posts {"postsToShow":3,"displayPostDate":true,"order":"asc","orderBy":"title"} /-->
+<!-- wp:spacer -->
+<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
+<!-- /wp:spacer -->
+<!-- wp:columns {"columns":1} -->
+<div class="wp-block-columns has-1-columns">
+       <!-- wp:column -->
+       <div class="wp-block-column">
+               <!-- wp:archives {"displayAsDropdown":false,"showPostCounts":false} /-->
+               
+               <!-- wp:paragraph -->
+               <p>paragraph inside column</p>
+               <!-- /wp:paragraph -->
+       </div>
+       <!-- /wp:column -->
+</div>
+<!-- /wp:columns -->
+';
+
+       public $filtered_content = '
+
+<p>paragraph</p>
+
+
+
+
+               <p>paragraph inside column</p>
+               
+';
+
+       /**
+        * Fake block rendering function.
+        *
+        * @since 5.2.0
+        *
+        * @return string Block output.
+        */
+       function render_fake_block() {
+               return get_the_excerpt( self::$post_id );
+       }
+
+       /**
+        * Set up.
+        *
+        * @since 5.2.0
+        */
+       function setUp() {
+               parent::setUp();
+               self::$post_id = $this->factory()->post->create(
+                       array(
+                               'post_excerpt' => '', // Empty excerpt, so it has to be generated.
+                               'post_content' => '<!-- wp:core/fake /-->',
+                       )
+               );
+               register_block_type(
+                       'core/fake',
+                       array(
+                               'render_callback' => array( $this, 'render_fake_block' ),
+                       )
+               );
+       }
+
+       /**
+        * Tear down.
+        *
+        * @since 5.2.0
+        */
+       function tearDown() {
+               parent::tearDown();
+               $registry = WP_Block_Type_Registry::get_instance();
+               $registry->unregister( 'core/fake' );
+               wp_delete_post( self::$post_id, true );
+       }
+
+       /**
+        * Tests excerpt_remove_blocks().
+        *
+        * @ticket 46133
+        */
+       function test_excerpt_remove_blocks() {
+               // Simple dynamic block..
+               $content = '<!-- wp:core/block /-->';
+
+               $this->assertEmpty( excerpt_remove_blocks( $content ) );
+
+               // Dynamic block with options, embedded in other content.
+               $this->assertEquals( $this->filtered_content, excerpt_remove_blocks( $this->content ) );
+       }
+
+       /**
+        * Tests that dynamic blocks don't cause an out-of-memory error.
+        *
+        * When dynamic blocks happen to generate an excerpt, they can cause an
+        * infinite loop if that block is part of the post's content.
+        *
+        * `wp_trim_excerpt()` applies the `the_content` filter, which has
+        * `do_blocks` attached to it, trying to render the block which again will
+        * attempt to return an excerpt of that post.
+        *
+        * This infinite loop can be avoided by stripping dynamic blocks before
+        * `the_content` gets applied, just like shortcodes.
+        *
+        * @ticket 46133
+        */
+       function test_excerpt_infinite_loop() {
+               $query = new WP_Query(
+                       array(
+                               'post__in' => array( self::$post_id ),
+                       )
+               );
+               $query->the_post();
+               $this->assertEmpty( do_blocks( '<!-- wp:core/fake /-->' ) );
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>