1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helick\Blocks; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Block as CarbonFieldsBlock; |
6
|
|
|
use Exception; |
7
|
|
|
use Helick\Blocks\Contracts\Composable; |
8
|
|
|
use Helick\Blocks\Exceptions\BlockException; |
9
|
|
|
|
10
|
|
|
abstract class Block implements Composable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The block's display name. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $name = ''; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The block's description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = ''; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The block's category. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $category = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The block's icon. |
35
|
|
|
* |
36
|
|
|
* @see https://developer.wordpress.org/resource/dashicons/ |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $icon = ''; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The block's keywords. |
44
|
|
|
* |
45
|
|
|
* @var string[] |
46
|
|
|
*/ |
47
|
|
|
protected $keywords = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Control the block's preview mode. |
51
|
|
|
* |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
protected $preview = true; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Control the block's ability of containing nested blocks. |
58
|
|
|
* |
59
|
|
|
* @var bool |
60
|
|
|
*/ |
61
|
|
|
protected $nested = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Control the position at which the nested blocks will render. |
65
|
|
|
* |
66
|
|
|
* The possible values are: |
67
|
|
|
* - above, |
68
|
|
|
* - below. |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
protected $nestedPosition = ''; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* This property allows you to set a template of blocks which every new instance of your block will contain. |
76
|
|
|
* |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
protected $nestedTemplates = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* This property allows you to lock the area that contains the nested blocks. |
83
|
|
|
* |
84
|
|
|
* The possible values are: |
85
|
|
|
* - all – prevents all operations. |
86
|
|
|
* - insert – prevents inserting or removing blocks, but allows moving existing ones. |
87
|
|
|
* - false – prevents locking from being applied to the nested blocks area even if a parent block contains locking. |
88
|
|
|
* - null – disables any locks applied to the area. |
89
|
|
|
* |
90
|
|
|
* @var string |
91
|
|
|
*/ |
92
|
|
|
protected $nestedLock = ''; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* This property allows you to restrict the type of blocks that can be inserted in the nested blocks area. |
96
|
|
|
* |
97
|
|
|
* @var null|string[] |
98
|
|
|
*/ |
99
|
|
|
protected $nestedBlocks = null; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* This property allows you to restrict the block to be inserted to specific block types. |
103
|
|
|
* |
104
|
|
|
* @var null|string|string[] |
105
|
|
|
*/ |
106
|
|
|
protected $parent = null; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* The block's template. |
110
|
|
|
* |
111
|
|
|
* @var string|string[] |
112
|
|
|
*/ |
113
|
|
|
protected $template = ''; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Captures the output that is generated when a template is included. |
117
|
|
|
* This property is static to prevent object scope resolution. |
118
|
|
|
* |
119
|
|
|
* @param string $template |
120
|
|
|
* @param array $data |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
* |
124
|
|
|
* @throws Exception |
125
|
|
|
*/ |
126
|
|
|
private static function capture(string $template, array $data): string |
127
|
|
|
{ |
128
|
|
|
extract($data, EXTR_SKIP); |
129
|
|
|
|
130
|
|
|
ob_start(); |
131
|
|
|
|
132
|
|
|
try { |
133
|
|
|
include $template; |
134
|
|
|
} catch (Exception $e) { |
135
|
|
|
ob_end_clean(); |
136
|
|
|
|
137
|
|
|
throw $e; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return ob_get_clean(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Fields to be attached to the block. |
145
|
|
|
* |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public function fields(): array |
149
|
|
|
{ |
150
|
|
|
return []; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Data to be passed to the rendered block. |
155
|
|
|
* |
156
|
|
|
* @param array $fields |
157
|
|
|
* |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
public function with(array $fields): array |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
return []; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Compose the block. |
167
|
|
|
* |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
|
|
final public function compose(): void |
171
|
|
|
{ |
172
|
|
|
if (empty($this->name)) { |
173
|
|
|
throw BlockException::forEmptyName(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
CarbonFieldsBlock::make($this->name) |
177
|
|
|
->add_fields($this->fields()) |
178
|
|
|
->set_description($this->description) |
179
|
|
|
->set_category($this->category) |
180
|
|
|
->set_icon($this->icon) |
181
|
|
|
->set_keywords($this->keywords) |
182
|
|
|
->set_inner_blocks($this->nested) |
183
|
|
|
->set_inner_blocks_position($this->nestedPosition) |
184
|
|
|
->set_inner_blocks_template($this->nestedTemplates) |
185
|
|
|
->set_inner_blocks_template_lock($this->nestedLock) |
186
|
|
|
->set_allowed_inner_blocks($this->nestedBlocks) |
187
|
|
|
->set_parent($this->parent) |
188
|
|
|
->set_render_callback([$this, 'render']); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Render the block. |
193
|
|
|
* |
194
|
|
|
* [!!] Global variables with the same key name as local variables will be |
195
|
|
|
* overwritten by the local variable. |
196
|
|
|
* |
197
|
|
|
* @param array $fields |
198
|
|
|
* @param array $attributes |
199
|
|
|
* @param array $blocks |
200
|
|
|
* |
201
|
|
|
* @return void |
202
|
|
|
* |
203
|
|
|
* @throws Exception |
204
|
|
|
*/ |
205
|
|
|
final public function render(array $fields, array $attributes, array $blocks): void |
206
|
|
|
{ |
207
|
|
|
$globalVariables = compact('fields', 'attributes', 'blocks'); |
208
|
|
|
$localVariables = $this->with($fields); |
209
|
|
|
|
210
|
|
|
$data = array_merge($globalVariables, $localVariables); |
211
|
|
|
|
212
|
|
|
echo static::capture($this->template(), $data); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get the block's template. |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
final protected function template(): string |
221
|
|
|
{ |
222
|
|
|
if (empty($this->template)) { |
223
|
|
|
throw BlockException::forEmptyTemplate(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$template = locate_template($this->template); |
|
|
|
|
227
|
|
|
if ($template === '') { |
228
|
|
|
throw BlockException::forNotFoundTemplate($this->template); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $template; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Get the block's path. |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
final protected function path(): string |
240
|
|
|
{ |
241
|
|
|
return dirname(__FILE__); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Get the block's uri. |
246
|
|
|
* |
247
|
|
|
* @param string $uri |
248
|
|
|
* |
249
|
|
|
* @return string |
250
|
|
|
*/ |
251
|
|
|
final protected function uri(string $uri = ''): string |
252
|
|
|
{ |
253
|
|
|
return str_replace(get_theme_file_path(), get_theme_file_uri(), home_url($uri)); |
|
|
|
|
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.