Fix iterator_to_array() error on PHP < 8.2 #78
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Fixes #77, which causes this error:
This is caused by a call to
iterator_to_array()
in parser code:vip-block-data-api/src/parser/content-parser.php
Lines 299 to 300 in 1593b60
iterator_to_array()
will only accept an array in PHP 8.2.Our plugin supports to PHP 8.0, which means 8.0 and 8.1 will currently show this error for any parsed post.
The changed code now tests for an array and uses it directly if available, which will work on prior PHP versions.
Steps to Test
Check out the
trunk
branch ofvip-block-data-api
and install on a WordPress installation with PHP version8.0
or8.1
.Go to a published post's Block Data API URL, e.g.
See the iterator error in the response data:
Error parsing post ID 1: iterator_to_array(): Argument #1 ($iterator) must be of type Traversable, array given
.Change the VIP Block Data API branch to this branch (
fix/php-8.1-iterator-error
).Reload the
wp-json
endpoint used in step 2.See that the post renders correctly.
Other Todos
Figure out why the minimum test matrix (PHP 8/WordPress 6.2) didn't catch this.See Fix wp-env to use proper PHP version in test matrix #79.
Ensure this fix is included in the next update of VIP's integrations.