-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Block Supports: Prevent Additional CSS duplication inside Query Loop #11859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4a92c05
1d6d46c
f7b4679
fd01377
25ebd8c
ef001a3
c78ddac
9d785fe
68a7ac7
aa8abc4
d151e2f
a081dfd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,18 @@ | |
| * }, | ||
| * ... | ||
| * } $parsed_block | ||
| * @phpstan-return array{ | ||
| * blockName: string|null, | ||
| * attrs: array{ | ||
| * className?: string, | ||
| * style?: array{ | ||
| * css?: string, | ||
| * ... | ||
| * }, | ||
| * ... | ||
| * }, | ||
| * ... | ||
| * } | ||
| */ | ||
| function wp_render_custom_css_support_styles( $parsed_block ) { | ||
| $custom_css = $parsed_block['attrs']['style']['css'] ?? null; | ||
|
|
@@ -49,20 +61,30 @@ function wp_render_custom_css_support_styles( $parsed_block ) { | |
| ? "$existing_class_name $class_name" | ||
| : $class_name; | ||
|
|
||
| _wp_array_set( $parsed_block, array( 'attrs', 'className' ), $updated_class_name ); | ||
| $parsed_block['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. | ||
| /** | ||
| * Skip CSS that has already been added. Blocks with identical attributes | ||
| * share the same class name and processed CSS via {@see wp_unique_id_from_values()}, | ||
| * so the same style would otherwise be enqueued more than once (e.g. inside | ||
| * a Query Loop or when blocks share identical custom CSS). | ||
| */ | ||
| wp_register_style( 'wp-block-custom-css', false, array( 'global-styles' ) ); | ||
| wp_add_inline_style( 'wp-block-custom-css', $processed_css ); | ||
| $handle = 'wp-block-custom-css'; | ||
| if ( ! wp_style_is( $handle, 'registered' ) ) { | ||
| wp_register_style( $handle, false, array( 'global-styles' ) ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a slight concern about this code. What happens if a consumer intentionally deregister However, since this logic was not introduced in this PR, it is not a blocker. We may be able to consider a fix in a follow-up. |
||
| } | ||
| $after_styles = wp_styles()->get_data( $handle, 'after' ); | ||
| if ( ! is_array( $after_styles ) ) { | ||
| $after_styles = array(); | ||
| } | ||
| if ( ! in_array( $processed_css, $after_styles, true ) ) { | ||
| wp_add_inline_style( $handle, $processed_css ); | ||
| } | ||
| } | ||
|
|
||
| return $parsed_block; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.