Skip to content

Commit 2c247e2

Browse files
authored
Call variation through callback so it's only loaded when needed - in support of trac 59969 (#56952)
* call variation through callback so it's only loaded when needed. * move gutenberg_add_hooked_blocks from 6.4 to 6.5 * cs fixes * fix version number * revert some code * adapting to variation_calback * B/C for plugin * cs fixes * cs fixes * cs fixes * shims to support block variation changes for WP < 6.5 * EOL for PHPCS
1 parent df1819d commit 2c247e2

File tree

6 files changed

+57
-11
lines changed

6 files changed

+57
-11
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Temporary compatibility shims for block APIs present in Gutenberg.
4+
*
5+
* @package gutenberg
6+
*/
7+
8+
/**
9+
* Shim for the `variation_callback` block type argument.
10+
*
11+
* @param array $args The block type arguments.
12+
* @return array The updated block type arguments.
13+
*/
14+
function gutenberg_register_block_type_args_shim( $args ) {
15+
if ( isset( $args['variation_callback'] ) && is_callable( $args['variation_callback'] ) ) {
16+
$args['variations'] = call_user_func( $args['variation_callback'] );
17+
unset( $args['variation_callback'] );
18+
}
19+
return $args;
20+
}
21+
22+
if ( ! method_exists( 'WP_Block_Type', 'get_variations' ) ) {
23+
add_filter( 'register_block_type_args', 'gutenberg_register_block_type_args_shim' );
24+
}

lib/load.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ function gutenberg_is_experiment_enabled( $name ) {
101101
require __DIR__ . '/compat/wordpress-6.4/kses.php';
102102

103103
// WordPress 6.5 compat.
104+
require __DIR__ . '/compat/wordpress-6.5/blocks.php';
104105
require __DIR__ . '/compat/wordpress-6.5/block-patterns.php';
105106
require __DIR__ . '/compat/wordpress-6.5/class-wp-navigation-block-renderer.php';
106107
require __DIR__ . '/compat/wordpress-6.5/kses.php';

packages/block-library/src/navigation-link/index.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,11 @@ function block_core_navigation_link_unregister_variation( $name ) {
368368

369369
/**
370370
* Register the navigation link block.
371+
* Returns an array of variations for the navigation link block.
371372
*
372-
* @uses render_block_core_navigation()
373-
* @throws WP_Error An WP_Error exception parsing the block definition.
373+
* @return array
374374
*/
375-
function register_block_core_navigation_link() {
375+
function build_navigation_link_block_variations() {
376376
// This will only handle post types and taxonomies registered until this point (init on priority 9).
377377
// See action hooks below for other post types and taxonomies.
378378
// See https://github.com/WordPress/gutenberg/issues/53826 for details.
@@ -407,11 +407,21 @@ function register_block_core_navigation_link() {
407407
}
408408
}
409409

410+
return array_merge( $built_ins, $variations );
411+
}
412+
413+
/**
414+
* Register the navigation link block.
415+
*
416+
* @uses render_block_core_navigation()
417+
* @throws WP_Error An WP_Error exception parsing the block definition.
418+
*/
419+
function register_block_core_navigation_link() {
410420
register_block_type_from_metadata(
411421
__DIR__ . '/navigation-link',
412422
array(
413-
'render_callback' => 'render_block_core_navigation_link',
414-
'variations' => array_merge( $built_ins, $variations ),
423+
'render_callback' => 'render_block_core_navigation_link',
424+
'variation_callback' => 'build_navigation_link_block_variations',
415425
)
416426
);
417427
}

packages/block-library/src/post-terms/index.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ function render_block_core_post_terms( $attributes, $content, $block ) {
5959
}
6060

6161
/**
62-
* Registers the `core/post-terms` block on the server.
62+
* Returns the available variations for the `core/post-terms` block.
63+
*
64+
* @return array The available variations for the block.
6365
*/
64-
function register_block_core_post_terms() {
66+
function build_post_term_block_variations() {
6567
$taxonomies = get_taxonomies(
6668
array(
6769
'publicly_queryable' => true,
@@ -103,11 +105,18 @@ function register_block_core_post_terms() {
103105
}
104106
}
105107

108+
return array_merge( $built_ins, $custom_variations );
109+
}
110+
111+
/**
112+
* Registers the `core/post-terms` block on the server.
113+
*/
114+
function register_block_core_post_terms() {
106115
register_block_type_from_metadata(
107116
__DIR__ . '/post-terms',
108117
array(
109-
'render_callback' => 'render_block_core_post_terms',
110-
'variations' => array_merge( $built_ins, $custom_variations ),
118+
'render_callback' => 'render_block_core_post_terms',
119+
'variation_callback' => 'build_post_term_block_variations',
111120
)
112121
);
113122
}

packages/block-library/src/template-part/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ function register_block_core_template_part() {
281281
register_block_type_from_metadata(
282282
__DIR__ . '/template-part',
283283
array(
284-
'render_callback' => 'render_block_core_template_part',
285-
'variations' => build_template_part_block_variations(),
284+
'render_callback' => 'render_block_core_template_part',
285+
'variation_callback' => 'build_template_part_block_variations',
286286
)
287287
);
288288
}

phpcs.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@
115115
<element value="apply_block_core_search_border_style"/>
116116
<element value="apply_block_core_search_border_styles"/>
117117
<element value="build_dropdown_script_block_core_categories"/>
118+
<element value="build_navigation_link_block_variations"/>
119+
<element value="build_post_term_block_variations"/>
118120
<element value="build_template_part_block_area_variations"/>
119121
<element value="build_template_part_block_instance_variations"/>
120122
<element value="build_template_part_block_variations"/>

0 commit comments

Comments
 (0)