Make WordPress Core

Changeset 56610


Ignore:
Timestamp:
09/18/2023 12:40:11 PM (5 months ago)
Author:
gziolo
Message:

Blocks: Introduce helper function to retrieve hooked blocks

In order to implement Block Hooks (see #59313), we added block_hooks field to the WP_Block_Type class, as well as to block registration related functions. In this follow-up, new helper function gets introduced that is going to compute the list of hooked blocks by other registered blocks for a given block type.

Extracted from https://github.com/WordPress/wordpress-develop/pull/5158 and covered with unit tests.

Props ockham.
Fixes #59383.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r56598 r56610  
    741741
    742742/**
     743 * Retrieves block types (and positions) hooked into the given block.
     744 *
     745 * @since 6.4.0
     746 *
     747 * @param string $name Block type name including namespace.
     748 * @return array Associative array of `$block_type_name => $position` pairs.
     749 */
     750function get_hooked_blocks( $name ) {
     751    $block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
     752    $hooked_blocks = array();
     753    foreach ( $block_types as $block_type ) {
     754        foreach ( $block_type->block_hooks as $anchor_block_type => $relative_position ) {
     755            if ( $anchor_block_type === $name ) {
     756                $hooked_blocks[ $block_type->name ] = $relative_position;
     757            }
     758        }
     759    }
     760    return $hooked_blocks;
     761}
     762
     763/**
    743764 * Given an array of attributes, returns a string in the serialized attributes
    744765 * format prepared for post content.
Note: See TracChangeset for help on using the changeset viewer.