0
votes

I have a client that's been relying on a WP plugin for years that was abandoned by the author a few years ago. I've been able to make some fixes to make it work with an upgrade to PHP 7.2, but this one puzzles me. The more I research it, I feel like the less I understand about how to fix it.

Warning below:

Warning: count(): Parameter must be an array or an object that implements Countable in /app/public/wp-content/plugins/ozh-admin-drop-down-menu/inc/core.php on line 311 (first line below is 311)

if (!count($wp_ozh_adminmenu)) {
    $wp_ozh_adminmenu = (array)get_option('ozh_adminmenu');
    unset($wp_ozh_adminmenu[0]);
}
1
Is $wp_ozh_adminmenu an array? Can you make sure that? - Satish Saini
and what's the value of $wp_ozh_adminmenu? I guess it's a string. what about a test like... if (!is_array($wp_ozh_adminmenu) || count($wp_ozh_adminmenu) === 0) - kuh-chan
Not sure I'm following, but just wrapped the above code in: if (!is_array($wp_ozh_adminmenu) || count($wp_ozh_adminmenu) === 0) { } That did not help. - Ray

1 Answers

2
votes

Before PHP 7.2 when passing a non-array to count(), it returned 1.
Now, as you can see in the breaking changes, count() emits a warning when you give it a non-array.

A simple workaround is to check if it's an array before using it. It may require a bit more checks, if you want to allow arrays and string for example.

<?php
if (!is_array($wp_ozh_adminmenu) || !count($wp_ozh_adminmenu)) {
    $wp_ozh_adminmenu = (array)get_option('ozh_adminmenu');
    unset($wp_ozh_adminmenu[0]);
}