(latest-posts) Blank category passed to array_column
causes PHP warning and possibly other unwanted side-effects
#53648
Labels
[Block] Latest Posts
Affects the Latest Posts Block
[Status] In Progress
Tracking issues with work in progress
[Type] Bug
An existing feature does not function as intended
Description
We've been noticing quite a few warnings in a large multisite instance (WordPress.com) with the following message:
array_column() expects parameter 1 to be array, string given
It originates from this line https://github.com/WordPress/gutenberg/blame/trunk/packages/block-library/src/latest-posts/index.php#L60 and happens because
@attributes['categories']
is a blank string instead of the expected array of categories. Here's a dump of$attributes
from one of the affected sites.The bug manifests itself as a warning being output in place of the
latest-posts
widget 😬 . The warningarray_column() expects parameter 1 to be array, string given
is printed verbatim where the widget was supposed to render. The$attributes
array looks like this in this case:Normally, if categories are not set for the block, then it won't include the
categories
attribute as part of the array, but in this case, for some reason it does. This corruption seems to happen when the block widget is saved (more details here).Here's the stacktrace from one of the sites:
While it shows
twentysixteen
there, it's not caused by this specific theme. It seems to happy with other legacy themes as well. It does seem related to block widgets, though.I found a similar issue from 2020 here: #21359, but it was related to a recent schema change that happened at the time, which shouldn't be the case now. cc'ing @ryelle for insights just in case 👋🏻
Also cc'ing @noisysocks as he was part of the original Slack discussion about this issue 👋🏻
Step-by-step reproduction instructions
I couldn't reproduce it on a new site, but you can force-reproduce it by setting the
$attributes['categories']
to''
here: https://github.com/WordPress/gutenberg/blame/trunk/packages/block-library/src/latest-posts/index.php#L60.Screenshots, screen recording, code snippet
No response
Environment info
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
The text was updated successfully, but these errors were encountered: