<!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>[57565] trunk: Editor: Add `viewScriptModule` handling to `block.json` 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/57565">57565</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/57565","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>gziolo</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-02-08 10:39:24 +0000 (Thu, 08 Feb 2024)</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: Add `viewScriptModule` handling to `block.json` metadata
Syncing changes from the Gutenberg plugin: https://github.com/WordPress/gutenberg/pull/57437.
Scripts and styles can be registered for blocks via `block.json` metadata. There is now a Modules API, but was no way to register or associate module assets with blocks via `block.json`.
Fixes <a href="https://core.trac.wordpress.org/ticket/60233">#60233</a>.
Props jonsurrell, gziolo, cbravobernal, luisherranz, youknowriad.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocksphp">trunk/src/wp-includes/blocks.php</a></li>
<li><a href="#trunksrcwpincludesclasswpblocktypephp">trunk/src/wp-includes/class-wp-block-type.php</a></li>
<li><a href="#trunksrcwpincludesclasswpblockphp">trunk/src/wp-includes/class-wp-block.php</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestblocktypescontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php</a></li>
<li><a href="#trunktestsphpunitdatablocksnoticeblockjson">trunk/tests/phpunit/data/blocks/notice/block.json</a></li>
<li><a href="#trunktestsphpunittestsblocksregisterphp">trunk/tests/phpunit/tests/blocks/register.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestblocktypecontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.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 2024-02-08 09:24:10 UTC (rev 57564)
+++ trunk/src/wp-includes/blocks.php 2024-02-08 10:39:24 UTC (rev 57565)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -36,6 +36,7 @@
</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"> * @since 6.1.0 Added `$index` parameter.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.5.0 Added support for `viewScriptModule` field.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $block_name Name of the block.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $field_name Name of the metadata field.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -52,6 +53,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( str_starts_with( $field_name, 'view' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $asset_handle .= '-view';
</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 ( str_ends_with( strtolower( $field_name ), 'scriptmodule' ) ) {
+ $asset_handle .= '-script-module';
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $index > 0 ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $asset_handle .= '-' . ( $index + 1 );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -59,12 +63,13 @@
</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"> $field_mappings = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'editorScript' => 'editor-script',
- 'script' => 'script',
- 'viewScript' => 'view-script',
- 'editorStyle' => 'editor-style',
- 'style' => 'style',
- 'viewStyle' => 'view-style',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'editorScript' => 'editor-script',
+ 'editorStyle' => 'editor-style',
+ 'script' => 'script',
+ 'style' => 'style',
+ 'viewScript' => 'view-script',
+ 'viewScriptModule' => 'view-script-module',
+ 'viewStyle' => 'view-style',
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $asset_handle = str_replace( '/', '-', $block_name ) .
</span><span class="cx" style="display: block; padding: 0 10px"> '-' . $field_mappings[ $field_name ];
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -123,6 +128,62 @@
</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">+ * Finds a script module ID for the selected block metadata field. It detects
+ * when a path to file was provided and optionally finds a corresponding asset
+ * file with details necessary to register the script module under with an
+ * automatically generated module ID. It returns unprocessed script module
+ * ID otherwise.
+ *
+ * @since 6.5.0
+ *
+ * @param array $metadata Block metadata.
+ * @param string $field_name Field name to pick from metadata.
+ * @param int $index Optional. Index of the script module ID to register when multiple
+ * items passed. Default 0.
+ * @return string|false Script module ID or false on failure.
+ */
+function register_block_script_module_id( $metadata, $field_name, $index = 0 ) {
+ if ( empty( $metadata[ $field_name ] ) ) {
+ return false;
+ }
+
+ $module_id = $metadata[ $field_name ];
+ if ( is_array( $module_id ) ) {
+ if ( empty( $module_id[ $index ] ) ) {
+ return false;
+ }
+ $module_id = $module_id[ $index ];
+ }
+
+ $module_path = remove_block_asset_path_prefix( $module_id );
+ if ( $module_id === $module_path ) {
+ return $module_id;
+ }
+
+ $path = dirname( $metadata['file'] );
+ $module_asset_raw_path = $path . '/' . substr_replace( $module_path, '.asset.php', - strlen( '.js' ) );
+ $module_id = generate_block_asset_handle( $metadata['name'], $field_name, $index );
+ $module_asset_path = wp_normalize_path(
+ realpath( $module_asset_raw_path )
+ );
+
+ $module_path_norm = wp_normalize_path( realpath( $path . '/' . $module_path ) );
+ $module_uri = get_block_asset_url( $module_path_norm );
+
+ $module_asset = ! empty( $module_asset_path ) ? require $module_asset_path : array();
+ $module_dependencies = isset( $module_asset['dependencies'] ) ? $module_asset['dependencies'] : array();
+
+ wp_register_script_module(
+ $module_id,
+ $module_uri,
+ $module_dependencies,
+ isset( $module_asset['version'] ) ? $module_asset['version'] : false
+ );
+
+ return $module_id;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Finds a script handle for the selected block metadata field. It detects
</span><span class="cx" style="display: block; padding: 0 10px"> * when a path to file was provided and optionally finds a corresponding asset
</span><span class="cx" style="display: block; padding: 0 10px"> * file with details necessary to register the script under automatically
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -314,7 +375,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.1.0 Added support for `render` field.
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.3.0 Added `selectors` field.
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.4.0 Added support for `blockHooks` field.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 6.5.0 Added support for `allowedBlocks` and `viewStyle` fields.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.5.0 Added support for `allowedBlocks`, `viewScriptModule`, and `viewStyle` fields.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $file_or_folder Path to the JSON file with metadata definition for
</span><span class="cx" style="display: block; padding: 0 10px"> * the block or path to the folder where the `block.json` file is located.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -490,6 +551,40 @@
</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">+ $module_fields = array(
+ 'viewScriptModule' => 'view_script_module_ids',
+ );
+ foreach ( $module_fields as $metadata_field_name => $settings_field_name ) {
+ if ( ! empty( $settings[ $metadata_field_name ] ) ) {
+ $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ];
+ }
+ if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
+ $modules = $metadata[ $metadata_field_name ];
+ $processed_modules = array();
+ if ( is_array( $modules ) ) {
+ for ( $index = 0; $index < count( $modules ); $index++ ) {
+ $result = register_block_script_module_id(
+ $metadata,
+ $metadata_field_name,
+ $index
+ );
+ if ( $result ) {
+ $processed_modules[] = $result;
+ }
+ }
+ } else {
+ $result = register_block_script_module_id(
+ $metadata,
+ $metadata_field_name
+ );
+ if ( $result ) {
+ $processed_modules[] = $result;
+ }
+ }
+ $settings[ $settings_field_name ] = $processed_modules;
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $style_fields = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'editorStyle' => 'editor_style_handles',
</span><span class="cx" style="display: block; padding: 0 10px"> 'style' => 'style_handles',
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpblocktypephp"></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-type.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-block-type.php 2024-02-08 09:24:10 UTC (rev 57564)
+++ trunk/src/wp-includes/class-wp-block-type.php 2024-02-08 10:39:24 UTC (rev 57565)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -227,6 +227,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> public $view_script_handles = array();
</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">+ * Block type front end only script module IDs.
+ *
+ * @since 6.5.0
+ * @var string[]
+ */
+ public $view_script_module_ids = array();
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Block type editor only style handles.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.1.0
</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 2024-02-08 09:24:10 UTC (rev 57564)
+++ trunk/src/wp-includes/class-wp-block.php 2024-02-08 10:39:24 UTC (rev 57565)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -470,6 +470,12 @@
</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( $this->block_type->view_script_module_ids ) ) {
+ foreach ( $this->block_type->view_script_module_ids as $view_script_module_id ) {
+ wp_enqueue_script_module( $view_script_module_id );
+ }
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ( ! empty( $this->block_type->style_handles ) ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $this->block_type->style_handles as $style_handle ) {
</span><span class="cx" style="display: block; padding: 0 10px"> wp_enqueue_style( $style_handle );
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestblocktypescontrollerphp"></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/rest-api/endpoints/class-wp-rest-block-types-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php 2024-02-08 09:24:10 UTC (rev 57564)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php 2024-02-08 10:39:24 UTC (rev 57565)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -240,6 +240,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.5.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.9.0 Renamed `$block_type` to `$item` to match parent class for PHP 8 named parameter support.
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.3.0 Added `selectors` field.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.5.0 Added `view_script_module_ids` field.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_Block_Type $item Block type data.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_REST_Request $request Full details about the request.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -291,6 +292,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'editor_script_handles',
</span><span class="cx" style="display: block; padding: 0 10px"> 'script_handles',
</span><span class="cx" style="display: block; padding: 0 10px"> 'view_script_handles',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'view_script_module_ids',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'editor_style_handles',
</span><span class="cx" style="display: block; padding: 0 10px"> 'style_handles',
</span><span class="cx" style="display: block; padding: 0 10px"> 'view_style_handles',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -584,6 +586,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'context' => array( 'embed', 'view', 'edit' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'readonly' => true,
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'view_script_module_ids' => array(
+ 'description' => __( 'Public facing script module IDs.' ),
+ 'type' => array( 'array' ),
+ 'default' => array(),
+ 'items' => array(
+ 'type' => 'string',
+ ),
+ 'context' => array( 'embed', 'view', 'edit' ),
+ 'readonly' => true,
+ ),
</ins><span class="cx" style="display: block; padding: 0 10px"> 'editor_style_handles' => array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => __( 'Editor style handles.' ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'type' => array( 'array' ),
</span></span></pre></div>
<a id="trunktestsphpunitdatablocksnoticeblockjson"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/data/blocks/notice/block.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/data/blocks/notice/block.json 2024-02-08 09:24:10 UTC (rev 57564)
+++ trunk/tests/phpunit/data/blocks/notice/block.json 2024-02-08 10:39:24 UTC (rev 57565)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -68,6 +68,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "editorScript": "tests-notice-editor-script",
</span><span class="cx" style="display: block; padding: 0 10px"> "script": "tests-notice-script",
</span><span class="cx" style="display: block; padding: 0 10px"> "viewScript": [ "tests-notice-view-script", "tests-notice-view-script-2" ],
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "viewScriptModule": [ "tests-notice-view-script-module", "tests-notice-view-script-module-2" ],
</ins><span class="cx" style="display: block; padding: 0 10px"> "editorStyle": "tests-notice-editor-style",
</span><span class="cx" style="display: block; padding: 0 10px"> "style": [ "tests-notice-style", "tests-notice-style-2" ],
</span><span class="cx" style="display: block; padding: 0 10px"> "viewStyle": [ "tests-notice-view-style" ],
</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 2024-02-08 09:24:10 UTC (rev 57564)
+++ trunk/tests/phpunit/tests/blocks/register.php 2024-02-08 10:39:24 UTC (rev 57565)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -138,6 +138,7 @@
</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"> * @ticket 50263
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 60233
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_generate_block_asset_handle() {
</span><span class="cx" style="display: block; padding: 0 10px"> $block_name = 'unit-tests/my-block';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -155,6 +156,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> generate_block_asset_handle( $block_name, 'viewScript', 99 )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'unit-tests-my-block-view-script-module',
+ generate_block_asset_handle( $block_name, 'viewScriptModule' )
+ );
+ $this->assertSame(
+ 'unit-tests-my-block-view-script-module-2',
+ generate_block_asset_handle( $block_name, 'viewScriptModule', 1 )
+ );
+ $this->assertSame(
+ 'unit-tests-my-block-view-script-module-100',
+ generate_block_asset_handle( $block_name, 'viewScriptModule', 99 )
+ );
+ $this->assertSame(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'unit-tests-my-block-editor-style-2',
</span><span class="cx" style="display: block; padding: 0 10px"> generate_block_asset_handle( $block_name, 'editorStyle', 1 )
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -199,6 +212,52 @@
</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">+ * @ticket 60233
+ */
+ public function test_generate_block_asset_handle_core_block_module() {
+ $block_name = 'core/paragraph';
+
+ $this->assertSame(
+ 'wp-block-paragraph-editor-script-module',
+ generate_block_asset_handle( $block_name, 'editorScriptModule' )
+ );
+ $this->assertSame(
+ 'wp-block-paragraph-editor-script-module-2',
+ generate_block_asset_handle( $block_name, 'editorScriptModule', 1 )
+ );
+ $this->assertSame(
+ 'wp-block-paragraph-editor-script-module-100',
+ generate_block_asset_handle( $block_name, 'editorScriptModule', 99 )
+ );
+
+ $this->assertSame(
+ 'wp-block-paragraph-view-script-module',
+ generate_block_asset_handle( $block_name, 'viewScriptModule' )
+ );
+ $this->assertSame(
+ 'wp-block-paragraph-view-script-module-2',
+ generate_block_asset_handle( $block_name, 'viewScriptModule', 1 )
+ );
+ $this->assertSame(
+ 'wp-block-paragraph-view-script-module-100',
+ generate_block_asset_handle( $block_name, 'viewScriptModule', 99 )
+ );
+
+ $this->assertSame(
+ 'wp-block-paragraph-script-module',
+ generate_block_asset_handle( $block_name, 'scriptModule' )
+ );
+ $this->assertSame(
+ 'wp-block-paragraph-script-module-2',
+ generate_block_asset_handle( $block_name, 'scriptModule', 1 )
+ );
+ $this->assertSame(
+ 'wp-block-paragraph-script-module-100',
+ generate_block_asset_handle( $block_name, 'scriptModule', 99 )
+ );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 50263
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_field_not_found_register_block_script_handle() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -232,6 +291,115 @@
</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">+ * @ticket 60233
+ */
+ public function test_field_not_found_register_block_script_module_id() {
+ $result = register_block_script_module_id( array(), 'viewScriptModule' );
+
+ $this->assertFalse( $result );
+ }
+
+ /**
+ * @ticket 60233
+ */
+ public function test_empty_string_value_do_not_register_block_script_module_id() {
+ $metadata = array( 'viewScriptModule' => '' );
+ $result = register_block_script_module_id( $metadata, 'viewScriptModule' );
+
+ $this->assertFalse( $result );
+ }
+
+ /**
+ * @ticket 60233
+ */
+ public function test_empty_array_value_do_not_register_block_script_module_id() {
+ $metadata = array( 'viewScriptModule' => array() );
+ $result = register_block_script_module_id( $metadata, 'viewScriptModule' );
+
+ $this->assertFalse( $result );
+ }
+
+ /**
+ * @ticket 60233
+ */
+ public function test_wrong_array_index_do_not_register_block_script_module_id() {
+ $metadata = array( 'viewScriptModule' => array( 'test-module_id' ) );
+ $result = register_block_script_module_id( $metadata, 'script', 1 );
+
+ $this->assertFalse( $result );
+ }
+
+ /**
+ * @ticket 60233
+ */
+ public function test_missing_asset_file_register_block_script_module_id() {
+ $metadata = array(
+ 'file' => __FILE__,
+ 'name' => 'unit-tests/test-block',
+ 'viewScriptModule' => 'file:./blocks/notice/missing-asset.js',
+ );
+ $result = register_block_script_module_id( $metadata, 'viewScriptModule' );
+
+ $this->assertSame( 'unit-tests-test-block-view-script-module', $result );
+ }
+
+ /**
+ * @ticket 60233
+ */
+ public function test_handle_passed_register_block_script_module_id() {
+ $metadata = array(
+ 'viewScriptModule' => 'test-script-module-id',
+ );
+ $result = register_block_script_module_id( $metadata, 'viewScriptModule' );
+
+ $this->assertSame( 'test-script-module-id', $result );
+ }
+
+ /**
+ * @ticket 60233
+ */
+ public function test_handles_passed_register_block_script_module_ids() {
+ $metadata = array(
+ 'viewScriptModule' => array( 'test-id', 'test-id-other' ),
+ );
+
+ $result = register_block_script_module_id( $metadata, 'viewScriptModule' );
+ $this->assertSame( 'test-id', $result );
+
+ $result = register_block_script_module_id( $metadata, 'viewScriptModule', 1 );
+ $this->assertSame( 'test-id-other', $result );
+ }
+
+ /**
+ * @ticket 60233
+ */
+ public function test_success_register_block_script_module_id() {
+ $metadata = array(
+ 'file' => DIR_TESTDATA . '/blocks/notice/block.json',
+ 'name' => 'unit-tests/test-block',
+ 'viewScriptModule' => 'file:./block.js',
+ );
+ $result = register_block_script_module_id( $metadata, 'viewScriptModule' );
+
+ $this->assertSame( 'unit-tests-test-block-view-script-module', $result );
+
+ // Test the behavior directly within the unit test
+ $this->assertFalse(
+ strpos(
+ wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $metadata['viewScriptModule'] ) ),
+ trailingslashit( wp_normalize_path( get_template_directory() ) )
+ ) === 0
+ );
+
+ $this->assertFalse(
+ strpos(
+ wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $metadata['viewScriptModule'] ) ),
+ trailingslashit( wp_normalize_path( get_stylesheet_directory() ) )
+ ) === 0
+ );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * @ticket 50263
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_handle_passed_register_block_script_handle() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -245,7 +413,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_handles_passed_register_block_script_handles() {
</span><span class="cx" style="display: block; padding: 0 10px"> $metadata = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'script' => array( 'test-script-handle', 'test-script-handle-2' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'script' => array( 'test-script-handle', 'test-script-handle-other' ),
</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"> $result = register_block_script_handle( $metadata, 'script' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -252,7 +420,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( 'test-script-handle', $result );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $result = register_block_script_handle( $metadata, 'script', 1 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( 'test-script-handle-2', $result, 1 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( 'test-script-handle-other', $result );
</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">@@ -751,6 +919,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 50328
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 57585
</span><span class="cx" style="display: block; padding: 0 10px"> * @ticket 59797
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 60233
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_block_registers_with_metadata_fixture() {
</span><span class="cx" style="display: block; padding: 0 10px"> $result = register_block_type_from_metadata(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -854,6 +1023,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $result->view_script_handles
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSameSets(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ array( 'tests-notice-view-script-module', 'tests-notice-view-script-module-2' ),
+ $result->view_script_module_ids
+ );
+ $this->assertSameSets(
</ins><span class="cx" style="display: block; padding: 0 10px"> array( 'tests-notice-editor-style' ),
</span><span class="cx" style="display: block; padding: 0 10px"> $result->editor_style_handles
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestblocktypecontrollerphp"></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/rest-api/rest-block-type-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php 2024-02-08 09:24:10 UTC (rev 57564)
+++ trunk/tests/phpunit/tests/rest-api/rest-block-type-controller.php 2024-02-08 10:39:24 UTC (rev 57565)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -261,6 +261,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSameSets( array(), $data['editor_script_handles'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSameSets( array(), $data['script_handles'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSameSets( array(), $data['view_script_handles'] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSameSets( array(), $data['view_script_module_ids'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertSameSets( array(), $data['editor_style_handles'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSameSets( array(), $data['style_handles'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( $data['is_dynamic'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -339,6 +340,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSameSets( array(), $data['editor_script_handles'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSameSets( array(), $data['script_handles'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSameSets( array(), $data['view_script_handles'] );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSameSets( array(), $data['view_script_module_ids'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertSameSets( array(), $data['editor_style_handles'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSameSets( array(), $data['style_handles'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( $data['is_dynamic'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -562,7 +564,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $response = rest_get_server()->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px"> $data = $response->get_data();
</span><span class="cx" style="display: block; padding: 0 10px"> $properties = $data['schema']['properties'];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertCount( 32, $properties );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertCount( 33, $properties );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'api_version', $properties );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'name', $properties );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'title', $properties );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -586,6 +588,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'editor_script_handles', $properties );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'script_handles', $properties );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'view_script_handles', $properties );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertArrayHasKey( 'view_script_module_ids', $properties );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'editor_style_handles', $properties );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'style_handles', $properties );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'view_style_handles', $properties, 'schema must contain view_style_handles' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -718,6 +721,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'editor_script_handles',
</span><span class="cx" style="display: block; padding: 0 10px"> 'script_handles',
</span><span class="cx" style="display: block; padding: 0 10px"> 'view_script_handles',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'view_script_module_ids',
</ins><span class="cx" style="display: block; padding: 0 10px"> 'editor_style_handles',
</span><span class="cx" style="display: block; padding: 0 10px"> 'style_handles',
</span><span class="cx" style="display: block; padding: 0 10px"> // Deprecated fields.
</span></span></pre>
</div>
</div>
</body>
</html>