150 |
176 |
151 /** |
177 /** |
152 * Context values inherited by blocks of this type. |
178 * Context values inherited by blocks of this type. |
153 * |
179 * |
154 * @since 5.5.0 |
180 * @since 5.5.0 |
|
181 * @var string[] |
|
182 */ |
|
183 private $uses_context = array(); |
|
184 |
|
185 /** |
|
186 * Context provided by blocks of this type. |
|
187 * |
|
188 * @since 5.5.0 |
|
189 * @var string[]|null |
|
190 */ |
|
191 public $provides_context = null; |
|
192 |
|
193 /** |
|
194 * Block hooks for this block type. |
|
195 * |
|
196 * A block hook is specified by a block type and a relative position. |
|
197 * The hooked block will be automatically inserted in the given position |
|
198 * next to the "anchor" block whenever the latter is encountered. |
|
199 * |
|
200 * @since 6.4.0 |
|
201 * @var string[] |
|
202 */ |
|
203 public $block_hooks = array(); |
|
204 |
|
205 /** |
|
206 * Block type editor only script handles. |
|
207 * |
|
208 * @since 6.1.0 |
|
209 * @var string[] |
|
210 */ |
|
211 public $editor_script_handles = array(); |
|
212 |
|
213 /** |
|
214 * Block type front end and editor script handles. |
|
215 * |
|
216 * @since 6.1.0 |
|
217 * @var string[] |
|
218 */ |
|
219 public $script_handles = array(); |
|
220 |
|
221 /** |
|
222 * Block type front end only script handles. |
|
223 * |
|
224 * @since 6.1.0 |
|
225 * @var string[] |
|
226 */ |
|
227 public $view_script_handles = array(); |
|
228 |
|
229 /** |
|
230 * Block type front end only script module IDs. |
|
231 * |
|
232 * @since 6.5.0 |
|
233 * @var string[] |
|
234 */ |
|
235 public $view_script_module_ids = array(); |
|
236 |
|
237 /** |
|
238 * Block type editor only style handles. |
|
239 * |
|
240 * @since 6.1.0 |
|
241 * @var string[] |
|
242 */ |
|
243 public $editor_style_handles = array(); |
|
244 |
|
245 /** |
|
246 * Block type front end and editor style handles. |
|
247 * |
|
248 * @since 6.1.0 |
|
249 * @var string[] |
|
250 */ |
|
251 public $style_handles = array(); |
|
252 |
|
253 /** |
|
254 * Block type front end only style handles. |
|
255 * |
|
256 * @since 6.5.0 |
|
257 * @var string[] |
|
258 */ |
|
259 public $view_style_handles = array(); |
|
260 |
|
261 /** |
|
262 * Deprecated block type properties for script and style handles. |
|
263 * |
|
264 * @since 6.1.0 |
|
265 * @var string[] |
|
266 */ |
|
267 private $deprecated_properties = array( |
|
268 'editor_script', |
|
269 'script', |
|
270 'view_script', |
|
271 'editor_style', |
|
272 'style', |
|
273 ); |
|
274 |
|
275 /** |
|
276 * Attributes supported by every block. |
|
277 * |
|
278 * @since 6.0.0 Added `lock`. |
|
279 * @since 6.5.0 Added `metadata`. |
155 * @var array |
280 * @var array |
156 */ |
281 */ |
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; |
|
166 |
|
167 /** |
|
168 * Block type editor only script handle. |
|
169 * |
|
170 * @since 5.0.0 |
|
171 * @var string|null |
|
172 */ |
|
173 public $editor_script = null; |
|
174 |
|
175 /** |
|
176 * Block type front end and editor script handle. |
|
177 * |
|
178 * @since 5.0.0 |
|
179 * @var string|null |
|
180 */ |
|
181 public $script = null; |
|
182 |
|
183 /** |
|
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. |
|
193 * |
|
194 * @since 5.0.0 |
|
195 * @var string|null |
|
196 */ |
|
197 public $editor_style = null; |
|
198 |
|
199 /** |
|
200 * Block type front end and editor style handle. |
|
201 * |
|
202 * @since 5.0.0 |
|
203 * @var string|null |
|
204 */ |
|
205 public $style = null; |
|
206 |
|
207 /** |
|
208 * Attributes supported by every block. |
|
209 * |
|
210 * @since 6.0.0 |
|
211 * @var array |
|
212 */ |
|
213 const GLOBAL_ATTRIBUTES = array( |
282 const GLOBAL_ATTRIBUTES = array( |
214 'lock' => array( 'type' => 'object' ), |
283 'lock' => array( 'type' => 'object' ), |
|
284 'metadata' => array( 'type' => 'object' ), |
215 ); |
285 ); |
216 |
286 |
217 /** |
287 /** |
218 * Constructor. |
288 * Constructor. |
219 * |
289 * |
225 * `uses_context`, and `provides_context` properties. |
295 * `uses_context`, and `provides_context` properties. |
226 * @since 5.6.0 Added the `api_version` property. |
296 * @since 5.6.0 Added the `api_version` property. |
227 * @since 5.8.0 Added the `variations` property. |
297 * @since 5.8.0 Added the `variations` property. |
228 * @since 5.9.0 Added the `view_script` property. |
298 * @since 5.9.0 Added the `view_script` property. |
229 * @since 6.0.0 Added the `ancestor` property. |
299 * @since 6.0.0 Added the `ancestor` property. |
|
300 * @since 6.1.0 Added the `editor_script_handles`, `script_handles`, `view_script_handles`, |
|
301 * `editor_style_handles`, and `style_handles` properties. |
|
302 * Deprecated the `editor_script`, `script`, `view_script`, `editor_style`, and `style` properties. |
|
303 * @since 6.3.0 Added the `selectors` property. |
|
304 * @since 6.4.0 Added the `block_hooks` property. |
|
305 * @since 6.5.0 Added the `allowed_blocks`, `variation_callback`, and `view_style_handles` properties. |
230 * |
306 * |
231 * @see register_block_type() |
307 * @see register_block_type() |
232 * |
308 * |
233 * @param string $block_type Block type name including namespace. |
309 * @param string $block_type Block type name including namespace. |
234 * @param array|string $args { |
310 * @param array|string $args { |
235 * Optional. Array or string of arguments for registering a block type. Any arguments may be defined, |
311 * 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. |
312 * however the ones described below are supported by default. Default empty array. |
237 * |
313 * |
238 * @type string $api_version Block API version. |
314 * @type string $api_version Block API version. |
239 * @type string $title Human-readable block type label. |
315 * @type string $title Human-readable block type label. |
240 * @type string|null $category Block type category classification, used in |
316 * @type string|null $category Block type category classification, used in |
241 * search interfaces to arrange block types by category. |
317 * search interfaces to arrange block types by category. |
242 * @type array|null $parent Setting parent lets a block require that it is only |
318 * @type string[]|null $parent Setting parent lets a block require that it is only |
243 * available when nested within the specified blocks. |
319 * available when nested within the specified blocks. |
244 * @type array|null $ancestor Setting ancestor makes a block available only inside the specified |
320 * @type string[]|null $ancestor Setting ancestor makes a block available only inside the specified |
245 * block types at any position of the ancestor's block subtree. |
321 * block types at any position of the ancestor's block subtree. |
246 * @type string|null $icon Block type icon. |
322 * @type string[]|null $allowed_blocks Limits which block types can be inserted as children of this block type. |
247 * @type string $description A detailed block type description. |
323 * @type string|null $icon Block type icon. |
248 * @type string[] $keywords Additional keywords to produce block type as |
324 * @type string $description A detailed block type description. |
249 * result in search interfaces. |
325 * @type string[] $keywords Additional keywords to produce block type as |
250 * @type string|null $textdomain The translation textdomain. |
326 * result in search interfaces. |
251 * @type array $styles Alternative block styles. |
327 * @type string|null $textdomain The translation textdomain. |
252 * @type array $variations Block variations. |
328 * @type array[] $styles Alternative block styles. |
253 * @type array|null $supports Supported features. |
329 * @type array[] $variations Block variations. |
254 * @type array|null $example Structured data for the block preview. |
330 * @type array $selectors Custom CSS selectors for theme.json style generation. |
255 * @type callable|null $render_callback Block type render callback. |
331 * @type array|null $supports Supported features. |
256 * @type array|null $attributes Block type attributes property schemas. |
332 * @type array|null $example Structured data for the block preview. |
257 * @type array $uses_context Context values inherited by blocks of this type. |
333 * @type callable|null $render_callback Block type render callback. |
258 * @type array|null $provides_context Context provided by blocks of this type. |
334 * @type callable|null $variation_callback Block type variations callback. |
259 * @type string|null $editor_script Block type editor only script handle. |
335 * @type array|null $attributes Block type attributes property schemas. |
260 * @type string|null $script Block type front end and editor script handle. |
336 * @type string[] $uses_context Context values inherited by blocks of this type. |
261 * @type string|null $view_script Block type front end only script handle. |
337 * @type string[]|null $provides_context Context provided by blocks of this type. |
262 * @type string|null $editor_style Block type editor only style handle. |
338 * @type string[] $block_hooks Block hooks. |
263 * @type string|null $style Block type front end and editor style handle. |
339 * @type string[] $editor_script_handles Block type editor only script handles. |
|
340 * @type string[] $script_handles Block type front end and editor script handles. |
|
341 * @type string[] $view_script_handles Block type front end only script handles. |
|
342 * @type string[] $editor_style_handles Block type editor only style handles. |
|
343 * @type string[] $style_handles Block type front end and editor style handles. |
|
344 * @type string[] $view_style_handles Block type front end only style handles. |
264 * } |
345 * } |
265 */ |
346 */ |
266 public function __construct( $block_type, $args = array() ) { |
347 public function __construct( $block_type, $args = array() ) { |
267 $this->name = $block_type; |
348 $this->name = $block_type; |
268 |
349 |
269 $this->set_props( $args ); |
350 $this->set_props( $args ); |
|
351 } |
|
352 |
|
353 /** |
|
354 * Proxies getting values for deprecated properties for script and style handles for backward compatibility. |
|
355 * Gets the value for the corresponding new property if the first item in the array provided. |
|
356 * |
|
357 * @since 6.1.0 |
|
358 * |
|
359 * @param string $name Deprecated property name. |
|
360 * |
|
361 * @return string|string[]|null|void The value read from the new property if the first item in the array provided, |
|
362 * null when value not found, or void when unknown property name provided. |
|
363 */ |
|
364 public function __get( $name ) { |
|
365 if ( 'variations' === $name ) { |
|
366 return $this->get_variations(); |
|
367 } |
|
368 |
|
369 if ( 'uses_context' === $name ) { |
|
370 return $this->get_uses_context(); |
|
371 } |
|
372 |
|
373 if ( ! in_array( $name, $this->deprecated_properties, true ) ) { |
|
374 return; |
|
375 } |
|
376 |
|
377 $new_name = $name . '_handles'; |
|
378 |
|
379 if ( ! property_exists( $this, $new_name ) || ! is_array( $this->{$new_name} ) ) { |
|
380 return null; |
|
381 } |
|
382 |
|
383 if ( count( $this->{$new_name} ) > 1 ) { |
|
384 return $this->{$new_name}; |
|
385 } |
|
386 return isset( $this->{$new_name}[0] ) ? $this->{$new_name}[0] : null; |
|
387 } |
|
388 |
|
389 /** |
|
390 * Proxies checking for deprecated properties for script and style handles for backward compatibility. |
|
391 * Checks whether the corresponding new property has the first item in the array provided. |
|
392 * |
|
393 * @since 6.1.0 |
|
394 * |
|
395 * @param string $name Deprecated property name. |
|
396 * |
|
397 * @return bool Returns true when for the new property the first item in the array exists, |
|
398 * or false otherwise. |
|
399 */ |
|
400 public function __isset( $name ) { |
|
401 if ( in_array( $name, array( 'variations', 'uses_context' ), true ) ) { |
|
402 return true; |
|
403 } |
|
404 |
|
405 if ( ! in_array( $name, $this->deprecated_properties, true ) ) { |
|
406 return false; |
|
407 } |
|
408 |
|
409 $new_name = $name . '_handles'; |
|
410 return isset( $this->{$new_name}[0] ); |
|
411 } |
|
412 |
|
413 /** |
|
414 * Proxies setting values for deprecated properties for script and style handles for backward compatibility. |
|
415 * Sets the value for the corresponding new property as the first item in the array. |
|
416 * It also allows setting custom properties for backward compatibility. |
|
417 * |
|
418 * @since 6.1.0 |
|
419 * |
|
420 * @param string $name Property name. |
|
421 * @param mixed $value Property value. |
|
422 */ |
|
423 public function __set( $name, $value ) { |
|
424 if ( ! in_array( $name, $this->deprecated_properties, true ) ) { |
|
425 $this->{$name} = $value; |
|
426 return; |
|
427 } |
|
428 |
|
429 $new_name = $name . '_handles'; |
|
430 |
|
431 if ( is_array( $value ) ) { |
|
432 $filtered = array_filter( $value, 'is_string' ); |
|
433 |
|
434 if ( count( $filtered ) !== count( $value ) ) { |
|
435 _doing_it_wrong( |
|
436 __METHOD__, |
|
437 sprintf( |
|
438 /* translators: %s: The '$value' argument. */ |
|
439 __( 'The %s argument must be a string or a string array.' ), |
|
440 '<code>$value</code>' |
|
441 ), |
|
442 '6.1.0' |
|
443 ); |
|
444 } |
|
445 |
|
446 $this->{$new_name} = array_values( $filtered ); |
|
447 return; |
|
448 } |
|
449 |
|
450 if ( ! is_string( $value ) ) { |
|
451 return; |
|
452 } |
|
453 |
|
454 $this->{$new_name} = array( $value ); |
270 } |
455 } |
271 |
456 |
272 /** |
457 /** |
273 * Renders the block type output for given attributes. |
458 * Renders the block type output for given attributes. |
274 * |
459 * |