Skip to content

Commit

Permalink
Support trimming whitespace at end of lines and removing extra lines
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0ttkclark committed Sep 28, 2023
1 parent a413ee7 commit 3161b78
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 96 deletions.
31 changes: 23 additions & 8 deletions classes/PodsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( '/(<p[^>]*>\s*(\s|&nbsp;|<br\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;
}

/**
Expand Down
40 changes: 21 additions & 19 deletions classes/fields/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,44 +40,46 @@ 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',
'dependency' => true,
],
],
],
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,
'type' => 'number',
'help' => __( 'Set to -1 for no limit', 'pods' ),
],
];

return $options;
}

/**
Expand Down
51 changes: 28 additions & 23 deletions classes/fields/html.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function setup() {
*/
public function options() {
return [
static::$type . '_content' => [
static::$type . '_content' => [
'label' => __( 'HTML Content', 'pods' ),
'type' => 'code',
],
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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,
],
],
];
}

Expand Down
45 changes: 25 additions & 20 deletions classes/fields/paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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 ],
Expand Down
37 changes: 21 additions & 16 deletions classes/fields/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,40 @@ 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',
'dependency' => true,
],
],
],
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 ],
Expand Down
Loading

0 comments on commit 3161b78

Please sign in to comment.