Wp-rocket: PHP Warning: array_flip(): Can only flip STRING and INTEGER in inc/common/purge.php on line 144

Created on 22 Oct 2020  ·  9Comments  ·  Source: wp-media/wp-rocket

Before submitting an issue please check that you’ve completed the following steps:

  • Made sure you’re on the latest version ✔️
  • Used the search feature to ensure that the bug hasn’t been reported before ✔️

Describe the bug
A customer reported they are receiving this warning when they try to clear the cache:

Warning: array_flip(): Can only flip STRING and INTEGER values! in /wp-content/plugins/wp-rocket/inc/common/purge.php on line 144
Warning: Cannot modify header information - headers already sent by (output started at /wp-content/plugins/wp-rocket/inc/common/purge.php:144) in /wp-admin/post.php on line 231

It is happening here:
https://github.com/wp-media/wp-rocket/blob/master/inc/common/purge.php#L144

Expected behavior
Per Tonya's comment, this is something we should guard against in Rocket. By using a guard clause or type cast to a string.

Additional context
Related ticket: https://secure.helpscout.net/conversation/1317241097/204840?folderId=2683093
Remy reviewed the ticket . Here are his notes:

"not related to the update, they must have a strange value in there somewhere when we collect the URLs to purge. Something returning false maybe instead of the URL"

Backlog Grooming (for WP Media dev team use only)

  • [x] Reproduce the problem
  • [x] Identify the root cause
  • [x] Scope a solution
  • [x] Estimate the effort
[XS] good first issue cache low minor bug

All 9 comments

Reproduce the issue ✅

Reproduced on test site

Identify the root cause ✅

If the value is not a string or integer, this will cause the warning when using array_flip()

Scope a solution ✅

The double array_flip() is a trick to do what array_unique() does, and is more performant on very large arrays.

But, considering the fact that it's possible to not have string/integer only in some edge cases, and that this array is never very large, We can replace this call by a call to array_unique() instead.

We can also add a array_filter() in there, to remove any value that would evaluate to empty, as we don't want to pass this kind of value to the purge.

Estimate the effort ✅

Effort [XS]

+1
When should we expect this bug to be resolved? Thanks!

@vmanthos @Tabrisrp Can this issue be fixed by a Technical Support Engineer?

@NataliaDrause Awesome :)

Steps to reproduce:
1- Add
$purge_urls[] = false;
here:
https://github.com/wp-media/wp-rocket/blob/b409700c18f88668c15a408665ef454d5ef998f1/inc/common/purge.php#L143
2- Update post
3- Warning exist in debug.log

Thanks to @piotrbak

@Mai-Saad were you testing the modified code?
https://github.com/wp-media/wp-rocket/pull/3579/files/968ba25b2a9432efae928e7c9b0b2df308a21744
I have tested different values, including
$purge_urls[] = false;
$purge_urls[] = null;
$purge_urls[] = 0;
$purge_urls[] = "";
and they are being filtered out by array_filter() and I get no warning

@Mai-Saad I am not able to reproduce the warning.
I am logging the array _before_ and _after_ array_filter() as here:

$purge_urls[] = false;
    error_log( date('[ Y-m-d H:i:s ] ', $_SERVER['REQUEST_TIME'] ). "\n". "BEFORE" . "\n" . print_r($purge_urls, true) . "\n", 3, ABSPATH . "/purge_log.log" );
    $purge_urls = array_filter( $purge_urls, 'is_string' );
error_log( date('[ Y-m-d H:i:s ] ', $_SERVER['REQUEST_TIME'] ). "\n". "AFTER" . "\n" . print_r($purge_urls, true) . "\n", 3, ABSPATH . "/purge_log.log" );
    return array_flip( array_flip( $purge_urls ) );

the non-string values are filtered out (screenshot of the error_log https://jmp.sh/cIP3ypS ) >> no warnings in debug.log.

Could you please re-test it and share what warning do you get? Thank you!

Was this page helpful?
0 / 5 - 0 ratings