mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@55192 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -532,4 +532,43 @@ class WP_REST_Global_Styles_Controller_Test extends WP_Test_REST_Controller_Test
|
||||
$this->assertArrayHasKey( 'https://api.w.org/action-edit-css', $links );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WP_REST_Global_Styles_Controller::update_item
|
||||
* @ticket 57536
|
||||
*/
|
||||
public function test_update_item_valid_styles_css() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
if ( is_multisite() ) {
|
||||
grant_super_admin( self::$admin_id );
|
||||
}
|
||||
$request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'styles' => array( 'css' => 'body { color: red; }' ),
|
||||
)
|
||||
);
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertSame( 'body { color: red; }', $data['styles']['css'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers WP_REST_Global_Styles_Controller::update_item
|
||||
* @ticket 57536
|
||||
*/
|
||||
public function test_update_item_invalid_styles_css() {
|
||||
wp_set_current_user( self::$admin_id );
|
||||
if ( is_multisite() ) {
|
||||
grant_super_admin( self::$admin_id );
|
||||
}
|
||||
$request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$global_styles_id );
|
||||
$request->set_body_params(
|
||||
array(
|
||||
'styles' => array( 'css' => '<p>test</p> body { color: red; }' ),
|
||||
)
|
||||
);
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_custom_css_illegal_markup', $response, 400 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,36 @@
|
||||
*/
|
||||
class Tests_Theme_wpThemeJson extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* Administrator ID.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private static $administrator_id;
|
||||
|
||||
/**
|
||||
* User ID.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private static $user_id;
|
||||
|
||||
public static function set_up_before_class() {
|
||||
parent::set_up_before_class();
|
||||
|
||||
static::$administrator_id = self::factory()->user->create(
|
||||
array(
|
||||
'role' => 'administrator',
|
||||
)
|
||||
);
|
||||
|
||||
if ( is_multisite() ) {
|
||||
grant_super_admin( self::$administrator_id );
|
||||
}
|
||||
|
||||
static::$user_id = self::factory()->user->create();
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 52991
|
||||
* @ticket 54336
|
||||
@@ -4505,4 +4535,93 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase {
|
||||
|
||||
$this->assertSame( $expected_styles, $theme_json->get_stylesheet() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 57536
|
||||
*/
|
||||
public function test_get_custom_css_handles_global_custom_css() {
|
||||
$theme_json = new WP_Theme_JSON(
|
||||
array(
|
||||
'version' => WP_Theme_JSON::LATEST_SCHEMA,
|
||||
'styles' => array(
|
||||
'css' => 'body { color:purple; }',
|
||||
),
|
||||
)
|
||||
);
|
||||
$custom_css = 'body { color:purple; }';
|
||||
$this->assertSame( $custom_css, $theme_json->get_custom_css() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that custom CSS is kept for users with correct capabilities and removed for others.
|
||||
*
|
||||
* @ticket 57536
|
||||
*
|
||||
* @dataProvider data_custom_css_for_user_caps
|
||||
*
|
||||
* @param string $user_property The property name for current user.
|
||||
* @param array $expected Expected results.
|
||||
*/
|
||||
public function test_custom_css_for_user_caps( $user_property, array $expected ) {
|
||||
wp_set_current_user( static::${$user_property} );
|
||||
|
||||
$actual = WP_Theme_JSON::remove_insecure_properties(
|
||||
array(
|
||||
'version' => WP_Theme_JSON::LATEST_SCHEMA,
|
||||
'styles' => array(
|
||||
'css' => 'body { color:purple; }',
|
||||
'blocks' => array(
|
||||
'core/separator' => array(
|
||||
'color' => array(
|
||||
'background' => 'blue',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertSameSetsWithIndex( $expected, $actual );
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function data_custom_css_for_user_caps() {
|
||||
return array(
|
||||
'allows custom css for users with caps' => array(
|
||||
'user_property' => 'administrator_id',
|
||||
'expected' => array(
|
||||
'version' => WP_Theme_JSON::LATEST_SCHEMA,
|
||||
'styles' => array(
|
||||
'css' => 'body { color:purple; }',
|
||||
'blocks' => array(
|
||||
'core/separator' => array(
|
||||
'color' => array(
|
||||
'background' => 'blue',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
'removes custom css for users without caps' => array(
|
||||
'user_property' => 'user_id',
|
||||
'expected' => array(
|
||||
'version' => WP_Theme_JSON::LATEST_SCHEMA,
|
||||
'styles' => array(
|
||||
'blocks' => array(
|
||||
'core/separator' => array(
|
||||
'color' => array(
|
||||
'background' => 'blue',
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user