Skip to content
This repository was archived by the owner on Dec 18, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
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
23 changes: 6 additions & 17 deletions commands/stubs/templates/block.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,10 @@
);
?>

<?php if ( ! $is_preview ) : ?>
<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<div <?php echo get_block_wrapper_attributes( array( 'class' => ':BLOCK_HTML_BASE_CLASS__outer-wrapper' ) ); ?>>
<?php endif; ?>

<div class=":BLOCK_HTML_BASE_CLASS__wrapper">
<div class=":BLOCK_HTML_BASE_CLASS__inner">
<InnerBlocks
allowedBlocks="<?php echo esc_attr( wp_json_encode( $allowed_inner_blocks ) ); ?>"
template="<?php echo esc_attr( wp_json_encode( $inner_block_template ) ); ?>"
class=":BLOCK_HTML_BASE_CLASS__inner-blocks"
/>
</div>
<div class=":BLOCK_HTML_BASE_CLASS__main">
<InnerBlocks
allowedBlocks="<?php echo esc_attr( wp_json_encode( $allowed_inner_blocks ) ); ?>"
template="<?php echo esc_attr( wp_json_encode( $inner_block_template ) ); ?>"
class=":BLOCK_HTML_BASE_CLASS__inner-blocks"
/>
</div>

<?php if ( ! $is_preview ) : ?>
</div>
<?php endif; ?>
56 changes: 51 additions & 5 deletions includes/class-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@

namespace Creode_Blocks;

use Exception;

