Skip to content

Commit

Permalink
Split up handling for removing empty p tags so that is in it’s own op…
Browse files Browse the repository at this point in the history
…tion that can be optionally enabled for trimming
  • Loading branch information
sc0ttkclark committed Sep 28, 2023
1 parent aed06a8 commit a413ee7
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 150 deletions.
17 changes: 12 additions & 5 deletions classes/PodsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -981,20 +981,27 @@ public function trim_whitespace( $value, $options = null ) {
return $value;
}

$trim_p_brs = 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;
}

// Check if we should trim the content.
$trim_p_brs = 1 === (int) pods_v( static::$type . '_trim_p_brs', $options, 0 );
}

// Maybe trim empty p tags.
$value = preg_replace( '/^\s*<p[^>]*>(\s|&nbsp;|<\/?\s?br\s?\/?>)*<\/?p>(.*)$/Umis', '$2', $value );
$value = preg_replace( '/^\s*(\s|&nbsp;|<\/?\s?br\s?\/?>)*(.*)$/Umis', '$2', $value );
$value = preg_replace( '/^(.*)<p[^>]*>(\s|&nbsp;|<\/?\s?br\s?\/?>)*<\/?p>\s*$/Umis', '$1', $value );
$value = preg_replace( '/^(.*)(\s|&nbsp;|<\/?\s?br\s?\/?>)*\s*$/Umis', '$1', $value );
if ( $trim_p_brs ) {
// Maybe trim the p tags that are empty or filled with whitespace / 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 );
}

return trim( $value );
}
Expand Down
39 changes: 24 additions & 15 deletions classes/fields/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,41 @@ public function setup() {
*/
public function options() {

$options = array(
'output_options' => array(
'label' => __( 'Output Options', 'pods' ),
'type' => 'boolean_group',
'boolean_group' => array(
static::$type . '_trim' => array(
$options = [
'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 . '_allow_shortcode' => array(
],
static::$type . '_allow_shortcode' => [
'label' => __( 'Allow Shortcodes', 'pods' ),
'default' => 0,
'type' => 'boolean',
'dependency' => true,
),
),
),
static::$type . '_max_length' => array(
],
],
],
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,
'default' => - 1,
'type' => 'number',
'help' => __( 'Set to -1 for no limit', 'pods' ),
),
);
],
];

