diff --git a/wp-includes/block-editor.php b/wp-includes/block-editor.php index af873178eb..18152756d5 100644 --- a/wp-includes/block-editor.php +++ b/wp-includes/block-editor.php @@ -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; } diff --git a/wp-includes/block-supports/custom-css.php b/wp-includes/block-supports/custom-css.php new file mode 100644 index 0000000000..9d5b13426f --- /dev/null +++ b/wp-includes/block-supports/custom-css.php @@ -0,0 +1,133 @@ +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( '#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', + ) +); diff --git a/wp-includes/class-wp-theme-json.php b/wp-includes/class-wp-theme-json.php index c7929dad9e..b989bab4a6 100644 --- a/wp-includes/class-wp-theme-json.php +++ b/wp-includes/class-wp-theme-json.php @@ -1520,12 +1520,13 @@ class WP_Theme_JSON { * * @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 ) ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index c4d361c62d..fb3e782b6f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '7.0-alpha-61677'; +$wp_version = '7.0-alpha-61678'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. diff --git a/wp-settings.php b/wp-settings.php index a85536345a..b437ae153b 100644 --- a/wp-settings.php +++ b/wp-settings.php @@ -415,6 +415,7 @@ require ABSPATH . WPINC . '/block-supports/block-style-variations.php'; 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';