9
|
1 |
<?php |
|
2 |
/** |
|
3 |
* Blocks API: WP_Block_Type class |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Blocks |
|
7 |
* @since 5.0.0 |
|
8 |
*/ |
|
9 |
|
|
10 |
/** |
|
11 |
* Core class representing a block type. |
|
12 |
* |
|
13 |
* @since 5.0.0 |
|
14 |
* |
|
15 |
* @see register_block_type() |
|
16 |
*/ |
|
17 |
class WP_Block_Type { |
16
|
18 |
|
9
|
19 |
/** |
18
|
20 |
* Block API version. |
|
21 |
* |
|
22 |
* @since 5.6.0 |
|
23 |
* @var int |
|
24 |
*/ |
|
25 |
public $api_version = 1; |
|
26 |
|
|
27 |
/** |
9
|
28 |
* Block type key. |
|
29 |
* |
|
30 |
* @since 5.0.0 |
|
31 |
* @var string |
|
32 |
*/ |
|
33 |
public $name; |
|
34 |
|
|
35 |
/** |
18
|
36 |
* Human-readable block type label. |
|
37 |
* |
16
|
38 |
* @since 5.5.0 |
|
39 |
* @var string |
|
40 |
*/ |
|
41 |
public $title = ''; |
|
42 |
|
|
43 |
/** |
18
|
44 |
* Block type category classification, used in search interfaces |
|
45 |
* to arrange block types by category. |
|
46 |
* |
16
|
47 |
* @since 5.5.0 |
|
48 |
* @var string|null |
|
49 |
*/ |
|
50 |
public $category = null; |
|
51 |
|
|
52 |
/** |
18
|
53 |
* Setting parent lets a block require that it is only available |
|
54 |
* when nested within the specified blocks. |
|
55 |
* |
16
|
56 |
* @since 5.5.0 |
|
57 |
* @var array|null |
|
58 |
*/ |
|
59 |
public $parent = null; |
|
60 |
|
|
61 |
/** |
19
|
62 |
* Setting ancestor makes a block available only inside the specified |
|
63 |
* block types at any position of the ancestor's block subtree. |
|
64 |
* |
|
65 |
* @since 6.0.0 |
|
66 |
* @var array|null |
|
67 |
*/ |
|
68 |
public $ancestor = null; |
|
69 |
|
|
70 |
/** |
18
|
71 |
* Block type icon. |
|
72 |
* |
16
|
73 |
* @since 5.5.0 |
|
74 |
* @var string|null |
|
75 |
*/ |
|
76 |
public $icon = null; |
|
77 |
|
|
78 |
/** |
18
|
79 |
* A detailed block type description. |
|
80 |
* |
16
|
81 |
* @since 5.5.0 |
|
82 |
* @var string |
|
83 |
*/ |
|
84 |
public $description = ''; |
|
85 |
|
|
86 |
/** |
18
|
87 |
* Additional keywords to produce block type as result |
|
88 |
* in search interfaces. |
|
89 |
* |
16
|
90 |
* @since 5.5.0 |
19
|
91 |
* @var string[] |
16
|
92 |
*/ |
|
93 |
public $keywords = array(); |
|
94 |
|
|
95 |
/** |
18
|
96 |
* The translation textdomain. |
|
97 |
* |
16
|
98 |
* @since 5.5.0 |
|
99 |
* @var string|null |
|
100 |
*/ |
|
101 |
public $textdomain = null; |
|
102 |
|
|
103 |
/** |
18
|
104 |
* Alternative block styles. |
|
105 |
* |
16
|
106 |
* @since 5.5.0 |
|
107 |
* @var array |
|
108 |
*/ |
|
109 |
public $styles = array(); |
|
110 |
|
|
111 |
/** |
18
|
112 |
* Block variations. |
|
113 |
* |
|
114 |
* @since 5.8.0 |
|
115 |
* @var array |
|
116 |
*/ |
|
117 |
public $variations = array(); |
|
118 |
|
|
119 |
/** |
|
120 |
* Supported features. |
|
121 |
* |
16
|
122 |
* @since 5.5.0 |
|
123 |
* @var array|null |
|
124 |
*/ |
|
125 |
public $supports = null; |
|
126 |
|
|
127 |
/** |
18
|
128 |
* Structured data for the block preview. |
|
129 |
* |
16
|
130 |
* @since 5.5.0 |
|
131 |
* @var array|null |
|
132 |
*/ |
|
133 |
public $example = null; |
|
134 |
|
|
135 |
/** |
9
|
136 |
* Block type render callback. |
|
137 |
* |
|
138 |
* @since 5.0.0 |
|
139 |
* @var callable |
|
140 |
*/ |
16
|
141 |
public $render_callback = null; |
9
|
142 |
|
|
143 |
/** |
|
144 |
* Block type attributes property schemas. |
|
145 |
* |
|
146 |
* @since 5.0.0 |
16
|
147 |
* @var array|null |
|
148 |
*/ |
|
149 |
public $attributes = null; |
|
150 |
|
|
151 |
/** |
|
152 |
* Context values inherited by blocks of this type. |
|
153 |
* |
|
154 |
* @since 5.5.0 |
9
|
155 |
* @var array |
|
156 |
*/ |
16
|
157 |
public $uses_context = array(); |
|
158 |
|
|
159 |
/** |
|
160 |
* Context provided by blocks of this type. |
|
161 |
* |
|
162 |
* @since 5.5.0 |
|
163 |
* @var array|null |
|
164 |
*/ |
|
165 |
public $provides_context = null; |
9
|
166 |
|
|
167 |
/** |
19
|
168 |
* Block type editor only script handle. |
9
|
169 |
* |
|
170 |
* @since 5.0.0 |
16
|
171 |
* @var string|null |
9
|
172 |
*/ |
16
|
173 |
public $editor_script = null; |
9
|
174 |
|
|
175 |
/** |
19
|
176 |
* Block type front end and editor script handle. |
9
|
177 |
* |
|
178 |
* @since 5.0.0 |
16
|
179 |
* @var string|null |
9
|
180 |
*/ |
16
|
181 |
public $script = null; |
9
|
182 |
|
|
183 |
/** |
19
|
184 |
* Block type front end only script handle. |
|
185 |
* |
|
186 |
* @since 5.9.0 |
|
187 |
* @var string|null |
|
188 |
*/ |
|
189 |
public $view_script = null; |
|
190 |
|
|
191 |
/** |
|
192 |
* Block type editor only style handle. |
9
|
193 |
* |
|
194 |
* @since 5.0.0 |
16
|
195 |
* @var string|null |
9
|
196 |
*/ |
16
|
197 |
public $editor_style = null; |
9
|
198 |
|
|
199 |
/** |
19
|
200 |
* Block type front end and editor style handle. |
9
|
201 |
* |
|
202 |
* @since 5.0.0 |
16
|
203 |
* @var string|null |
9
|
204 |
*/ |
16
|
205 |
public $style = null; |
9
|
206 |
|
|
207 |
/** |
19
|
208 |
* Attributes supported by every block. |
|
209 |
* |
|
210 |
* @since 6.0.0 |
|
211 |
* @var array |
|
212 |
*/ |
|
213 |
const GLOBAL_ATTRIBUTES = array( |
|
214 |
'lock' => array( 'type' => 'object' ), |
|
215 |
); |
|
216 |
|
|
217 |
/** |
9
|
218 |
* Constructor. |
|
219 |
* |
|
220 |
* Will populate object properties from the provided arguments. |
|
221 |
* |
|
222 |
* @since 5.0.0 |
18
|
223 |
* @since 5.5.0 Added the `title`, `category`, `parent`, `icon`, `description`, |
|
224 |
* `keywords`, `textdomain`, `styles`, `supports`, `example`, |
|
225 |
* `uses_context`, and `provides_context` properties. |
|
226 |
* @since 5.6.0 Added the `api_version` property. |
|
227 |
* @since 5.8.0 Added the `variations` property. |
19
|
228 |
* @since 5.9.0 Added the `view_script` property. |
|
229 |
* @since 6.0.0 Added the `ancestor` property. |
9
|
230 |
* |
|
231 |
* @see register_block_type() |
|
232 |
* |
|
233 |
* @param string $block_type Block type name including namespace. |
18
|
234 |
* @param array|string $args { |
|
235 |
* Optional. Array or string of arguments for registering a block type. Any arguments may be defined, |
|
236 |
* however the ones described below are supported by default. Default empty array. |
|
237 |
* |
|
238 |
* @type string $api_version Block API version. |
|
239 |
* @type string $title Human-readable block type label. |
|
240 |
* @type string|null $category Block type category classification, used in |
|
241 |
* search interfaces to arrange block types by category. |
|
242 |
* @type array|null $parent Setting parent lets a block require that it is only |
|
243 |
* available when nested within the specified blocks. |
19
|
244 |
* @type array|null $ancestor Setting ancestor makes a block available only inside the specified |
|
245 |
* block types at any position of the ancestor's block subtree. |
18
|
246 |
* @type string|null $icon Block type icon. |
|
247 |
* @type string $description A detailed block type description. |
19
|
248 |
* @type string[] $keywords Additional keywords to produce block type as |
18
|
249 |
* result in search interfaces. |
|
250 |
* @type string|null $textdomain The translation textdomain. |
|
251 |
* @type array $styles Alternative block styles. |
|
252 |
* @type array $variations Block variations. |
|
253 |
* @type array|null $supports Supported features. |
|
254 |
* @type array|null $example Structured data for the block preview. |
|
255 |
* @type callable|null $render_callback Block type render callback. |
|
256 |
* @type array|null $attributes Block type attributes property schemas. |
|
257 |
* @type array $uses_context Context values inherited by blocks of this type. |
|
258 |
* @type array|null $provides_context Context provided by blocks of this type. |
19
|
259 |
* @type string|null $editor_script Block type editor only script handle. |
|
260 |
* @type string|null $script Block type front end and editor script handle. |
|
261 |
* @type string|null $view_script Block type front end only script handle. |
|
262 |
* @type string|null $editor_style Block type editor only style handle. |
|
263 |
* @type string|null $style Block type front end and editor style handle. |
18
|
264 |
* } |
9
|
265 |
*/ |
|
266 |
public function __construct( $block_type, $args = array() ) { |
|
267 |
$this->name = $block_type; |
|
268 |
|
|
269 |
$this->set_props( $args ); |
|
270 |
} |
|
271 |
|
|
272 |
/** |
|
273 |
* Renders the block type output for given attributes. |
|
274 |
* |
|
275 |
* @since 5.0.0 |
|
276 |
* |
|
277 |
* @param array $attributes Optional. Block attributes. Default empty array. |
|
278 |
* @param string $content Optional. Block content. Default empty string. |
|
279 |
* @return string Rendered block type output. |
|
280 |
*/ |
|
281 |
public function render( $attributes = array(), $content = '' ) { |
|
282 |
if ( ! $this->is_dynamic() ) { |
|
283 |
return ''; |
|
284 |
} |
|
285 |
|
|
286 |
$attributes = $this->prepare_attributes_for_render( $attributes ); |
|
287 |
|
|
288 |
return (string) call_user_func( $this->render_callback, $attributes, $content ); |
|
289 |
} |
|
290 |
|
|
291 |
/** |
|
292 |
* Returns true if the block type is dynamic, or false otherwise. A dynamic |
|
293 |
* block is one which defers its rendering to occur on-demand at runtime. |
|
294 |
* |
|
295 |
* @since 5.0.0 |
|
296 |
* |
18
|
297 |
* @return bool Whether block type is dynamic. |
9
|
298 |
*/ |
|
299 |
public function is_dynamic() { |
|
300 |
return is_callable( $this->render_callback ); |
|
301 |
} |
|
302 |
|
|
303 |
/** |
|
304 |
* Validates attributes against the current block schema, populating |
|
305 |
* defaulted and missing values. |
|
306 |
* |
|
307 |
* @since 5.0.0 |
|
308 |
* |
16
|
309 |
* @param array $attributes Original block attributes. |
|
310 |
* @return array Prepared block attributes. |
9
|
311 |
*/ |
|
312 |
public function prepare_attributes_for_render( $attributes ) { |
|
313 |
// If there are no attribute definitions for the block type, skip |
18
|
314 |
// processing and return verbatim. |
9
|
315 |
if ( ! isset( $this->attributes ) ) { |
|
316 |
return $attributes; |
|
317 |
} |
|
318 |
|
|
319 |
foreach ( $attributes as $attribute_name => $value ) { |
|
320 |
// If the attribute is not defined by the block type, it cannot be |
|
321 |
// validated. |
|
322 |
if ( ! isset( $this->attributes[ $attribute_name ] ) ) { |
|
323 |
continue; |
|
324 |
} |
|
325 |
|
|
326 |
$schema = $this->attributes[ $attribute_name ]; |
|
327 |
|
|
328 |
// Validate value by JSON schema. An invalid value should revert to |
|
329 |
// its default, if one exists. This occurs by virtue of the missing |
|
330 |
// attributes loop immediately following. If there is not a default |
|
331 |
// assigned, the attribute value should remain unset. |
18
|
332 |
$is_valid = rest_validate_value_from_schema( $value, $schema, $attribute_name ); |
9
|
333 |
if ( is_wp_error( $is_valid ) ) { |
|
334 |
unset( $attributes[ $attribute_name ] ); |
|
335 |
} |
|
336 |
} |
|
337 |
|
|
338 |
// Populate values of any missing attributes for which the block type |
|
339 |
// defines a default. |
|
340 |
$missing_schema_attributes = array_diff_key( $this->attributes, $attributes ); |
|
341 |
foreach ( $missing_schema_attributes as $attribute_name => $schema ) { |
|
342 |
if ( isset( $schema['default'] ) ) { |
|
343 |
$attributes[ $attribute_name ] = $schema['default']; |
|
344 |
} |
|
345 |
} |
|
346 |
|
|
347 |
return $attributes; |
|
348 |
} |
|
349 |
|
|
350 |
/** |
|
351 |
* Sets block type properties. |
|
352 |
* |
|
353 |
* @since 5.0.0 |
|
354 |
* |
|
355 |
* @param array|string $args Array or string of arguments for registering a block type. |
18
|
356 |
* See WP_Block_Type::__construct() for information on accepted arguments. |
9
|
357 |
*/ |
|
358 |
public function set_props( $args ) { |
|
359 |
$args = wp_parse_args( |
|
360 |
$args, |
|
361 |
array( |
|
362 |
'render_callback' => null, |
|
363 |
) |
|
364 |
); |
|
365 |
|
|
366 |
$args['name'] = $this->name; |
|
367 |
|
19
|
368 |
// Setup attributes if needed. |
|
369 |
if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) { |
|
370 |
$args['attributes'] = array(); |
|
371 |
} |
|
372 |
|
|
373 |
// Register core attributes. |
|
374 |
foreach ( static::GLOBAL_ATTRIBUTES as $attr_key => $attr_schema ) { |
|
375 |
if ( ! array_key_exists( $attr_key, $args['attributes'] ) ) { |
|
376 |
$args['attributes'][ $attr_key ] = $attr_schema; |
|
377 |
} |
|
378 |
} |
|
379 |
|
16
|
380 |
/** |
|
381 |
* Filters the arguments for registering a block type. |
|
382 |
* |
|
383 |
* @since 5.5.0 |
|
384 |
* |
|
385 |
* @param array $args Array of arguments for registering a block type. |
|
386 |
* @param string $block_type Block type name including namespace. |
|
387 |
*/ |
|
388 |
$args = apply_filters( 'register_block_type_args', $args, $this->name ); |
|
389 |
|
9
|
390 |
foreach ( $args as $property_name => $property_value ) { |
|
391 |
$this->$property_name = $property_value; |
|
392 |
} |
|
393 |
} |
|
394 |
|
|
395 |
/** |
|
396 |
* Get all available block attributes including possible layout attribute from Columns block. |
|
397 |
* |
|
398 |
* @since 5.0.0 |
|
399 |
* |
|
400 |
* @return array Array of attributes. |
|
401 |
*/ |
|
402 |
public function get_attributes() { |
|
403 |
return is_array( $this->attributes ) ? |
16
|
404 |
$this->attributes : |
|
405 |
array(); |
9
|
406 |
} |
|
407 |
} |