Skip to content
2 changes: 2 additions & 0 deletions src/wp-includes/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,8 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex
$editor_settings = apply_filters_deprecated( 'block_editor_settings', array( $editor_settings, $post ), '5.8.0', 'block_editor_settings_all' );
}

$editor_settings['canEditCSS'] = current_user_can( 'edit_css' );

return $editor_settings;
}

Expand Down
133 changes: 133 additions & 0 deletions src/wp-includes/block-supports/custom-css.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/**
* Custom CSS block support.
*
* @package WordPress
*/

/**
* Render the custom CSS stylesheet and add class name to block as required.
*
* @since 7.0.0
*
* @param array $parsed_block The parsed block.
* @return array The same parsed block with custom CSS class name added if appropriate.
*/
function wp_render_custom_css_support_styles( $parsed_block ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] );

if ( ! block_has_support( $block_type, 'customCSS', true ) ) {
return $parsed_block;
}

$custom_css = trim( $parsed_block['attrs']['style']['css'] ?? '' );

if ( empty( $custom_css ) ) {
return $parsed_block;
}

// Validate CSS doesn't contain HTML markup (same validation as global styles REST API).
if ( preg_match( '#</?\w+#', $custom_css ) ) {
return $parsed_block;
}

// Generate a unique class name for this block instance.
$class_name = wp_unique_id_from_values( $parsed_block, 'wp-custom-css-' );
$updated_class_name = isset( $parsed_block['attrs']['className'] )
? $parsed_block['attrs']['className'] . " $class_name"
: $class_name;

_wp_array_set( $parsed_block, array( 'attrs', 'className' ), $updated_class_name );

// Process the custom CSS using the same method as global styles.
$selector = '.' . $class_name;
$processed_css = WP_Theme_JSON::process_blocks_custom_css( $custom_css, $selector );

if ( ! empty( $processed_css ) ) {
/*
* Register and add inline style for block custom CSS.
* The style depends on global-styles to ensure custom CSS loads after
* and can override global styles.
*/
wp_register_style( 'wp-block-custom-css', false, array( 'global-styles' ) );
wp_add_inline_style( 'wp-block-custom-css', $processed_css );
}

return $parsed_block;
}

/**
* Enqueues the block custom CSS styles.
*
* @since 7.0.0
*/
function wp_enqueue_block_custom_css() {
wp_enqueue_style( 'wp-block-custom-css' );
}

/**
* Applies the custom CSS class name to the block's rendered HTML.
*
* The class name is generated in `wp_render_custom_css_support_styles`
* and stored in block attributes. This filter adds it to the actual markup.
*
* @since 7.0.0
*
* @param string $block_content Rendered block content.
* @param array $block Block object.
* @return string Filtered block content.
*/
function wp_render_custom_css_class_name( $block_content, $block ) {
$class_string = $block['attrs']['className'] ?? '';
preg_match( '/\bwp-custom-css-\S+\b/', $class_string, $matches );

if ( empty( $matches ) ) {
return $block_content;
}

$tags = new WP_HTML_Tag_Processor( $block_content );

if ( $tags->next_tag() ) {
$tags->add_class( 'has-custom-css' );
$tags->add_class( $matches[0] );
}

return $tags->get_updated_html();
}

add_filter( 'render_block', 'wp_render_custom_css_class_name', 10, 2 );
add_filter( 'render_block_data', 'wp_render_custom_css_support_styles', 10, 1 );
add_action( 'wp_enqueue_scripts', 'wp_enqueue_block_custom_css', 1 );

/**
* Registers the style block attribute for block types that support it.
*
* @param WP_Block_Type $block_type Block Type.
*/
function wp_register_custom_css_support( $block_type ) {
// Setup attributes and styles within that if needed.
if ( ! $block_type->attributes ) {
$block_type->attributes = array();
}

// Check for existing style attribute definition e.g. from block.json.
if ( array_key_exists( 'style', $block_type->attributes ) ) {
return;
}

$has_custom_css_support = block_has_support( $block_type, array( 'customCSS' ), true );

if ( $has_custom_css_support ) {
$block_type->attributes['style'] = array(
'type' => 'object',
);
}
}

