Make WordPress Core

Changeset 55939


Ignore:
Timestamp:
06/19/2023 10:31:45 AM (12 months ago)
Author:
spacedmonkey
Message:

Themes: Fix caching issue in get_post_templates method in WP_Theme.

Fix an edge case caching issue, where if a developer use the wp_cache_themes_persistently filter and is running the site in multisite configuration, it meant block template are incorrectly cached. Block templates are stored in the posts table. But in a multisite configuration, different sites on the multisite could have different block templates stored in there post table. As themes cache group is a global group, it resulted in incorrect values being cached.

Props maniu, spacedmonkey, hellofromTonya, oglekler, mukesh27, joemcgill.
Fixes #57886.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme.php

    r55885 r55939  
    12841284            }
    12851285
    1286             if ( current_theme_supports( 'block-templates' ) ) {
    1287                 $block_templates = get_block_templates( array(), 'wp_template' );
    1288                 foreach ( get_post_types( array( 'public' => true ) ) as $type ) {
    1289                     foreach ( $block_templates as $block_template ) {
    1290                         if ( ! $block_template->is_custom ) {
    1291                             continue;
    1292                         }
    1293 
    1294                         if ( isset( $block_template->post_types ) && ! in_array( $type, $block_template->post_types, true ) ) {
    1295                             continue;
    1296                         }
    1297 
    1298                         $post_templates[ $type ][ $block_template->slug ] = $block_template->title;
     1286            $this->cache_add( 'post_templates', $post_templates );
     1287        }
     1288
     1289        if ( current_theme_supports( 'block-templates' ) ) {
     1290            $block_templates = get_block_templates( array(), 'wp_template' );
     1291            foreach ( get_post_types( array( 'public' => true ) ) as $type ) {
     1292                foreach ( $block_templates as $block_template ) {
     1293                    if ( ! $block_template->is_custom ) {
     1294                        continue;
    12991295                    }
    1300                 }
    1301             }
    1302 
    1303             $this->cache_add( 'post_templates', $post_templates );
     1296
     1297                    if ( isset( $block_template->post_types ) && ! in_array( $type, $block_template->post_types, true ) ) {
     1298                        continue;
     1299                    }
     1300
     1301                    $post_templates[ $type ][ $block_template->slug ] = $block_template->title;
     1302                }
     1303            }
    13041304        }
    13051305
Note: See TracChangeset for help on using the changeset viewer.