<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[56978] trunk: Themes: Make caches for block patterns clearable.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/56978">56978</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/56978","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>joemcgill</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-10-20 19:06:46 +0000 (Fri, 20 Oct 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Themes: Make caches for block patterns clearable.
In <a href="https://core.trac.wordpress.org/changeset/56765">[56765]</a>, theme block pattern files were cached to a transient as a performance enhancement. However, transients are not easily clearable when caches are flushed on environments not using a persistent cache, which can lead to errors if the theme files are renamed, edited, or moved.
This changes the caching mechanism to use `wp_cache_set()` instead, and caches these values to the global group so they are still persistent on environments using an object cache, and will be cleared by a cache flush.
In addition, the helper `_wp_get_block_patterns` has been moved `WP_Theme::get_block_patterns` for consistency with other block related theme methods and cache helpers for these values, `WP_Theme::get_pattern_cache` and `WP_Theme::set_pattern_cache`, have been made private.
Relevant unit tests updated.
Props: afercia, flixos90, mukesh27, joemcgill.
Fixes <a href="https://core.trac.wordpress.org/ticket/59633">#59633</a>. See <a href="https://core.trac.wordpress.org/ticket/59591">#59591</a>, <a href="https://core.trac.wordpress.org/ticket/59490">#59490</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblockpatternsphp">trunk/src/wp-includes/block-patterns.php</a></li>
<li><a href="#trunksrcwpincludesclasswpthemephp">trunk/src/wp-includes/class-wp-theme.php</a></li>
<li><a href="#trunktestsphpunitteststhemewpGetGlobalStylesheetphp">trunk/tests/phpunit/tests/theme/wpGetGlobalStylesheet.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunitteststhemewpThemeGetBlockPatternsphp">trunk/tests/phpunit/tests/theme/wpThemeGetBlockPatterns.php</a></li>
</ul>
<h3>Removed Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsblockswpGetBlockPatternsphp">trunk/tests/phpunit/tests/blocks/wpGetBlockPatterns.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesblockpatternsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/block-patterns.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-patterns.php 2023-10-20 17:11:10 UTC (rev 56977)
+++ trunk/src/wp-includes/block-patterns.php 2023-10-20 19:06:46 UTC (rev 56978)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -341,7 +341,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $registry = WP_Block_Patterns_Registry::get_instance();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $themes as $theme ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $patterns = _wp_get_block_patterns( $theme );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $patterns = $theme->get_block_patterns();
</ins><span class="cx" style="display: block; padding: 0 10px"> $dirpath = $theme->get_stylesheet_directory() . '/patterns/';
</span><span class="cx" style="display: block; padding: 0 10px"> $text_domain = $theme->get( 'TextDomain' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -387,170 +387,3 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'init', '_register_theme_block_patterns' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-/**
- * Gets block pattern data for a specified theme.
- * Each pattern is defined as a PHP file and defines
- * its metadata using plugin-style headers. The minimum required definition is:
- *
- * /**
- * * Title: My Pattern
- * * Slug: my-theme/my-pattern
- * *
- *
- * The output of the PHP source corresponds to the content of the pattern, e.g.:
- *
- * <main><p><?php echo "Hello"; ?></p></main>
- *
- * If applicable, this will collect from both parent and child theme.
- *
- * Other settable fields include:
- *
- * - Description
- * - Viewport Width
- * - Inserter (yes/no)
- * - Categories (comma-separated values)
- * - Keywords (comma-separated values)
- * - Block Types (comma-separated values)
- * - Post Types (comma-separated values)
- * - Template Types (comma-separated values)
- *
- * @since 6.4.0
- * @access private
- *
- * @param WP_Theme $theme Theme object.
- * @return array Block pattern data.
- */
-function _wp_get_block_patterns( WP_Theme $theme ) {
- $can_use_cached = ! wp_is_development_mode( 'theme' );
-
- $pattern_data = $theme->get_pattern_cache();
- if ( is_array( $pattern_data ) ) {
- if ( $can_use_cached ) {
- return $pattern_data;
- }
- // If in development mode, clear pattern cache.
- $theme->delete_pattern_cache();
- }
-
- $dirpath = $theme->get_stylesheet_directory() . '/patterns/';
- $pattern_data = array();
-
- if ( ! file_exists( $dirpath ) ) {
- if ( $can_use_cached ) {
- $theme->set_pattern_cache( $pattern_data );
- }
- return $pattern_data;
- }
- $files = glob( $dirpath . '*.php' );
- if ( ! $files ) {
- if ( $can_use_cached ) {
- $theme->set_pattern_cache( $pattern_data );
- }
- return $pattern_data;
- }
-
- $default_headers = array(
- 'title' => 'Title',
- 'slug' => 'Slug',
- 'description' => 'Description',
- 'viewportWidth' => 'Viewport Width',
- 'inserter' => 'Inserter',
- 'categories' => 'Categories',
- 'keywords' => 'Keywords',
- 'blockTypes' => 'Block Types',
- 'postTypes' => 'Post Types',
- 'templateTypes' => 'Template Types',
- );
-
- $properties_to_parse = array(
- 'categories',
- 'keywords',
- 'blockTypes',
- 'postTypes',
- 'templateTypes',
- );
-
- foreach ( $files as $file ) {
- $pattern = get_file_data( $file, $default_headers );
-
- if ( empty( $pattern['slug'] ) ) {
- _doing_it_wrong(
- __FUNCTION__,
- sprintf(
- /* translators: 1: file name. */
- __( 'Could not register file "%s" as a block pattern ("Slug" field missing)' ),
- $file
- ),
- '6.0.0'
- );
- continue;
- }
-
- if ( ! preg_match( '/^[A-z0-9\/_-]+$/', $pattern['slug'] ) ) {
- _doing_it_wrong(
- __FUNCTION__,
- sprintf(
- /* 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']
- ),
- '6.0.0'
- );
- }
-
- // Title is a required property.
- if ( ! $pattern['title'] ) {
- _doing_it_wrong(
- __FUNCTION__,
- sprintf(
- /* translators: 1: file name. */
- __( 'Could not register file "%s" as a block pattern ("Title" field missing)' ),
- $file
- ),
- '6.0.0'
- );
- continue;
- }
-
- // For properties of type array, parse data as comma-separated.
- foreach ( $properties_to_parse as $property ) {
- if ( ! empty( $pattern[ $property ] ) ) {
- $pattern[ $property ] = array_filter( wp_parse_list( (string) $pattern[ $property ] ) );
- } else {
- unset( $pattern[ $property ] );
- }
- }
-
- // Parse properties of type int.
- $property = 'viewportWidth';
- if ( ! empty( $pattern[ $property ] ) ) {
- $pattern[ $property ] = (int) $pattern[ $property ];
- } else {
- unset( $pattern[ $property ] );
- }
-
- // Parse properties of type bool.
- $property = 'inserter';
- if ( ! empty( $pattern[ $property ] ) ) {
- $pattern[ $property ] = in_array(
- strtolower( $pattern[ $property ] ),
- array( 'yes', 'true' ),
- true
- );
- } else {
- unset( $pattern[ $property ] );
- }
-
- $key = str_replace( $dirpath, '', $file );
-
- $pattern_data[ $key ] = $pattern;
- }
-
- if ( $can_use_cached ) {
- $theme->set_pattern_cache( $pattern_data );
- }
-
- return $pattern_data;
-}
</del></span></pre></div>
<a id="trunksrcwpincludesclasswpthemephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-theme.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-theme.php 2023-10-20 17:11:10 UTC (rev 56977)
+++ trunk/src/wp-includes/class-wp-theme.php 2023-10-20 19:06:46 UTC (rev 56978)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -845,48 +845,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * 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
- */
- public function delete_pattern_cache() {
- delete_transient( 'wp_theme_patterns_' . $this->stylesheet );
- }
-
- /**
</del><span class="cx" style="display: block; padding: 0 10px"> * Gets a raw, unformatted theme header.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * The header is sanitized, but is not translated, and is not marked up for display.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1841,6 +1799,213 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Gets block pattern data for a specified theme.
+ * Each pattern is defined as a PHP file and defines
+ * its metadata using plugin-style headers. The minimum required definition is:
+ *
+ * /**
+ * * Title: My Pattern
+ * * Slug: my-theme/my-pattern
+ * *
+ *
+ * The output of the PHP source corresponds to the content of the pattern, e.g.:
+ *
+ * <main><p><?php echo "Hello"; ?></p></main>
+ *
+ * If applicable, this will collect from both parent and child theme.
+ *
+ * Other settable fields include:
+ *
+ * - Description
+ * - Viewport Width
+ * - Inserter (yes/no)
+ * - Categories (comma-separated values)
+ * - Keywords (comma-separated values)
+ * - Block Types (comma-separated values)
+ * - Post Types (comma-separated values)
+ * - Template Types (comma-separated values)
+ *
+ * @since 6.4.0
+ *
+ * @return array Block pattern data.
+ */
+ public function get_block_patterns() {
+ $can_use_cached = ! wp_is_development_mode( 'theme' );
+
+ $pattern_data = $this->get_pattern_cache();
+ if ( is_array( $pattern_data ) ) {
+ if ( $can_use_cached ) {
+ return $pattern_data;
+ }
+ // If in development mode, clear pattern cache.
+ $this->delete_pattern_cache();
+ }
+
+ $dirpath = $this->get_stylesheet_directory() . '/patterns/';
+ $pattern_data = array();
+
+ if ( ! file_exists( $dirpath ) ) {
+ if ( $can_use_cached ) {
+ $this->set_pattern_cache( $pattern_data );
+ }
+ return $pattern_data;
+ }
+ $files = glob( $dirpath . '*.php' );
+ if ( ! $files ) {
+ if ( $can_use_cached ) {
+ $this->set_pattern_cache( $pattern_data );
+ }
+ return $pattern_data;
+ }
+
+ $default_headers = array(
+ 'title' => 'Title',
+ 'slug' => 'Slug',
+ 'description' => 'Description',
+ 'viewportWidth' => 'Viewport Width',
+ 'inserter' => 'Inserter',
+ 'categories' => 'Categories',
+ 'keywords' => 'Keywords',
+ 'blockTypes' => 'Block Types',
+ 'postTypes' => 'Post Types',
+ 'templateTypes' => 'Template Types',
+ );
+
+ $properties_to_parse = array(
+ 'categories',
+ 'keywords',
+ 'blockTypes',
+ 'postTypes',
+ 'templateTypes',
+ );
+
+ foreach ( $files as $file ) {
+ $pattern = get_file_data( $file, $default_headers );
+
+ if ( empty( $pattern['slug'] ) ) {
+ _doing_it_wrong(
+ __FUNCTION__,
+ sprintf(
+ /* translators: 1: file name. */
+ __( 'Could not register file "%s" as a block pattern ("Slug" field missing)' ),
+ $file
+ ),
+ '6.0.0'
+ );
+ continue;
+ }
+
+ if ( ! preg_match( '/^[A-z0-9\/_-]+$/', $pattern['slug'] ) ) {
+ _doing_it_wrong(
+ __FUNCTION__,
+ sprintf(
+ /* 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']
+ ),
+ '6.0.0'
+ );
+ }
+
+ // Title is a required property.
+ if ( ! $pattern['title'] ) {
+ _doing_it_wrong(
+ __FUNCTION__,
+ sprintf(
+ /* translators: 1: file name. */
+ __( 'Could not register file "%s" as a block pattern ("Title" field missing)' ),
+ $file
+ ),
+ '6.0.0'
+ );
+ continue;
+ }
+
+ // For properties of type array, parse data as comma-separated.
+ foreach ( $properties_to_parse as $property ) {
+ if ( ! empty( $pattern[ $property ] ) ) {
+ $pattern[ $property ] = array_filter( wp_parse_list( (string) $pattern[ $property ] ) );
+ } else {
+ unset( $pattern[ $property ] );
+ }
+ }
+
+ // Parse properties of type int.
+ $property = 'viewportWidth';
+ if ( ! empty( $pattern[ $property ] ) ) {
+ $pattern[ $property ] = (int) $pattern[ $property ];
+ } else {
+ unset( $pattern[ $property ] );
+ }
+
+ // Parse properties of type bool.
+ $property = 'inserter';
+ if ( ! empty( $pattern[ $property ] ) ) {
+ $pattern[ $property ] = in_array(
+ strtolower( $pattern[ $property ] ),
+ array( 'yes', 'true' ),
+ true
+ );
+ } else {
+ unset( $pattern[ $property ] );
+ }
+
+ $key = str_replace( $dirpath, '', $file );
+
+ $pattern_data[ $key ] = $pattern;
+ }
+
+ if ( $can_use_cached ) {
+ $this->set_pattern_cache( $pattern_data );
+ }
+
+ return $pattern_data;
+ }
+
+ /**
+ * Gets block pattern cache.
+ *
+ * @since 6.4.0
+ *
+ * @return array|false Returns an array of patterns if cache is found, otherwise false.
+ */
+ private function get_pattern_cache() {
+ if ( ! $this->exists() ) {
+ return false;
+ }
+ $pattern_data = wp_cache_get( '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.
+ */
+ private function set_pattern_cache( array $patterns ) {
+ $pattern_data = array(
+ 'version' => $this->get( 'Version' ),
+ 'patterns' => $patterns,
+ );
+ wp_cache_set( 'wp_theme_patterns_' . $this->stylesheet, $pattern_data );
+ }
+
+ /**
+ * Clears block pattern cache.
+ *
+ * @since 6.4.0
+ */
+ public function delete_pattern_cache() {
+ wp_cache_delete( 'wp_theme_patterns_' . $this->stylesheet );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Enables a theme for all sites on the current network.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.6.0
</span></span></pre></div>
<a id="trunktestsphpunittestsblockswpGetBlockPatternsphp"></a>
<div class="delfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Deleted: trunk/tests/phpunit/tests/blocks/wpGetBlockPatterns.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/wpGetBlockPatterns.php 2023-10-20 17:11:10 UTC (rev 56977)
+++ trunk/tests/phpunit/tests/blocks/wpGetBlockPatterns.php 2023-10-20 19:06:46 UTC (rev 56978)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,153 +0,0 @@
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-<?php
-/**
- * Tests for _wp_get_block_patterns.
- *
- * @package WordPress
- * @subpackage Blocks
- * @since 6.4.0
- *
- * @group blocks
- *
- * @covers ::_wp_get_block_patterns
- */
-class Tests_Blocks_WpGetBlockPatterns extends WP_UnitTestCase {
- /**
- * @ticket 59490
- *
- * @dataProvider data_wp_get_block_patterns
- *
- * @param string $theme The theme's slug.
- * @param array $expected The expected pattern data.
- */
- public function test_should_return_block_patterns( $theme, $expected ) {
- $patterns = _wp_get_block_patterns( wp_get_theme( $theme ) );
- $this->assertSameSets( $expected, $patterns );
- }
-
- /**
- * @ticket 59490
- */
- public function test_delete_theme_cache() {
- $theme = wp_get_theme( 'block-theme-patterns' );
- _wp_get_block_patterns( $theme );
- $this->assertSameSets(
- array(
- 'cta.php' => array(
- 'title' => 'Centered Call To Action',
- 'slug' => 'block-theme-patterns/cta',
- 'description' => '',
- 'categories' => array( 'call-to-action' ),
- ),
- ),
- $theme->get_pattern_cache(),
- 'The transient for block theme patterns should be set'
- );
- $theme->delete_pattern_cache();
- $this->assertFalse(
- $theme->get_pattern_cache(),
- 'The transient for block theme patterns should have been cleared'
- );
- }
-
- /**
- * @ticket 59490
- */
- public function test_should_clear_transient_after_switching_theme() {
- switch_theme( 'block-theme' );
- $theme1 = wp_get_theme();
- _wp_get_block_patterns( $theme1 );
- $this->assertSameSets(
- array(),
- $theme1->get_pattern_cache(),
- 'The transient for block theme should be set'
- );
- switch_theme( 'block-theme-patterns' );
- $this->assertFalse( $theme1->get_pattern_cache(), 'Transient should not be set for block theme after switch theme' );
- $theme2 = wp_get_theme();
- $this->assertFalse( $theme2->get_pattern_cache(), 'Transient should not be set for block theme patterns before being requested' );
- _wp_get_block_patterns( $theme2 );
- $this->assertSameSets(
- array(
- 'cta.php' => array(
- 'title' => 'Centered Call To Action',
- 'slug' => 'block-theme-patterns/cta',
- 'description' => '',
- 'categories' => array( 'call-to-action' ),
- ),
-
- ),
- $theme2->get_pattern_cache(),
- 'The transient for block theme patterns should be set'
- );
- }
-
- /**
- * Data provider.
- *
- * @return array[]
- */
- public function data_wp_get_block_patterns() {
- return array(
- array(
- 'theme' => 'block-theme',
- 'patterns' => array(),
- ),
- array(
- 'theme' => 'block-theme-child',
- 'patterns' => array(),
- ),
- array(
- 'theme' => 'block-theme-patterns',
- 'patterns' => array(
- 'cta.php' => array(
- 'title' => 'Centered Call To Action',
- 'slug' => 'block-theme-patterns/cta',
- 'description' => '',
- 'categories' => array( 'call-to-action' ),
- ),
- ),
- ),
- array(
- 'theme' => 'broken-theme',
- 'patterns' => array(),
- ),
- array(
- 'theme' => 'invalid',
- 'patterns' => array(),
- ),
- );
- }
-
- /**
- * Tests that _wp_get_block_patterns() clears existing transient when in theme development mode.
- *
- * @ticket 59591
- */
- public function test_should_clear_existing_transient_when_in_development_mode() {
- $theme = wp_get_theme( 'block-theme-patterns' );
-
- // Calling the function should set the cache.
- _wp_get_block_patterns( $theme );
- $this->assertSameSets(
- array(
- 'cta.php' => array(
- 'title' => 'Centered Call To Action',
- 'slug' => 'block-theme-patterns/cta',
- 'description' => '',
- 'categories' => array( 'call-to-action' ),
- ),
- ),
- $theme->get_pattern_cache(),
- 'The transient for block theme patterns should be set'
- );
-
- // Calling the function while in theme development mode should clear the cache.
- $GLOBALS['_wp_tests_development_mode'] = 'theme';
- _wp_get_block_patterns( $theme );
- unset( $GLOBALS['_wp_tests_development_mode'] ); // Reset to not pollute other tests.
- $this->assertFalse(
- $theme->get_pattern_cache(),
- 'The transient for block theme patterns should have been cleared due to theme development mode'
- );
- }
-}
</del></span></pre></div>
<a id="trunktestsphpunitteststhemewpGetGlobalStylesheetphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/theme/wpGetGlobalStylesheet.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/theme/wpGetGlobalStylesheet.php 2023-10-20 17:11:10 UTC (rev 56977)
+++ trunk/tests/phpunit/tests/theme/wpGetGlobalStylesheet.php 2023-10-20 19:06:46 UTC (rev 56978)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -26,6 +26,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> private $switch_to_default_theme_at_teardown = false;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public function tear_down() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Reset development mode after each test.
+ unset( $GLOBALS['_wp_tests_development_mode'] );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Reset the theme support.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $this->remove_theme_support_at_teardown ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->remove_theme_support_at_teardown = false;
</span></span></pre></div>
<a id="trunktestsphpunitteststhemewpThemeGetBlockPatternsphpfromrev56977trunktestsphpunittestsblockswpGetBlockPatternsphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/tests/theme/wpThemeGetBlockPatterns.php (from rev 56977, trunk/tests/phpunit/tests/blocks/wpGetBlockPatterns.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/theme/wpThemeGetBlockPatterns.php (rev 0)
+++ trunk/tests/phpunit/tests/theme/wpThemeGetBlockPatterns.php 2023-10-20 19:06:46 UTC (rev 56978)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,198 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Tests for WP_Theme::get_block_patterns.
+ *
+ * @package WordPress
+ * @subpackage Blocks
+ * @since 6.4.0
+ *
+ * @group blocks
+ * @group themes
+ *
+ * @covers WP_Theme::get_block_patterns
+ */
+class Tests_Theme_WPThemeGetBlockPatterns extends WP_UnitTestCase {
+
+ public static function wpSetUpBeforeClass() {
+ // Ensure development mode is reset before running these tests.
+ unset( $GLOBALS['_wp_tests_development_mode'] );
+ }
+
+ public static function wpTearDownAfterClass() {
+ // Ensure development mode is reset after running these tests.
+ unset( $GLOBALS['_wp_tests_development_mode'] );
+ }
+
+ /**
+ * Test helper to access the private get_pattern_cache method of a theme.
+ *
+ * @param WP_Theme $wp_theme A WP_Theme object.
+ * @return array|false Returns an array of patterns if cache is found, otherwise false.
+ */
+ private function get_pattern_cache( $wp_theme ) {
+ $reflection = new ReflectionMethod( $wp_theme, 'get_pattern_cache' );
+ $reflection->setAccessible( true );
+
+ $pattern_cache = $reflection->invoke( $wp_theme, 'get_pattern_cache' );
+ $reflection->setAccessible( false );
+
+ return $pattern_cache;
+ }
+
+ /**
+ * @ticket 59490
+ *
+ * @dataProvider data_get_block_patterns
+ *
+ * @param string $theme_slug The theme's slug.
+ * @param array $expected The expected pattern data.
+ */
+ public function test_should_return_block_patterns( $theme_slug, $expected ) {
+ $theme = wp_get_theme( $theme_slug );
+ $patterns = $theme->get_block_patterns();
+ $this->assertSameSets( $expected, $patterns );
+ }
+
+ /**
+ * @ticket 59490
+ *
+ * @covers WP_Theme::delete_pattern_cache
+ */
+ public function test_delete_pattern_cache() {
+ $theme = wp_get_theme( 'block-theme-patterns' );
+
+ $this->assertTrue( $theme->exists(), 'The test theme could not be found.' );
+
+ $theme->get_block_patterns();
+
+ $this->assertSameSets(
+ array(
+ 'cta.php' => array(
+ 'title' => 'Centered Call To Action',
+ 'slug' => 'block-theme-patterns/cta',
+ 'description' => '',
+ 'categories' => array( 'call-to-action' ),
+ ),
+ ),
+ $this->get_pattern_cache( $theme ),
+ 'The cache for block theme patterns should match the expected.'
+ );
+ $theme->delete_pattern_cache();
+ $this->assertFalse(
+ $this->get_pattern_cache( $theme ),
+ 'The cache for block theme patterns should have been cleared.'
+ );
+ }
+
+ /**
+ * @ticket 59490
+ */
+ public function test_should_clear_cache_after_switching_theme() {
+ switch_theme( 'block-theme' );
+ $theme1 = wp_get_theme();
+
+ $this->assertTrue( $theme1->exists(), 'The block-theme test theme could not be found.' );
+
+ $theme1->get_block_patterns();
+ $this->assertSameSets(
+ array(),
+ $this->get_pattern_cache( $theme1 ),
+ 'The cache for block theme should be empty.'
+ );
+
+ switch_theme( 'block-theme-patterns' );
+
+ $theme2 = wp_get_theme();
+ $this->assertTrue( $theme2->exists(), 'The block-theme-patterns test theme could not be found.' );
+
+ $this->assertFalse( $this->get_pattern_cache( $theme1 ), 'Cache should not be set for block theme after switch theme.' );
+ $this->assertFalse( $this->get_pattern_cache( $theme2 ), 'Cache should not be set for block theme patterns before being requested.' );
+
+ $theme2->get_block_patterns( $theme2 );
+ $this->assertSameSets(
+ array(
+ 'cta.php' => array(
+ 'title' => 'Centered Call To Action',
+ 'slug' => 'block-theme-patterns/cta',
+ 'description' => '',
+ 'categories' => array( 'call-to-action' ),
+ ),
+
+ ),
+ $this->get_pattern_cache( $theme2 ),
+ 'The cache for block theme patterns should match the expected.'
+ );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public function data_get_block_patterns() {
+ return array(
+ array(
+ 'theme' => 'block-theme',
+ 'patterns' => array(),
+ ),
+ array(
+ 'theme' => 'block-theme-child',
+ 'patterns' => array(),
+ ),
+ array(
+ 'theme' => 'block-theme-patterns',
+ 'patterns' => array(
+ 'cta.php' => array(
+ 'title' => 'Centered Call To Action',
+ 'slug' => 'block-theme-patterns/cta',
+ 'description' => '',
+ 'categories' => array( 'call-to-action' ),
+ ),
+ ),
+ ),
+ array(
+ 'theme' => 'broken-theme',
+ 'patterns' => array(),
+ ),
+ array(
+ 'theme' => 'invalid',
+ 'patterns' => array(),
+ ),
+ );
+ }
+
+ /**
+ * Tests that WP_Theme::get_block_patterns() clears existing cache when in theme development mode.
+ *
+ * @ticket 59591
+ */
+ public function test_should_clear_existing_cache_when_in_development_mode() {
+ $theme = wp_get_theme( 'block-theme-patterns' );
+
+ $this->assertTrue( $theme->exists(), 'The test theme could not be found.' );
+
+ // Calling the function should set the cache.
+ $theme->get_block_patterns();
+ $this->assertSameSets(
+ array(
+ 'cta.php' => array(
+ 'title' => 'Centered Call To Action',
+ 'slug' => 'block-theme-patterns/cta',
+ 'description' => '',
+ 'categories' => array( 'call-to-action' ),
+ ),
+ ),
+ $this->get_pattern_cache( $theme ),
+ 'The cache for block theme patterns should be set.'
+ );
+
+ // Calling the function while in theme development mode should clear the cache.
+ $GLOBALS['_wp_tests_development_mode'] = 'theme';
+ $theme->get_block_patterns( $theme );
+ unset( $GLOBALS['_wp_tests_development_mode'] ); // Reset to not pollute other tests.
+ $this->assertFalse(
+ $this->get_pattern_cache( $theme ),
+ 'The cache for block theme patterns should have been cleared due to theme development mode.'
+ );
+ }
+}
</ins></span></pre>
</div>
</div>
</body>
</html>