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
5 changes: 5 additions & 0 deletions lib/compat/wordpress-6.4/block-hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ function gutenberg_add_hooked_blocks( $settings, $metadata ) {

$exposed_settings = array_intersect_key( $settings, $fields_to_pick );

// If the block has a variations callback, call it and add the variations to the block.
if ( isset( $exposed_settings['variations'] ) && is_callable( $exposed_settings['variations'] ) ) {
$exposed_settings['variations'] = call_user_func( $exposed_settings['variations'] );
}

// TODO: Make work for blocks registered via direct call to gutenberg_add_hooked_block().
wp_add_inline_script(
'wp-blocks',
Expand Down
19 changes: 14 additions & 5 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 ),
'variations' => 'build_navigation_link_block_variations',
)
);
}
Expand Down
15 changes: 12 additions & 3 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 ),
'variations' => 'build_post_term_block_variations',
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/template-part/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ function register_block_core_template_part() {
__DIR__ . '/template-part',
array(
'render_callback' => 'render_block_core_template_part',
'variations' => build_template_part_block_variations(),
'variations' => 'build_template_part_block_variations',
)
);
}
Expand Down