Make WordPress Core

Changeset 57668

Timestamp:
02/20/2024 03:20:39 PM (5 months ago)
Author:
Bernhard Reiter
Message:

Block Hooks: Allow hooked_block filters to return null.

Allow returning null from the hooked_block and hooked_block_{$hooked_block_type} filters to suppress a hooked block from being inserted. This is required to allow extenders conditionally inserting a hooked block based on e.g. the value of an attribute of the anchor block.

Props swissspidy, gziolo, joshuatf, tomjcafferkey.
Fixes 60580.

Location:
trunk
Files:
3 edited

Legend:

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

    r57660 r57668  
    898898         * @since 6.5.0
    899899         *
    900          * @param array                           $parsed_hooked_block The parsed block array for the given hooked block type.
     900         * @param array.
    901901         * @param string                          $hooked_block_type   The hooked block type name.
    902902         * @param string                          $relative_position   The relative position of the hooked block.
     
    914914         * @since 6.5.0
    915915         *
    916          * @param array                           $parsed_hooked_block The parsed block array for the given hooked block type.
     916         * @param array.
    917917         * @param string                          $hooked_block_type   The hooked block type name.
    918918         * @param string                          $relative_position   The relative position of the hooked block.
     
    923923        $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context );
    924924
     925
     926
     927
     928
    925929        // It's possible that the filter returned a block of a different type, so we explicitly
    926930        // look for the original `$hooked_block_type` in the `ignoredHookedBlocks` metadata.
     
    961965    if ( empty( $hooked_block_types ) ) {
    962966        return '';
     967
     968
     969
     970
     971
     972
     973
     974
     975
     976
     977
     978
     979
     980
     981
     982
     983
     984
     985
    963986    }
    964987
  • trunk/tests/phpunit/tests/blocks/insertHookedBlocks.php

    r57660 r57668  
    181181        );
    182182    }
     183
     184
     185
     186
     187
     188
     189
     190
     191
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
     202
     203
     204
     205
     206
     207
     208
     209
     210
     211
     212
     213
     214
     215
     216
     217
     218
     219
     220
    183221}
  • trunk/tests/phpunit/tests/blocks/setIgnoredHookedBlocksMetadata.php

    r57666 r57668  
    145145        );
    146146    }
     147
     148
     149
     150
     151
     152
     153
     154
     155
     156
     157
     158
     159
     160
     161
     162
     163
     164
     165
     166
     167
     168
     169
     170
     171
     172
     173
     174
     175
     176
     177
     178
    147179}
Note: See TracChangeset for help on using the changeset viewer.