diff --git a/commands/stubs/templates/block.php b/commands/stubs/templates/block.php
index 6d19fc4..20ee6eb 100644
--- a/commands/stubs/templates/block.php
+++ b/commands/stubs/templates/block.php
@@ -34,21 +34,10 @@
);
?>
-
-
-
':BLOCK_HTML_BASE_CLASS__outer-wrapper' ) ); ?>>
-
-
-
-
diff --git a/includes/class-block.php b/includes/class-block.php
index 9080116..9cf7563 100644
--- a/includes/class-block.php
+++ b/includes/class-block.php
@@ -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.
*
@@ -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() ) );
}
}
);
@@ -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;
}
@@ -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.
*
@@ -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.
*
@@ -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,
@@ -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';
diff --git a/includes/traits/trait-has-modifier-classes.php b/includes/traits/trait-has-modifier-classes.php
index 930ab36..31e5e6f 100644
--- a/includes/traits/trait-has-modifier-classes.php
+++ b/includes/traits/trait-has-modifier-classes.php
@@ -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() )
)
);
}
@@ -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();
+ }
}
diff --git a/includes/traits/trait-has-reduce-bottom-space-option.php b/includes/traits/trait-has-reduce-bottom-space-option.php
index 6af99d6..310ff3d 100644
--- a/includes/traits/trait-has-reduce-bottom-space-option.php
+++ b/includes/traits/trait-has-reduce-bottom-space-option.php
@@ -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',
@@ -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' );
diff --git a/includes/traits/trait-has-unique-id.php b/includes/traits/trait-has-unique-id.php
index b4cf5a0..94469e7 100644
--- a/includes/traits/trait-has-unique-id.php
+++ b/includes/traits/trait-has-unique-id.php
@@ -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(
diff --git a/includes/traits/trait-restrict-to-editor-context.php b/includes/traits/trait-restrict-to-editor-context.php
index 7cae026..20a7508 100644
--- a/includes/traits/trait-restrict-to-editor-context.php
+++ b/includes/traits/trait-restrict-to-editor-context.php
@@ -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',
diff --git a/templates/default-wrapper.php b/templates/default-wrapper.php
new file mode 100644
index 0000000..f4aa8db
--- /dev/null
+++ b/templates/default-wrapper.php
@@ -0,0 +1,65 @@
+
+
+
+
+
$block_name . '__outer-wrapper' ) ); ?>>
+
+
+
+
+ get_template(); ?>
+
+
+
+
+
+