Skip to content

Commit

Permalink
Compat changes for REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Feb 9, 2024
1 parent 5380af8 commit f82922e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
25 changes: 25 additions & 0 deletions lib/compat/wordpress-6.5/scripts-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) ) {
Expand Down
18 changes: 2 additions & 16 deletions lib/experimental/script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down

0 comments on commit f82922e

Please sign in to comment.