Skip to content

Commit

Permalink
Add pattern support to heading block
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Aug 24, 2023
1 parent efbf443 commit 16f5b8d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
65 changes: 56 additions & 9 deletions lib/block-supports/pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,53 @@ function gutenberg_register_pattern_support( $block_type ) {
}
}

/**
* Creates an HTML tag processor based off the block's content and selector
* for the pattern sourced attribute.
*
* @param string $block_content

Check failure on line 38 in lib/block-supports/pattern.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Missing parameter comment
* @param string $selector

Check failure on line 39 in lib/block-supports/pattern.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Missing parameter comment
*
* @return void

Check failure on line 41 in lib/block-supports/pattern.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Function return type is void, but function contains return statement
*/
function gutenberg_create_pattern_content_processor( $block_content, $selector ) {
if ( ! $selector ) {
return false;
}

$tags = new WP_HTML_Tag_Processor( $block_content );
$is_selector_list = strpos( $selector, ',' ) !== false;

if ( ! $is_selector_list ) {
// TODO: The retrieval via selector could do with some work.
$found = $tags->next_tag( array( 'tag_name' => $selector ) );

return $found ? $tags : false;
}

$found = false;
$selectors = explode( ',', $selector );

foreach ( $selectors as $tag_selector ) {
// TODO: The retrieval via selector could do with some work.
$found = $tags->next_tag( array( 'tag_name' => $tag_selector ) );

if ( $found ) {
break;
}

// TODO: Revisit whether a bookmark can be used here. The need for
// the bookmark to be on a found tag meant that you already needed
// to have searched and found a tag which made the rest of this
// search awkward. Perhaps we could wrap the block in a div and
// create the processor from that content, bookmarking that outer
// div if the current approach isn't performant.
$tags = new WP_HTML_Tag_Processor( $block_content );
}

return $found ? $tags : false;
}

function gutenberg_render_block_pattern_data( $block_content, $block, $block_instance ) {

Check failure on line 81 in lib/block-supports/pattern.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Missing doc comment for function gutenberg_render_block_pattern_data()
$block_type = $block_instance->block_type;

Expand Down Expand Up @@ -72,17 +119,17 @@ function gutenberg_render_block_pattern_data( $block_content, $block, $block_ins
continue;
}

$tags = new WP_HTML_Tag_Processor( $block_content );
$found = $tags->next_tag(
array(
// TODO: Improve the retrieval via selector, see block connections work.
'tag_name' => $block_type->attributes[ $pattern_attribute ]['selector'],
)
);
$selector = _wp_array_get( $block_type->attributes, array( $pattern_attribute, 'selector' ), null );

if ( ! $found ) {
if ( ! $selector ) {
continue;
};
}

$tags = gutenberg_create_pattern_content_processor( $block_content, $selector );

if ( ! $tags ) {
continue;
}

// Only inner html content and DOM attributes are currently processed.

Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/heading/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@
"textTransform": true
}
},
"__experimentalSlashInserter": true
"__experimentalSlashInserter": true,
"__experimentalPattern": {
"content": "content"
}
},
"editorStyle": "wp-block-heading-editor",
"style": "wp-block-heading"
Expand Down

0 comments on commit 16f5b8d

Please sign in to comment.