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 |
/** |
|
20 |
* Block type key. |
|
21 |
* |
|
22 |
* @since 5.0.0 |
|
23 |
* @var string |
|
24 |
*/ |
|
25 |
public $name; |
|
26 |
|
|
27 |
/** |
16
|
28 |
* @since 5.5.0 |
|
29 |
* @var string |
|
30 |
*/ |
|
31 |
public $title = ''; |
|
32 |
|
|
33 |
/** |
|
34 |
* @since 5.5.0 |
|
35 |
* @var string|null |
|
36 |
*/ |
|
37 |
public $category = null; |
|
38 |
|
|
39 |
/** |
|
40 |
* @since 5.5.0 |
|
41 |
* @var array|null |
|
42 |
*/ |
|
43 |
public $parent = null; |
|
44 |
|
|
45 |
/** |
|
46 |
* @since 5.5.0 |
|
47 |
* @var string|null |
|
48 |
*/ |
|
49 |
public $icon = null; |
|
50 |
|
|
51 |
/** |
|
52 |
* @since 5.5.0 |
|
53 |
* @var string |
|
54 |
*/ |
|
55 |
public $description = ''; |
|
56 |
|
|
57 |
/** |
|
58 |
* @since 5.5.0 |
|
59 |
* @var array |
|
60 |
*/ |
|
61 |
public $keywords = array(); |
|
62 |
|
|
63 |
/** |
|
64 |
* @since 5.5.0 |
|
65 |
* @var string|null |
|
66 |
*/ |
|
67 |
public $textdomain = null; |
|
68 |
|
|
69 |
/** |
|
70 |
* @since 5.5.0 |
|
71 |
* @var array |
|
72 |
*/ |
|
73 |
public $styles = array(); |
|
74 |
|
|
75 |
/** |
|
76 |
* @since 5.5.0 |
|
77 |
* @var array|null |
|
78 |
*/ |
|
79 |
public $supports = null; |
|
80 |
|
|
81 |
/** |
|
82 |
* @since 5.5.0 |
|
83 |
* @var array|null |
|
84 |
*/ |
|
85 |
public $example = null; |
|
86 |
|
|
87 |
/** |
9
|
88 |
* Block type render callback. |
|
89 |
* |
|
90 |
* @since 5.0.0 |
|
91 |
* @var callable |
|
92 |
*/ |
16
|
93 |
public $render_callback = null; |
9
|
94 |
|
|
95 |
/** |
|
96 |
* Block type attributes property schemas. |
|
97 |
* |
|
98 |
* @since 5.0.0 |
16
|
99 |
* @var array|null |
|
100 |
*/ |
|
101 |
public $attributes = null; |
|
102 |
|
|
103 |
/** |
|
104 |
* Context values inherited by blocks of this type. |
|
105 |
* |
|
106 |
* @since 5.5.0 |
9
|
107 |
* @var array |
|
108 |
*/ |
16
|
109 |
public $uses_context = array(); |
|
110 |
|
|
111 |
/** |
|
112 |
* Context provided by blocks of this type. |
|
113 |
* |
|
114 |
* @since 5.5.0 |
|
115 |
* @var array|null |
|
116 |
*/ |
|
117 |
public $provides_context = null; |
9
|
118 |
|
|
119 |
/** |
|
120 |
* Block type editor script handle. |
|
121 |
* |
|
122 |
* @since 5.0.0 |
16
|
123 |
* @var string|null |
9
|
124 |
*/ |
16
|
125 |
public $editor_script = null; |
9
|
126 |
|
|
127 |
/** |
|
128 |
* Block type front end script handle. |
|
129 |
* |
|
130 |
* @since 5.0.0 |
16
|
131 |
* @var string|null |
9
|
132 |
*/ |
16
|
133 |
public $script = null; |
9
|
134 |
|
|
135 |
/** |
|
136 |
* Block type editor style handle. |
|
137 |
* |
|
138 |
* @since 5.0.0 |
16
|
139 |
* @var string|null |
9
|
140 |
*/ |
16
|
141 |
public $editor_style = null; |
9
|
142 |
|
|
143 |
/** |
|
144 |
* Block type front end style handle. |
|
145 |
* |
|
146 |
* @since 5.0.0 |
16
|
147 |
* @var string|null |
9
|
148 |
*/ |
16
|
149 |
public $style = null; |
9
|
150 |
|
|
151 |
/** |
|
152 |
* Constructor. |
|
153 |
* |
|
154 |
* Will populate object properties from the provided arguments. |
|
155 |
* |
|
156 |
* @since 5.0.0 |
|
157 |
* |
|
158 |
* @see register_block_type() |
|
159 |
* |
|
160 |
* @param string $block_type Block type name including namespace. |
|
161 |
* @param array|string $args Optional. Array or string of arguments for registering a block type. |
|
162 |
* Default empty array. |
|
163 |
*/ |
|
164 |
public function __construct( $block_type, $args = array() ) { |
|
165 |
$this->name = $block_type; |
|
166 |
|
|
167 |
$this->set_props( $args ); |
|
168 |
} |
|
169 |
|
|
170 |
/** |
|
171 |
* Renders the block type output for given attributes. |
|
172 |
* |
|
173 |
* @since 5.0.0 |
|
174 |
* |
|
175 |
* @param array $attributes Optional. Block attributes. Default empty array. |
|
176 |
* @param string $content Optional. Block content. Default empty string. |
|
177 |
* @return string Rendered block type output. |
|
178 |
*/ |
|
179 |
public function render( $attributes = array(), $content = '' ) { |
|
180 |
if ( ! $this->is_dynamic() ) { |
|
181 |
return ''; |
|
182 |
} |
|
183 |
|
|
184 |
$attributes = $this->prepare_attributes_for_render( $attributes ); |
|
185 |
|
|
186 |
return (string) call_user_func( $this->render_callback, $attributes, $content ); |
|
187 |
} |
|
188 |
|
|
189 |
/** |
|
190 |
* Returns true if the block type is dynamic, or false otherwise. A dynamic |
|
191 |
* block is one which defers its rendering to occur on-demand at runtime. |
|
192 |
* |
|
193 |
* @since 5.0.0 |
|
194 |
* |
|
195 |
* @return boolean Whether block type is dynamic. |
|
196 |
*/ |
|
197 |
public function is_dynamic() { |
|
198 |
return is_callable( $this->render_callback ); |
|
199 |
} |
|
200 |
|
|
201 |
/** |
|
202 |
* Validates attributes against the current block schema, populating |
|
203 |
* defaulted and missing values. |
|
204 |
* |
|
205 |
* @since 5.0.0 |
|
206 |
* |
16
|
207 |
* @param array $attributes Original block attributes. |
|
208 |
* @return array Prepared block attributes. |
9
|
209 |
*/ |
|
210 |
public function prepare_attributes_for_render( $attributes ) { |
|
211 |
// If there are no attribute definitions for the block type, skip |
|
212 |
// processing and return vebatim. |
|
213 |
if ( ! isset( $this->attributes ) ) { |
|
214 |
return $attributes; |
|
215 |
} |
|
216 |
|
|
217 |
foreach ( $attributes as $attribute_name => $value ) { |
|
218 |
// If the attribute is not defined by the block type, it cannot be |
|
219 |
// validated. |
|
220 |
if ( ! isset( $this->attributes[ $attribute_name ] ) ) { |
|
221 |
continue; |
|
222 |
} |
|
223 |
|
|
224 |
$schema = $this->attributes[ $attribute_name ]; |
|
225 |
|
|
226 |
// Validate value by JSON schema. An invalid value should revert to |
|
227 |
// its default, if one exists. This occurs by virtue of the missing |
|
228 |
// attributes loop immediately following. If there is not a default |
|
229 |
// assigned, the attribute value should remain unset. |
|
230 |
$is_valid = rest_validate_value_from_schema( $value, $schema ); |
|
231 |
if ( is_wp_error( $is_valid ) ) { |
|
232 |
unset( $attributes[ $attribute_name ] ); |
|
233 |
} |
|
234 |
} |
|
235 |
|
|
236 |
// Populate values of any missing attributes for which the block type |
|
237 |
// defines a default. |
|
238 |
$missing_schema_attributes = array_diff_key( $this->attributes, $attributes ); |
|
239 |
foreach ( $missing_schema_attributes as $attribute_name => $schema ) { |
|
240 |
if ( isset( $schema['default'] ) ) { |
|
241 |
$attributes[ $attribute_name ] = $schema['default']; |
|
242 |
} |
|
243 |
} |
|
244 |
|
|
245 |
return $attributes; |
|
246 |
} |
|
247 |
|
|
248 |
/** |
|
249 |
* Sets block type properties. |
|
250 |
* |
|
251 |
* @since 5.0.0 |
|
252 |
* |
|
253 |
* @param array|string $args Array or string of arguments for registering a block type. |
|
254 |
*/ |
|
255 |
public function set_props( $args ) { |
|
256 |
$args = wp_parse_args( |
|
257 |
$args, |
|
258 |
array( |
|
259 |
'render_callback' => null, |
|
260 |
) |
|
261 |
); |
|
262 |
|
|
263 |
$args['name'] = $this->name; |
|
264 |
|
16
|
265 |
/** |
|
266 |
* Filters the arguments for registering a block type. |
|
267 |
* |
|
268 |
* @since 5.5.0 |
|
269 |
* |
|
270 |
* @param array $args Array of arguments for registering a block type. |
|
271 |
* @param string $block_type Block type name including namespace. |
|
272 |
*/ |
|
273 |
$args = apply_filters( 'register_block_type_args', $args, $this->name ); |
|
274 |
|
9
|
275 |
foreach ( $args as $property_name => $property_value ) { |
|
276 |
$this->$property_name = $property_value; |
|
277 |
} |
|
278 |
} |
|
279 |
|
|
280 |
/** |
|
281 |
* Get all available block attributes including possible layout attribute from Columns block. |
|
282 |
* |
|
283 |
* @since 5.0.0 |
|
284 |
* |
|
285 |
* @return array Array of attributes. |
|
286 |
*/ |
|
287 |
public function get_attributes() { |
|
288 |
return is_array( $this->attributes ) ? |
16
|
289 |
$this->attributes : |
|
290 |
array(); |
9
|
291 |
} |
|
292 |
} |