From a210dcc7cbe81106abfee6fa3c64772de66b7858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ce=CC=81dric=20Andrietti?= Date: Fri, 6 Nov 2026 16:37:56 +0100 Subject: [PATCH] feat(text): add wpautop option to the_text helper Align with BFF so textarea content can use paragraphs instead of nl2br double breaks. Co-authored-by: Cursor --- inc/Helpers/Formatting/Text.php | 39 ++++++++++++++++++++++---------- src/scss/wp-block/paragraph.scss | 12 ++++++++++ 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/inc/Helpers/Formatting/Text.php b/inc/Helpers/Formatting/Text.php index 97f6468..c5a05da 100644 --- a/inc/Helpers/Formatting/Text.php +++ b/inc/Helpers/Formatting/Text.php @@ -4,22 +4,24 @@ use function BEA\Theme\Framework\Helpers\Formatting\Escape\escape_content_value; /** - * @usage BEA\Theme\Framework\Helpers\Formatting\Text\the_text( 'text' => 'Lorem ipsum', [ 'before' => '

', 'after' => '

' ] ); + * @usage BEA\Theme\Framework\Helpers\Formatting\Text\the_text( $text, [ 'before' => '

', 'after' => '

' ] ); + * @usage BEA\Theme\Framework\Helpers\Formatting\Text\the_text( $text, [ 'wpautop' => true, 'before' => '
', 'after' => '
' ] ); * * @param string $value Text to display * @param array $settings { * Optional. Settings for the text markup. * - * @type string $before Optional. Markup to prepend to the text. Default empty. - * @type string $after Optional. Markup to prepend to the text. Default empty. - * @type string $escape Optional. Markup to prepend to the item. Default esc_html. + * @type string $before Optional. Markup to prepend to the text. Default empty. + * @type string $after Optional. Markup to append after the text. Default empty. + * @type string $escape Optional. Escape callback name (e.g. esc_html, wp_kses_post). Default esc_html. + * @type bool $wpautop Optional. When true, uses wp_kses_post if escape is still the default esc_html, then wpautop(). Default false. * * } * * @return void */ function the_text( string $value, array $settings = [] ): void { - echo get_the_text( $value, $settings ); + echo get_the_text( $value, $settings ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** @@ -30,9 +32,10 @@ function the_text( string $value, array $settings = [] ): void { * @param array $settings { * Optional. Settings for the text markup. * - * @type string $before Optional. Markup to prepend to the text. Default empty. - * @type string $after Optional. Markup to prepend to the text. Default empty. - * @type string $escape Optional. Markup to prepend to the item. Default esc_html. + * @type string $before Optional. Markup to prepend to the text. Default empty. + * @type string $after Optional. Markup to append after the text. Default empty. + * @type string $escape Optional. Escape callback name (e.g. esc_html, wp_kses_post). Default esc_html. + * @type bool $wpautop Optional. When true, uses wp_kses_post if escape is still the default esc_html, then wpautop(). Default false. * * } * @@ -46,14 +49,26 @@ function get_the_text( string $value, array $settings = [] ): string { $settings = wp_parse_args( $settings, [ - 'before' => '', - 'after' => '', - 'escape' => 'esc_html', + 'before' => '', + 'after' => '', + 'escape' => 'esc_html', + 'wpautop' => false, ] ); $settings = apply_filters( 'bea_theme_framework_text_settings', $settings, $value ); - $value = apply_filters( 'bea_theme_framework_text_value', escape_content_value( $value, $settings['escape'] ), $settings ); + + if ( ! empty( $settings['wpautop'] ) && 'esc_html' === $settings['escape'] ) { + $settings['escape'] = 'wp_kses_post'; + } + + $value = escape_content_value( $value, $settings['escape'] ); + + if ( ! empty( $settings['wpautop'] ) ) { + $value = wpautop( $value ); + } + + $value = apply_filters( 'bea_theme_framework_text_value', $value, $settings ); return $settings['before'] . $value . $settings['after']; } diff --git a/src/scss/wp-block/paragraph.scss b/src/scss/wp-block/paragraph.scss index 00300a3..0326598 100644 --- a/src/scss/wp-block/paragraph.scss +++ b/src/scss/wp-block/paragraph.scss @@ -23,3 +23,15 @@ p { line-height: var(--wp--custom--default--line-height); } } + +// For paragraphs generated by wpautop (e.g. excerpts), paragraphs are wrapped in a div with +// a class like "is-style-large". Apply the text style to the p elements inside the div. +div { + @each $size in $paragraph-sizes { + &.is-style-#{$size} { + & > p:where(:not([class*="is-style-"])) { + @include text(#{$size}); + } + } + } +}