| @@ -46,9 +46,9 @@ | ||
| 46 | 46 | $updated_class_name = is_string( $existing_class_name ) |
| 47 | 47 | ? "$existing_class_name $class_name" |
| 48 | 48 | : $class_name; |
| 49 | 49 | |
| 50 | - _wp_array_set( $parsed_block, array( 'attrs', 'className' ), $updated_class_name ); | |
| 50 | + $parsed_block['attrs']['className'] = $updated_class_name; | |
| 51 | 51 | |
| 52 | 52 | // Process the custom CSS using the same method as global styles. |
| 53 | 53 | $selector = '.' . $class_name; |
| 54 | 54 | $processed_css = WP_Theme_JSON_Gutenberg::process_blocks_custom_css( $custom_css, $selector ); |
| @@ -53,15 +53,25 @@ | ||
| 53 | 53 | $selector = '.' . $class_name; |
| 54 | 54 | $processed_css = WP_Theme_JSON_Gutenberg::process_blocks_custom_css( $custom_css, $selector ); |
| 55 | 55 | |
| 56 | 56 | 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. | |
| 57 | + /** | |
| 58 | + * Reuse one handle so identical custom CSS is enqueued only once via | |
| 59 | + * {@see wp_unique_id_from_values()}. Explicitly declare the `wp-block-library` | |
| 60 | + * dependency so `global-styles` is guaranteed to print after it, preventing | |
| 61 | + * block default styles from unintentionally overriding global styles. | |
| 61 | 62 | */ |
| 62 | - wp_register_style( 'wp-block-custom-css', false, array( 'global-styles' ) ); | |
| 63 | - wp_add_inline_style( 'wp-block-custom-css', $processed_css ); | |
| 63 | + $handle = 'wp-block-custom-css'; | |
| 64 | + if ( ! wp_style_is( $handle, 'registered' ) ) { | |
| 65 | + wp_register_style( $handle, false, array( 'wp-block-library', 'global-styles' ) ); | |
| 66 | + } | |
| 67 | + $after_styles = wp_styles()->get_data( $handle, 'after' ); | |
| 68 | + if ( ! is_array( $after_styles ) ) { | |
| 69 | + $after_styles = array(); | |
| 70 | + } | |
| 71 | + if ( ! in_array( $processed_css, $after_styles, true ) ) { | |
| 72 | + wp_add_inline_style( $handle, $processed_css ); | |
| 73 | + } | |
| 64 | 74 | } |
| 65 | 75 | |
| 66 | 76 | return $parsed_block; |
| 67 | 77 | } |
| @@ -80,9 +90,9 @@ | ||
| 80 | 90 | * and stored in block attributes. This filter adds it to the actual markup. |
| 81 | 91 | * |
| 82 | 92 | * @param string $block_content Rendered block content. |
| 83 | 93 | * @param array $block Block object. |
| 84 | - * @return string Filtered block content. | |
| 94 | + * @return string Filtered block content. | |
| 85 | 95 | * |
| 86 | 96 | * @phpstan-param array{ |
| 87 | 97 | * attrs: array{ |
| 88 | 98 | * className?: string, |