Make WordPress Core


Ignore:
Timestamp:
05/24/2021 05:38:59 PM (5 years ago)
Author:
jorgefilipecosta
Message:

Block Editor: Add Global Styles support using theme.json file.

This is the second piece of landing the theme.json processing in WordPress core.
It includes the mechanism that outputs the CSS styles of a theme.json file.

Props nosolosw, youknowriad.
See #53175.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/script-loader.php

    r50934 r50973  
    22342234
    22352235/**
     2236 * Enqueues the global styles defined via theme.json.
     2237 *
     2238 * @since 5.8.0
     2239 *
     2240 * @return void
     2241 */
     2242function wp_enqueue_global_styles() {
     2243    if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) {
     2244        return;
     2245    }
     2246
     2247    $can_use_cache = (
     2248        ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) &&
     2249        ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&
     2250        ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) &&
     2251        ! is_admin()
     2252    );
     2253
     2254    $stylesheet = null;
     2255    if ( $can_use_cache ) {
     2256        $cache = get_transient( 'global_styles' );
     2257        if ( $cache ) {
     2258            $stylesheet = $cache;
     2259        }
     2260    }
     2261
     2262    if ( null === $stylesheet ) {
     2263        $settings   = get_default_block_editor_settings();
     2264        $theme_json = WP_Theme_JSON_Resolver::get_merged_data( $settings );
     2265        $stylesheet = $theme_json->get_stylesheet();
     2266
     2267        if ( $can_use_cache ) {
     2268            set_transient( 'global_styles', $stylesheet, MINUTE_IN_SECONDS );
     2269        }
     2270    }
     2271
     2272    if ( empty( $stylesheet ) ) {
     2273        return;
     2274    }
     2275
     2276    wp_register_style( 'global-styles', false, array(), true, true );
     2277    wp_add_inline_style( 'global-styles', $stylesheet );
     2278    wp_enqueue_style( 'global-styles' );
     2279}
     2280
     2281/**
    22362282 * Checks if the editor scripts and styles for all registered block types
    22372283 * should be enqueued on the current screen.
Note: See TracChangeset for help on using the changeset viewer.