On this page
WP_Block::__construct( array $block, array $available_context = array(), WP_Block_Type_Registry $registry = null )
Constructor.
Description
Populates object properties from the provided block instance argument.
The given array of context values will not necessarily be available on the instance itself, but is treated as the full set of values provided by the block’s ancestry. This is assigned to the private available_context property. Only values which are configured to consumed by the block via its registered type will be assigned to the block’s context property.
Parameters
$blockarray Required-
Array of parsed block properties.
$available_contextarray Optional-
array of ancestry context values.
Default:
array() $registryWP_Block_Type_Registry Optional-
block type registry.
Default:
null
Source
File: wp-includes/class-wp-block.php. View all references
public function __construct( $block, $available_context = array(), $registry = null ) {
$this->parsed_block = $block;
$this->name = $block['blockName'];
if ( is_null( $registry ) ) {
$registry = WP_Block_Type_Registry::get_instance();
}
$this->registry = $registry;
$this->block_type = $registry->get_registered( $this->name );
$this->available_context = $available_context;
if ( ! empty( $this->block_type->uses_context ) ) {
foreach ( $this->block_type->uses_context as $context_name ) {
if ( array_key_exists( $context_name, $this->available_context ) ) {
$this->context[ $context_name ] = $this->available_context[ $context_name ];
}
}
}
if ( ! empty( $block['innerBlocks'] ) ) {
$child_context = $this->available_context;
if ( ! empty( $this->block_type->provides_context ) ) {
foreach ( $this->block_type->provides_context as $context_name => $attribute_name ) {
if ( array_key_exists( $attribute_name, $this->attributes ) ) {
$child_context[ $context_name ] = $this->attributes[ $attribute_name ];
}
}
}
$this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry );
}
if ( ! empty( $block['innerHTML'] ) ) {
$this->inner_html = $block['innerHTML'];
}
if ( ! empty( $block['innerContent'] ) ) {
$this->inner_content = $block['innerContent'];
}
}
Related
Uses
| Uses | Description |
|---|---|
| WP_Block_List::__construct() wp-includes/class-wp-block-list.php | Constructor. |
| WP_Block_Type_Registry::get_instance() wp-includes/class-wp-block-type-registry.php | Utility method to retrieve the main instance of the class. |
Used By
| Used By | Description |
|---|---|
| WP_Block_List::offsetGet() wp-includes/class-wp-block-list.php | |
| render_block() wp-includes/blocks.php | Renders a single block into a HTML string. |
Changelog
| Version | Description |
|---|---|
| 5.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_block/__construct