Opened 7 years ago
Last modified 6 weeks ago
#49094 new defect (bug)
preg_match() expects parameter 2 to be string on line 417 of wp-includes/class-wp-block-parser.php
| Reported by: | thenerdcave | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Editor | Version: | 5.3.2 |
| Severity: | critical | Keywords: | |
| Cc: | Focuses: |
Description
wp version 5.3.2
os MacOS 10.15.2
browser FireFox 71.0
Building a custom theme and on a very simple page if I have a qry param in the url that is an array (ex. ?c[]=3&c[]=6) I get the following warnings. If I change the url param to a string like c=3 then the warnings go away.
Warning: preg_match() expects parameter 2 to be string, array given in /wp-includes/class-wp-block-parser.php on line 417 Warning: strlen() expects parameter 1 to be string, array given in /wp-includes/class-wp-block-parser.php on line 489
The reason the url has these params is for a custom data filter with a multi-select menu.
After a google search it appears that this bug was introduced in version 5.3.1 and based on my findings to fix the issue that is accurate.
The bug appears to be related to the parse_blocks() function, I was able to trace the issue back to this location: blocks.php line 271 function filter_block_content();
If I update the function like so,
function filter_block_content( $text, $allowed_html = 'post', $allowed_protocols = array() ) {
$result = '';
if ( !is_array( $text ) ) {
$blocks = parse_blocks( $text );
foreach ( $blocks as $block ) {
$block = filter_block_kses( $block, $allowed_html, $allowed_protocols );
$result .= serialize_block( $block );
}
}
return $result;
}
by adding the if !is_array() condition the page works properly and warnings go away. I'm not 100% sure if this is the correct place for this fix but wanted to let you know about the issue, since I don't believe WP should be trying to parse qry params this way.
Hope this information is helpful.
Change History (3)
#3
@
6 weeks ago
Can someone dive deeper into this? It seems like something else is wrong, rather than the reported mechanism. Adding an is_array() check is distracting here because the type of the $text argument is string. If something is passing something that isn’t a string, it’s wrong at the call site.
Given that this occurs with URL query arguments, that should be a huge clue. Something is probably expecting a single argument for something like a selected menu, and then passing it into wp_kses(), never expecting that PHP might create an array.
For example, @thenerdcave maybe the multi-select menu is the cause here? Somewhere something is making an assumption about $_GET and the best place to resolve this is where that’s read. Unfortunately while the proposed fix might work in this very specific case it’s adding runtime code to hide a bug that appeared beforehand during the request and it’s not an appropriate resolution.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
On WP 5.4 this also breaks the Customizer when saving WP-Customizer fields that save arrays to theme_mod instead of strings. Example:
On "publish" in Customizer, wp-ajax.php is called with the parameter
The saving fails with
and the Customizer breaks without updating the value.
@thenerdcave suggestion fixes it for me, too.