-
Notifications
You must be signed in to change notification settings - Fork 3.1k
fix potential fatal error on Interactivity API processing directives … #9641
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
base: trunk
Are you sure you want to change the base?
fix potential fatal error on Interactivity API processing directives … #9641
Conversation
…when wrong HTML tags
|
Hi @hugosolar! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
sirreal
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @hugosolar! Did you consider adding a unit test that covers this?
This change seems good to me but I'd like @luisherranz and/or @DAreRodz to review.
| } else { | ||
| if ( 0 !== count( $p->get_attribute_names_with_prefix( 'data-wp-each-child' ) ) ) { | ||
| $each_child_attrs = $p->get_attribute_names_with_prefix( 'data-wp-each-child' ); | ||
| if ( ! is_countable( $each_child_attrs ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer a null check here, it's more consistent with other HTML API code:
| if ( ! is_countable( $each_child_attrs ) ) { | |
| if ( null === $each_child_attrs ) { |
DAreRodz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch, @hugosolar. 🙂
I left a comment. I would be great if you could add a unit test for this case, as @sirreal mentioned. Unit tests for the Interactivity API are inside the tests/phpunit/tests/interactivity-api folder.
| if ( ! is_countable( $each_child_attrs ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| if ( 0 !== count( $each_child_attrs ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure right now, but I think the else block below should execute when $each_child_attrs is null, shouldn't it?
At least that's the behavior in PHP < 8.0, when count() returned 0 if null is passed.
| if ( ! is_countable( $each_child_attrs ) ) { | |
| continue; | |
| } | |
| if ( 0 !== count( $each_child_attrs ) ) { | |
| if ( null !== $each_child_attrs && 0 !== count( $each_child_attrs ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're right. I thought it only contained a foreach with another get_attribute_names_with_prefix() but it contains another conditional block.
That other get_attribute_names_with_prefix has the same problem where it lacks a null check.
|
👋 @hugosolar Just checking in, are you able to finish this up or would you like some help? The next release is approaching and it would be nice to get this fix in place. |
|
@sirreal I've pushed the fix that seems to work better regarding the tests I've made here The error is throwing this checked that line but can't see anything wrong with it |
|
Thanks for the help @sirreal ! |
This may be an edge case for posts created with Classic editor or classic editor block.
_process_directivesfunction withinWP_Interactivity_APIclass will process html and check for closer tags.if there's a wrong tag like
</br>(in my case) will result in a TypeError:by checking the line of code,
count()function is applied toget_attribute_names_with_prefix()method fromWP_HTML_Tag_Processorwhich can eventually returnnullfor newer PHP version (8.0.0 and later) this will throw a fatal error.
probably worth getting an extra check if the funcion is returning a countable variable
Trac ticket: https://core.trac.wordpress.org/ticket/63891