Skip to content

Commit 9eebddd

Browse files
committed
Convert escaped shortcode brackets to html chars
1 parent c5755be commit 9eebddd

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/wp-includes/shortcodes.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ function do_shortcode_tag( $m ) {
394394

395395
// Allow [[foo]] syntax for escaping a tag.
396396
if ( '[' === $m[1] && ']' === $m[6] ) {
397-
return substr( $m[0], 1, -1 );
397+
return '[' . substr( $m[0], 2, -2 ) . ']';
398398
}
399399

400400
$tag = $m[2];
@@ -469,6 +469,7 @@ function do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ) {
469469
']' => ']',
470470
);
471471
$content = strtr( $content, $trans );
472+
472473
$trans = array(
473474
'[' => '[',
474475
']' => ']',
@@ -543,6 +544,11 @@ function do_shortcodes_in_html_tags( $content, $ignore_html, $tagnames ) {
543544
$count = 0;
544545
$new_attr = preg_replace_callback( "/$pattern/", 'do_shortcode_tag', $attr, -1, $count );
545546
if ( $count > 0 ) {
547+
/**
548+
* Prevent escaped shortcodes to break the attribute reverting html chars back to brackets
549+
*/
550+
$new_attr = strtr( $new_attr, array( '[' => '[', ']' => ']' ));
551+
546552
// Sanitize the shortcode output using KSES.
547553
$new_attr = wp_kses_one_attr( $new_attr, $elname );
548554
if ( '' !== trim( $new_attr ) ) {
@@ -757,7 +763,7 @@ function strip_shortcodes( $content ) {
757763
function strip_shortcode_tag( $m ) {
758764
// Allow [[foo]] syntax for escaping a tag.
759765
if ( '[' === $m[1] && ']' === $m[6] ) {
760-
return substr( $m[0], 1, -1 );
766+
return '[' . substr( $m[0], 2, -2 ) . ']';
761767
}
762768

763769
return $m[1] . $m[6];

tests/phpunit/tests/shortcode.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,17 +307,17 @@ public function test_nested_tags() {
307307
*/
308308
public function test_tag_escaped() {
309309
$out = do_shortcode( '[[footag]] [[bartag foo="bar"]]' );
310-
$this->assertSame( '[footag] [bartag foo="bar"]', $out );
310+
$this->assertSame( '[footag] [bartag foo="bar"]', $out );
311311

312312
$out = do_shortcode( '[[footag /]] [[bartag foo="bar" /]]' );
313-
$this->assertSame( '[footag /] [bartag foo="bar" /]', $out );
313+
$this->assertSame( '[footag /] [bartag foo="bar" /]', $out );
314314

315315
$out = do_shortcode( '[[baztag foo="bar"]the content[/baztag]]' );
316-
$this->assertSame( '[baztag foo="bar"]the content[/baztag]', $out );
316+
$this->assertSame( '[baztag foo="bar"]the content[/baztag]', $out );
317317

318318
// Double escaped.
319319
$out = do_shortcode( '[[[footag]]] [[[bartag foo="bar"]]]' );
320-
$this->assertSame( '[[footag]] [[bartag foo="bar"]]', $out );
320+
$this->assertSame( '[[footag]] [[bartag foo="bar"]]', $out );
321321
}
322322

323323
public function test_tag_not_escaped() {
@@ -443,6 +443,14 @@ public function data_strip_shortcodes() {
443443
);
444444
}
445445

446+
/**
447+
* @ticket 26649
448+
*/
449+
public function test_escaped_shortcode_should_not_execute() {
450+
add_shortcode('example', fn () => 'foo');
451+
$this->assertSame( '[example]', do_shortcode( strip_shortcodes( '[[example]]' ) ) );
452+
}
453+
446454
/**
447455
* @ticket 37767
448456
*/
@@ -575,11 +583,11 @@ public function data_escaping() {
575583
),
576584
array(
577585
'<div [[gallery]]>',
578-
'<div [gallery]>',
586+
'<div &#091;gallery&#093;>',
579587
),
580588
array(
581589
'<[[gallery]]>',
582-
'<[gallery]>',
590+
'<&#091;gallery&#093;>',
583591
),
584592
array(
585593
'<div style="selector:url([[gallery]])">',

0 commit comments

Comments
 (0)