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
24 changes: 24 additions & 0 deletions lib/compat/wordpress-6.5/blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Temporary compatibility shims for block APIs present in Gutenberg.
*
* @package gutenberg
*/

/**
* 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;
}

if ( ! method_exists( 'WP_Block_Type', 'get_variations' ) ) {
add_filter( 'register_block_type_args', 'gutenberg_register_block_type_args_shim' );
}
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.4/kses.php';

// WordPress 6.5 compat.
require __DIR__ . '/compat/wordpress-6.5/blocks.php';
require __DIR__ . '/compat/wordpress-6.5/block-patterns.php';
require __DIR__ . '/compat/wordpress-6.5/class-wp-navigation-block-renderer.php';
require __DIR__ . '/compat/wordpress-6.5/kses.php';
Expand Down
20 changes: 15 additions & 5 deletions packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,11 @@ function block_core_navigation_link_unregister_variation( $name ) {

/**
* 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 @@ -407,11 +407,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