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

Rough POC of respecting "supports multiple" in the auto-inserted block metadata #53925

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 23 additions & 5 deletions lib/experimental/auto-inserting-blocks.php
@@ -1,10 +1,19 @@
<?php

Check failure on line 1 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Class file names should be based on the class name with "class-" prepended. Expected class-autoinserttracker.php, but found auto-inserting-blocks.php.

Check failure on line 1 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Missing file doc comment
/**
* Auto-inserting blocks.
*
* @package gutenberg
*/

Check failure on line 6 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

There must be no blank lines after the class comment

class AutoInsertTracker {

Check warning on line 8 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Found precision alignment of 1 spaces.

Check failure on line 8 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected 0 tabs, found 1 spaces

Check failure on line 8 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

The "AutoInsertTracker" class should be guarded against redeclaration.
static private $has_inserted = [];

Check failure on line 9 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Missing member variable doc comment

Check failure on line 9 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Short array syntax is not allowed
public static function block_has_been_inserted( $block ) {

Check failure on line 10 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Missing doc comment for function block_has_been_inserted()
return isset( self::$has_inserted[ $block['blockName'] ] );
}
public static function set_block_has_been_inserted( $block ) {

Check failure on line 13 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Missing doc comment for function set_block_has_been_inserted()
self::$has_inserted[ $block['blockName'] ] = true;
}
}

Check warning on line 16 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Found precision alignment of 1 spaces.

Check failure on line 16 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected 0 tabs, found 1 spaces
/**
* Return a function that auto-inserts a block next to a given "anchor" block.
*
Expand All @@ -19,23 +28,29 @@
* @param string $relative_position The position relative to the given block.
* Can be 'before', 'after', 'first_child', or 'last_child'.
* @param string $anchor_block_type The auto-inserted block will be inserted next to instances of this block type.
* @param bool $allow_multiple Whether the anchor block allows multiple instances.
* @return callable A function that accepts a block's content and returns the content with the inserted block.
*/
function gutenberg_auto_insert_block( $inserted_block, $relative_position, $anchor_block_type ) {
return function( $block ) use ( $inserted_block, $relative_position, $anchor_block_type ) {
function gutenberg_auto_insert_block( $inserted_block, $relative_position, $anchor_block_type, $allow_multiple ) {
return function( $block ) use ( $inserted_block, $relative_position, $anchor_block_type, $allow_multiple ) {
if ( AutoInsertTracker::block_has_been_inserted($inserted_block) && ! $allow_multiple ) {
return $block;
}
if ( $anchor_block_type === $block['blockName'] ) {
if ( 'first_child' === $relative_position ) {
array_unshift( $block['innerBlocks'], $inserted_block );
// Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`)
// when rendering blocks, we also need to prepend a value (`null`, to mark a block
// location) to that array.
array_unshift( $block['innerContent'], null );
AutoInsertTracker::set_block_has_been_inserted($inserted_block);
} elseif ( 'last_child' === $relative_position ) {
array_push( $block['innerBlocks'], $inserted_block );
// Since WP_Block::render() iterates over `inner_content` (rather than `inner_blocks`)
// when rendering blocks, we also need to prepend a value (`null`, to mark a block
// location) to that array.
array_push( $block['innerContent'], null );
AutoInsertTracker::set_block_has_been_inserted($inserted_block);
}
return $block;
}
Expand All @@ -59,6 +74,7 @@
// when rendering blocks, we also need to insert a value (`null`, to mark a block
// location) into that array.
array_splice( $block['innerContent'], $chunk_index, 0, array( null ) );
AutoInsertTracker::set_block_has_been_inserted($inserted_block);
}
return $block;
};
Expand All @@ -75,7 +91,8 @@
if ( ! isset( $metadata['__experimentalAutoInsert'] ) ) {
return $settings;
}
$auto_insert = $metadata['__experimentalAutoInsert'];

Check warning on line 94 in lib/experimental/auto-inserting-blocks.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
$allow_multiple = $metadata['supports']['multiple'];

/**
* Map the camelCased position string from block.json to the snake_cased block type position
Expand Down Expand Up @@ -108,7 +125,7 @@

$mapped_position = $property_mappings[ $position ];

gutenberg_register_auto_inserted_block( $inserted_block_name, $mapped_position, $anchor_block_name );
gutenberg_register_auto_inserted_block( $inserted_block_name, $mapped_position, $anchor_block_name, $allow_multiple );

$settings['auto_insert'][ $anchor_block_name ] = $mapped_position;
}
Expand All @@ -132,9 +149,10 @@
* @param string $position The desired position of the auto-inserted block, relative to its anchor block.
* Can be 'before', 'after', 'first_child', or 'last_child'.
* @param string $anchor_block The name of the block to insert the auto-inserted block next to.
* @param bool $allow_multiple Whether the anchor block allows multiple instances.
* @return void
*/
function gutenberg_register_auto_inserted_block( $inserted_block, $position, $anchor_block ) {
function gutenberg_register_auto_inserted_block( $inserted_block, $position, $anchor_block, $allow_multiple ) {
$inserted_block = array(
'blockName' => $inserted_block,
'attrs' => array(),
Expand All @@ -143,7 +161,7 @@
'innerBlocks' => array(),
);

$inserter = gutenberg_auto_insert_block( $inserted_block, $position, $anchor_block );
$inserter = gutenberg_auto_insert_block( $inserted_block, $position, $anchor_block, $allow_multiple );
add_filter( 'gutenberg_serialize_block', $inserter, 10, 1 );
}

Expand Down