| @@ -7,8 +7,10 @@ | ||
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | 9 | * Render the custom CSS stylesheet and add class name to block as required. |
| 10 | 10 | * |
| 11 | + * @since 7.0.0 | |
| 12 | + * | |
| 11 | 13 | * @param array $parsed_block The parsed block. |
| 12 | 14 | * @return array The same parsed block with custom CSS class name added if appropriate. |
| 13 | 15 | * |
| 14 | 16 | * @phpstan-param array{ |
| @@ -46,9 +48,9 @@ | ||
| 46 | 48 | $updated_class_name = is_string( $existing_class_name ) |
| 47 | 49 | ? "$existing_class_name $class_name" |
| 48 | 50 | : $class_name; |
| 49 | 51 | |
| 50 | - _wp_array_set( $parsed_block, array( 'attrs', 'className' ), $updated_class_name ); | |
| 52 | + $parsed_block['attrs']['className'] = $updated_class_name; | |
| 51 | 53 | |
| 52 | 54 | // Process the custom CSS using the same method as global styles. |
| 53 | 55 | $selector = '.' . $class_name; |
| 54 | 56 | $processed_css = WP_Theme_JSON_Gutenberg::process_blocks_custom_css( $custom_css, $selector ); |
| @@ -53,15 +55,25 @@ | ||
| 53 | 55 | $selector = '.' . $class_name; |
| 54 | 56 | $processed_css = WP_Theme_JSON_Gutenberg::process_blocks_custom_css( $custom_css, $selector ); |
| 55 | 57 | |
| 56 | 58 | if ( ! empty( $processed_css ) ) { |
| 57 | - /* | |
| 58 | - * Register and add inline style for block custom CSS. | |
| 59 | - * The style depends on global-styles to ensure custom CSS loads after | |
| 60 | - * and can override global styles. | |
| 59 | + /** | |
| 60 | + * Reuse one handle so identical custom CSS is enqueued only once via | |
| 61 | + * {@see wp_unique_id_from_values()}. Explicitly declare the `wp-block-library` | |
| 62 | + * dependency so `global-styles` is guaranteed to print after it, preventing | |
| 63 | + * block default styles from unintentionally overriding global styles. | |
| 61 | 64 | */ |
| 62 | - wp_register_style( 'wp-block-custom-css', false, array( 'global-styles' ) ); | |
| 63 | - wp_add_inline_style( 'wp-block-custom-css', $processed_css ); | |
| 65 | + $handle = 'wp-block-custom-css'; | |
| 66 | + if ( ! wp_style_is( $handle, 'registered' ) ) { | |
| 67 | + wp_register_style( $handle, false, array( 'wp-block-library', 'global-styles' ) ); | |
| 68 | + } | |
| 69 | + $after_styles = wp_styles()->get_data( $handle, 'after' ); | |
| 70 | + if ( ! is_array( $after_styles ) ) { | |
| 71 | + $after_styles = array(); | |
| 72 | + } | |
| 73 | + if ( ! in_array( $processed_css, $after_styles, true ) ) { | |
| 74 | + wp_add_inline_style( $handle, $processed_css ); | |
| 75 | + } | |
| 64 | 76 | } |
| 65 | 77 | |
| 66 | 78 | return $parsed_block; |
| 67 | 79 | } |
| @@ -67,8 +79,10 @@ | ||
| 67 | 79 | } |
| 68 | 80 | |
| 69 | 81 | /** |
| 70 | 82 | * Enqueues the block custom CSS styles. |
| 83 | + * | |
| 84 | + * @since 7.0.0 | |
| 71 | 85 | */ |
| 72 | 86 | function gutenberg_enqueue_block_custom_css() { |
| 73 | 87 | wp_enqueue_style( 'wp-block-custom-css' ); |
| 74 | 88 | } |
| @@ -78,11 +92,13 @@ | ||
| 78 | 92 | * |
| 79 | 93 | * The class name is generated in {@see gutenberg_render_custom_css_support_styles()} |
| 80 | 94 | * and stored in block attributes. This filter adds it to the actual markup. |
| 81 | 95 | * |
| 96 | + * @since 7.0.0 | |
| 97 | + * | |
| 82 | 98 | * @param string $block_content Rendered block content. |
| 83 | 99 | * @param array $block Block object. |
| 84 | - * @return string Filtered block content. | |
| 100 | + * @return string Filtered block content. | |
| 85 | 101 | * |
| 86 | 102 | * @phpstan-param array{ |
| 87 | 103 | * attrs: array{ |
| 88 | 104 | * className?: string, |
| @@ -140,8 +156,10 @@ | ||
| 140 | 156 | |
| 141 | 157 | /** |
| 142 | 158 | * Registers the style block attribute for block types that support it. |
| 143 | 159 | * |
| 160 | + * @since 7.0.0 | |
| 161 | + * | |
| 144 | 162 | * @param WP_Block_Type $block_type Block Type. |
| 145 | 163 | */ |
| 146 | 164 | function gutenberg_register_custom_css_support( $block_type ) { |
| 147 | 165 | // Setup attributes and styles within that if needed. |
| @@ -169,8 +187,10 @@ | ||
| 169 | 187 | * Uses WP_Block_Parser::next_token() to scan block tokens and surgically |
| 170 | 188 | * replace only the attribute JSON that changed — no parse_blocks() + |
| 171 | 189 | * serialize_blocks() round-trip needed. |
| 172 | 190 | * |
| 191 | + * @since 7.0.0 | |
| 192 | + * | |
| 173 | 193 | * @param string $content Post content to filter, expected to be escaped with slashes. |
| 174 | 194 | * @return string Filtered post content with block custom CSS removed. |
| 175 | 195 | */ |
| 176 | 196 | function gutenberg_strip_custom_css_from_blocks( $content ) { |
| @@ -249,8 +269,10 @@ | ||
| 249 | 269 | } |
| 250 | 270 | |
| 251 | 271 | /** |
| 252 | 272 | * Adds the filters to strip custom CSS from block content on save. |
| 273 | + * | |
| 274 | + * @since 7.0.0 | |
| 253 | 275 | * @access private |
| 254 | 276 | */ |
| 255 | 277 | function gutenberg_custom_css_kses_init_filters() { |
| 256 | 278 | add_filter( 'content_save_pre', 'gutenberg_strip_custom_css_from_blocks', 8 ); |
| @@ -258,8 +280,10 @@ | ||
| 258 | 280 | } |
| 259 | 281 | |
| 260 | 282 | /** |
| 261 | 283 | * Removes the filters that strip custom CSS from block content on save. |
| 284 | + * | |
| 285 | + * @since 7.0.0 | |
| 262 | 286 | * @access private |
| 263 | 287 | */ |
| 264 | 288 | function gutenberg_custom_css_remove_filters() { |
| 265 | 289 | remove_filter( 'content_save_pre', 'gutenberg_strip_custom_css_from_blocks', 8 ); |
| @@ -267,8 +291,10 @@ | ||
| 267 | 291 | } |
| 268 | 292 | |
| 269 | 293 | /** |
| 270 | 294 | * Registers the custom CSS content filters if the user does not have the edit_css capability. |
| 295 | + * | |
| 296 | + * @since 7.0.0 | |
| 271 | 297 | * @access private |
| 272 | 298 | */ |
| 273 | 299 | function gutenberg_custom_css_kses_init() { |
| 274 | 300 | gutenberg_custom_css_remove_filters(); |
| @@ -282,8 +308,10 @@ | ||
| 282 | 308 | * |
| 283 | 309 | * This filter is the last being executed on force_filtered_html_on_import. |
| 284 | 310 | * If the input of the filter is true it means we are in an import situation and should |
| 285 | 311 | * enable the custom CSS filters, independently of the user capabilities. |
| 312 | + * | |
| 313 | + * @since 7.0.0 | |
| 286 | 314 | * @access private |
| 287 | 315 | * |
| 288 | 316 | * @param mixed $arg Input argument of the filter. |
| 289 | 317 | * @return mixed Input argument of the filter. |