<!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>[57026] trunk: Blocks: Parse the arguments earlier in `register_block_type_from_metadata()`.</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/57026">57026</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/57026","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>SergeyBiryukov</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-10-28 01:00:14 +0000 (Sat, 28 Oct 2023)</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'>Blocks: Parse the arguments earlier in `register_block_type_from_metadata()`.
This makes it possible to register a block by passing an array of arguments, without the presence of a `block.json` file.
Follow-up to <a href="https://core.trac.wordpress.org/changeset/48141">[48141]</a>, <a href="https://core.trac.wordpress.org/changeset/49948">[49948]</a>.
Props aristath, spacedmonkey, mukesh27, costdev, audrasjb, oglekler, felipeelia, hellofromTonya.
Fixes <a href="https://core.trac.wordpress.org/ticket/56865">#56865</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocksphp">trunk/src/wp-includes/blocks.php</a></li>
<li><a href="#trunktestsphpunittestsblocksregisterphp">trunk/tests/phpunit/tests/blocks/register.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 2023-10-27 20:56:59 UTC (rev 57025)
+++ trunk/src/wp-includes/blocks.php 2023-10-28 01:00:14 UTC (rev 57026)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -352,13 +352,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $file_or_folder;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $is_core_block = str_starts_with( $file_or_folder, ABSPATH . WPINC );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- if ( ! $is_core_block && ! file_exists( $metadata_file ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // If the block is not a core block, the metadata file must exist.
+ $metadata_file_exists = $is_core_block || file_exists( $metadata_file );
+ if ( ! $metadata_file_exists && empty( $args['name'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</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"> // Try to get metadata from the static cache for core blocks.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $metadata = false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $metadata = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $is_core_block ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $core_blocks_meta[ $core_block_name ] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -367,15 +368,16 @@
</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"> // If metadata is not found in the static cache, read it from the file.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! $metadata ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $metadata_file_exists && empty( $metadata ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
</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 ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! is_array( $metadata ) || ( empty( $metadata['name'] ) && empty( $args['name'] ) ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $metadata['file'] = wp_normalize_path( realpath( $metadata_file ) );
</del><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $metadata['file'] = $metadata_file_exists ? wp_normalize_path( realpath( $metadata_file ) ) : null;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the metadata provided for registering a block type.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -404,6 +406,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $settings = array();
</span><span class="cx" style="display: block; padding: 0 10px"> $property_mappings = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'apiVersion' => 'api_version',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'name' => 'name',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'title' => 'title',
</span><span class="cx" style="display: block; padding: 0 10px"> 'category' => 'category',
</span><span class="cx" style="display: block; padding: 0 10px"> 'parent' => 'parent',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -426,12 +429,41 @@
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $property_mappings as $key => $mapped_key ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $metadata[ $key ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $settings[ $mapped_key ] = $metadata[ $key ];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $textdomain && isset( $i18n_schema->$key ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $metadata_file_exists && $textdomain && isset( $i18n_schema->$key ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $settings[ $mapped_key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $settings[ $key ], $textdomain );
</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><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $metadata['render'] ) ) {
+ $template_path = wp_normalize_path(
+ realpath(
+ dirname( $metadata['file'] ) . '/' .
+ remove_block_asset_path_prefix( $metadata['render'] )
+ )
+ );
+ if ( $template_path ) {
+ /**
+ * Renders the block on the server.
+ *
+ * @since 6.1.0
+ *
+ * @param array $attributes Block attributes.
+ * @param string $content Block default content.
+ * @param WP_Block $block Block instance.
+ *
+ * @return string Returns the block content.
+ */
+ $settings['render_callback'] = static function ( $attributes, $content, $block ) use ( $template_path ) {
+ ob_start();
+ require $template_path;
+ return ob_get_clean();
+ };
+ }
+ }
+
+ $settings = array_merge( $settings, $args );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $script_fields = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'editorScript' => 'editor_script_handles',
</span><span class="cx" style="display: block; padding: 0 10px"> 'script' => 'script_handles',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -438,6 +470,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'viewScript' => 'view_script_handles',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $script_fields as $metadata_field_name => $settings_field_name ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $settings[ $metadata_field_name ] ) ) {
+ $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ];
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $scripts = $metadata[ $metadata_field_name ];
</span><span class="cx" style="display: block; padding: 0 10px"> $processed_scripts = array();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -470,6 +505,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'style' => 'style_handles',
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $style_fields as $metadata_field_name => $settings_field_name ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $settings[ $metadata_field_name ] ) ) {
+ $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ];
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $styles = $metadata[ $metadata_field_name ];
</span><span class="cx" style="display: block; padding: 0 10px"> $processed_styles = array();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -530,33 +568,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">- if ( ! empty( $metadata['render'] ) ) {
- $template_path = wp_normalize_path(
- realpath(
- dirname( $metadata['file'] ) . '/' .
- remove_block_asset_path_prefix( $metadata['render'] )
- )
- );
- if ( $template_path ) {
- /**
- * Renders the block on the server.
- *
- * @since 6.1.0
- *
- * @param array $attributes Block attributes.
- * @param string $content Block default content.
- * @param WP_Block $block Block instance.
- *
- * @return string Returns the block content.
- */
- $settings['render_callback'] = static function ( $attributes, $content, $block ) use ( $template_path ) {
- ob_start();
- require $template_path;
- return ob_get_clean();
- };
- }
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the settings determined from the block type metadata.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -565,15 +576,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $settings Array of determined settings for registering a block type.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $metadata Metadata provided for registering a block type.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $settings = apply_filters(
- 'block_type_metadata_settings',
- array_merge(
- $settings,
- $args
- ),
- $metadata
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $settings = apply_filters( 'block_type_metadata_settings', $settings, $metadata );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $metadata['name'] = ! empty( $settings['name'] ) ? $settings['name'] : $metadata['name'];
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return WP_Block_Type_Registry::get_instance()->register(
</span><span class="cx" style="display: block; padding: 0 10px"> $metadata['name'],
</span><span class="cx" style="display: block; padding: 0 10px"> $settings
</span></span></pre></div>
<a id="trunktestsphpunittestsblocksregisterphp"></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/register.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/register.php 2023-10-27 20:56:59 UTC (rev 57025)
+++ trunk/tests/phpunit/tests/blocks/register.php 2023-10-28 01:00:14 UTC (rev 57026)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -600,6 +600,126 @@
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Tests registering a block using arguments instead of a block.json file.
+ *
+ * @ticket 56865
+ *
+ * @covers ::register_block_type_from_metadata
+ */
+ public function test_register_block_type_from_metadata_with_arguments() {
+ $result = register_block_type_from_metadata(
+ '',
+ array(
+ 'api_version' => 2,
+ 'name' => 'tests/notice-from-array',
+ 'title' => 'Notice from array',
+ 'category' => 'common',
+ 'icon' => 'star',
+ 'description' => 'Shows warning, error or success notices… (registered from an array)',
+ 'keywords' => array(
+ 'alert',
+ 'message',
+ ),
+ 'textdomain' => 'notice-from-array',
+ )
+ );
+
+ $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' );
+ $this->assertSame( 2, $result->api_version, 'The API version is incorrect' );
+ $this->assertSame( 'tests/notice-from-array', $result->name, 'The block name is incorrect' );
+ $this->assertSame( 'Notice from array', $result->title, 'The block title is incorrect' );
+ $this->assertSame( 'common', $result->category, 'The block category is incorrect' );
+ $this->assertSame( 'star', $result->icon, 'The block icon is incorrect' );
+ $this->assertSame(
+ 'Shows warning, error or success notices… (registered from an array)',
+ $result->description,
+ 'The block description is incorrect'
+ );
+ $this->assertSameSets( array( 'alert', 'message' ), $result->keywords, 'The block keywords are incorrect' );
+ }
+
+ /**
+ * Tests that defined $args can properly override the block.json file.
+ *
+ * @ticket 56865
+ *
+ * @covers ::register_block_type_from_metadata
+ */
+ public function test_block_registers_with_args_override() {
+ $result = register_block_type_from_metadata(
+ DIR_TESTDATA . '/blocks/notice',
+ array(
+ 'name' => 'tests/notice-with-overrides',
+ 'title' => 'Overriden title',
+ 'style' => array( 'tests-notice-style-overridden' ),
+ )
+ );
+
+ $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' );
+ $this->assertSame( 2, $result->api_version, 'The API version is incorrect' );
+ $this->assertSame( 'tests/notice-with-overrides', $result->name, 'The block name was not overridden' );
+ $this->assertSame( 'Overriden title', $result->title, 'The block title was not overridden' );
+ $this->assertSameSets(
+ array( 'tests-notice-editor-script' ),
+ $result->editor_script_handles,
+ 'The block editor script is incorrect'
+ );
+ $this->assertSameSets(
+ array( 'tests-notice-style-overridden' ),
+ $result->style_handles,
+ 'The block style was not overridden'
+ );
+ $this->assertIsCallable( $result->render_callback );
+ }
+
+ /**
+ * Tests that when the `name` is missing, `register_block_type_from_metadata()`
+ * will return `false`.
+ *
+ * @ticket 56865
+ *
+ * @covers ::register_block_type_from_metadata
+ *
+ * @dataProvider data_register_block_registers_with_args_override_returns_false_when_name_is_missing
+ *
+ * @param string $file The metadata file.
+ * @param array $args Array of block type arguments.
+ */
+ public function test_block_registers_with_args_override_returns_false_when_name_is_missing( $file, $args ) {
+ $this->assertFalse( register_block_type_from_metadata( $file, $args ) );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public function data_register_block_registers_with_args_override_returns_false_when_name_is_missing() {
+ return array(
+ 'no block.json file and no name argument' => array(
+ 'file' => '', // No block.json file.
+ 'args' => array(
+ 'title' => 'Overriden title',
+ 'style' => array( 'tests-notice-style-overridden' ),
+ ),
+ ),
+ 'existing file and args not an array' => array(
+ // A file that exists but is empty. This will bypass the file_exists() check.
+ 'file' => DIR_TESTDATA . '/blocks/notice/block.js',
+ 'args' => false,
+ ),
+ 'existing file and args[name] missing' => array(
+ // A file that exists but is empty. This will bypass the file_exists() check.
+ 'file' => DIR_TESTDATA . '/blocks/notice/block.js',
+ 'args' => array(
+ 'title' => 'Overriden title',
+ 'style' => array( 'tests-notice-style-overridden' ),
+ ),
+ ),
+ );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Tests that the function returns the registered block when the `block.json`
</span><span class="cx" style="display: block; padding: 0 10px"> * is found in the fixtures directory.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span></span></pre>
</div>
</div>
</body>
</html>