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 iterator_to_array() error on PHP < 8.2 #78

Merged
merged 3 commits into from
Sep 10, 2024

Conversation

alecgeatches
Copy link
Contributor

@alecgeatches alecgeatches commented Sep 10, 2024

Description

Fixes #77, which causes this error:

{
  "code": "vip-block-data-api-parser-error",
  "message": "Error parsing post ID 2: iterator_to_array(): Argument #1 ($iterator) must be of type Traversable, array given",
  "data": {
    "status": 500
  }
}

This is caused by a call to iterator_to_array() in parser code:

// WP_Block#inner_blocks can be an array or WP_Block_List (iterable).
$inner_blocks = iterator_to_array( $block->inner_blocks );

iterator_to_array() will only accept an array in PHP 8.2.

PHP Changelog for iterator_to_array() showing 8.2 change

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

  1. Check out the trunk branch of vip-block-data-api and install on a WordPress installation with PHP version 8.0 or 8.1.

  2. Go to a published post's Block Data API URL, e.g.

    https://my.lndo.site/wp-json/vip-block-data-api/v1/posts/1/blocks
    
  3. 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.

  4. Change the VIP Block Data API branch to this branch (fix/php-8.1-iterator-error).

  5. Reload the wp-json endpoint used in step 2.

  6. See that the post renders correctly.

Other Todos

chriszarate
chriszarate previously approved these changes Sep 10, 2024
Copy link
Member

@chriszarate chriszarate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix

Copy link
Contributor

@ingeniumed ingeniumed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bumped up the plugin version as well

@ingeniumed ingeniumed merged commit 079599c into trunk Sep 10, 2024
4 checks passed
@ingeniumed ingeniumed deleted the fix/php-8.1-iterator-error branch September 10, 2024 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API error in latest version 1.4.0
3 participants