Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Updates for the abstract block class #2566

Merged
merged 1 commit into from
May 27, 2020
Merged
Changes from all commits
Commits
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
12 changes: 10 additions & 2 deletions src/BlockTypes/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
}

Expand Down Expand Up @@ -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( '/<div /', '<div ' . $this->get_html_data_attributes( $attributes ), $content, 1 );
return preg_replace( '/<div /', '<div ' . $this->get_html_data_attributes( $attributes ) . ' ', $content, 1 );
}

/**
Expand All @@ -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( '/(?<!\ )[A-Z]/', '-$0', $key ) ) ) . '="' . esc_attr( $value ) . '"';
}

Expand Down