From 3161b7878cddca2dd15c30140bb40ef412cdfa38 Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Wed, 27 Sep 2023 20:26:48 -0500 Subject: [PATCH] Support trimming whitespace at end of lines and removing extra lines --- classes/PodsField.php | 31 ++++++++++++++++------ classes/fields/code.php | 40 ++++++++++++++-------------- classes/fields/html.php | 51 ++++++++++++++++++++---------------- classes/fields/paragraph.php | 45 +++++++++++++++++-------------- classes/fields/text.php | 37 +++++++++++++++----------- classes/fields/wysiwyg.php | 25 +++++++++++------- 6 files changed, 133 insertions(+), 96 deletions(-) diff --git a/classes/PodsField.php b/classes/PodsField.php index 9da0992f22..69245aa899 100644 --- a/classes/PodsField.php +++ b/classes/PodsField.php @@ -981,29 +981,44 @@ public function trim_whitespace( $value, $options = null ) { return $value; } + $trim = true; $trim_p_brs = false; + $trim_lines = false; if ( $options ) { $options = ( is_array( $options ) || is_object( $options ) ) ? $options : (array) $options; // Check if we should trim the content. - if ( 0 === (int) pods_v( static::$type . '_trim', $options, 0 ) ) { - return $value; - } + $trim = 1 === (int) pods_v( static::$type . '_trim', $options, 0 ); - // Check if we should trim the content. + // Check if we should remove the "p" tags that are empty or that only contain whitespace and "br" tags. $trim_p_brs = 1 === (int) pods_v( static::$type . '_trim_p_brs', $options, 0 ); + + // Check if we should remove whitespace at the end of lines. + $trim_lines = 1 === (int) pods_v( static::$type . '_trim_lines', $options, 0 ); } if ( $trim_p_brs ) { - // Maybe trim the p tags that are empty or filled with whitespace / br tags. + // Remove the "p" tags that are empty or that only contain whitespace and "br" tags. $value = preg_replace( '/(]*>\s*(\s| |)*\s*<\/?p>)/Umi', '', $value ); - // Remove consecutive blank lines. - $value = preg_replace( '/([\n\r]\s*[\n\r])+/', "\n", $value ); + // Remove 3+ consecutive blank lines. + $value = preg_replace( '/([\n\r]\s*[\n\r]\s*[\n\r])+/', "\n", $value ); } - return trim( $value ); + if ( $trim_lines ) { + // Trim whitespace at the end of lines. + $value = preg_replace( '/\h+$/m', '', $value ); + + // Remove 3+ consecutive blank lines. + $value = preg_replace( '/([\n\r]\s*[\n\r]\s*[\n\r])+/', "\n", $value ); + } + + if ( $trim ) { + $value = trim( $value ); + } + + return $value; } /** diff --git a/classes/fields/code.php b/classes/fields/code.php index a57045c927..1a6181a4e1 100644 --- a/classes/fields/code.php +++ b/classes/fields/code.php @@ -40,19 +40,32 @@ public function setup() { * {@inheritdoc} */ public function options() { - - $options = [ + return [ 'output_options' => [ 'label' => __( 'Output Options', 'pods' ), 'type' => 'boolean_group', 'boolean_group' => [ - static::$type . '_trim' => [ - 'label' => __( 'Trim extra whitespace before/after contents', 'pods' ), - 'default' => 1, - 'type' => 'boolean', - 'dependency' => true, + static::$type . '_trim' => [ + 'label' => __( 'Trim extra whitespace before/after contents', 'pods' ), + 'default' => 1, + 'type' => 'boolean', + ], + static::$type . '_trim_lines' => [ + 'label' => __( 'Trim whitespace at the end of lines', 'pods' ), + 'default' => 0, + 'type' => 'boolean', + ], + static::$type . '_trim_p_brs' => [ + 'label' => __( 'Remove blank lines including empty "p" tags and "br" tags', 'pods' ), + 'default' => 0, + 'type' => 'boolean', ], - static::$type . '_allow_shortcode' => [ + static::$type . '_trim_extra_lines' => [ + 'label' => __( 'Remove extra blank lines (when there are 3+ blank lines, replace with a maximum of 2)', 'pods' ), + 'default' => 0, + 'type' => 'boolean', + ], + static::$type . '_allow_shortcode' => [ 'label' => __( 'Allow Shortcodes', 'pods' ), 'default' => 0, 'type' => 'boolean', @@ -60,15 +73,6 @@ public function options() { ], ], ], - static::$type . '_trim_p_brs' => [ - 'label' => __( 'Remove blank lines including "p" tags that are empty or only contain whitespace and "br" tags', 'pods' ), - 'default' => 0, - 'type' => 'boolean', - 'dependency' => true, - 'depends-on' => [ - static::$type . '_trim' => 1, - ], - ], static::$type . '_max_length' => [ 'label' => __( 'Maximum Length', 'pods' ), 'default' => - 1, @@ -76,8 +80,6 @@ public function options() { 'help' => __( 'Set to -1 for no limit', 'pods' ), ], ]; - - return $options; } /** diff --git a/classes/fields/html.php b/classes/fields/html.php index 3351f53d11..9c565c20dc 100644 --- a/classes/fields/html.php +++ b/classes/fields/html.php @@ -40,7 +40,7 @@ public function setup() { */ public function options() { return [ - static::$type . '_content' => [ + static::$type . '_content' => [ 'label' => __( 'HTML Content', 'pods' ), 'type' => 'code', ], @@ -50,17 +50,31 @@ public function options() { 'type' => 'boolean', 'help' => __( 'By disabling the form label, the HTML will show as full width without the label text. Only the HTML content will be displayed in the form.', 'pods' ), ], - 'output_options' => [ - 'label' => __( 'Output Options', 'pods' ), - 'type' => 'boolean_group', + 'output_options' => [ + 'label' => __( 'Output Options', 'pods' ), + 'type' => 'boolean_group', 'boolean_group' => [ - static::$type . '_trim' => [ - 'label' => __( 'Trim extra whitespace before/after contents', 'pods' ), - 'default' => 1, - 'type' => 'boolean', - 'dependency' => true, + static::$type . '_trim' => [ + 'label' => __( 'Trim extra whitespace before/after contents', 'pods' ), + 'default' => 1, + 'type' => 'boolean', ], - static::$type . '_oembed' => [ + static::$type . '_trim_lines' => [ + 'label' => __( 'Trim whitespace at the end of lines', 'pods' ), + 'default' => 0, + 'type' => 'boolean', + ], + static::$type . '_trim_p_brs' => [ + 'label' => __( 'Remove blank lines including empty "p" tags and "br" tags', 'pods' ), + 'default' => 0, + 'type' => 'boolean', + ], + static::$type . '_trim_extra_lines' => [ + 'label' => __( 'Remove extra blank lines (when there are 3+ blank lines, replace with a maximum of 2)', 'pods' ), + 'default' => 0, + 'type' => 'boolean', + ], + static::$type . '_oembed' => [ 'label' => __( 'Enable oEmbed', 'pods' ), 'default' => 0, 'type' => 'boolean', @@ -69,7 +83,7 @@ public function options() { 'http://codex.wordpress.org/Embeds', ], ], - static::$type . '_wptexturize' => [ + static::$type . '_wptexturize' => [ 'label' => __( 'Enable wptexturize', 'pods' ), 'default' => 1, 'type' => 'boolean', @@ -78,7 +92,7 @@ public function options() { 'http://codex.wordpress.org/Function_Reference/wptexturize', ], ], - static::$type . '_convert_chars' => [ + static::$type . '_convert_chars' => [ 'label' => __( 'Enable convert_chars', 'pods' ), 'default' => 1, 'type' => 'boolean', @@ -87,7 +101,7 @@ public function options() { 'http://codex.wordpress.org/Function_Reference/convert_chars', ], ], - static::$type . '_wpautop' => [ + static::$type . '_wpautop' => [ 'label' => __( 'Enable wpautop', 'pods' ), 'default' => 1, 'type' => 'boolean', @@ -96,7 +110,7 @@ public function options() { 'http://codex.wordpress.org/Function_Reference/wpautop', ], ], - static::$type . '_allow_shortcode' => [ + static::$type . '_allow_shortcode' => [ 'label' => __( 'Allow Shortcodes', 'pods' ), 'default' => 0, 'type' => 'boolean', @@ -108,15 +122,6 @@ public function options() { ], ], ], - static::$type . '_trim_p_brs' => [ - 'label' => __( 'Remove blank lines including empty "p" tags and "br" tags', 'pods' ), - 'default' => 0, - 'type' => 'boolean', - 'dependency' => true, - 'depends-on' => [ - static::$type . '_trim' => 1, - ], - ], ]; } diff --git a/classes/fields/paragraph.php b/classes/fields/paragraph.php index acd59daa6c..63ea1d3639 100644 --- a/classes/fields/paragraph.php +++ b/classes/fields/paragraph.php @@ -47,19 +47,33 @@ public function options() { 'label' => __( 'Output Options', 'pods' ), 'type' => 'boolean_group', 'boolean_group' => [ - static::$type . '_trim' => [ - 'label' => __( 'Trim extra whitespace before/after contents', 'pods' ), - 'default' => 1, - 'type' => 'boolean', - 'dependency' => true, + static::$type . '_trim' => [ + 'label' => __( 'Trim extra whitespace before/after contents', 'pods' ), + 'default' => 1, + 'type' => 'boolean', + ], + static::$type . '_trim_lines' => [ + 'label' => __( 'Trim whitespace at the end of lines', 'pods' ), + 'default' => 0, + 'type' => 'boolean', ], - static::$type . '_allow_html' => [ + static::$type . '_trim_p_brs' => [ + 'label' => __( 'Remove blank lines including empty "p" tags and "br" tags', 'pods' ), + 'default' => 0, + 'type' => 'boolean', + ], + static::$type . '_trim_extra_lines' => [ + 'label' => __( 'Remove extra blank lines (when there are 3+ blank lines, replace with a maximum of 2)', 'pods' ), + 'default' => 0, + 'type' => 'boolean', + ], + static::$type . '_allow_html' => [ 'label' => __( 'Allow HTML', 'pods' ), 'default' => 1, 'type' => 'boolean', 'dependency' => true, ], - static::$type . '_oembed' => [ + static::$type . '_oembed' => [ 'label' => __( 'Enable oEmbed', 'pods' ), 'default' => 0, 'type' => 'boolean', @@ -68,7 +82,7 @@ public function options() { 'https://wordpress.org/support/article/embeds/', ], ], - static::$type . '_wptexturize' => [ + static::$type . '_wptexturize' => [ 'label' => __( 'Enable wptexturize', 'pods' ), 'default' => 1, 'type' => 'boolean', @@ -77,7 +91,7 @@ public function options() { 'https://developer.wordpress.org/reference/functions/wptexturize/', ], ], - static::$type . '_convert_chars' => [ + static::$type . '_convert_chars' => [ 'label' => __( 'Enable convert_chars', 'pods' ), 'default' => 1, 'type' => 'boolean', @@ -86,7 +100,7 @@ public function options() { 'https://developer.wordpress.org/reference/functions/convert_chars/', ], ], - static::$type . '_wpautop' => [ + static::$type . '_wpautop' => [ 'label' => __( 'Enable wpautop', 'pods' ), 'default' => 1, 'type' => 'boolean', @@ -95,7 +109,7 @@ public function options() { 'https://developer.wordpress.org/reference/functions/wpautop/', ], ], - static::$type . '_allow_shortcode' => [ + static::$type . '_allow_shortcode' => [ 'label' => __( 'Allow Shortcodes', 'pods' ), 'default' => 0, 'type' => 'boolean', @@ -107,15 +121,6 @@ public function options() { ], ], ], - static::$type . '_trim_p_brs' => [ - 'label' => __( 'Remove blank lines including empty "p" tags and "br" tags', 'pods' ), - 'default' => 0, - 'type' => 'boolean', - 'dependency' => true, - 'depends-on' => [ - static::$type . '_trim' => 1, - ], - ], static::$type . '_allowed_html_tags' => [ 'label' => __( 'Allowed HTML Tags', 'pods' ), 'depends-on' => [ static::$type . '_allow_html' => true ], diff --git a/classes/fields/text.php b/classes/fields/text.php index 0d96a693cb..3f242703a8 100644 --- a/classes/fields/text.php +++ b/classes/fields/text.php @@ -43,19 +43,33 @@ public function options() { 'label' => __( 'Output Options', 'pods' ), 'type' => 'boolean_group', 'boolean_group' => [ - static::$type . '_trim' => [ - 'label' => __( 'Trim extra whitespace before/after contents', 'pods' ), - 'default' => 1, - 'type' => 'boolean', - 'dependency' => true, + static::$type . '_trim' => [ + 'label' => __( 'Trim extra whitespace before/after contents', 'pods' ), + 'default' => 1, + 'type' => 'boolean', + ], + static::$type . '_trim_lines' => [ + 'label' => __( 'Trim whitespace at the end of lines', 'pods' ), + 'default' => 0, + 'type' => 'boolean', + ], + static::$type . '_trim_p_brs' => [ + 'label' => __( 'Remove blank lines including empty "p" tags and "br" tags', 'pods' ), + 'default' => 0, + 'type' => 'boolean', ], - static::$type . '_allow_html' => [ + static::$type . '_trim_extra_lines' => [ + 'label' => __( 'Remove extra blank lines (when there are 3+ blank lines, replace with a maximum of 2)', 'pods' ), + 'default' => 0, + 'type' => 'boolean', + ], + static::$type . '_allow_html' => [ 'label' => __( 'Allow HTML', 'pods' ), 'default' => 0, 'type' => 'boolean', 'dependency' => true, ], - static::$type . '_allow_shortcode' => [ + static::$type . '_allow_shortcode' => [ 'label' => __( 'Allow Shortcodes', 'pods' ), 'default' => 0, 'type' => 'boolean', @@ -63,15 +77,6 @@ public function options() { ], ], ], - static::$type . '_trim_p_brs' => [ - 'label' => __( 'Remove blank lines including empty "p" tags and "br" tags', 'pods' ), - 'default' => 0, - 'type' => 'boolean', - 'dependency' => true, - 'depends-on' => [ - static::$type . '_trim' => 1, - ], - ], static::$type . '_allowed_html_tags' => [ 'label' => __( 'Allowed HTML Tags', 'pods' ), 'depends-on' => [ static::$type . '_allow_html' => true ], diff --git a/classes/fields/wysiwyg.php b/classes/fields/wysiwyg.php index dfe1e068a3..df0716eed7 100644 --- a/classes/fields/wysiwyg.php +++ b/classes/fields/wysiwyg.php @@ -82,7 +82,21 @@ public function options() { 'label' => __( 'Trim extra whitespace before/after contents', 'pods' ), 'default' => 1, 'type' => 'boolean', - 'dependency' => true, + ], + static::$type . '_trim_lines' => [ + 'label' => __( 'Trim whitespace at the end of lines', 'pods' ), + 'default' => 0, + 'type' => 'boolean', + ], + static::$type . '_trim_p_brs' => [ + 'label' => __( 'Remove blank lines including empty "p" tags and "br" tags', 'pods' ), + 'default' => 0, + 'type' => 'boolean', + ], + static::$type . '_trim_extra_lines' => [ + 'label' => __( 'Remove extra blank lines (when there are 3+ blank lines, replace with a maximum of 2)', 'pods' ), + 'default' => 0, + 'type' => 'boolean', ], static::$type . '_oembed' => [ 'label' => __( 'Enable oEmbed', 'pods' ), @@ -132,15 +146,6 @@ public function options() { ], ], ], - static::$type . '_trim_p_brs' => [ - 'label' => __( 'Remove blank lines including empty "p" tags and "br" tags', 'pods' ), - 'default' => 0, - 'type' => 'boolean', - 'dependency' => true, - 'depends-on' => [ - static::$type . '_trim' => 1, - ], - ], static::$type . '_allowed_html_tags' => [ 'label' => __( 'Allowed HTML Tags', 'pods' ), 'default' => '',