<!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>[49608] trunk: Editor: Move pre_render_block, render_block_data, render_block_context</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/49608">49608</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/49608","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>noisysocks</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-11-16 00:45:55 +0000 (Mon, 16 Nov 2020)</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'>Editor: Move pre_render_block, render_block_data, render_block_context
Move the pre_render_block, render_block_data, and render_block_context
filters from render_block() to WP_Block. This ensures that they are
called for all blocks, including nested blocks, not just top-level
blocks.
Fixes <a href="https://core.trac.wordpress.org/ticket/51612">#51612</a>.
Props gaambo, gziolo, TimothyBlynJacobs.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocksphp">trunk/src/wp-includes/blocks.php</a></li>
<li><a href="#trunksrcwpincludesclasswpblockphp">trunk/src/wp-includes/class-wp-block.php</a></li>
<li><a href="#trunktestsphpunittestsblocksblockphp">trunk/tests/phpunit/tests/blocks/block.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 2020-11-15 17:44:56 UTC (rev 49607)
+++ trunk/src/wp-includes/blocks.php 2020-11-16 00:45:55 UTC (rev 49608)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -662,7 +662,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> global $post, $wp_query;
</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">- * Allows render_block() to be short-circuited, by returning a non-null value.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Allows render_block() or WP_Block::render() to be short-circuited, by
+ * returning a non-null value.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.1.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -674,18 +675,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return $pre_render;
</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">- $source_block = $parsed_block;
-
- /**
- * Filters the block being rendered in render_block(), before it's processed.
- *
- * @since 5.1.0
- *
- * @param array $parsed_block The block being rendered.
- * @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content.
- */
- $parsed_block = apply_filters( 'render_block_data', $parsed_block, $source_block );
-
</del><span class="cx" style="display: block; padding: 0 10px"> $context = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $post instanceof WP_Post ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -707,16 +696,6 @@
</span><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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
- * Filters the default context provided to a rendered block.
- *
- * @since 5.5.0
- *
- * @param array $context Default context.
- * @param array $parsed_block Block being rendered, filtered by `render_block_data`.
- */
- $context = apply_filters( 'render_block_context', $context, $parsed_block );
-
</del><span class="cx" style="display: block; padding: 0 10px"> $block = new WP_Block( $parsed_block, $context );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return $block->render();
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpblockphp"></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/class-wp-block.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-block.php 2020-11-15 17:44:56 UTC (rev 49607)
+++ trunk/src/wp-includes/class-wp-block.php 2020-11-16 00:45:55 UTC (rev 49608)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -11,6 +11,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.5.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @property array $attributes
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @property array $context
+ * @property WP_Block[] $inner_blocks;
+ * @property string $inner_html;
+ * @property array $inner_content;
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> class WP_Block {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -23,6 +27,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public $parsed_block;
</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">+ * All available context of the current hierarchy.
+ *
+ * @since 5.5.0
+ * @var array
+ * @access protected
+ */
+ protected $available_context;
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Name of block.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @example "core/paragraph"
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -41,93 +54,147 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public $block_type;
</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">- * Block context values.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Map of block property names and their cached value.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 5.5.0
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Some block properties are computed lazily using a getter function. The
+ * result is then cached here for subsequent use.
+ *
+ * @since 5.6.0
</ins><span class="cx" style="display: block; padding: 0 10px"> * @var array
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public $context = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected $cached_properties = array();
</ins><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">- * All available context of the current hierarchy.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Creates a block instance from a backing `$parsed_block` array and list of
+ * `$available_context`. From these, the block's dynamic properties can be
+ * derived.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * The given array of context values will not necessarily be available on
+ * the instance itself, but is treated as the full set of values provided by
+ * the block's ancestry. This is assigned to the private `available_context`
+ * property. Only values which are configured to consumed by the block via
+ * its registered type will be assigned to the block's `context` property.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @since 5.5.0
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @var array
- * @access protected
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param array $parsed_block Array of parsed block properties.
+ * @param array $available_context Optional array of ancestry context values.
+ * @param WP_Block_Type_Registry $registry Optional block type registry.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- protected $available_context;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function __construct( $parsed_block, $available_context = array(), $registry = null ) {
+ if ( is_null( $registry ) ) {
+ $this->registry = WP_Block_Type_Registry::get_instance();
+ } else {
+ $this->registry = $registry;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->reset( $parsed_block, $available_context );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * List of inner blocks (of this same class)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Changes the backing `$parsed_block` and `$available_context` used to
+ * derive the block's dynamic properties.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 5.5.0
- * @var WP_Block[]
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.6.0
+
+ * @param array $parsed_block Array of parsed block properties.
+ * @param array $available_context Optional array of ancestry context values.
+ * @param array $cached_properties Optional cache of dynamic properties to use.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public $inner_blocks = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function reset(
+ $parsed_block,
+ $available_context = array(),
+ $cached_properties = array()
+ ) {
+ $this->parsed_block = $parsed_block;
+ $this->available_context = $available_context;
+ $this->name = $parsed_block['blockName'];
+ $this->block_type = $this->registry->get_registered( $this->name );
+ $this->cached_properties = $cached_properties;
+ }
</ins><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">- * Resultant HTML from inside block comment delimiters after removing inner
- * blocks.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Getter used for the block's dynamic properties:
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @example "...Just <!-- wp:test /--> testing..." -> "Just testing..."
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * - `$block->attributes`
+ * - `$block->context`
+ * - `$block->inner_blocks`
+ * - `$block->inner_html`
+ * - `$block->inner_content`
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Each dynamic property is obtained by calling the associated getter
+ * function (e.g. `this->get_attributes()`). The result is then cached in
+ * `$this->cached_attributes` for subsequent calls.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @since 5.5.0
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @var string
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @param string $name Property name.
+ * @return array|null Prepared attributes, or null.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public $inner_html = '';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function __get( $name ) {
+ if ( method_exists( $this, "get_$name" ) ) {
+ if ( ! isset( $this->cached_properties[ $name ] ) ) {
+ $this->cached_properties[ $name ] = $this->{"get_$name"}();
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $this->cached_properties[ $name ];
+ }
+
+ return null;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * List of string fragments and null markers where inner blocks were found
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Block attributes.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @example array(
- * 'inner_html' => 'BeforeInnerAfter',
- * 'inner_blocks' => array( block, block ),
- * 'inner_content' => array( 'Before', null, 'Inner', null, 'After' ),
- * )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Use `$block->attributes` to access this.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 5.5.0
- * @var array
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.6.0
+ * @return array
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public $inner_content = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function get_attributes() {
+ $attributes = isset( $this->parsed_block['attrs'] ) ?
+ $this->parsed_block['attrs'] :
+ array();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! is_null( $this->block_type ) ) {
+ return $this->block_type->prepare_attributes_for_render( $attributes );
+ }
+
+ return $attributes;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Constructor.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Block context values.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Populates object properties from the provided block instance argument.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Use `$block->context` to access this.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * The given array of context values will not necessarily be available on
- * the instance itself, but is treated as the full set of values provided by
- * the block's ancestry. This is assigned to the private `available_context`
- * property. Only values which are configured to consumed by the block via
- * its registered type will be assigned to the block's `context` property.
- *
- * @since 5.5.0
- *
- * @param array $block Array of parsed block properties.
- * @param array $available_context Optional array of ancestry context values.
- * @param WP_Block_Type_Registry $registry Optional block type registry.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.6.0
+ * @return array
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function __construct( $block, $available_context = array(), $registry = null ) {
- $this->parsed_block = $block;
- $this->name = $block['blockName'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function get_context() {
+ $context = array();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( is_null( $registry ) ) {
- $registry = WP_Block_Type_Registry::get_instance();
- }
-
- $this->block_type = $registry->get_registered( $this->name );
-
- $this->available_context = $available_context;
-
</del><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $this->block_type->uses_context ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $this->block_type->uses_context as $context_name ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( array_key_exists( $context_name, $this->available_context ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->context[ $context_name ] = $this->available_context[ $context_name ];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $context[ $context_name ] = $this->available_context[ $context_name ];
</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><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! empty( $block['innerBlocks'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $context;
+ }
+
+ /**
+ * List of inner blocks (of this same class).
+ *
+ * Use `$block->inner_blocks` to access this.
+ *
+ * @since 5.6.0
+ * @return WP_Block[]
+ */
+ protected function get_inner_blocks() {
+ if ( ! empty( $this->parsed_block['innerBlocks'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $child_context = $this->available_context;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $this->block_type->provides_context ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -138,45 +205,55 @@
</span><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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return new WP_Block_List(
+ $this->parsed_block['innerBlocks'],
+ $child_context,
+ $this->registry
+ );
</ins><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">- if ( ! empty( $block['innerHTML'] ) ) {
- $this->inner_html = $block['innerHTML'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return array();
+ }
+
+ /**
+ * Resultant HTML from inside block comment delimiters after removing inner
+ * blocks.
+ *
+ * Use `$block->inner_html` to access this.
+ *
+ * @example "...Just <!-- wp:test /--> testing..." -> "Just testing..."
+ *
+ * @since 5.6.0
+ * @return string
+ */
+ protected function get_inner_html() {
+ if ( ! empty( $this->parsed_block['innerHTML'] ) ) {
+ return $this->parsed_block['innerHTML'];
</ins><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">- if ( ! empty( $block['innerContent'] ) ) {
- $this->inner_content = $block['innerContent'];
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return '';
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Returns a value from an inaccessible property.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * List of string fragments and null markers where inner blocks were found
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * This is used to lazily initialize the `attributes` property of a block,
- * such that it is only prepared with default attributes at the time that
- * the property is accessed. For all other inaccessible properties, a `null`
- * value is returned.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Use `$block->inner_content` to access this.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 5.5.0
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @example array(
+ * 'inner_html' => 'BeforeInnerAfter',
+ * 'inner_blocks' => array( block, block ),
+ * 'inner_content' => array( 'Before', null, 'Inner', null, 'After' ),
+ * )
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $name Property name.
- * @return array|null Prepared attributes, or null.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.6.0
+ * @return array
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function __get( $name ) {
- if ( 'attributes' === $name ) {
- $this->attributes = isset( $this->parsed_block['attrs'] ) ?
- $this->parsed_block['attrs'] :
- array();
-
- if ( ! is_null( $this->block_type ) ) {
- $this->attributes = $this->block_type->prepare_attributes_for_render( $this->attributes );
- }
-
- return $this->attributes;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function get_inner_content() {
+ if ( ! empty( $this->parsed_block['innerContent'] ) ) {
+ return $this->parsed_block['innerContent'];
</ins><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 null;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return array();
</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><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -193,6 +270,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function render( $options = array() ) {
</span><span class="cx" style="display: block; padding: 0 10px"> global $post;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /** This filter is documented in src/wp-includes/blocks.php. */
+ $pre_render = apply_filters( 'pre_render_block', null, $this->parsed_block );
+ if ( ! is_null( $pre_render ) ) {
+ return $pre_render;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $options = wp_parse_args(
</span><span class="cx" style="display: block; padding: 0 10px"> $options,
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -200,7 +284,47 @@
</span><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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $is_dynamic = $options['dynamic'] && $this->name && null !== $this->block_type && $this->block_type->is_dynamic();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $initial_parsed_block = $this->parsed_block;
+ $initial_available_context = $this->available_context;
+ $initial_cached_properties = $this->cached_properties;
+
+ /**
+ * Filters a block which is to be rendered by render_block() or
+ * WP_Block::render().
+ *
+ * @since 5.1.0
+ *
+ * @param array $parsed_block The block being rendered.
+ * @param array $source_block An un-modified copy of $parsed_block, as it appeared in the source content.
+ */
+ $parsed_block = apply_filters(
+ 'render_block_data',
+ $this->parsed_block,
+ $initial_parsed_block
+ );
+
+ /**
+ * Filters the default context of a block which is to be rendered by
+ * render_block() or WP_Block::render().
+ *
+ * @since 5.5.0
+ *
+ * @param array $available_context Default context.
+ * @param array $parsed_block Block being rendered, filtered by `render_block_data`.
+ */
+ $available_context = apply_filters(
+ 'render_block_context',
+ $this->available_context,
+ $this->parsed_block
+ );
+
+ $this->reset( $parsed_block, $available_context );
+
+ $is_dynamic = $options['dynamic']
+ && $this->name
+ && null !== $this->block_type
+ && $this->block_type->is_dynamic();
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $block_content = '';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $options['dynamic'] || empty( $this->block_type->skip_inner_blocks ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -218,7 +342,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> WP_Block_Supports::$block_to_render = $this->parsed_block;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $block_content = (string) call_user_func( $this->block_type->render_callback, $this->attributes, $block_content, $this );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $block_content = (string) call_user_func(
+ $this->block_type->render_callback,
+ $this->attributes,
+ $block_content,
+ $this
+ );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> WP_Block_Supports::$block_to_render = $parent;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -241,7 +370,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $block_content The block content about to be appended.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $block The full block, including name and attributes.
</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 apply_filters( 'render_block', $block_content, $this->parsed_block );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $block_content = apply_filters( 'render_block', $block_content, $this->parsed_block );
+
+ $this->reset(
+ $initial_parsed_block,
+ $initial_available_context,
+ $initial_cached_properties
+ );
+
+ return $block_content;
</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></span></pre></div>
<a id="trunktestsphpunittestsblocksblockphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/blocks/block.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/block.php 2020-11-15 17:44:56 UTC (rev 49607)
+++ trunk/tests/phpunit/tests/blocks/block.php 2020-11-16 00:45:55 UTC (rev 49608)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -45,6 +45,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return 'Original: "' . $content . '", from block "' . $parsed_block['blockName'] . '"';
</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">+ function filter_pre_render_block( $pre_render, $parsed_block ) {
+ if ( 'core/skip' === $parsed_block['blockName'] ) {
+ return 'Hello world!';
+ }
+ return null;
+ }
+
+ function filter_render_block_data( $parsed_block, $source_block ) {
+ $parsed_block['attrs']['tag'] = $parsed_block['attrs']['tag'] . '-filtered';
+ return $parsed_block;
+ }
+
+ function filter_render_block_context( $available_context, $parsed_block ) {
+ $available_context['core/recordId'] += 1;
+ return $available_context;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 49927
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -394,4 +411,127 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'abc', $block->render() );
</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">+ /**
+ * @ticket 51612
+ */
+ function test_applies_pre_render_block_filter() {
+ $this->registry->register( 'core/skip', array() );
+
+ add_filter( 'pre_render_block', array( $this, 'filter_pre_render_block' ), 10, 2 );
+
+ $parsed_blocks = parse_blocks( '<!-- wp:skip /-->' );
+ $parsed_block = $parsed_blocks[0];
+ $context = array();
+ $block = new WP_Block( $parsed_block, $context, $this->registry );
+
+ $rendered_content = $block->render();
+
+ remove_filter( 'pre_render_block', array( $this, 'filter_pre_render_block' ) );
+
+ $this->assertSame( 'Hello world!', $rendered_content );
+ }
+
+ /**
+ * @ticket 51612
+ */
+ function test_applies_pre_render_block_filter_to_inner_blocks() {
+ $this->registry->register( 'core/outer', array() );
+ $this->registry->register( 'core/skip', array() );
+
+ add_filter( 'pre_render_block', array( $this, 'filter_pre_render_block' ), 10, 2 );
+
+ $parsed_blocks = parse_blocks( '<!-- wp:outer --><!-- wp:skip /--> How are you?<!-- /wp:outer -->' );
+ $parsed_block = $parsed_blocks[0];
+ $context = array();
+ $block = new WP_Block( $parsed_block, $context, $this->registry );
+
+ $rendered_content = $block->render();
+
+ remove_filter( 'pre_render_block', array( $this, 'filter_pre_render_block' ) );
+
+ $this->assertSame( 'Hello world! How are you?', $rendered_content );
+ }
+
+ /**
+ * @ticket 51612
+ */
+ function test_applies_render_block_data_filter() {
+ $this->registry->register(
+ 'core/wrapper',
+ array(
+ 'attributes' => array(
+ 'tag' => array(
+ 'type' => 'string',
+ ),
+ ),
+ 'render_callback' => function( $block_attributes, $content ) {
+ return sprintf(
+ '<%1$s>%2$s</%1$s>',
+ $block_attributes['tag'],
+ $content
+ );
+ },
+ )
+ );
+
+ add_filter( 'render_block_data', array( $this, 'filter_render_block_data' ), 10, 2 );
+
+ $parsed_blocks = parse_blocks( '<!-- wp:wrapper {"tag":"outer"} --><!-- wp:wrapper {"tag":"inner"} -->Hello!<!-- /wp:wrapper --><!-- /wp:wrapper -->' );
+ $parsed_block = $parsed_blocks[0];
+ $context = array();
+ $block = new WP_Block( $parsed_block, $context, $this->registry );
+
+ $rendered_content = $block->render();
+
+ remove_filter( 'render_block_data', array( $this, 'filter_render_block_data' ) );
+
+ $this->assertSame( '<outer-filtered><inner-filtered>Hello!</inner-filtered></outer-filtered>', $rendered_content );
+ }
+
+ /**
+ * @ticket 51612
+ */
+ function test_applies_render_block_context_filter() {
+ $this->registry->register(
+ 'core/provider',
+ array(
+ 'attributes' => array(
+ 'recordId' => array(
+ 'type' => 'number',
+ ),
+ ),
+ 'uses_context' => array( 'core/recordId' ),
+ 'provides_context' => array(
+ 'core/recordId' => 'recordId',
+ ),
+ )
+ );
+ $this->registry->register(
+ 'core/consumer',
+ array(
+ 'uses_context' => array( 'core/recordId' ),
+ 'render_callback' => function( $block_attributes, $content, $block ) {
+ return sprintf( 'Record ID: %d ', $block->context['core/recordId'] );
+ },
+ )
+ );
+
+ add_filter( 'render_block_context', array( $this, 'filter_render_block_context' ), 10, 2 );
+
+ $parsed_blocks = parse_blocks( '<!-- wp:consumer /--><!-- wp:provider {"recordId":20} --><!-- wp:consumer /--><!-- /wp:provider -->' );
+ $context = array( 'core/recordId' => 10 );
+
+ $rendered_content = '';
+
+ foreach ( $parsed_blocks as $parsed_block ) {
+ $block = new WP_Block( $parsed_block, $context, $this->registry );
+
+ $rendered_content .= $block->render();
+ }
+
+ remove_filter( 'render_block_context', array( $this, 'filter_render_block_context' ) );
+
+ $this->assertSame( 'Record ID: 11 Record ID: 21 ', $rendered_content );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>