Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

"PHP Warning: foreach() argument must be of type array|object, bool" Across 5 Files #386

Open
DerekPadula opened this issue May 25, 2024 · 3 comments
Assignees
Labels

Comments

@DerekPadula
Copy link

Expected Behavior
That the plugin would not generate warnings on each page load for this situation because there wouldn't be errors.

Current Behavior
My site's PHP error logs are generating a PHP warning across 5 files each time a visitor requests a Webcomic page that states the following: "PHP Warning: foreach() argument must be of type array|object, bool". The content loads as it should, but this occurs hundreds of times a day. Here are the 5 files:

[22-May-2024 13:49:54 UTC] PHP Warning: foreach() argument must be of type array|object, bool given in /home/websitedomain/public_html/wp-content/plugins/webcomic/lib/taxonomy/media.php on line 154
[22-May-2024 13:49:54 UTC] PHP Warning: foreach() argument must be of type array|object, bool given in /home/websitedomain/public_html/wp-content/plugins/webcomic/lib/restrict/metabox/age.php on line 243
[22-May-2024 13:49:54 UTC] PHP Warning: foreach() argument must be of type array|object, bool given in /home/websitedomain/public_html/wp-content/plugins/webcomic/lib/restrict/metabox/password.php on line 161
[22-May-2024 13:49:54 UTC] PHP Warning: foreach() argument must be of type array|object, bool given in /home/websitedomain/public_html/wp-content/plugins/webcomic/lib/restrict/metabox/roles.php on line 248
[22-May-2024 13:49:54 UTC] PHP Warning: foreach() argument must be of type array|object, bool given in /home/websitedomain/public_html/wp-content/plugins/webcomic/lib/twitter/metabox/status.php on line 337
[22-May-2024

Reproduction Steps
Each error is related to this type of foreach loop:

foreach ( $comics as $comic ) {
$collection = get_post_type( $comic );

^ This was in /lib/restrict/metabox/age.php. They're all like this.

Suggested Fix
It seems to be an issue related to the code being "iterable." Searches online provide suggestions for how to fix it using "is_iterable", but I'm not a PHP programmer and don't know how to resolve it, so I'm reporting the bug here. All of their suggestions are overly complicated and confusing for someone who doesn't understand the language well enough to know what to edit.

Perhaps this has something to do with the current way of writing PHP? I'm using PHP 8.1 on WordPress 6.5.3, and will soon upgrade PHP to 8.3.

Context
I know that you're not developing this plugin any further, so would it be possible to provide a fix for a single example in a response to this bug report? Then I (and future users) can make edits to each of the 5 files using a similar syntax.

Thank you.

@ykyu
Copy link

ykyu commented May 25, 2024

Just guessing here, but $comics is being set using a get_post_meta() call, which can return an array, a string, or false. foreach only works with arrays, so if the call returns anything other than an array, this error would occur.

If you wrap the foreach in an if statement to check that $comics is an array, you should be able to prevent the foreach from evaluating if $comics is false. Nothing will happen if $comics ends up being something other than array, which is already what's occuring, which is why the page still displays/loads as expected. Let me know if this works for you.

if(is_array($comics)) { 

  foreach ( $comics as $comic ) {
  $collection = get_post_type( $comic );
  
  ..
  
  } //end of foreach
} //end of if

@DerekPadula
Copy link
Author

Thank you for the solution attempt. I don't know why, but the PHP error has stopped appearing in my log without having made your suggested change. There might be something else on the site that triggers the error that I'm unaware of.

If it comes back, then I'll make this change and see if that resolves it.

For now, you can close this, or do so a couple weeks from now just to be sure.

@ykyu
Copy link

ykyu commented May 29, 2024

The files triggering the error are pretty specific, so if some relevant settings were disabled, there may no longer be any need for those files to be called -- or, the circumstances leading to get_post_meta() evaluating to false may have changed, and it now properly evaluates to an array -- which would also resolve the issue.

This is a pretty low stakes error though, since the preferred outcome is what happens anyway, regardless of the error. My solution just prevents the error from happening/being logged, but the outcome is still the same.

Not the maintainer/contributor, so I can't actually mark this as resolved (I think you can though?), but I'm glad the problem is gone for you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants