Skip to content

Commit

Permalink
Ensure that unit tests cover gutenberg_serialize_blocks polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Sep 25, 2023
1 parent 7a04903 commit 3f56b22
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
58 changes: 31 additions & 27 deletions lib/compat/wordpress-6.4/block-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ function gutenberg_add_hooked_blocks( $settings, $metadata ) {

return $settings;
}
add_filter( 'block_type_metadata_settings', 'gutenberg_add_hooked_blocks', 10, 2 );

/**
* Register a hooked block for automatic insertion into a given block hook.
Expand Down Expand Up @@ -256,7 +255,6 @@ function gutenberg_parse_and_serialize_block_templates( $query_result ) {

return $query_result;
}
add_filter( 'get_block_templates', 'gutenberg_parse_and_serialize_block_templates', 10, 1 );

/**
* Filters the block template object after it has been (potentially) fetched from the theme file.
Expand All @@ -279,7 +277,37 @@ function gutenberg_parse_and_serialize_blocks( $block_template ) {

return $block_template;
}
add_filter( 'get_block_file_template', 'gutenberg_parse_and_serialize_blocks', 10, 1 );

/**
* Register the `block_hooks` field for the block-types REST API controller.
*
* @return void
*/
function gutenberg_register_block_hooks_rest_field() {
register_rest_field(
'block-type',
'block_hooks',
array(
'schema' => array(
'description' => __( 'This block is automatically inserted near any occurence of the block types used as keys of this map, into a relative position given by the corresponding value.', 'gutenberg' ),
'patternProperties' => array(
'^[a-zA-Z0-9-]+/[a-zA-Z0-9-]+$' => array(
'type' => 'string',
'enum' => array( 'before', 'after', 'first_child', 'last_child' ),
),
),
),
)
);
}

// Install the polyfill for Block Hooks only if it isn't already handled in WordPress core.
if ( ! function_exists( 'traverse_and_serialize_blocks' ) ) {
add_filter( 'block_type_metadata_settings', 'gutenberg_add_hooked_blocks', 10, 2 );
add_filter( 'get_block_templates', 'gutenberg_parse_and_serialize_block_templates', 10, 1 );
add_filter( 'get_block_file_template', 'gutenberg_parse_and_serialize_blocks', 10, 1 );
add_action( 'rest_api_init', 'gutenberg_register_block_hooks_rest_field' );
}

// Helper functions.
// -----------------
Expand Down Expand Up @@ -345,27 +373,3 @@ function gutenberg_serialize_block( $block ) {
function gutenberg_serialize_blocks( $blocks ) {
return implode( '', array_map( 'gutenberg_serialize_block', $blocks ) );
}

/**
* Register the `block_hooks` field for the block-types REST API controller.
*
* @return void
*/
function gutenberg_register_block_hooks_rest_field() {
register_rest_field(
'block-type',
'block_hooks',
array(
'schema' => array(
'description' => __( 'This block is automatically inserted near any occurence of the block types used as keys of this map, into a relative position given by the corresponding value.', 'gutenberg' ),
'patternProperties' => array(
'^[a-zA-Z0-9-]+/[a-zA-Z0-9-]+$' => array(
'type' => 'string',
'enum' => array( 'before', 'after', 'first_child', 'last_child' ),
),
),
),
)
);
}
add_action( 'rest_api_init', 'gutenberg_register_block_hooks_rest_field' );
5 changes: 1 addition & 4 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ function gutenberg_is_experiment_enabled( $name ) {

// WordPress 6.4 compat.
require __DIR__ . '/compat/wordpress-6.4/blocks.php';
if ( ! function_exists( 'traverse_and_serialize_blocks' ) ) {
// Install the polyfill for Block Hooks only if it isn't already handled in WordPress core.
require __DIR__ . '/compat/wordpress-6.4/block-hooks.php';
}
require __DIR__ . '/compat/wordpress-6.4/block-hooks.php';
require __DIR__ . '/compat/wordpress-6.4/block-patterns.php';
require __DIR__ . '/compat/wordpress-6.4/script-loader.php';
require __DIR__ . '/compat/wordpress-6.4/kses.php';
Expand Down
12 changes: 10 additions & 2 deletions phpunit/tests/blocks/renderHookedBlocks.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use PHP_CodeSniffer\Generators\HTML;

/**
* Tests for hooked blocks rendering.
*
Expand All @@ -12,6 +10,16 @@
* @group blocks
*/
class Tests_Blocks_RenderHookedBlocks extends WP_UnitTestCase {
public function set_up() {
parent::set_up();
add_filter( 'block_type_metadata_settings', 'gutenberg_add_hooked_blocks', 10, 2 );
}

public function tear_down() {
remove_filter( 'block_type_metadata_settings', 'gutenberg_add_hooked_blocks' );
parent::tear_down();
}

/**
* @ticket 59313
*/
Expand Down

0 comments on commit 3f56b22

Please sign in to comment.