Skip to content

Fix: cast $blog_id to int in wp_cache_clear_cache() to prevent crash on non-multisite installs#1042

Closed
DeveloperBatuhanALGUL wants to merge 1 commit into
Automattic:trunkfrom
DeveloperBatuhanALGUL:fix/wp-cache-clear-cache-blog-id-type
Closed

Fix: cast $blog_id to int in wp_cache_clear_cache() to prevent crash on non-multisite installs#1042
DeveloperBatuhanALGUL wants to merge 1 commit into
Automattic:trunkfrom
DeveloperBatuhanALGUL:fix/wp-cache-clear-cache-blog-id-type

Conversation

@DeveloperBatuhanALGUL
Copy link
Copy Markdown
Contributor

Problem

When a third-party theme or plugin calls wp_cache_clear_cache('all', false) with a non-integer $blog_id, the function passes that value directly to get_supercache_dir(), which internally calls get_blog_option() — a multisite-only function. On non-multisite installs, this causes a fatal error.

Fixes #1019

Fix

Cast $blog_id to int at the top of the function. This ensures any non-integer value (e.g. 'all', false) is normalized to 0, which correctly triggers the single-site cache clearing path.

Testing

  1. On a non-multisite WordPress install, call wp_cache_clear_cache('all', false)
  2. Confirm no fatal error occurs and cache is cleared correctly

Copy link
Copy Markdown
Contributor

@donnchawp donnchawp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was generated by AI.

The (int) $blog_id cast is correct and resolves #1019: (int) 'all' and (int) false both normalize to 0, routing to the single-site path. Normalizing the parameter to the type the function assumes is the right data-level fix. That one line should be the entire PR.

The other four changes (@fclose( $fr )if ( $fr ) { fclose( $fr ); }) are unrelated to #1019 and should be dropped:

  1. Out of scope. The PR title, body, and linked issue describe only the cast. The fclose edits in wp_cache_get_ob() / wp_cache_shutdown_callback() are undocumented and touch a different code path.

  2. Two are dead code. At lines ~2511 and ~2965 the new if ( $fr ) { fclose( $fr ); } sits inside an existing if ( $fr ) { block — the guard can never be false. Revert these to bare fclose( $fr );.

  3. Inconsistent error handling. In the ! $gz branch a guarded close now sits beside @fclose( $fr2 ) and @unlink( ... ), so the error path is no longer uniform. Either leave the block's existing @ style untouched, or change it consistently — not half.

Please resubmit with only the $blog_id cast.

Minor (no change required): global declarations conventionally precede statements, but order doesn't affect global semantics here.

@donnchawp
Copy link
Copy Markdown
Contributor

This comment was generated by AI.

For visibility: to unblock a release, the two genuinely-needed fixes in this PR have been split into focused PRs:

So this PR actually addressed two real issues. The remaining two changes here (the fclose( $fr ) guards at the call sites that are already inside if ( $fr ) blocks) are redundant and were the basis of the earlier change request.

No action needed on your side — once #1053 and #1054 land, this PR can be closed as superseded. Thanks for surfacing both bugs.

@donnchawp donnchawp closed this May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants