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/tests/phpunit/tests/rest-api/rest-global-styles-controller.php

    r55177 r55192  
    533533        }
    534534    }
     535
     536    /**
     537     * @covers WP_REST_Global_Styles_Controller::update_item
     538     * @ticket 57536
     539     */
     540    public function test_update_item_valid_styles_css() {
     541        wp_set_current_user( self::$admin_id );
     542        if ( is_multisite() ) {
     543            grant_super_admin( self::$admin_id );
     544        }
     545        $request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
     546        $request->set_body_params(
     547            array(
     548                'styles' => array( 'css' => 'body { color: red; }' ),
     549            )
     550        );
     551        $response = rest_get_server()->dispatch( $request );
     552        $data     = $response->get_data();
     553        $this->assertSame( 'body { color: red; }', $data['styles']['css'] );
     554    }
     555
     556    /**
     557     * @covers WP_REST_Global_Styles_Controller::update_item
     558     * @ticket 57536
     559     */
     560    public function test_update_item_invalid_styles_css() {
     561        wp_set_current_user( self::$admin_id );
     562        if ( is_multisite() ) {
     563            grant_super_admin( self::$admin_id );
     564        }
     565        $request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
     566        $request->set_body_params(
     567            array(
     568                'styles' => array( 'css' => '<p>test</p> body { color: red; }' ),
     569            )
     570        );
     571        $response = rest_get_server()->dispatch( $request );
     572        $this->assertErrorResponse( 'rest_custom_css_illegal_markup', $response, 400 );
     573    }
    535574}
Note: See TracChangeset for help on using the changeset viewer.