/**
* Abstract class to extend for each block.
*/
abstract class Block {

use Trait_Has_Modifier_Classes;

/**
* Singleton instance of this class.
*
Expand Down Expand Up @@ -51,7 +56,7 @@ function () {
$this->register_acf_fields();

foreach ( $this->child_blocks() as $child_block ) {
$this->register_child_block( 'acf/' . $this->name(), $child_block, array( 'acf/' . $this->name() ) );
$this->register_child_block( 'acf/' . $this->get_name(), $child_block, array( 'acf/' . $this->get_name() ) );
}
}
);
Expand Down Expand Up @@ -101,7 +106,7 @@ public static function init(): void {
add_filter(
'creode_block_instances',
function ( array $instances ) use ( $instance ) {
$instances[ $instance->name() ] = $instance;
$instances[ $instance->get_name() ] = $instance;

return $instances;
}
Expand All @@ -115,6 +120,22 @@ function ( array $instances ) use ( $instance ) {
*/
abstract protected function name(): string;

/**
* Get the block's name.
*
* @throws Exception If the name isn't valid.
* @return string The block's name.
*/
public function get_name(): string {
$name = $this->name();

if ( ! preg_match( '/^[a-z0-9-]+$/', $name ) ) {
throw new Exception( 'All block names should be lowercase and hyphen-separated. The following is invalid: "' . esc_html( $name ) . '".' );
}

return $name;
}

/**
* Function for providing the block's label to be used within the WordPress UI.
*
Expand Down Expand Up @@ -149,6 +170,31 @@ protected function fields(): array {
*/
abstract protected function template(): string;

/**
* Get the path to the render template.
*
* @throws Exception If the template file does not exist.
* @return string The path to the render template.
*/
public function get_template(): string {
$template = $this->template();

if ( ! file_exists( $template ) ) {
throw new Exception( 'The following template file does not exist: ' . esc_html( $template ) . '.' );
}

return $template;
}

/**
* Returns whether the default wrapper template should be used.
*
* @return bool Whether the default wrapper template should be used.
*/
protected function use_default_wrapper_template(): bool {
return true;
}

/**
* Function for providing the block's category.
*
Expand Down Expand Up @@ -202,7 +248,7 @@ protected function register_acf_block(): void {
'description' => $this->description(),
'category' => $this->category(),
'icon' => $this->icon,
'render' => $this->template(),
'render' => $this->use_default_wrapper_template() ? __DIR__ . '/../templates/default-wrapper.php' : $this->get_template(),
'textdomain' => 'wordpress-blocks',
'supports' => $this->supports(),
'providesContext' => $this->provides_context,
Expand All @@ -214,13 +260,13 @@ protected function register_acf_block(): void {
* Registers the block type using the provided block data.
*
* @param array $block_data The block data.
* @throws \Exception If filesystem cannot be accessed.
* @throws Exception If filesystem cannot be accessed.
* @return void
*/
protected function register_block_type( array $block_data ): void {
$wp_filesystem = $this->get_filesystem();
if ( ! $wp_filesystem ) {
throw new \Exception( 'Cannot cache block. Could not get filesystem.' );
throw new Exception( 'Cannot cache block. Could not get filesystem.' );
}

$cache_folder = WP_CONTENT_DIR . '/cache/wp-blocks';
Expand Down
6 changes: 4 additions & 2 deletions includes/traits/trait-has-modifier-classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function get_modifier_class_string( string $base_class = 'example-block__
function ( string $modifier_class ) use ( $base_class ) {
return $base_class . '--' . $modifier_class;
},
apply_filters( 'block-' . $this->name() . '-modifier-classes', $this->modifier_classes() )
apply_filters( 'block-' . $this->get_name() . '-modifier-classes', $this->modifier_classes() )
)
);
}
Expand All @@ -35,5 +35,7 @@ function ( string $modifier_class ) use ( $base_class ) {
*
* @return array An array of terms.
*/
abstract protected function modifier_classes(): array;
protected function modifier_classes(): array {
return array();
}
}
4 changes: 2 additions & 2 deletions includes/traits/trait-has-reduce-bottom-space-option.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private function trait_has_reduce_bottom_space_option_dependency_check() {
* Adds a "Reduce Bottom Space" field to the current block field group.
*/
private function add_reduce_bottom_space_field() {
$block_name = $this->name();
$block_name = $this->get_name();

add_filter(
'block-' . $block_name . '-fields',
Expand Down Expand Up @@ -66,7 +66,7 @@ function ( array $fields ) use ( $block_name ) {
*/
private function add_reduce_bottom_space_modifier_class() {
add_filter(
'block-' . $this->name() . '-modifier-classes',
'block-' . $this->get_name() . '-modifier-classes',
function ( array $classes ) {
if ( ! empty( $this->get_field( 'reduce_bottom_space' ) ) ) {
array_push( $classes, 'reduce-bottom-space' );
Expand Down
2 changes: 1 addition & 1 deletion includes/traits/trait-has-unique-id.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait Trait_Has_Unique_Id {
* @return string A unique ID.
*/
public function get_unique_id(): string {
$block_name = $this->name();
$block_name = $this->get_name();
$iterator = apply_filters( $block_name . '_iterator', 0 );

add_filter(
Expand Down
2 changes: 1 addition & 1 deletion includes/traits/trait-restrict-to-editor-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function restrict_to_post_editor() {
* @link https://developer.wordpress.org/reference/classes/wp_block_editor_context/ Block context documentation.
*/
protected function restrict_to_editor_context( string $editor_context_name ) {
$block_name = 'acf/' . $this->name();
$block_name = 'acf/' . $this->get_name();

add_filter(
'allowed_block_types_all',
Expand Down
65 changes: 65 additions & 0 deletions templates/default-wrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Defualt block wrapper template.
*
* @package Creode Blocks
*/

/**
* Ensure $block is available if rendering is via an AJAX request.
*
* @var array Details about the current block.
*/
//phpcs:ignore -- requires auth;
$block = ( empty( $block ) && acf_verify_ajax() && isset( $_REQUEST['block'] ) ) ? json_decode( wp_unslash( $_REQUEST['block'] ), true ) : $block;

/**
* If block is still not available, do nothing.
*/
if ( empty( $block ) ) {
return;
}

/**
* If block name is not available, do nothing.
*/
if ( empty( $block['name'] ) ) {
return;
}

/**
* Assign the block's name to a variable and remove the prefix.
*
* @var string The block's name.
*/
$block_name = str_replace( 'acf/', '', $block['name'] );

/**
* Retrieve the Creode_Blocks\Block instance for this block type.
*
* @var Creode_Blocks\Block The instance for this block type.
*/
$creode_block = Creode_Blocks\Helpers::get_block_by_name( $block_name );

/**
* If Creode_Blocks\Block instance cannot be found, do nothing.
*/
if ( ! $creode_block ) {
return;
}
?>

<?php if ( ! $is_preview ) : ?>
<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<div <?php echo get_block_wrapper_attributes( array( 'class' => $block_name . '__outer-wrapper' ) ); ?>>
<?php endif; ?>

<div class="<?php echo esc_attr( $block_name ); ?>__wrapper <?php echo esc_attr( $creode_block->get_modifier_class_string( $block_name . '__wrapper' ) ); ?>">
<div class="<?php echo esc_attr( $block_name ); ?>__inner">
<?php require $creode_block->get_template(); ?>
</div>
</div>

<?php if ( ! $is_preview ) : ?>
</div>
<?php endif; ?>