Skip to content

Commit 525d876

Browse files
Allow custom block prefix
1 parent b8fa3c5 commit 525d876

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec
44

55
## [Unreleased]
66

7+
## [2.1.0] - 2021-09-22
8+
9+
### Changed
10+
11+
- Now allows registering of a custom block prefix instead of the default `bm`.
12+
713
## [2.0.3] - 2021-07-30
814

915
### Fixed

README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,25 @@ class My_Plugin {
2424
}
2525
```
2626

27-
# Required Folder Structure
27+
### Defining a custom block prefix
28+
The trait will default to using the 'bm' prefix for blocks. Blocks are named with a prefix to scope them to the project they are a part of. This also influences how the script and style handles are registered ($prefix-block-$block_name).
29+
30+
Your project probably requires its custom prefix. You can set it on the main plugin class like so:
31+
32+
```
33+
use BernskioldMedia\WP\Block_Plugin_Support\Traits\Has_Blocks;
34+
35+
class My_Plugin {
36+
37+
use Has_Blocks;
38+
39+
// Set my custom block prefix.
40+
protected static string $block_prefix = 'my-prefix';
41+
42+
}
43+
```
44+
45+
## Required Folder Structure
2846

2947
The trait assumes the following folder structure:
3048

@@ -34,14 +52,15 @@ The trait assumes the following folder structure:
3452

3553
`languages/` is the location of the translation files. The handle and domain are both set to `{$block_prefix}-{$block_name}`.
3654

37-
# Required Methods
55+
## Required Methods
3856

3957
The trait relies on two methods to be implemented.
4058

4159
- `get_url()` should return the URL to the plugin directory.
4260
- `get_path()` should return the path to the plugin directory.
61+
- `get_textdomain()` should return the string plugin textdomain.
4362

44-
# Hooks & Filters
63+
## Hooks & Filters
4564

4665
We strive to make all code easily customizable with plenty of filters and hooks as needed. You never know when or why you might need that simple one-off customization.
4766

src/Traits/Has_Blocks.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
* Designed to extend a core plugin file that is a plugin
99
* that provides blocks for output.
1010
*
11-
* @property array $conditional_blocks An array of blocks that are only loaded if the corresponding class name exists. blockname => classname.
12-
* @property array $dynamic_blocks An array of blocks that are dynamic, meaning they use PHP to load on the frontend. blockname => classname.
11+
* @property array $conditional_blocks An array of blocks that are only loaded if the corresponding class name exists. blockname => classname.
12+
* @property array $dynamic_blocks An array of blocks that are dynamic, meaning they use PHP to load on the frontend. blockname => classname.
13+
* @property string $block_prefix The prefix for all the blocks in this plugin.
1314
*
1415
* @package BernskioldMedia\WP\Block_Plugin_Support\Traits
1516
*/
@@ -43,7 +44,8 @@ public function register_blocks(): void {
4344
$asset_meta = include static::get_path() . 'dist/blocks/' . $name . '.asset.php';
4445

4546
// Register the script with WordPress.
46-
wp_register_script( 'bm-block-' . $name, static::get_url( 'dist/blocks/' . $name . '.js' ), $asset_meta['dependencies'], $asset_meta['version'], true );
47+
wp_register_script( static::get_block_prefix() . '-block-' . $name, static::get_url( 'dist/blocks/' . $name . '.js' ), $asset_meta['dependencies'],
48+
$asset_meta['version'], true );
4749

4850
// Register the block. Dynamic blocks get their callback.
4951
if ( isset( static::$dynamic_blocks[ $name ] ) ) {
@@ -55,29 +57,28 @@ public function register_blocks(): void {
5557
register_block_type( $directory, [
5658
'render_callback' => [ static::$dynamic_blocks[ $name ], 'render' ],
5759
] );
58-
} else {
60+
}
61+
else {
5962
register_block_type( $directory );
6063
}
6164

6265
// Load translations.
63-
wp_set_script_translations( 'bm-block-' . $name, static::get_textdomain(), static::get_path( 'languages/' ) );
66+
wp_set_script_translations( static::get_block_prefix() . '-block-' . $name, static::get_textdomain(), static::get_path( 'languages/' ) );
6467
}
6568
}
6669

6770
/**
68-
* Get the URL to the plugin folder, or the specified
69-
* file relative to the plugin folder home.
70-
*/
71-
abstract public static function get_url( string $file = '' ): string;
72-
73-
/**
74-
* Get the path to the plugin folder, or the specified
75-
* file relative to the plugin folder home.
71+
* Get the prefix that we use when registering the blocks. To set your
72+
* own prefix, just define the protected static string $block_prefix property
73+
* on your base plugin class.
74+
*
75+
* @return string
7676
*/
77-
abstract public static function get_path( string $file = '' ): string;
77+
protected static function get_block_prefix(): string {
78+
if ( property_exists( static::class, 'block_prefix' ) ) {
79+
return static::$block_prefix;
80+
}
7881

79-
/**
80-
* Get the textdomain for the plugin.
81-
*/
82-
abstract public static function get_textdomain(): string;
82+
return 'bm';
83+
}
8384
}

0 commit comments

Comments
 (0)