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