Make WordPress Core


Ignore:
Timestamp:
10/09/2023 04:38:25 PM (16 months ago)
Author:
Bernhard Reiter
Message:

Blocks: Call get_hooked_blocks only once per template/part/pattern.

Prior to this changeset, get_hooked_blocks was called four times for every parsed block in each template, template part, and pattern. With this changeset applied, get_hooked_blocks is called only once per template, template part, or pattern.

Additionally, get_hooked_blocks is called only once when returning the list of all registered patterns. (The latter modification brings the implementation closer to its state prior to Block Hooks.)

Finally, when there are no registered hooked blocks or hooked_block_types filters, parsing, hooked block insertion, and re-serializing is skipped altogether.

Props gziolo, flixos90, joemcgill, dmsnell, spacedmonkey, hellofromtonya.
Fixes #59383.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/getHookedBlocks.php

    r56759 r56805  
    6363     */
    6464    public function test_get_hooked_blocks_no_match_found() {
    65         $result = get_hooked_blocks( 'tests/no-hooked-blocks' );
     65        $result = get_hooked_blocks();
    6666
    6767        $this->assertSame( array(), $result );
     
    9999        $this->assertSame(
    100100            array(
    101                 'before' => array(
    102                     'tests/injected-one',
    103                     'tests/injected-two',
     101                'tests/hooked-at-before'           => array(
     102                    'before' => array(
     103                        'tests/injected-one',
     104                        'tests/injected-two',
     105                    ),
     106                ),
     107                'tests/hooked-at-after'            => array(
     108                    'after' => array(
     109                        'tests/injected-one',
     110                        'tests/injected-two',
     111                    ),
     112                ),
     113                'tests/hooked-at-before-and-after' => array(
     114                    'before' => array(
     115                        'tests/injected-one',
     116                    ),
     117                    'after'  => array(
     118                        'tests/injected-two',
     119                    ),
     120                ),
     121                'tests/hooked-at-first-child'      => array(
     122                    'first_child' => array(
     123                        'tests/injected-two',
     124                    ),
     125                ),
     126                'tests/hooked-at-last-child'       => array(
     127                    'last_child' => array(
     128                        'tests/injected-two',
     129                    ),
    104130                ),
    105131            ),
    106             get_hooked_blocks( 'tests/hooked-at-before' ),
    107             'block hooked at the before position'
    108         );
    109         $this->assertSame(
    110             array(
    111                 'after' => array(
    112                     'tests/injected-one',
    113                     'tests/injected-two',
    114                 ),
    115             ),
    116             get_hooked_blocks( 'tests/hooked-at-after' ),
    117             'block hooked at the after position'
    118         );
    119         $this->assertSame(
    120             array(
    121                 'first_child' => array(
    122                     'tests/injected-two',
    123                 ),
    124             ),
    125             get_hooked_blocks( 'tests/hooked-at-first-child' ),
    126             'block hooked at the first child position'
    127         );
    128         $this->assertSame(
    129             array(
    130                 'last_child' => array(
    131                     'tests/injected-two',
    132                 ),
    133             ),
    134             get_hooked_blocks( 'tests/hooked-at-last-child' ),
    135             'block hooked at the last child position'
    136         );
    137         $this->assertSame(
    138             array(
    139                 'before' => array(
    140                     'tests/injected-one',
    141                 ),
    142                 'after'  => array(
    143                     'tests/injected-two',
    144                 ),
    145             ),
    146             get_hooked_blocks( 'tests/hooked-at-before-and-after' ),
    147             'block hooked before one block and after another'
     132            get_hooked_blocks()
    148133        );
    149134    }
Note: See TracChangeset for help on using the changeset viewer.