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

Fix template part area variation matching #49500

Merged
merged 2 commits into from
May 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions packages/block-library/src/template-part/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,34 @@ function render_block_core_template_part( $attributes ) {
/**
* Returns an array of area variation objects for the template part block.
*
* @param array $instance_variations The variations for instances.
*
* @return array Array containing the block variation objects.
*/
function build_template_part_block_area_variations() {
function build_template_part_block_area_variations( $instance_variations ) {
$variations = array();
$defined_areas = get_allowed_block_template_part_areas();

foreach ( $defined_areas as $area ) {
if ( 'uncategorized' !== $area['area'] ) {
$has_instance_for_area = false;
foreach ( $instance_variations as $variation ) {
if ( $variation['attributes']['area'] === $area['area'] ) {
$has_instance_for_area = true;
break;
}
}

$scope = $has_instance_for_area ? array() : array( 'inserter' );

$variations[] = array(
'name' => $area['area'],
'name' => 'area_' . $area['area'],
'title' => $area['label'],
'description' => $area['description'],
'attributes' => array(
'area' => $area['area'],
),
'scope' => array( 'inserter' ),
'scope' => $scope,
'icon' => $area['icon'],
);
}
Expand Down Expand Up @@ -223,7 +236,7 @@ function build_template_part_block_instance_variations() {

foreach ( $template_parts as $template_part ) {
$variations[] = array(
'name' => sanitize_title( $template_part->slug ),
'name' => 'instance_' . sanitize_title( $template_part->slug ),
'title' => $template_part->title,
// If there's no description for the template part don't show the
// block description. This is a bit hacky, but prevent the fallback
Expand Down Expand Up @@ -255,7 +268,9 @@ function build_template_part_block_instance_variations() {
* @return array Array containing the block variation objects.
*/
function build_template_part_block_variations() {
return array_merge( build_template_part_block_area_variations(), build_template_part_block_instance_variations() );
$instance_variations = build_template_part_block_instance_variations();
$area_variations = build_template_part_block_area_variations( $instance_variations );
return array_merge( $area_variations, $instance_variations );
}

/**
Expand Down