Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion src/wp-includes/formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ function _wptexturize_pushpop_element( $text, &$stack, $disabled_elements ) {
* after conversion become `<br />` tags, unless `$br` is set to '0' or 'false'.
*
* @since 0.71
* @since 7.3.0 Shortcodes listed by the {@see 'no_autop_shortcodes'} filter are left untouched.
*
* @param string $text The text which has to be formatted.
* @param bool $br Optional. If set, this will convert all remaining line breaks
Expand All @@ -444,7 +445,8 @@ function _wptexturize_pushpop_element( $text, &$stack, $disabled_elements ) {
* @return string Text which has been converted into correct paragraph tags.
*/
function wpautop( $text, $br = true ) {
$pre_tags = array();
$pre_tags = array();
$shortcode_placeholders = array();

if ( '' === trim( $text ) ) {
return '';
Expand Down Expand Up @@ -481,6 +483,30 @@ function wpautop( $text, $br = true ) {

$text .= $last_part;
}

/*
* Shortcodes that opted out of autop shouldn't be touched.
* Replace them with placeholders and bring them back after autop.
*/
if ( str_contains( $text, '[' ) ) {
/**
* Filters the list of shortcodes whose content wpautop() should not format.
*
* Line breaks inside the listed shortcodes, including the content between
* their opening and closing tags, are left as they are instead of being
* converted to paragraphs and `<br />` tags.
*
* @since 7.3.0
*
* @param string[] $no_autop_shortcodes An array of shortcode names. Default empty array.
*/
$no_autop_shortcodes = (array) apply_filters( 'no_autop_shortcodes', array() );

if ( ! empty( $no_autop_shortcodes ) ) {
$text = _autop_replace_shortcodes( $text, $no_autop_shortcodes, $shortcode_placeholders );
}
}

// Change multiple <br>'s into two line breaks, which will turn into paragraphs.
$text = preg_replace( '|<br\s*/?>\s*<br\s*/?>|', "\n\n", $text );

Expand Down Expand Up @@ -591,6 +617,11 @@ function wpautop( $text, $br = true ) {
$text = preg_replace( '!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $text );
$text = preg_replace( "|\n</p>$|", '</p>', $text );

// Replace placeholder shortcodes with their original content.
if ( ! empty( $shortcode_placeholders ) ) {
$text = str_replace( array_keys( $shortcode_placeholders ), array_values( $shortcode_placeholders ), $text );
}

// Replace placeholder <pre> tags with their original content.
if ( ! empty( $pre_tags ) ) {
$text = str_replace( array_keys( $pre_tags ), array_values( $pre_tags ), $text );
Expand All @@ -604,6 +635,39 @@ function wpautop( $text, $br = true ) {
return $text;
}

/**
* Replaces shortcodes with placeholders so wpautop() doesn't format them.
*
* Escaped shortcodes, such as `[[tag]]`, are left in place.
*
* @since 7.3.0
* @access private
*
* @param string $text The text containing the shortcodes.
* @param string[] $tagnames Names of the shortcodes to replace.
* @param string[] $shortcode_placeholders Passed by reference. Placeholders are added to this array,
* keyed by placeholder with the original shortcode as the value.
* @return string The text with the shortcodes replaced by placeholders.
*/
function _autop_replace_shortcodes( $text, $tagnames, &$shortcode_placeholders ) {
return preg_replace_callback(
'/' . get_shortcode_regex( $tagnames ) . '/',
static function ( $matches ) use ( &$shortcode_placeholders ) {
// Leave escaped shortcodes, [[tag]], alone.
if ( '[' === $matches[1] && ']' === $matches[6] ) {
return $matches[0];
}

$placeholder = '%wp-autop-shortcode-' . count( $shortcode_placeholders ) . '%';

$shortcode_placeholders[ $placeholder ] = $matches[0];

return $placeholder;
},
$text
);
}

/**
* Separates HTML elements and comments from the text.
*
Expand Down
156 changes: 156 additions & 0 deletions tests/phpunit/tests/formatting/wpAutop.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,4 +660,160 @@ public function test_that_wpautop_ignores_inline_scripts() {

$this->assertSameIgnoreEOL( $expected, trim( wpautop( $content ) ) );
}

/**
* Opts a shortcode out of autop through the `no_autop_shortcodes` filter.
*
* @param string $tagname Shortcode name.
*/
private function opt_out_of_autop( $tagname ) {
add_filter(
'no_autop_shortcodes',
static function ( $tagnames ) use ( $tagname ) {
$tagnames[] = $tagname;
return $tagnames;
}
);
}

/**
* Shortcodes are formatted by default.
*
* @ticket 6984
*/
public function test_shortcode_content_is_formatted_by_default() {
$this->assertSame(
"<p>[code]a</p>\n<p>b<br />\nc[/code]</p>\n",
wpautop( "[code]a\n\nb\nc[/code]" )
);
}

/**
* @ticket 6984
*/
public function test_no_autop_shortcodes_filter_leaves_shortcode_content_untouched() {
$this->opt_out_of_autop( 'code' );

$this->assertSame(
"<p>Before</p>\n<p>[code]line1\n\nline2\nline3[/code]</p>\n<p>After</p>\n",
wpautop( "Before\n\n[code]line1\n\nline2\nline3[/code]\n\nAfter" )
);
}

/**
* @ticket 6984
*/
public function test_no_autop_shortcodes_filter_only_affects_listed_shortcodes() {
$this->opt_out_of_autop( 'code' );

$this->assertSame(
"<p>[other]a</p>\n<p>b[/other]</p>\n",
wpautop( "[other]a\n\nb[/other]" )
);
}

/**
* @ticket 6984
*/
public function test_no_autop_shortcodes_filter_handles_multiple_shortcodes() {
$this->opt_out_of_autop( 'code' );

$this->assertSame(
"<p>[code]a\n\nb[/code]</p>\n<p>[code]c\n\nd[/code]</p>\n",
wpautop( "[code]a\n\nb[/code]\n\n[code]c\n\nd[/code]" )
);
}

/**
* @ticket 6984
*/
public function test_no_autop_shortcodes_filter_keeps_surrounding_text_formatted() {
$this->opt_out_of_autop( 'code' );

$this->assertSame(
"<p>Text [code]a\n\nb[/code] more<br />\ntext</p>\n",
wpautop( "Text [code]a\n\nb[/code] more\ntext" )
);
}

/**
* @ticket 6984
*/
public function test_no_autop_shortcodes_filter_keeps_line_breaks_in_attributes() {
$this->opt_out_of_autop( 'code' );

$this->assertSame(
"<p>[code lang=\"php\"\nfoo=\"1\"] x</p>\n<p>y</p>\n",
wpautop( "[code lang=\"php\"\nfoo=\"1\"] x\n\ny" )
);
}

/**
* @ticket 6984
*/
public function test_no_autop_shortcodes_filter_ignores_escaped_shortcodes() {
$this->opt_out_of_autop( 'code' );

$this->assertSame(
"<p>[[code]]x</p>\n<p>y[[/code]]</p>\n",
wpautop( "[[code]]x\n\ny[[/code]]" )
);
}

/**
* Replacement-like characters in the content must be preserved.
*
* @ticket 6984
*/
public function test_no_autop_shortcodes_filter_preserves_replacement_characters() {
$this->opt_out_of_autop( 'code' );

$this->assertSame(
"<p>[code]\$1 \\1\n\nx[/code]</p>\n",
wpautop( "[code]\$1 \\1\n\nx[/code]" )
);
}

/**
* @ticket 6984
*/
public function test_no_autop_shortcodes_filter_without_line_breaks_conversion() {
$this->opt_out_of_autop( 'code' );

$this->assertSame(
"<p>[code]a\nb[/code]\nc\nd</p>\n",
wpautop( "[code]a\nb[/code]\nc\nd", false )
);
}

/**
* The shortcode callback should receive the content as written, when the
* shortcode content is processed in the same order as the `the_content` filter.
*
* @ticket 6984
*/
public function test_no_autop_shortcodes_filter_passes_content_unchanged_to_shortcode_callback() {
$code = '$text = str_replace( array(\'<p><p>\', \'</p></p>\'), array(\'<p>\', \'</p>\'), $text);' . "\n\nif ( \$a ) {\n\techo 1;\n}";

add_shortcode(
'code',
static function ( $atts, $content = '' ) {
return '<pre class="code">' . $content . '</pre>';
}
);

$this->opt_out_of_autop( 'code' );

try {
// Same order as the default `the_content` filters: wpautop(), shortcode_unautop(), do_shortcode().
$actual = do_shortcode( shortcode_unautop( wpautop( "Intro.\n\n[code lang=\"php\"]{$code}[/code]\n\nOutro." ) ) );
} finally {
remove_shortcode( 'code' );
}

$this->assertSame(
"<p>Intro.</p>\n<pre class=\"code\">{$code}</pre>\n<p>Outro.</p>\n",
$actual
);
}
}
Loading