From f82922e902d0f1fe87615a3d3c8d5a54e1747755 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 8 Feb 2024 15:45:28 +0100 Subject: [PATCH] Compat changes for REST API --- lib/compat/wordpress-6.5/scripts-modules.php | 25 ++++++++++++++++++++ lib/experimental/script-modules.php | 18 ++------------ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/compat/wordpress-6.5/scripts-modules.php b/lib/compat/wordpress-6.5/scripts-modules.php index ecfb4155550091..8fd8978d9e8f5d 100644 --- a/lib/compat/wordpress-6.5/scripts-modules.php +++ b/lib/compat/wordpress-6.5/scripts-modules.php @@ -92,6 +92,31 @@ function gutenberg_filter_render_block_enqueue_view_script_modules( $block_conte } add_filter( 'render_block', 'gutenberg_filter_render_block_enqueue_view_script_modules', 10, 3 ); + + /** + * Registers a REST field for block types to provide view script module IDs. + * + * Adds the `view_script_module_ids` and `view_module_ids` (deprecated) field to block type objects in the REST API, which + * lists the script module IDs for any script modules associated with the + * block's viewScriptModule key. + */ + function gutenberg_register_view_script_module_ids_rest_field() { + register_rest_field( + 'block-type', + 'view_script_module_ids', + array( + 'get_callback' => function ( $item ) { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $item['name'] ); + if ( isset( $block_type->view_script_module_ids ) ) { + return $block_type->view_script_module_ids; + } + return array(); + }, + ) + ); + } + + add_action( 'rest_api_init', 'gutenberg_register_view_script_module_ids_rest_field' ); } if ( ! function_exists( 'wp_register_script_module' ) ) { diff --git a/lib/experimental/script-modules.php b/lib/experimental/script-modules.php index f5cba1914e3d6e..e22b757c119501 100644 --- a/lib/experimental/script-modules.php +++ b/lib/experimental/script-modules.php @@ -142,7 +142,7 @@ function gutenberg_generate_block_asset_module_id( $block_name, $field_name, $in * lists the script module IDs for any script modules associated with the * block's viewScriptModule key. */ -function gutenberg_register_view_script_module_ids_rest_field() { +function gutenberg_register_view_module_ids_rest_field() { // @todo remove view_module_ids support in Gutenberg >= 17.8 (replaced by view_script_module_ids). register_rest_field( 'block-type', @@ -157,23 +157,9 @@ function gutenberg_register_view_script_module_ids_rest_field() { }, ) ); - - register_rest_field( - 'block-type', - 'view_script_module_ids', - array( - 'get_callback' => function ( $item ) { - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $item['name'] ); - if ( isset( $block_type->view_script_module_ids ) ) { - return $block_type->view_script_module_ids; - } - return array(); - }, - ) - ); } -add_action( 'rest_api_init', 'gutenberg_register_view_script_module_ids_rest_field' ); +add_action( 'rest_api_init', 'gutenberg_register_view_module_ids_rest_field' ); /** * Registers the module if no module with that module identifier has already