From 3bc56b3de03eca9420f40594940612d91f12c4d6 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 3 Oct 2023 18:19:23 +0000 Subject: [PATCH] Editor: Simplify return shape and logic of `_wp_get_block_patterns()`. Follow up to [56765]. Props spacedmonkey. Fixes #59490. Built from https://develop.svn.wordpress.org/trunk@56771 git-svn-id: http://core.svn.wordpress.org/trunk@56283 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/block-patterns.php | 41 ++++++++++++---------------------- wp-includes/class-wp-theme.php | 35 ++++++++++++++++++++++++++++- wp-includes/version.php | 2 +- 3 files changed, 49 insertions(+), 29 deletions(-) diff --git a/wp-includes/block-patterns.php b/wp-includes/block-patterns.php index 0bd5f4bdc8..dd70d2b589 100644 --- a/wp-includes/block-patterns.php +++ b/wp-includes/block-patterns.php @@ -341,11 +341,11 @@ function _register_theme_block_patterns() { $registry = WP_Block_Patterns_Registry::get_instance(); foreach ( $themes as $theme ) { - $pattern_data = _wp_get_block_patterns( $theme ); - $dirpath = $theme->get_stylesheet_directory() . '/patterns/'; - $text_domain = $theme->get( 'TextDomain' ); + $patterns = _wp_get_block_patterns( $theme ); + $dirpath = $theme->get_stylesheet_directory() . '/patterns/'; + $text_domain = $theme->get( 'TextDomain' ); - foreach ( $pattern_data['patterns'] as $file => $pattern_data ) { + foreach ( $patterns as $file => $pattern_data ) { if ( $registry->is_registered( $pattern_data['slug'] ) ) { continue; } @@ -405,42 +405,29 @@ add_action( 'init', '_register_theme_block_patterns' ); * @param WP_Theme $theme Theme object. * @return array Block pattern data. */ - function _wp_get_block_patterns( WP_Theme $theme ) { - if ( ! $theme->exists() ) { - return array( - 'version' => false, - 'patterns' => array(), - ); - } - - $transient_name = 'wp_theme_patterns_' . $theme->get_stylesheet(); - $version = $theme->get( 'Version' ); $can_use_cached = ! wp_is_development_mode( 'theme' ); if ( $can_use_cached ) { - $pattern_data = get_transient( $transient_name ); - if ( is_array( $pattern_data ) && $pattern_data['version'] === $version ) { + $pattern_data = $theme->get_pattern_cache(); + if ( is_array( $pattern_data ) ) { return $pattern_data; } } - $pattern_data = array( - 'version' => $version, - 'patterns' => array(), - ); $dirpath = $theme->get_stylesheet_directory() . '/patterns/'; + $pattern_data = array(); if ( ! file_exists( $dirpath ) ) { if ( $can_use_cached ) { - set_transient( $transient_name, $pattern_data ); + $theme->set_pattern_cache( $pattern_data ); } return $pattern_data; } $files = glob( $dirpath . '*.php' ); if ( ! $files ) { if ( $can_use_cached ) { - set_transient( $transient_name, $pattern_data ); + $theme->set_pattern_cache( $pattern_data ); } return $pattern_data; } @@ -473,7 +460,7 @@ function _wp_get_block_patterns( WP_Theme $theme ) { _doing_it_wrong( __FUNCTION__, sprintf( - /* translators: %s: file name. */ + /* translators: 1: file name. */ __( 'Could not register file "%s" as a block pattern ("Slug" field missing)' ), $file ), @@ -486,7 +473,7 @@ function _wp_get_block_patterns( WP_Theme $theme ) { _doing_it_wrong( __FUNCTION__, sprintf( - /* translators: %1s: file name; %2s: slug value found. */ + /* translators: 1: file name; 2: slug value found. */ __( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")' ), $file, $pattern['slug'] @@ -500,7 +487,7 @@ function _wp_get_block_patterns( WP_Theme $theme ) { _doing_it_wrong( __FUNCTION__, sprintf( - /* translators: %1s: file name. */ + /* translators: 1: file name. */ __( 'Could not register file "%s" as a block pattern ("Title" field missing)' ), $file ), @@ -540,11 +527,11 @@ function _wp_get_block_patterns( WP_Theme $theme ) { $key = str_replace( $dirpath, '', $file ); - $pattern_data['patterns'][ $key ] = $pattern; + $pattern_data[ $key ] = $pattern; } if ( $can_use_cached ) { - set_transient( $transient_name, $pattern_data ); + $theme->set_pattern_cache( $pattern_data ); } return $pattern_data; diff --git a/wp-includes/class-wp-theme.php b/wp-includes/class-wp-theme.php index e5142d0f08..89c4cb976a 100644 --- a/wp-includes/class-wp-theme.php +++ b/wp-includes/class-wp-theme.php @@ -825,7 +825,40 @@ final class WP_Theme implements ArrayAccess { } /** - * Clear block pattern cache. + * Gets block pattern cache. + * + * @since 6.4.0 + * + * @return array|false Returns an array of patterns if cache is found, otherwise false. + */ + public function get_pattern_cache() { + if ( ! $this->exists() ) { + return false; + } + $pattern_data = get_transient( 'wp_theme_patterns_' . $this->stylesheet ); + if ( is_array( $pattern_data ) && $pattern_data['version'] === $this->get( 'Version' ) ) { + return $pattern_data['patterns']; + } + return false; + } + + /** + * Sets block pattern cache. + * + * @since 6.4.0 + * + * @param array $patterns Block patterns data to set in cache. + */ + public function set_pattern_cache( array $patterns ) { + $pattern_data = array( + 'version' => $this->get( 'Version' ), + 'patterns' => $patterns, + ); + set_transient( 'wp_theme_patterns_' . $this->stylesheet, $pattern_data ); + } + + /** + * Clears block pattern cache. * * @since 6.4.0 */ diff --git a/wp-includes/version.php b/wp-includes/version.php index adea75f453..3c03de6cd6 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-beta2-56770'; +$wp_version = '6.4-beta2-56771'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.