Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a9b1993
Insert a string after each comment-content block
ockham Apr 26, 2023
6ac30c1
Use an actual block
ockham Apr 26, 2023
03ebf7e
Start implementing basic last-child mechanism
ockham Apr 26, 2023
9382109
Bit of polish
ockham Apr 26, 2023
01de9dc
More polish
ockham Apr 26, 2023
df6cf73
More polish
ockham Apr 27, 2023
9db02c3
Consistency
ockham Apr 27, 2023
9a46647
Formatting
ockham May 2, 2023
d1b0225
Auto-insert Avatar block under comments
ockham May 2, 2023
6f36442
Use render_block_data filter to insert child block
ockham May 31, 2023
02883ef
Make child block insertion more flexible
ockham May 31, 2023
8bd8c6d
Remove child block insertion from render_block hook
ockham May 31, 2023
e1fde47
Simplify render_block logic
ockham May 31, 2023
9e36ff1
Comment typo
ockham May 31, 2023
1188192
Switch places
ockham May 31, 2023
e8161e8
Insert Social Icon after Post Content
ockham May 31, 2023
05f3bd4
Add comment to render_block hook
ockham May 31, 2023
db0434d
Remove now-obsolete filter args
ockham May 31, 2023
829806e
A bit nicer still
ockham May 31, 2023
da7290d
Manually create inserted block
ockham May 31, 2023
4076c4e
Formatting
ockham May 31, 2023
97fbbae
Fix unit test
ockham May 31, 2023
8b78c34
A bit easier on the eye
ockham Jun 1, 2023
8e9c832
Use null rather than block instance in innerContent
ockham Jun 1, 2023
3666509
A bit nicer to read yet
ockham Jun 5, 2023
5e33b1d
Block pattern-based auto-insertion
ockham Jun 7, 2023
aa2c270
Support autoInsert arrays
ockham Jun 7, 2023
924c955
Support block pattern auto-insertion before and after anchor block
ockham Jun 7, 2023
0a07027
Clarify comments
ockham Jun 7, 2023
74762f3
Whitespace
ockham Jun 7, 2023
3bc7af7
Add TODOs
ockham Jun 7, 2023
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
111 changes: 111 additions & 0 deletions lib/experimental/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,114 @@ function gutenberg_register_metadata_attribute( $args ) {
return $args;
}
add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' );

