-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractBlock.php
More file actions
91 lines (80 loc) · 2.34 KB
/
Copy pathAbstractBlock.php
File metadata and controls
91 lines (80 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* Abstract Block class.
*
* Provides a clean interface for registering dynamic WordPress blocks.
* For static blocks (JSON-only, no server-side render), use the AssetLoader
* class with `register_block_manifest()` directly.
*
* @package rtCamp\WPFramework\Contracts\Abstracts
* @since 1.0.0
*/
declare( strict_types = 1 );
namespace rtCamp\WPFramework\Contracts\Abstracts;
use rtCamp\WPFramework\Contracts\Interfaces\Registrable;
/**
* Class - AbstractBlock
*/
abstract class AbstractBlock implements Registrable {
/**
* Get the block name (including namespace).
*
* E.g. 'my-plugin/hero-banner'.
*
* @return non-empty-string
*/
abstract public static function get_name(): string;
/**
* Render the block on the server side.
*
* Public because WordPress calls it directly as the block's render_callback
* (wired in register_block()); a protected method would fatal when core invokes it.
*
* @param array<string, mixed> $attributes Block attributes.
* @param string $content Block inner content.
* @param \WP_Block $block Block instance.
*
* @return string Rendered block HTML.
*/
abstract public function render( array $attributes, string $content, \WP_Block $block ): string;
/**
* {@inheritDoc}
*/
public function register_hooks(): void {
add_action( 'init', [ $this, 'register_block' ] );
}
/**
* Register the block type with WordPress.
*/
public function register_block(): void {
$args = $this->get_block_args();
// Force our render method, overriding any render_callback from get_block_args().
$args['render_callback'] = [ $this, 'render' ];
$block_dir = $this->get_block_dir();
if ( $block_dir ) {
register_block_type( $block_dir, $args );
} else {
register_block_type( static::get_name(), $args );
}
}
/**
* Get the path to the block's build directory (containing block.json).
*
* Return null to register without a block.json (purely programmatic block).
*
* @return string|null Absolute path to the block directory, or null.
*/
protected function get_block_dir(): ?string {
return null;
}
/**
* Additional arguments to pass to register_block_type().
*
* Override to add attributes, supports, styles, etc.
*
* @return array<string, mixed>
*/
protected function get_block_args(): array {
return [];
}
}