Skip to content

Fix str_starts_with() null deprecation in get_current_url_supercache_dir#851

Closed
davidcarma wants to merge 1 commit intoAutomattic:trunkfrom
davidcarma:fix/null-wp-cache-home-path
Closed

Fix str_starts_with() null deprecation in get_current_url_supercache_dir#851
davidcarma wants to merge 1 commit intoAutomattic:trunkfrom
davidcarma:fix/null-wp-cache-home-path

Conversation

@davidcarma
Copy link
Copy Markdown

Summary

  • Fixes a PHP 8.1+ deprecation warning: str_starts_with(): Passing null to parameter #2 ($needle) of type string is deprecated on line 857 of wp-cache-phase2.php
  • The global $wp_cache_home_path can be null when not defined in the cache config, which gets passed directly to str_starts_with() and rtrim() as a null needle/characters parameter
  • The fix coalesces $wp_cache_home_path to an empty string and skips the prefix check entirely when the home path is empty, avoiding both the deprecation and the semantically incorrect rtrim(null, '/') call

Reproduction

  1. Run any WP-CLI command or trigger a WP-Super-Cache operation on PHP 8.1+ where $wp_cache_home_path is not set in the cache config
  2. Observe the deprecation notice:
    Deprecated: str_starts_with(): Passing null to parameter #2 ($needle) of type string is deprecated
    in /var/www/html/wp-content/plugins/wp-super-cache/wp-cache-phase2.php on line 857
    

Changes

-  if ( ! str_starts_with( $uri, $wp_cache_home_path ) ) {
-    $uri = rtrim( $wp_cache_home_path, '/' ) . $uri;
+  $home_path = $wp_cache_home_path ?? '';
+  if ( $home_path !== '' && ! str_starts_with( $uri, $home_path ) ) {
+    $uri = rtrim( $home_path, '/' ) . $uri;
   }

Notes

  • Related but different from PR Check for null string before escaping #848, which addressed a null $wp_cache_request_uri passed to esc_url_raw() on line 394
  • This is a minimal, targeted fix with no behavioural change when $wp_cache_home_path is properly set

Made with Cursor

On PHP 8.1+, passing null to str_starts_with() as the $needle parameter
triggers a deprecation notice. The global $wp_cache_home_path can be null
when it is not defined in the cache config, causing:

  Deprecated: str_starts_with(): Passing null to parameter #2 ($needle)
  of type string is deprecated in wp-cache-phase2.php on line 857

This coalesces $wp_cache_home_path to an empty string and skips the
prefix check entirely when the home path is empty, which avoids both the
deprecation warning and the incorrect rtrim(null, '/') call inside the
conditional body.

Made-with: Cursor
@github-actions
Copy link
Copy Markdown

Thank you for your interest!

Pull requests should be made against the monorepo at https://github.com/Automattic/jetpack.

@github-actions github-actions bot closed this Mar 18, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Mar 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant