Opened 3 years ago
Last modified 5 weeks ago
#59509 new defect (bug)
Shortcode attributes named 0 are ignored
| Reported by: | ourous | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Shortcodes | Version: | 6.3.1 |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
Shortcode attributes in the form 0=... are not picked up during parsing.
This is because the parser checks for an empty name with empty(..), which also returns true for the string '0'.
Change History (6)
This ticket was mentioned in PR #7633 on WordPress/wordpress-develop by @debarghyabanerjee.
2 years ago
#2
- Keywords has-patch added; needs-patch removed
Trac Ticket: Core-59509
## Overview
- This pull request addresses an issue in the shortcode_parse_atts() function, where attributes in the form of numeric keys (e.g., 0=value) were not being parsed correctly. The existing implementation used empty() to check for attribute names, which incorrectly evaluated numeric strings as empty, leading to the omission of such attributes.
## Changes Made
- Updated the parsing logic in shortcode_parse_atts() to use isset() instead of empty() for checking the presence of attribute names. This change ensures that numeric keys are properly recognized and included in the parsed attributes
array.
## Rationale
- The ability to parse numeric attribute names is important for shortcodes that may use them as valid parameters. This modification improves the robustness of the shortcode parsing functionality, making it more consistent with expected usage scenarios.
## Updated Code Snippet:
if ( isset( $m[1] ) && '' !== $m[1] ) {
$atts[ strtolower( $m[1] ) ] = stripcslashes( $m[2] );
}
## Testing:
- Verified that existing shortcode functionality remains unaffected.
## Impact
- This change is backward-compatible and only affects the internal logic of attribute parsing. It enhances the flexibility of shortcode attributes without breaking existing implementations.
## Documentation
- The documentation for shortcode_parse_atts() has been reviewed and remains accurate.
@debarghyabanerjee commented on PR #7633:
2 years ago
#3
Hi @SergeyBiryukov , can you please take a look into this PR. Thanks.
2 years ago
#4
@Debarghya-Banerjee @SergeyBiryukov
Looking good to me. At first I thought it might be broken and would require a filter to allow null or other optional changes. But logic-wise this looks solid to me.
This ticket was mentioned in PR #14240 on WordPress/wordpress-develop by @therssoftware.
5 weeks ago
#5
- Keywords has-unit-tests added; needs-unit-tests removed
Description
In shortcode_parse_atts(), attributes matching named patterns (attr="value", attr='value', or attr=value) are checked using ! empty( $m[1] ), ! empty( $m[3] ), and ! empty( $m[5] ).
In PHP, empty("0") evaluates to true. Consequently, when an attribute name is '0' (such as [my_shortcode 0="foo"], 0='bar', or 0=baz), all ! empty() conditions evaluate to false and the attribute is silently dropped from the returned array. Shortcode callbacks never receive the attribute, and shortcode_atts() falls back to its default value.
Solution
- In
src/wp-includes/shortcodes.phpinsideshortcode_parse_atts(), replace theempty()checks with explicit non-empty string checks:if ( isset( $m[1] ) && '' !== $m[1] ) { $atts[ strtolower( $m[1] ) ] = stripcslashes( $m[2] ); } elseif ( isset( $m[3] ) && '' !== $m[3] ) { $atts[ strtolower( $m[3] ) ] = stripcslashes( $m[4] ); } elseif ( isset( $m[5] ) && '' !== $m[5] ) { $atts[ strtolower( $m[5] ) ] = stripcslashes( $m[6] ); }
- Added dedicated unit test suite in
tests/phpunit/tests/shortcode/shortcodeParseAtts.phpcovering:- Double-quoted, single-quoted, and unquoted attributes named
0. - Empty values with numeric keys (
0="",0=''). - Multiple numeric keys (
0,1,2). - Mixed numeric, string, and positional attributes.
- Integration with
shortcode_atts()and end-to-enddo_shortcode()execution.
- Double-quoted, single-quoted, and unquoted attributes named
Testing Instructions
- Run the dedicated unit test file:
vendor/bin/phpunit tests/phpunit/tests/shortcode/shortcodeParseAtts.php
- Run the full shortcode suite to confirm no regressions:
vendor/bin/phpunit --filter Shortcode
- Run coding standards checks:
vendor/bin/phpcs src/wp-includes/shortcodes.php tests/phpunit/tests/shortcode/shortcodeParseAtts.php
#6
@
5 weeks ago
Replying to SergeyBiryukov:
Looks like this happens because the
shortcode_parse_atts()function has some checks like! empty( $m[1] ), which should perhaps be replaced with'' !== $m[1].
I have addressed this issue and added dedicated unit tests.
### Cause
In PHP, empty('0') evaluates to true. In shortcode_parse_atts(), the checks ! empty( $m[1] ), ! empty( $m[3] ), and ! empty( $m[5] ) evaluated to false whenever an attribute was named 0 (e.g. [my_shortcode 0="foo"]). This caused the parser to silently omit the attribute.
### Fix
- Updated
shortcode_parse_atts()to checkisset( $m[1] ) && '' !== $m[1](and similarly for$m[3]and$m[5]). - Added dedicated unit tests in
tests/phpunit/tests/shortcode/shortcodeParseAtts.phpcovering0="val",0='val',0=val, empty values, multiple numeric attributes, mixed named/positional attributes, integration withshortcode_atts(), and end-to-enddo_shortcode()execution. - All 10 dedicated tests and 168 suite tests pass (100% OK), and PHPCS passes with 0 errors.
PR: https://github.com/WordPress/wordpress-develop/pull/14240
Hi there, welcome to WordPress Trac! Thanks for the report, I was able to reproduce the issue.
Looks like this happens because the
shortcode_parse_atts()function has some checks like! empty( $m[1] ), which should perhaps be replaced with'' !== $m[1].