Automattic\WooCommerce\Blocks\BlockTypes
SavedForLater::set_hooked_block_attributes
Seed a default heading inner block on the auto-injected block.
Method of the class: SavedForLater{}
No Hooks.
Returns
Array|null.
Usage
$SavedForLater = new SavedForLater(); $SavedForLater->set_hooked_block_attributes( $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block );
- $parsed_hooked_block(array|null) (required)
- The parsed hooked block array, or null to suppress insertion.
- $hooked_block_type(string) (required)
- The hooked block type name.
- $relative_position(string) (required)
- Position of the insertion point.
- $parsed_anchor_block(array) (required)
- The anchor block, in parsed block array format.
SavedForLater::set_hooked_block_attributes() SavedForLater::set hooked block attributes code WC 10.9.4
public function set_hooked_block_attributes( $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block ) {
if ( null === $parsed_hooked_block || 'after' !== $relative_position ) {
return $parsed_hooked_block;
}
if ( ! isset( $parsed_anchor_block['blockName'] ) || 'woocommerce/cart' !== $parsed_anchor_block['blockName'] ) {
return $parsed_hooked_block;
}
// Seed a `core/heading` inner block so freshly-injected instances
// ship with the same heading the editor template seeds. We append
// unconditionally — extensions are free to hook
// `hooked_block_woocommerce/saved-for-later` to add their own
// inner blocks, and gating on `empty( innerBlocks )` would silently
// suppress our heading whenever any other extension ran first.
//
// `core/heading` is a static block, so the serialised markup must
// match what the editor would have saved (`<h2 class="wp-block-heading">…</h2>`)
// or it'll fail block validation when the cart page is opened in the
// editor. `attrs.content` mirrors what the editor's template seeds
// (`{ content, level }`) so the parsed shape round-trips identically;
// the value is the raw string because attrs are JSON-encoded into the
// block comment and `esc_html()` would corrupt translations whose text
// contains `&`, `<`, etc. The matching `null` push onto `innerContent`
// is what makes `WP_Block::render()` walk into the heading when
// building `$content`.
$list_heading = __( 'Saved for later', 'woocommerce' );
$heading_html = '<h2 class="wp-block-heading">' . esc_html( $list_heading ) . '</h2>';
if ( ! isset( $parsed_hooked_block['innerBlocks'] ) || ! is_array( $parsed_hooked_block['innerBlocks'] ) ) {
$parsed_hooked_block['innerBlocks'] = array();
}
$parsed_hooked_block['innerBlocks'][] = array(
'blockName' => 'core/heading',
'attrs' => array(
'level' => 2,
'content' => $list_heading,
),
'innerBlocks' => array(),
'innerHTML' => $heading_html,
'innerContent' => array( $heading_html ),
);
if ( ! isset( $parsed_hooked_block['innerContent'] ) || ! is_array( $parsed_hooked_block['innerContent'] ) ) {
$parsed_hooked_block['innerContent'] = array();
}
$parsed_hooked_block['innerContent'][] = null;
return $parsed_hooked_block;
}