Make WordPress Core


Ignore:
Timestamp:
02/02/2023 06:50:54 PM (22 months ago)
Author:
flixos90
Message:

Editor: Add support for custom CSS in global styles.

This changeset introduces functions wp_get_global_styles_custom_css() and wp_enqueue_global_styles_custom_css(), which allow accessing and enqueuing custom CSS added via global styles.

Custom CSS via global styles is handled separately from custom CSS via the Customizer. If a site uses both features, the custom CSS from both sources will be loaded. The global styles custom CSS is then loaded after the Customizer custom CSS, so if there are any conflicts between the rules, the global styles take precedence.

Similarly to e.g. [55185], the result is cached in a non-persistent cache, except when WP_DEBUG is on to avoid interrupting the theme developer's workflow.

Props glendaviesnz, oandregal, ntsekouras, mamaduka, davidbaumwald, hellofromtonya, flixos90.
Fixes #57536.

File:
1 edited

Legend:

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

    r55176 r55192  
    426426            'textTransform'  => null,
    427427        ),
     428        'css'        => null,
    428429    );
    429430
     
    10011002        if ( in_array( 'presets', $types, true ) ) {
    10021003            $stylesheet .= $this->get_preset_classes( $setting_nodes, $origins );
     1004        }
     1005
     1006        return $stylesheet;
     1007    }
     1008
     1009    /**
     1010     * Returns the global styles custom css.
     1011     *
     1012     * @since 6.2.0
     1013     *
     1014     * @return string
     1015     */
     1016    public function get_custom_css() {
     1017        // Add the global styles root CSS.
     1018        $stylesheet = _wp_array_get( $this->theme_json, array( 'styles', 'css' ), '' );
     1019
     1020        // Add the global styles block CSS.
     1021        if ( isset( $this->theme_json['styles']['blocks'] ) ) {
     1022            foreach ( $this->theme_json['styles']['blocks'] as $name => $node ) {
     1023                $custom_block_css = _wp_array_get( $this->theme_json, array( 'styles', 'blocks', $name, 'css' ) );
     1024                if ( $custom_block_css ) {
     1025                    $selector    = static::$blocks_metadata[ $name ]['selector'];
     1026                    $stylesheet .= $this->process_blocks_custom_css( $custom_block_css, $selector );
     1027                }
     1028            }
    10031029        }
    10041030
     
    27412767            }
    27422768
    2743             $output = static::remove_insecure_styles( $input );
     2769            // The global styles custom CSS is not sanitized, but can only be edited by users with 'edit_css' capability.
     2770            if ( isset( $input['css'] ) && current_user_can( 'edit_css' ) ) {
     2771                $output = $input;
     2772            } else {
     2773                $output = static::remove_insecure_styles( $input );
     2774            }
    27442775
    27452776            /*
Note: See TracChangeset for help on using the changeset viewer.