Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Change the empty() check to empty string check in shortcode_parse_atts() #7633

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
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
6 changes: 3 additions & 3 deletions src/wp-includes/shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,11 +616,11 @@ function shortcode_parse_atts( $text ) {
$text = preg_replace( "/[\x{00a0}\x{200b}]+/u", ' ', $text );
if ( preg_match_all( $pattern, $text, $match, PREG_SET_ORDER ) ) {
foreach ( $match as $m ) {
if ( ! empty( $m[1] ) ) {
if ( isset( $m[1] ) && '' !== $m[1] ) {
$atts[ strtolower( $m[1] ) ] = stripcslashes( $m[2] );
} elseif ( ! empty( $m[3] ) ) {
} elseif ( isset( $m[3] ) && '' !== $m[3] ) {
$atts[ strtolower( $m[3] ) ] = stripcslashes( $m[4] );
} elseif ( ! empty( $m[5] ) ) {
} elseif ( isset( $m[5] ) && '' !== $m[5] ) {
$atts[ strtolower( $m[5] ) ] = stripcslashes( $m[6] );
} elseif ( isset( $m[7] ) && strlen( $m[7] ) ) {
$atts[] = stripcslashes( $m[7] );
Expand Down
77 changes: 77 additions & 0 deletions tests/phpunit/tests/shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -1019,4 +1019,81 @@ public function test_shortcode_parse_atts_empty() {
$this->assertIsArray( $out, 'Return value is not an array' );
$this->assertEmpty( $out, 'Returned array is not empty' );
}

/**
* @dataProvider data_should_return_expected_attributes_when_shortcode_is_parsed
*/
public function test_should_return_expected_attributes_when_shortcode_is_parsed( $input, $expected ) {
$this->assertSame( $expected, shortcode_parse_atts( $input ) );
}

public function data_should_return_expected_attributes_when_shortcode_is_parsed() {
return array(
'basic_attributes' => array(
'attr1="value1" attr2="value2"',
array(
'attr1' => 'value1',
'attr2' => 'value2',
),
),
'mixed_case_attributes' => array(
'Attr1="value1" ATTR2="value2"',
array(
'attr1' => 'value1',
'attr2' => 'value2',
),
),
'single_quotes' => array(
"attr1='value1' attr2='value2'",
array(
'attr1' => 'value1',
'attr2' => 'value2',
),
),
'unclosed_html_elements' => array(
'attr1="<div>value1"',
array(
'attr1' => '<div>value1',
),
),
'no_attributes' => array(
'',
array(),
),
'extra_whitespace' => array(
' attr1 = "value1" attr2 = "value2"',
array(
'attr1' => 'value1',
'attr2' => 'value2',
),
),
'html_in_attributes' => array(
'attr1="<b>Bold</b>"',
array(
'attr1' => '<b>Bold</b>',
),
),
'special_characters' => array(
'attr1="value!@#$%^&*()_+"',
array(
'attr1' => 'value!@#$%^&*()_+',
),
),
'numeric_attribute_keys' => array(
'0="value0" 1="value1" 2="value2"',
array(
'0' => 'value0',
'1' => 'value1',
'2' => 'value2',
),
),
'malformed_attribute' => array(
'attr1="value1" attr2',
array(
'attr1' => 'value1',
0 => 'attr2',
),
),
);
}
}
Loading