Make WordPress Core

Changeset 57021


Ignore:
Timestamp:
10/27/2023 07:02:54 PM (12 months ago)
Author:
joemcgill
Message:

Upgrade/Install: Skip registering theme block patterns during the upgrade process.

This fixes a bug during the database upgrade process where a theme's functions.php file may not be loaded, leading to potential exceptions if the theme's pattern files use symbols (classes, functions, constants, etc.) that are declared only when the functions.php file is loaded. To do so, a check for wp_get_active_and_valid_themes() is added early to _register_theme_block_patterns(), which returns early if no active or valid themes are returned.

Props fabiankaegy, rajinsharwar, pbiron, huzaifaalmesbah, hellofromTonya, peterwilsoncc, joemcgill.
Fixes #59723.

Location:
trunk
Files:
2 edited

Legend:

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

    r56978 r57021  
    329329 */
    330330function _register_theme_block_patterns() {
     331
     332    /*
     333     * During the bootstrap process, a check for active and valid themes is run.
     334     * If no themes are returned, the theme's functions.php file will not be loaded,
     335     * which can lead to errors if patterns expect some variables or constants to
     336     * already be set at this point, so bail early if that is the case.
     337     */
     338    if ( empty( wp_get_active_and_valid_themes() ) ) {
     339        return;
     340    }
     341
    331342    /*
    332343     * Register patterns for the active theme. If the theme is a child theme,
  • trunk/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php

    r56960 r57021  
    483483        $this->assertTrue( $result );
    484484    }
     485
     486    /**
     487     * Ensures theme patterns are registered on init.
     488     *
     489     * @ticket 59723
     490     *
     491     * @covers ::_register_theme_block_patterns
     492     */
     493    public function test_register_theme_block_patterns_on_init() {
     494        // This test needs to use access static class properties.
     495        $registry = WP_Block_Patterns_Registry::get_instance();
     496
     497        // Ensure we're using a theme with patterns.
     498        switch_theme( 'twentytwentythree' );
     499
     500        $theme          = wp_get_theme();
     501        $theme_patterns = array_values( wp_list_pluck( $theme->get_block_patterns(), 'slug' ) );
     502
     503        // This helper is fired on the init hook.
     504        _register_theme_block_patterns();
     505
     506        $registered = wp_list_pluck( $registry->get_all_registered(), 'name' );
     507
     508        // Cleanup patterns registry.
     509        foreach ( $theme_patterns as $pattern ) {
     510            $registry->unregister( $pattern );
     511        }
     512
     513        $this->assertSameSets( $theme_patterns, array_intersect( $theme_patterns, $registered ), 'Could not confirm theme patterns were registered.' );
     514    }
     515
     516    /**
     517     * Ensures theme patterns are not registered when no themes are active and valid.
     518     *
     519     * @ticket 59723
     520     *
     521     * @covers ::_register_theme_block_patterns
     522     */
     523    public function test_register_theme_block_patterns_on_init_skipped_during_install() {
     524        // This test needs to use access static class properties.
     525        $registry = WP_Block_Patterns_Registry::get_instance();
     526
     527        // Ensure we're using a theme with patterns.
     528        switch_theme( 'twentytwentythree' );
     529
     530        $theme          = wp_get_theme();
     531        $theme_patterns = array_values( wp_list_pluck( $theme->get_block_patterns(), 'slug' ) );
     532
     533        /*
     534         * This will short-circuit theme activation.
     535         * @see wp_get_active_and_valid_themes().
     536         */
     537        wp_installing( true );
     538
     539        // This helper is fired on the init hook.
     540        _register_theme_block_patterns();
     541
     542        $registered = wp_list_pluck( $registry->get_all_registered(), 'name' );
     543
     544        // Cleanup.
     545        wp_installing( false );
     546
     547        $this->assertEmpty( array_intersect( $theme_patterns, $registered ), 'Theme patterns were were incorrectly registered.' );
     548    }
    485549}
Note: See TracChangeset for help on using the changeset viewer.