Make WordPress Core

Changeset 57683

Timestamp:
02/21/2024 06:48:30 PM (6 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
     184
     185
     186
     187
     188
     189
     190
     191
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
     202
     203
     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
    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
    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.