Skip to content

Commit

Permalink
Move gutenberg_block_bindings_replace_html inside check
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot committed Jun 6, 2024
1 parent dee02d3 commit 8a37087
Showing 1 changed file with 57 additions and 57 deletions.
114 changes: 57 additions & 57 deletions lib/compat/wordpress-6.5/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,68 +46,68 @@ function gutenberg_register_metadata_attribute( $args ) {
}
add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' );

/**
* Depending on the block attribute name, replace its value in the HTML based on the value provided.
*
* @param string $block_content Block Content.
* @param string $block_name The name of the block to process.
* @param string $attribute_name The attribute name to replace.
* @param mixed $source_value The value used to replace in the HTML.
* @return string The modified block content.
*/
function gutenberg_block_bindings_replace_html( $block_content, $block_name, string $attribute_name, $source_value ) {
var_dump( 'replace gutenberg' );
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
if ( ! isset( $block_type->attributes[ $attribute_name ]['source'] ) ) {
return $block_content;
}
// Only process block bindings if they are not processed in core.
if ( ! class_exists( 'WP_Block_Bindings_Registry' ) ) {
/**
* Depending on the block attribute name, replace its value in the HTML based on the value provided.
*
* @param string $block_content Block Content.
* @param string $block_name The name of the block to process.
* @param string $attribute_name The attribute name to replace.
* @param mixed $source_value The value used to replace in the HTML.
* @return string The modified block content.
*/
function gutenberg_block_bindings_replace_html( $block_content, $block_name, string $attribute_name, $source_value ) {
var_dump( 'replace gutenberg' );
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
if ( ! isset( $block_type->attributes[ $attribute_name ]['source'] ) ) {
return $block_content;
}

// Depending on the attribute source, the processing will be different.
switch ( $block_type->attributes[ $attribute_name ]['source'] ) {
case 'html':
case 'rich-text':
// Hardcode the selectors and processing until the HTML API is able to read CSS selectors and replace inner HTML.
// TODO: Use the HTML API instead.
if ( 'core/paragraph' === $block_name && 'content' === $attribute_name ) {
$selector = 'p';
}
if ( 'core/heading' === $block_name && 'content' === $attribute_name ) {
$selector = 'h[1-6]';
}
if ( 'core/button' === $block_name && 'text' === $attribute_name ) {
// Check if it is a <button> or <a> tag.
if ( preg_match( '/<button[^>]*>.*?<\/button>/', $block_content ) ) {
$selector = 'button';
} else {
$selector = 'a';
// Depending on the attribute source, the processing will be different.
switch ( $block_type->attributes[ $attribute_name ]['source'] ) {
case 'html':
case 'rich-text':
// Hardcode the selectors and processing until the HTML API is able to read CSS selectors and replace inner HTML.
// TODO: Use the HTML API instead.
if ( 'core/paragraph' === $block_name && 'content' === $attribute_name ) {
$selector = 'p';
}
}
if ( empty( $selector ) ) {
return $block_content;
}
$pattern = '/(<' . $selector . '[^>]*>).*?(<\/' . $selector . '>)/i';
return preg_replace( $pattern, '$1' . wp_kses_post( $source_value ) . '$2', $block_content );

case 'attribute':
$amended_content = new WP_HTML_Tag_Processor( $block_content );
if ( ! $amended_content->next_tag(
array(
// TODO: build the query from CSS selector.
'tag_name' => $block_type->attributes[ $attribute_name ]['selector'],
)
) ) {
return $block_content;
}
$amended_content->set_attribute( $block_type->attributes[ $attribute_name ]['attribute'], $source_value );
return $amended_content->get_updated_html();
if ( 'core/heading' === $block_name && 'content' === $attribute_name ) {
$selector = 'h[1-6]';
}
if ( 'core/button' === $block_name && 'text' === $attribute_name ) {
// Check if it is a <button> or <a> tag.
if ( preg_match( '/<button[^>]*>.*?<\/button>/', $block_content ) ) {
$selector = 'button';
} else {
$selector = 'a';
}
}
if ( empty( $selector ) ) {
return $block_content;
}
$pattern = '/(<' . $selector . '[^>]*>).*?(<\/' . $selector . '>)/i';
return preg_replace( $pattern, '$1' . wp_kses_post( $source_value ) . '$2', $block_content );

case 'attribute':
$amended_content = new WP_HTML_Tag_Processor( $block_content );
if ( ! $amended_content->next_tag(
array(
// TODO: build the query from CSS selector.
'tag_name' => $block_type->attributes[ $attribute_name ]['selector'],
)
) ) {
return $block_content;
}
$amended_content->set_attribute( $block_type->attributes[ $attribute_name ]['attribute'], $source_value );
return $amended_content->get_updated_html();

default:
return $block_content;
default:
return $block_content;
}
}
}

// Only process block bindings if they are not processed in core.
if ( ! class_exists( 'WP_Block_Bindings_Registry' ) ) {
/**
* Process the block bindings attribute.
*
Expand Down

0 comments on commit 8a37087

Please sign in to comment.