Skip to content

Commit d35f8a2

Browse files
authored
Merge pull request #199 from BeAPI/feat/wpautop
feat(text): add wpautop option to the_text helper
2 parents 0c4e96f + a210dcc commit d35f8a2

2 files changed

Lines changed: 39 additions & 12 deletions

File tree

‎inc/Helpers/Formatting/Text.php‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44
use function BEA\Theme\Framework\Helpers\Formatting\Escape\escape_content_value;
55

66
/**
7-
* @usage BEA\Theme\Framework\Helpers\Formatting\Text\the_text( 'text' => 'Lorem ipsum', [ 'before' => '<p>', 'after' => '</p>' ] );
7+
* @usage BEA\Theme\Framework\Helpers\Formatting\Text\the_text( $text, [ 'before' => '<p>', 'after' => '</p>' ] );
8+
* @usage BEA\Theme\Framework\Helpers\Formatting\Text\the_text( $text, [ 'wpautop' => true, 'before' => '<div>', 'after' => '</div>' ] );
89
*
910
* @param string $value Text to display
1011
* @param array $settings {
1112
* Optional. Settings for the text markup.
1213
*
13-
* @type string $before Optional. Markup to prepend to the text. Default empty.
14-
* @type string $after Optional. Markup to prepend to the text. Default empty.
15-
* @type string $escape Optional. Markup to prepend to the item. Default esc_html.
14+
* @type string $before Optional. Markup to prepend to the text. Default empty.
15+
* @type string $after Optional. Markup to append after the text. Default empty.
16+
* @type string $escape Optional. Escape callback name (e.g. esc_html, wp_kses_post). Default esc_html.
17+
* @type bool $wpautop Optional. When true, uses wp_kses_post if escape is still the default esc_html, then wpautop(). Default false.
1618
*
1719
* }
1820
*
1921
* @return void
2022
*/
2123
function the_text( string $value, array $settings = [] ): void {
22-
echo get_the_text( $value, $settings );
24+
echo get_the_text( $value, $settings ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
2325
}
2426

2527
/**
@@ -30,9 +32,10 @@ function the_text( string $value, array $settings = [] ): void {
3032
* @param array $settings {
3133
* Optional. Settings for the text markup.
3234
*
33-
* @type string $before Optional. Markup to prepend to the text. Default empty.
34-
* @type string $after Optional. Markup to prepend to the text. Default empty.
35-
* @type string $escape Optional. Markup to prepend to the item. Default esc_html.
35+
* @type string $before Optional. Markup to prepend to the text. Default empty.
36+
* @type string $after Optional. Markup to append after the text. Default empty.
37+
* @type string $escape Optional. Escape callback name (e.g. esc_html, wp_kses_post). Default esc_html.
38+
* @type bool $wpautop Optional. When true, uses wp_kses_post if escape is still the default esc_html, then wpautop(). Default false.
3639
*
3740
* }
3841
*
@@ -46,14 +49,26 @@ function get_the_text( string $value, array $settings = [] ): string {
4649
$settings = wp_parse_args(
4750
$settings,
4851
[
49-
'before' => '',
50-
'after' => '',
51-
'escape' => 'esc_html',
52+
'before' => '',
53+
'after' => '',
54+
'escape' => 'esc_html',
55+
'wpautop' => false,
5256
]
5357
);
5458

5559
$settings = apply_filters( 'bea_theme_framework_text_settings', $settings, $value );
56-
$value = apply_filters( 'bea_theme_framework_text_value', escape_content_value( $value, $settings['escape'] ), $settings );
60+
61+
if ( ! empty( $settings['wpautop'] ) && 'esc_html' === $settings['escape'] ) {
62+
$settings['escape'] = 'wp_kses_post';
63+
}
64+
65+
$value = escape_content_value( $value, $settings['escape'] );
66+
67+
if ( ! empty( $settings['wpautop'] ) ) {
68+
$value = wpautop( $value );
69+
}
70+
71+
$value = apply_filters( 'bea_theme_framework_text_value', $value, $settings );
5772

5873
return $settings['before'] . $value . $settings['after'];
5974
}

‎src/scss/wp-block/paragraph.scss‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,15 @@ p {
2323
line-height: var(--wp--custom--default--line-height);
2424
}
2525
}
26+
27+
// For paragraphs generated by wpautop (e.g. excerpts), paragraphs are wrapped in a div with
28+
// a class like "is-style-large". Apply the text style to the p elements inside the div.
29+
div {
30+
@each $size in $paragraph-sizes {
31+
&.is-style-#{$size} {
32+
& > p:where(:not([class*="is-style-"])) {
33+
@include text(#{$size});
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)