Skip to content

Commit 7010619

Browse files
readme update
1 parent 5d1003a commit 7010619

File tree

2 files changed

+19
-90
lines changed

2 files changed

+19
-90
lines changed

README.md

Lines changed: 14 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
# Block Plugin Support
22

3-
We find ourselves increasingly creating a bunch of plugins that add one or more blocks
4-
to the block editor.
3+
We find ourselves increasingly creating a bunch of plugins that add one or more blocks to the block editor.
54

6-
This requires a few functions to load and register. And since we use the same scaffold
7-
almost any time, it makes sense to split out the block part into a library.
5+
This requires a few functions to load and register. And since we use the same scaffold almost any time, it makes sense to split out the block part into a library.
86

9-
The library consists of a simple trait for making the plugin support blocks and an abstract class
10-
for a base server-side rendered block.
7+
The library consists of a simple trait for making the plugin support blocks and two abstract classes for a base server-side rendered block.
118

129
## Getting started
1310

14-
All you need to do is include the `Has_Blocks` trait in the plugin class, add the loader
15-
with your custom prefix when initializing the plugin, and finally, add
16-
the blocks by implementing the `blocks` method.
11+
All you need to do is include the `Has_Blocks` trait in the plugin class as long as you are using the latest version of our plugin base, which recognizes this trait automatically.
12+
13+
Blocks are assumed to be placed in a `blocks` subfolder, and they are being built to `dist/blocks`.
1714

1815
**This trait relies on an autoloader being used in the plugin!**
1916

@@ -24,97 +21,25 @@ class My_Plugin {
2421
2522
use Has_Blocks;
2623
27-
public function __construct() {
28-
29-
// Load blocks.
30-
$this->load_blocks( 'my-block-prefix' );
31-
32-
}
33-
34-
public function blocks() {
35-
36-
// Full JS only block.
37-
$this->add_block( 'my-js-block-name' );
38-
39-
// Server side rendered block, class extends abstract "Block" class.
40-
$this->add_block( 'my-server-side-rendered-block', [
41-
'render_callback' => [ My_Block::class, 'render' ],
42-
'attributes' => My_Block::get_attributes(),
43-
] );
44-
45-
// Load block conditionally on class being loaded.
46-
$this->add_block_if( 'My_Class', 'my-js-block' );
47-
48-
}
49-
5024
}
5125
```
5226

53-
### Customize Loading Priority
54-
55-
We run our block enqueue fairly late by default at a 999 priority on `enqueue_block_editor_assets`. To customize this order,
56-
you may override the following static variable on the class:
57-
58-
```
59-
/**
60-
* The priority of the block editor asset enqueue hook.
61-
*
62-
* @var integer
63-
*/
64-
protected static $block_assets_load_priority = 999;
65-
```
66-
67-
### Customize Dependencies
68-
69-
Each block requires a list of script dependencies when loaded. These are typically the core WordPress block library scripts,
70-
or any other script dependency that we need.
71-
72-
To customize this, add a `dependencies => []` section to the block arguments array when adding the block. A standard block would then look like this:
73-
74-
```
75-
$this->add_block( 'my-js-block-name', [
76-
'dependencies' => [
77-
'wp-i18n'
78-
],
79-
] );
80-
```
81-
82-
If you do not set dependencies for a specific block, the global dependency list will be used.
83-
84-
By default, the global dependency list contains a most-used set of WP scripts. To modify it, you can ovverride the `protected static $block_dependencies = []` class property.
85-
86-
```
87-
/**
88-
* Packages that are loaded as dependencies for the blocks globally,
89-
* unless the block specifies its own dependency list.
90-
*
91-
* @var string[]
92-
*/
93-
protected static $block_dependencies = [
94-
'wp-blocks',
95-
'wp-components',
96-
'wp-element',
97-
'wp-i18n',
98-
'wp-block-editor',
99-
];
100-
```
101-
10227
# Required Folder Structure
10328

10429
The trait assumes the following folder structure:
10530

106-
`dist/` is the location of built JavaScript blocks, named with the same name as you add the block with using `add_block()`.
31+
`blocks/` is the location of all blocks with one folder per block. The `index.js` file in each block folder is the entrypoint for the build script.
32+
33+
`dist/blocks` is the location of built JavaScript blocks with the same filename as the main folder name.
10734

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

11037
# Required Methods
11138

112-
The trait relies on four methods to be implemented, outside of `blocks()`. These are:
39+
The trait relies on two methods to be implemented.
11340

114-
- `get_version()` that should return a string of the current plugin version. Used for versioning the blocks.
115-
- `get_url()` should return the URL to the plugin directory.
116-
- `get_path()` should return the path to the plugin directory.
117-
- `get_textdomain()` should return the plugin textdomain.
41+
- `get_url()` should return the URL to the plugin directory.
42+
- `get_path()` should return the path to the plugin directory.
11843

11944
# Hooks & Filters
12045

@@ -124,6 +49,5 @@ Since this is a composer package, filters are housed inside of the "package name
12449

12550
## Filters
12651

127-
`bm_block_support_{$BLOCKNAME}_attributes`. Available for any dynamic block that extends `Block` and is reliant on `Block::$name` being set in the consuming dynamic block class. Allows you to customize the specific block attributes.
128-
129-
`bm_block_support_{$BLOCKNAME}_wrapper_args`. Available for any dynamic block that extends `Block` and is reliant on `Block::$name` being set in the consuming dynamic block class. Allows you to customize the args sent to `get_block_wrapper_attributes`.
52+
`bm_block_support_{$BLOCKNAME}_wrapper_args`. Available for any dynamic block that extends `Block` and is reliant on `Block::$name` being set in the consuming dynamic block class.
53+
Allows you to customize the args sent to `get_block_wrapper_attributes`.

src/Block.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
*/
1010
abstract class Block {
1111

12+
/**
13+
* The name of the block with prefix, such a bm/block-name.
14+
*/
15+
protected static string $block_name = '';
16+
1217
/**
1318
* By entering the block name here, we can expose a few filters
1419
* that makes customization easier.

0 commit comments

Comments
 (0)