Make WordPress Core


Ignore:
Timestamp:
01/18/2023 11:38:16 AM (4 years ago)
Author:
hellofromTonya
Message:

Themes: Introduce wp_theme_has_theme_json() for public consumption.

Adds wp_theme_has_theme_json() for public consumption, to replace the private internal Core-only WP_Theme_JSON_Resolver::theme_has_support() method. This new global function checks if a theme or its parent has a theme.json file.

For performance, results are cached as an integer 1 or 0 in the 'theme_json' group with 'wp_theme_has_theme_json' key. This is a non-persistent cache. Why? To make the derived data from theme.json is always fresh from the potential modifications done via hooks that can use dynamic data (modify the stylesheet depending on some option, settings depending on user permissions, etc.).

Also adds a new public function wp_clean_theme_json_cache() to clear the cache on 'switch_theme' and start_previewing_theme'.

References:

Follow-up to [54493], [53282], [52744], [52049], [50959].

Props oandregal, afragen, alexstine, aristath, azaozz, costdev, flixos90, hellofromTonya, mamaduka, mcsf, ocean90, spacedmonkey.
Fixes #56975.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/theme/wpThemeJsonResolver.php

    r55067 r55086  
    111111
    112112                // Reset data between tests.
    113                 WP_Theme_JSON_Resolver::clean_cached_data();
     113                wp_clean_theme_json_cache();
    114114                parent::tear_down();
    115115        }
     
    377377         */
    378378        public function test_get_core_data( $should_fire_filter, $core_is_cached, $blocks_are_cached ) {
    379                 WP_Theme_JSON_Resolver::clean_cached_data();
     379                wp_clean_theme_json_cache();
    380380
    381381                // If should cache core, then fire the method to cache it before running the tests.
     
    430430                        ),
    431431                );
    432         }
    433 
    434         /**
    435          * @ticket 52991
    436          */
    437         public function test_switching_themes_recalculates_data() {
    438                 // The "default" theme doesn't have theme.json support.
    439                 switch_theme( 'default' );
    440                 $default = WP_Theme_JSON_Resolver::theme_has_support();
    441 
    442                 // Switch to a theme that does have support.
    443                 switch_theme( 'block-theme' );
    444                 $has_theme_json_support = WP_Theme_JSON_Resolver::theme_has_support();
    445 
    446                 $this->assertFalse( $default );
    447                 $this->assertTrue( $has_theme_json_support );
    448432        }
    449433
     
    483467                remove_theme_support( 'appearance-tools' );
    484468
    485                 $this->assertFalse( WP_Theme_JSON_Resolver::theme_has_support() );
     469                $this->assertFalse( wp_theme_has_theme_json() );
    486470                $this->assertTrue( $settings['typography']['lineHeight'] );
    487471                $this->assertSame( $color_palette, $settings['color']['palette']['theme'] );
     
    644628                for ( $i = 0; $i < 3; $i++ ) {
    645629                        WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
    646                         WP_Theme_JSON_Resolver::clean_cached_data();
     630                        wp_clean_theme_json_cache();
    647631                }
    648632                $this->assertSame( 0, $global_styles_query_count, 'Unexpected SQL queries detected for the wp_global_style post type prior to creation.' );
     
    657641                for ( $i = 0; $i < 3; $i++ ) {
    658642                        $new_user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
    659                         WP_Theme_JSON_Resolver::clean_cached_data();
     643                        wp_clean_theme_json_cache();
    660644                        $this->assertSameSets( $user_cpt, $new_user_cpt, "User CPTs do not match on run {$i}." );
    661645                }
     
    674658                for ( $i = 0; $i < 3; $i++ ) {
    675659                        WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
    676                         WP_Theme_JSON_Resolver::clean_cached_data();
     660                        wp_clean_theme_json_cache();
    677661                }
    678662                $query_count = get_num_queries() - $query_count;
Note: See TracChangeset for help on using the changeset viewer.