return $options;
}
Expand Down
13 changes: 11 additions & 2 deletions classes/fields/html.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public function options() {
'label' => __( 'Output Options', 'pods' ),
'type' => 'boolean_group',
'boolean_group' => [
static::$type . '_trim' => array(
static::$type . '_trim' => [
'label' => __( 'Trim extra whitespace before/after contents', 'pods' ),
'default' => 1,
'type' => 'boolean',
'dependency' => true,
),
],
static::$type . '_oembed' => [
'label' => __( 'Enable oEmbed', 'pods' ),
'default' => 0,
Expand Down Expand Up @@ -108,6 +108,15 @@ 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
98 changes: 53 additions & 45 deletions classes/fields/paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,102 +42,110 @@ public function setup() {
* {@inheritdoc}
*/
public function options() {

$options = array(
'output_options' => array(
'label' => __( 'Output Options', 'pods' ),
'type' => 'boolean_group',
'boolean_group' => array(
static::$type . '_trim' => array(
$options = [
'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 . '_allow_html' => array(
],
static::$type . '_allow_html' => [
'label' => __( 'Allow HTML', 'pods' ),
'default' => 1,
'type' => 'boolean',
'dependency' => true,
),
static::$type . '_oembed' => array(
],
static::$type . '_oembed' => [
'label' => __( 'Enable oEmbed', 'pods' ),
'default' => 0,
'type' => 'boolean',
'help' => array(
'help' => [
__( 'Embed videos, images, tweets, and other content.', 'pods' ),
'https://wordpress.org/support/article/embeds/',
),
),
static::$type . '_wptexturize' => array(
],
],
static::$type . '_wptexturize' => [
'label' => __( 'Enable wptexturize', 'pods' ),
'default' => 1,
'type' => 'boolean',
'help' => array(
'help' => [
__( 'Transforms less-beautiful text characters into stylized equivalents.', 'pods' ),
'https://developer.wordpress.org/reference/functions/wptexturize/',
),
),
static::$type . '_convert_chars' => array(
],
],
static::$type . '_convert_chars' => [
'label' => __( 'Enable convert_chars', 'pods' ),
'default' => 1,
'type' => 'boolean',
'help' => array(
'help' => [
__( 'Converts text into valid XHTML and Unicode', 'pods' ),
'https://developer.wordpress.org/reference/functions/convert_chars/',
),
),
static::$type . '_wpautop' => array(
],
],
static::$type . '_wpautop' => [
'label' => __( 'Enable wpautop', 'pods' ),
'default' => 1,
'type' => 'boolean',
'help' => array(
'help' => [
__( 'Changes double line-breaks in the text into HTML paragraphs', 'pods' ),
'https://developer.wordpress.org/reference/functions/wpautop/',
),
),
static::$type . '_allow_shortcode' => array(
],
],
static::$type . '_allow_shortcode' => [
'label' => __( 'Allow Shortcodes', 'pods' ),
'default' => 0,
'type' => 'boolean',
'dependency' => true,
'help' => array(
'help' => [
__( 'Embed [shortcodes] that help transform your static content into dynamic content.', 'pods' ),
'https://codex.wordpress.org/Shortcode_API',
),
),
),
),
static::$type . '_allowed_html_tags' => array(
],
],
],
],
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' => array( static::$type . '_allow_html' => true ),
'depends-on' => [ static::$type . '_allow_html' => true ],
'default' => 'strong em a ul ol li b i',
'type' => 'text',
'help' => __( 'Format: strong em a ul ol li b i', 'pods' ),
),
static::$type . '_max_length' => array(
],
static::$type . '_max_length' => [
'label' => __( 'Maximum Length', 'pods' ),
'default' => -1,
'default' => - 1,
'type' => 'number',
'help' => __( 'Set to -1 for no limit', 'pods' ),
),
static::$type . '_placeholder' => array(
],
static::$type . '_placeholder' => [
'label' => __( 'HTML Placeholder', 'pods' ),
'default' => '',
'type' => 'text',
'help' => array(
'help' => [
__( 'Placeholders can provide instructions or an example of the required data format for a field. Please note: It is not a replacement for labels or description text, and it is less accessible for people using screen readers.', 'pods' ),
'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text',
),
),
);
],
],
];

if ( function_exists( 'Markdown' ) ) {
$options['output_options']['boolean_group'][ static::$type . '_allow_markdown' ] = array(
$options['output_options']['boolean_group'][ static::$type . '_allow_markdown' ] = [
'label' => __( 'Allow Markdown Syntax', 'pods' ),
'default' => 0,
'type' => 'boolean',
);
];
}

return $options;
Expand Down
58 changes: 32 additions & 26 deletions classes/fields/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,56 +38,62 @@ public function setup() {
* {@inheritdoc}
*/
public function options() {

$options = array(
'output_options' => array(
'label' => __( 'Output Options', 'pods' ),
'type' => 'boolean_group',
'boolean_group' => array(
static::$type . '_trim' => array(
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 . '_allow_html' => array(
],
static::$type . '_allow_html' => [
'label' => __( 'Allow HTML', 'pods' ),
'default' => 0,
'type' => 'boolean',
'dependency' => true,
),
static::$type . '_allow_shortcode' => array(
],
static::$type . '_allow_shortcode' => [
'label' => __( 'Allow Shortcodes', 'pods' ),
'default' => 0,
'type' => 'boolean',
'dependency' => true,
),
),
),
static::$type . '_allowed_html_tags' => array(
],
],
],
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' => array( static::$type . '_allow_html' => true ),
'depends-on' => [ static::$type . '_allow_html' => true ],
'default' => 'strong em a ul ol li b i',
'type' => 'text',
),
static::$type . '_max_length' => array(
],
static::$type . '_max_length' => [
'label' => __( 'Maximum Length', 'pods' ),
'default' => 255,
'type' => 'number',
'help' => __( 'Set to -1 for no limit', 'pods' ),
),
static::$type . '_placeholder' => array(
],
static::$type . '_placeholder' => [
'label' => __( 'HTML Placeholder', 'pods' ),
'default' => '',
'type' => 'text',
'help' => array(
'help' => [
__( 'Placeholders can provide instructions or an example of the required data format for a field. Please note: It is not a replacement for labels or description text, and it is less accessible for people using screen readers.', 'pods' ),
'https://www.w3.org/WAI/tutorials/forms/instructions/#placeholder-text',
),
),
);

return $options;
],
],
];
}

/**
Expand Down
Loading

0 comments on commit a413ee7

Please sign in to comment.