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

Lazy load variations for template parts. #4857

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wp-admin/includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2302,7 +2302,6 @@
'ancestor' => 'ancestor',
'keywords' => 'keywords',
'example' => 'example',
'variations' => 'variations',
);

foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
Expand All @@ -2317,6 +2316,7 @@

$blocks[ $block_name ][ $key ] = $block_type->{ $field };
}
$blocks[ $block_name ][ 'variations' ] = $block_type->get_variations();

Check failure on line 2319 in src/wp-admin/includes/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
}

return $blocks;
Expand Down
3 changes: 2 additions & 1 deletion src/wp-includes/blocks/template-part.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,9 @@ function register_block_core_template_part() {
__DIR__ . '/template-part',
array(
'render_callback' => 'render_block_core_template_part',
'variations' => build_template_part_block_variations(),
)
);
}
add_action( 'init', 'register_block_core_template_part' );

add_filter( 'get_variations_block_core/template-part', 'build_template_part_block_variations' );
19 changes: 19 additions & 0 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,25 @@ public function set_props( $args ) {
}
}

/**
* Get all variations.
*
* @since 6.4.0
*
* @return array Array of variations.
*/
public function get_variations() {
/**
* Filters the arguments for registering a variations.
*
* @since 5.5.0
*
* @param array $variations Array of arguments for registering a block type.
* @param string $block_type Block type name including namespace.
*/
return apply_filters( 'get_variations_block_' . $this->name, $this->variations, $this->name );
}

/**
* Get all available block attributes including possible layout attribute from Columns block.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,7 @@
'view_script_handles',
'editor_style_handles',
'style_handles',
'variations',
'block_hooks',
'block_hooks'

Check failure on line 295 in src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

There should be a comma after the last array item in a multi-line array.
),
$deprecated_fields
);
Expand All @@ -314,6 +313,11 @@
}
}

if ( rest_is_field_included( 'variations', $fields ) ) {
$field = $block_type->get_variations();
$data['variations'] = rest_sanitize_value_from_schema( $field, $schema['properties']['variations'] );
}

if ( rest_is_field_included( 'styles', $fields ) ) {
$styles = $this->style_registry->get_registered_styles_for_block( $block_type->name );
$styles = array_values( $styles );
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ public function test_block_registers_with_metadata_fixture() {
'keywords' => array( 'failure' ),
),
),
$result->variations
$result->get_variations()
);
$this->assertSame(
array(
Expand Down Expand Up @@ -1030,7 +1030,7 @@ public function test_block_registers_with_metadata_i18n_support() {
'keywords' => array( 'niepowodzenie' ),
),
),
$result->variations
$result->get_variations()
);
}

Expand Down