// Register the block support.
WP_Block_Supports::get_instance()->register(
'custom-css',
array(
'register_attribute' => 'wp_register_custom_css_support',
)
);
3 changes: 2 additions & 1 deletion src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -1520,12 +1520,13 @@ public function get_stylesheet( $types = array( 'variables', 'styles', 'presets'
*
* @since 6.2.0
* @since 6.6.0 Enforced 0-1-0 specificity for block custom CSS selectors.
* @since 7.0.0 Made public for use in custom-css block support.
*
* @param string $css The CSS to process.
* @param string $selector The selector to nest.
* @return string The processed CSS.
*/
protected function process_blocks_custom_css( $css, $selector ) {
public static function process_blocks_custom_css( $css, $selector ) {
$processed_css = '';

if ( empty( $css ) ) {
Expand Down
1 change: 1 addition & 0 deletions src/wp-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@
require ABSPATH . WPINC . '/block-supports/aria-label.php';
require ABSPATH . WPINC . '/block-supports/anchor.php';
require ABSPATH . WPINC . '/block-supports/block-visibility.php';
require ABSPATH . WPINC . '/block-supports/custom-css.php';
require ABSPATH . WPINC . '/style-engine.php';
require ABSPATH . WPINC . '/style-engine/class-wp-style-engine.php';
require ABSPATH . WPINC . '/style-engine/class-wp-style-engine-css-declarations.php';
Expand Down
134 changes: 134 additions & 0 deletions tests/phpunit/tests/block-supports/wpRenderCustomCssClassName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

/**
* @group block-supports
*
* @covers ::wp_render_custom_css_class_name
*/
class Tests_Block_Supports_WpRenderCustomCssClassName extends WP_UnitTestCase {

/**
* Tests that the custom CSS class name is applied to block content.
*
* @ticket 64544
*
* @covers ::wp_render_custom_css_class_name
*
* @dataProvider data_adds_class_to_content
*
* @param string $block_content The rendered block content.
* @param array $block The block data.
* @param string $expected_class The expected class in the output.
*/
public function test_adds_class_to_content( $block_content, $block, $expected_class ) {
$result = wp_render_custom_css_class_name( $block_content, $block );

$this->assertStringContainsString( $expected_class, $result, 'Custom CSS class should be present in the output.' );
}

/**
* Data provider.
*
* @return array
*/
public function data_adds_class_to_content() {
return array(
'class is added to block content' => array(
'block_content' => '<div class="wp-block-paragraph">Test content</div>',
'block' => array(
'blockName' => 'core/paragraph',
'attrs' => array(
'className' => 'wp-custom-css-123abc',
),
),
'expected_class' => 'wp-custom-css-123abc',
),
'class is extracted from mixed class names' => array(
'block_content' => '<p>Test content</p>',
'block' => array(
'blockName' => 'core/paragraph',
'attrs' => array(
'className' => 'my-class wp-custom-css-mixed123 another-class',
),
),
'expected_class' => 'wp-custom-css-mixed123',
),
);
}

/**
* Tests that existing classes are preserved when the custom CSS class is added.
*
* @ticket 64544
*
* @covers ::wp_render_custom_css_class_name
*/
public function test_preserves_existing_classes() {
$block_content = '<div class="existing-class another-class">Test content</div>';
$block = array(
'blockName' => 'core/paragraph',
'attrs' => array(
'className' => 'wp-custom-css-456def',
),
);

$result = wp_render_custom_css_class_name( $block_content, $block );

$this->assertStringContainsString( 'existing-class', $result, 'Existing classes should be preserved.' );
$this->assertStringContainsString( 'another-class', $result, 'All existing classes should be preserved.' );
$this->assertStringContainsString( 'wp-custom-css-456def', $result, 'Custom CSS class should be added.' );
}

/**
* Tests that block content is returned unchanged when no custom CSS class should be applied.
*
* @ticket 64544
*
* @covers ::wp_render_custom_css_class_name
*
* @dataProvider data_returns_unchanged_content
*
* @param string $block_content The rendered block content.
* @param array $block The block data.
*/
public function test_returns_unchanged_content( $block_content, $block ) {
$result = wp_render_custom_css_class_name( $block_content, $block );

$this->assertSame( $block_content, $result, 'Block content should remain unchanged.' );
}

/**
* Data provider.
*
* @return array
*/
public function data_returns_unchanged_content() {
return array(
'no custom CSS class in attrs' => array(
'block_content' => '<div class="wp-block-paragraph">Test content</div>',
'block' => array(
'blockName' => 'core/paragraph',
'attrs' => array(
'className' => 'some-other-class',
),
),
),
'className is not set in attrs' => array(
'block_content' => '<div class="wp-block-paragraph">Test content</div>',
'block' => array(
'blockName' => 'core/paragraph',
'attrs' => array(),
),
),
'block content is empty' => array(
'block_content' => '',
'block' => array(
'blockName' => 'core/paragraph',
'attrs' => array(
'className' => 'wp-custom-css-789ghi',
),
),
),
);
}
}
Loading
Loading