/**
* Auto-insert a block as another block's first or last inner block.
*
* @param array $parsed_block The block being rendered.
*/
function gutenberg_auto_insert_child_block( $parsed_block ) {
$block_patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered();

// TODO: Use a more efficient way to find block patterns anchored at this block.
// We might want to do this upon block pattern registration; a filter on `register_block_pattern`
// might come in handy, but alas, we don't have one yet.
foreach ( $block_patterns as $block_pattern ) {
if ( ! isset( $block_pattern['autoInsert'] ) || ! isset( $block_pattern['blockTypes'] ) ) {
continue;
}

// Is the current block listed among possible anchor blocks for the block pattern?
$index = array_search( $parsed_block['blockName'], $block_pattern['blockTypes'], true );
if ( false === $index ) {
continue;
}

// Determine index of anchor block in $block_pattern['blockTypes'] and use it to look up matching relative position.
if ( is_array( $block_pattern['autoInsert'] ) ) {
$index = $index % count( $block_pattern['autoInsert'] ); // Wrap around in case the array is too short.
if ( isset( $block_pattern['autoInsert'][ $index ] ) ) {
$relative_position = $block_pattern['autoInsert'][ $index ];
} // What if we don't have an autoInsert for this index?
} else {
$relative_position = $block_pattern['autoInsert'];
}

// Is the relative position of the block pattern set to first or last child?
if ( 'firstChild' !== $relative_position && 'lastChild' !== $relative_position ) {
continue;
}

$inserted_blocks = parse_blocks( $block_pattern['content'] );
$inserted_block = $inserted_blocks[0];

if ( 'firstChild' === $relative_position ) {
array_unshift( $parsed_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( $parsed_block['innerContent'], null );
} elseif ( 'lastChild' === $relative_position ) {
array_push( $parsed_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( $parsed_block['innerContent'], null );
}
}

return $parsed_block;
}
add_filter( 'render_block_data', 'gutenberg_auto_insert_child_block', 10, 1 );

/**
* Auto-insert blocks relative to a given block.
*
* @param string $block_content The block content.
* @param array $block The full block, including name and attributes.
*/
function gutenberg_auto_insert_blocks( $block_content, $block ) {
$block_patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered();

// TODO: Use a more efficient way to find block patterns anchored at this block.
// We might want to do this upon block pattern registration; a filter on `register_block_pattern`
// might come in handy, but alas, we don't have one yet.
foreach ( $block_patterns as $block_pattern ) {
if ( ! isset( $block_pattern['autoInsert'] ) || ! isset( $block_pattern['blockTypes'] ) ) {
continue;
}

// Is the current block listed among possible anchor blocks for the block pattern?
$index = array_search( $block['blockName'], $block_pattern['blockTypes'], true );
if ( false === $index ) {
continue;
}

// Determine index of anchor block in $block_pattern['blockTypes'] and use it to look up matching relative position.
if ( is_array( $block_pattern['autoInsert'] ) ) {
$index = $index % count( $block_pattern['autoInsert'] ); // Wrap around in case the array is too short.
if ( isset( $block_pattern['autoInsert'][ $index ] ) ) {
$relative_position = $block_pattern['autoInsert'][ $index ];
} // What if we don't have an autoInsert for this index?
} else {
$relative_position = $block_pattern['autoInsert'];
}

// Is the relative position of the block pattern set to before or after?
if ( 'before' !== $relative_position && 'after' !== $relative_position ) {
continue;
}

$inserted_blocks = parse_blocks( $block_pattern['content'] );
$inserted_content = render_block( $inserted_blocks[0] );

if ( 'before' === $relative_position ) {
$block_content = $inserted_content . $block_content;
} elseif ( 'after' === $relative_position ) {
$block_content = $block_content . $inserted_content;
}
}

return $block_content;
}
add_filter( 'render_block', 'gutenberg_auto_insert_blocks', 10, 2 );
19 changes: 19 additions & 0 deletions packages/block-library/src/avatar/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,22 @@ function register_block_core_avatar() {
);
}
add_action( 'init', 'register_block_core_avatar' );

/**
* Registers the `core/avatar` auto-insert block pattern.
*/
function register_avatar_auto_insert_block_pattern() {
register_block_pattern(
'core/avatar-auto-insert',
array(
'title' => __( 'Avatar Auto-insert block pattern' ),
'autoInsert' => array( 'lastChild' ),
'blockTypes' => array( 'core/comment-template' ),
'description' => _x( 'Display an avatar of a user, author, or comment.', 'Block pattern description' ),
'categories' => array( 'text' ),
'keywords' => array( 'avatar', 'user', 'author', 'comment' ),
'content' => '<!-- wp:avatar {"size":40,"style":{"border":{"radius":"10px"}}} /-->',
)
);
}
add_action( 'init', 'register_avatar_auto_insert_block_pattern', 100 );
21 changes: 21 additions & 0 deletions packages/block-library/src/social-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,24 @@ function block_core_social_link_get_color_classes( $context ) {

return ' ' . implode( ' ', $classes );
}

/**
* Registers the `core/social-link` auto-insert block pattern.
*/
function register_social_link_auto_insert_block_pattern() {
register_block_pattern(
'core/social-link-auto-insert',
array(
'title' => __( 'Social Link Auto-insert block pattern' ),
'autoInsert' => array( 'after' ),
'blockTypes' => array( 'core/post-content' ),
'description' => _x( 'Automatically add a WordPress Social Link block to the end of your posts.', 'Block pattern description' ),
'categories' => array( 'text' ),
'keywords' => array( 'avatar', 'user', 'author', 'comment' ),
'content' => '<!-- wp:social-links -->' .
'<ul class="wp-block-social-links"><!-- wp:social-link {"url":"https://wordpress.org","service":"wordpress"} /--></ul>' .
'<!-- /wp:social-links -->',
)
);
}
add_action( 'init', 'register_social_link_auto_insert_block_pattern', 100 );
5 changes: 5 additions & 0 deletions phpunit/blocks/render-comment-template-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ public function test_inner_block_inserted_by_render_block_data_is_retained() {
return $parsed_block;
};

// Remove auto-insertion filter so it won't collide.
remove_filter( 'render_block_data', 'gutenberg_auto_insert_child_block' );

add_filter( 'render_block_data', $render_block_data_callback, 10, 1 );
$parsed_blocks = parse_blocks(
'<!-- wp:comments --><!-- wp:comment-template --><!-- wp:comment-content /--><!-- /wp:comment-template --><!-- /wp:comments -->'
Expand All @@ -154,6 +157,8 @@ public function test_inner_block_inserted_by_render_block_data_is_retained() {
);
$block->render();
remove_filter( 'render_block_data', $render_block_data_callback );
// Add back auto-insertion filter.
add_filter( 'render_block_data', 'gutenberg_auto_insert_child_block', 10, 1 );

$this->assertSame( 5, $render_block_callback->get_call_count() );

Expand Down