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

get_site_option returns empty string causing exception in Plugin_Command.php #429

Open
Tiippex opened this issue Jul 28, 2024 · 3 comments

Comments

@Tiippex
Copy link

Tiippex commented Jul 28, 2024

Hi,

Issue

I've encountered an issue where get_site_option returns an empty string. This happens at Plugin_Command.php:694.

$auto_updates = get_site_option( Plugin_AutoUpdates_Command::SITE_OPTION );
Due to this, the following condition does not catch the empty string:

if ( false === $auto_updates ) {
    $auto_updates = [];
}

As a result, $auto_updates remains an empty string, which causes an exception later at Plugin_Command.php:720:

'auto_update' => in_array( $file, $auto_updates, true ),
The in_array function expects the haystack to be an array, and passing a string results in an exception.

According to the WordPress documentation, the get_site_option function can return a mixed value, but I'm not sure why it returns an empty string in this case.

PHP 8.0 in_array change

In PHP 8.0 and later, passing a non-array value to in_array results in a TypeError. In PHP 7.x and earlier, this would emit a warning and return false.

Workaround

My current workaround is to remove the strict comparison check: if ( false == $auto_updates ) {

Can you provide guidance on how to handle this scenario properly? Should there be an additional check to ensure $auto_updates is an array before using it in in_array?

Thanks for your help!

@ernilambar
Copy link
Member

ernilambar commented Jul 29, 2024

Yah, we should not assume value would be boolean or array only. May be we can do like this:

if ( false === $auto_updates || ! is_array( $auto_updates ) ) {
    $auto_updates = [];
}

@Tiippex
Copy link
Author

Tiippex commented Jul 29, 2024

if ( false === $auto_updates || ! is_array( $auto_updates ) ) {
$auto_updates = [];
}

Is there a case where the variable is especially false? I guess the error will always occur if no array is returned.
What's about this?

if ( ! is_array( $auto_updates ) ) {
    $auto_updates = [];
}

@Ellusu
Copy link

Ellusu commented Oct 17, 2024

Hi, I'm writing from Yoast Contributor Day. I'll try to solve this!

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

No branches or pull requests

4 participants