Make WordPress Core

Changeset 57683


Ignore:
Timestamp:
02/21/2024 06:48:30 PM (9 months ago)
Author:
joemcgill
Message:

Editor: Load pattern content only when used.

Previously, the content for all registered patterns would get loaded on each request when the patterns are registered. Instead, this stores the path the pattern file during registration and reads the content the first time the pattern is used, which is a performance optimization.

Props thekt12, spacedmonkey, gziolo, aristath, joemcgill.
Fixes #59532.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

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

    r57353 r57683  
    392392            }
    393393
    394             // The actual pattern content is the output of the file.
    395             ob_start();
    396             include $file_path;
    397             $pattern_data['content'] = ob_get_clean();
    398             if ( ! $pattern_data['content'] ) {
    399                 continue;
    400             }
     394            $pattern_data['file_path'] = $file_path;
    401395
    402396            // Translate the pattern metadata.
  • trunk/src/wp-includes/class-wp-block-patterns-registry.php

    r56960 r57683  
    102102        }
    103103
    104         if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) {
    105             _doing_it_wrong(
    106                 __METHOD__,
    107                 __( 'Pattern content must be a string.' ),
    108                 '5.5.0'
    109             );
    110             return false;
     104        if ( ! isset( $pattern_properties['file_path'] ) ) {
     105            if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) {
     106                _doing_it_wrong(
     107                    __METHOD__,
     108                    __( 'Pattern content must be a string.' ),
     109                    '5.5.0'
     110                );
     111                return false;
     112            }
    111113        }
    112114
     
    179181
    180182    /**
     183     * Retrieves the content of a registered block pattern.
     184     *
     185     * @since 6.5.0
     186     *
     187     * @param string $pattern_name      Block pattern name including namespace.
     188     * @param bool   $outside_init_only Optional. Return only patterns registered outside the `init` action. Default false.
     189     * @return string The content of the block pattern.
     190     */
     191    private function get_content( $pattern_name, $outside_init_only = false ) {
     192        if ( $outside_init_only ) {
     193            $patterns = &$this->registered_patterns_outside_init;
     194        } else {
     195            $patterns = &$this->registered_patterns;
     196        }
     197        if ( ! isset( $patterns[ $pattern_name ]['content'] ) && isset( $patterns[ $pattern_name ]['file_path'] ) ) {
     198            ob_start();
     199            include $patterns[ $pattern_name ]['file_path'];
     200            $patterns[ $pattern_name ]['content'] = ob_get_clean();
     201            unset( $patterns[ $pattern_name ]['file_path'] );
     202        }
     203        return $patterns[ $pattern_name ]['content'];
     204    }
     205
     206    /**
    181207     * Retrieves an array containing the properties of a registered block pattern.
    182208     *
     
    192218
    193219        $pattern            = $this->registered_patterns[ $pattern_name ];
     220        $pattern['content'] = $this->get_content( $pattern_name );
    194221        $pattern['content'] = $this->prepare_content( $pattern, get_hooked_blocks() );
    195222
     
    207234     */
    208235    public function get_all_registered( $outside_init_only = false ) {
    209         $patterns      = array_values(
    210             $outside_init_only
     236        $patterns      = $outside_init_only
    211237                ? $this->registered_patterns_outside_init
    212                 : $this->registered_patterns
    213         );
     238                : $this->registered_patterns;
    214239        $hooked_blocks = get_hooked_blocks();
     240
    215241        foreach ( $patterns as $index => $pattern ) {
     242            $pattern['content']            = $this->get_content( $pattern['name'], $outside_init_only );
    216243            $patterns[ $index ]['content'] = $this->prepare_content( $pattern, $hooked_blocks );
    217244        }
    218         return $patterns;
     245
     246        return array_values( $patterns );
    219247    }
    220248
Note: See TracChangeset for help on using the changeset viewer.