Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call variation through callback so it's only loaded when needed - in support of trac 59969 #56952

Merged
18 changes: 18 additions & 0 deletions lib/compat/wordpress-6.4/block-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,20 @@ function gutenberg_register_block_hooks_rest_field() {
);
}

/**
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
* Shim for the `variation_callback` block type argument.
*
* @param array $args The block type arguments.
* @return array The updated block type arguments.
*/
function gutenberg_register_block_type_args_shim( $args ) {
if ( isset( $args['variation_callback'] ) && is_callable( $args['variation_callback'] ) ) {
$args['variations'] = call_user_func( $args['variation_callback'] );
unset( $args['variation_callback'] );
}
return $args;
}

// 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 );
Expand All @@ -310,6 +324,10 @@ function gutenberg_register_block_hooks_rest_field() {
add_action( 'rest_api_init', 'gutenberg_register_block_hooks_rest_field' );
}

if ( ! method_exists( 'WP_Block_Type', 'get_variations' ) ) {
add_filter( 'register_block_type_args', 'gutenberg_register_block_type_args_shim' );
}

// Helper functions.
// -----------------
// The sole purpose of the following two functions (`gutenberg_serialize_block`
Expand Down
21 changes: 15 additions & 6 deletions packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,11 @@ function register_block_core_navigation_link_variation( $variation ) {
}

/**
* Register the navigation link block.
* Returns an array of variations for the navigation link block.
*
* @uses render_block_core_navigation()
* @throws WP_Error An WP_Error exception parsing the block definition.
* @return array
*/
function register_block_core_navigation_link() {
function build_navigation_link_block_variations() {
// This will only handle post types and taxonomies registered until this point (init on priority 9).
// See action hooks below for other post types and taxonomies.
// See https://github.com/WordPress/gutenberg/issues/53826 for details.
Expand Down Expand Up @@ -382,11 +381,21 @@ function register_block_core_navigation_link() {
}
}

return array_merge( $built_ins, $variations );
}

/**
* Register the navigation link block.
*
* @uses render_block_core_navigation()
* @throws WP_Error An WP_Error exception parsing the block definition.
*/
function register_block_core_navigation_link() {
register_block_type_from_metadata(
__DIR__ . '/navigation-link',
array(
'render_callback' => 'render_block_core_navigation_link',
'variations' => array_merge( $built_ins, $variations ),
'render_callback' => 'render_block_core_navigation_link',
'variation_callback' => 'build_navigation_link_block_variations',
)
);
}
Expand Down
17 changes: 13 additions & 4 deletions packages/block-library/src/post-terms/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ function render_block_core_post_terms( $attributes, $content, $block ) {
}

/**
* Registers the `core/post-terms` block on the server.
* Returns the available variations for the `core/post-terms` block.
*
* @return array The available variations for the block.
*/
function register_block_core_post_terms() {
function build_post_term_block_variations() {
$taxonomies = get_taxonomies(
array(
'publicly_queryable' => true,
Expand Down Expand Up @@ -103,11 +105,18 @@ function register_block_core_post_terms() {
}
}

return array_merge( $built_ins, $custom_variations );
}

/**
* Registers the `core/post-terms` block on the server.
*/
function register_block_core_post_terms() {
register_block_type_from_metadata(
__DIR__ . '/post-terms',
array(
'render_callback' => 'render_block_core_post_terms',
'variations' => array_merge( $built_ins, $custom_variations ),
'render_callback' => 'render_block_core_post_terms',
'variation_callback' => 'build_post_term_block_variations',
)
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/template-part/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ function register_block_core_template_part() {
register_block_type_from_metadata(
__DIR__ . '/template-part',
array(
'render_callback' => 'render_block_core_template_part',
'variations' => build_template_part_block_variations(),
'render_callback' => 'render_block_core_template_part',
'variation_callback' => 'build_template_part_block_variations',
)
);
}
Expand Down
2 changes: 2 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@
<element value="apply_block_core_search_border_style"/>
<element value="apply_block_core_search_border_styles"/>
<element value="build_dropdown_script_block_core_categories"/>
<element value="build_navigation_link_block_variations"/>
Copy link
Contributor

@anton-vlasenko anton-vlasenko Jan 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kt-12, I appreciate your efforts with the ignore list. However, these functions weren't part of the initial setup and don't pose any backward compatibility issues if we stick to the naming conventions enforced by the sniff.
For example, they could be renamed to block_core_template_part_build_area_variations() and block_core_post_terms_build_variations() respectively. Could you please rename them?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the follow-up #58538.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The names I suggested for these functions were not exactly correct, but the idea was to adhere to the coding standard. Thanks for fixing it, @Mamaduka.
I have approved your PR.

<element value="build_post_term_block_variations"/>
<element value="build_template_part_block_area_variations"/>
<element value="build_template_part_block_instance_variations"/>
<element value="build_template_part_block_variations"/>
Expand Down
Loading