diff --git a/src/BlockTypes/AbstractBlock.php b/src/BlockTypes/AbstractBlock.php index 31f51201445..2ed75497878 100644 --- a/src/BlockTypes/AbstractBlock.php +++ b/src/BlockTypes/AbstractBlock.php @@ -37,8 +37,13 @@ abstract class AbstractBlock { /** * Constructor + * + * @param string $block_name Optional set block name during construct. */ - public function __construct() { + public function __construct( $block_name = '' ) { + if ( $block_name ) { + $this->block_name = $block_name; + } add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ] ); } @@ -102,7 +107,7 @@ public function enqueue_editor_assets() { * @return string Rendered block with data attributes. */ protected function inject_html_data_attributes( $content, array $attributes ) { - return preg_replace( '/
get_html_data_attributes( $attributes ), $content, 1 ); + return preg_replace( '/
get_html_data_attributes( $attributes ) . ' ', $content, 1 ); } /** @@ -118,6 +123,9 @@ protected function get_html_data_attributes( array $attributes ) { if ( is_bool( $value ) ) { $value = $value ? 'true' : 'false'; } + if ( ! is_scalar( $value ) ) { + $value = wp_json_encode( $value ); + } $data[] = 'data-' . esc_attr( strtolower( preg_replace( '/(?