From 1833e3aee67e924d6ea1cdd72a8a6a4106d80d4a Mon Sep 17 00:00:00 2001 From: Adam Wood <1017872+adamwoodnz@users.noreply.github.com> Date: Thu, 24 Oct 2024 11:57:46 +1300 Subject: [PATCH] Switch to using CSS to avoid o2 script tags from affecting layout Hide empty p tags and remove margin from second to last element if there is an o2-data script tag. --- mu-plugins/blocks/notice/index.php | 4 ++++ mu-plugins/blocks/notice/postcss/style.pcss | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/mu-plugins/blocks/notice/index.php b/mu-plugins/blocks/notice/index.php index cb17817b..5181b8fc 100644 --- a/mu-plugins/blocks/notice/index.php +++ b/mu-plugins/blocks/notice/index.php @@ -49,6 +49,10 @@ function render_callout_as_notice( $attr, $content, $tag ) { // Sanitize message content. $content = wp_kses_post( $content ); + // Temporarily disable o2 processing while formatting content. + add_filter( 'o2_process_the_content', '__return_false', 1 ); + $content = apply_filters( 'the_content', $content ); + remove_filter( 'o2_process_the_content', '__return_false', 1 ); // Create a unique placeholder for the content. // Directly processing `$content` with `do_blocks` can lead to buggy layouts on make.wp.org. diff --git a/mu-plugins/blocks/notice/postcss/style.pcss b/mu-plugins/blocks/notice/postcss/style.pcss index 77bc89f6..33aff04d 100644 --- a/mu-plugins/blocks/notice/postcss/style.pcss +++ b/mu-plugins/blocks/notice/postcss/style.pcss @@ -27,11 +27,20 @@ align-self: start; } + & p:empty { + display: none; + } + & p:first-child { margin-block-start: 0; } - & p:last-child { + /* o2 adds a data script tag to the notice content on some Make blogs. + * In this case we need to remove bottom margin from the second to last element instead. + */ + + & p:last-child, + &:has(.o2-data) :nth-last-child(2) { margin-block-end: 0; }