1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\entities\editor; |
4
|
|
|
|
5
|
|
|
use EventEspresso\core\services\assets\BlockAssetManagerInterface; |
6
|
|
|
use WP_Block_Type; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Block |
10
|
|
|
* Registers a Block type with WordPress core |
11
|
|
|
* and executes all logic as necessary |
12
|
|
|
* ALL blocks should be located in |
13
|
|
|
* \core\domain\entities\editor\blocks\ |
14
|
|
|
* under the appropriate namespace root |
15
|
|
|
* |
16
|
|
|
* @package EventEspresso\core\services\editor |
17
|
|
|
* @author Brent Christensen |
18
|
|
|
* @since $VID:$ |
19
|
|
|
*/ |
20
|
|
|
abstract class Block implements BlockInterface |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* BlockAssetManager that this editor block uses for asset registration |
25
|
|
|
* |
26
|
|
|
* @var BlockAssetManagerInterface $block_asset_manager |
27
|
|
|
*/ |
28
|
|
|
protected $block_asset_manager; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array $attributes |
32
|
|
|
*/ |
33
|
|
|
private $attributes; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* If set to true, then the block will render its content client side |
37
|
|
|
* If false, then the block will render its content server side using the renderBlock() method |
38
|
|
|
* |
39
|
|
|
* @var bool $dynamic |
40
|
|
|
*/ |
41
|
|
|
private $dynamic = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string $block_type |
45
|
|
|
*/ |
46
|
|
|
private $block_type; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var array $supported_routes |
50
|
|
|
*/ |
51
|
|
|
private $supported_routes; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var WP_Block_Type $wp_block_type |
55
|
|
|
*/ |
56
|
|
|
private $wp_block_type; |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* BlockLoader constructor. |
61
|
|
|
* |
62
|
|
|
* @param BlockAssetManagerInterface $block_asset_manager |
63
|
|
|
*/ |
64
|
|
|
public function __construct(BlockAssetManagerInterface $block_asset_manager) |
65
|
|
|
{ |
66
|
|
|
$this->block_asset_manager = $block_asset_manager; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function blockType() |
74
|
|
|
{ |
75
|
|
|
return $this->block_type; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function namespacedBlockType() |
83
|
|
|
{ |
84
|
|
|
return self::NAME_SPACE . '/' . $this->block_type; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $block_type |
90
|
|
|
*/ |
91
|
|
|
protected function setBlockType($block_type) |
92
|
|
|
{ |
93
|
|
|
$this->block_type = $block_type; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* BlockAssetManager that this editor block uses for asset registration |
99
|
|
|
* |
100
|
|
|
* @return BlockAssetManagerInterface |
101
|
|
|
*/ |
102
|
|
|
public function assetManager() |
103
|
|
|
{ |
104
|
|
|
return $this->block_asset_manager; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param WP_Block_Type $wp_block_type |
110
|
|
|
*/ |
111
|
|
|
protected function setWpBlockType($wp_block_type) |
112
|
|
|
{ |
113
|
|
|
$this->wp_block_type = $wp_block_type; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* returns an array of fully qualified class names |
118
|
|
|
* for RouteMatchSpecificationInterface objects |
119
|
|
|
* that specify routes that the block should be loaded for. |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function supportedRoutes() |
124
|
|
|
{ |
125
|
|
|
return $this->supported_routes; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array $supported_routes |
131
|
|
|
*/ |
132
|
|
|
protected function setSupportedRoutes(array $supported_routes) |
133
|
|
|
{ |
134
|
|
|
$this->supported_routes = $supported_routes; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public function attributes() |
142
|
|
|
{ |
143
|
|
|
return $this->attributes; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param array $attributes |
149
|
|
|
*/ |
150
|
|
|
public function setAttributes(array $attributes) |
151
|
|
|
{ |
152
|
|
|
$this->attributes = $attributes; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function isDynamic() |
160
|
|
|
{ |
161
|
|
|
return $this->dynamic; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param bool $dynamic |
167
|
|
|
*/ |
168
|
|
|
public function setDynamic($dynamic = true) |
169
|
|
|
{ |
170
|
|
|
$this->dynamic = filter_var($dynamic, FILTER_VALIDATE_BOOLEAN); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Registers the Editor Block with WP core; |
176
|
|
|
* Returns the registered block type on success, or false on failure. |
177
|
|
|
* |
178
|
|
|
* @return WP_Block_Type|false |
179
|
|
|
*/ |
180
|
|
|
public function registerBlock() |
181
|
|
|
{ |
182
|
|
|
$args = array( |
183
|
|
|
'attributes' => $this->attributes(), |
184
|
|
|
'editor_script' => $this->block_asset_manager->getEditorScriptHandle(), |
185
|
|
|
'editor_style' => $this->block_asset_manager->getEditorStyleHandle(), |
186
|
|
|
'script' => $this->block_asset_manager->getScriptHandle(), |
187
|
|
|
'style' => $this->block_asset_manager->getStyleHandle(), |
188
|
|
|
); |
189
|
|
|
if ($this->isDynamic()) { |
190
|
|
|
$args['render_callback'] = array($this, 'renderBlock'); |
191
|
|
|
} |
192
|
|
|
$wp_block_type = register_block_type( |
193
|
|
|
new WP_Block_Type( |
194
|
|
|
$this->namespacedBlockType(), |
195
|
|
|
$args |
196
|
|
|
) |
197
|
|
|
); |
198
|
|
|
$this->setWpBlockType($wp_block_type); |
199
|
|
|
return $wp_block_type; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return WP_Block_Type|false The registered block type on success, or false on failure. |
205
|
|
|
*/ |
206
|
|
|
public function unRegisterBlock() |
207
|
|
|
{ |
208
|
|
|
return unregister_block_type($this->namespacedBlockType()); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return array |
215
|
|
|
*/ |
216
|
|
|
public function getEditorContainer() |
217
|
|
|
{ |
218
|
|
|
return array( |
219
|
|
|
$this->namespacedBlockType(), |
220
|
|
|
array(), |
221
|
|
|
); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* returns the rendered HTML for the block |
227
|
|
|
* |
228
|
|
|
* @param array $attributes |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
|
|
public function renderBlock(array $attributes = array()) |
232
|
|
|
{ |
233
|
|
|
return ''; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|