webcomic icon indicating copy to clipboard operation
webcomic copied to clipboard

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

Open DerekPadula opened this issue 9 months ago • 3 comments

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.

DerekPadula avatar May 25 '24 07:05 DerekPadula