Skip to content

Super Cache: fix PHP warning about NULL used in str_replace#32629

Merged
donnchawp merged 2 commits intotrunkfrom
fix/super_cache_wp_cache_request_uri_defined
Aug 23, 2023
Merged

Super Cache: fix PHP warning about NULL used in str_replace#32629
donnchawp merged 2 commits intotrunkfrom
fix/super_cache_wp_cache_request_uri_defined

Conversation

@donnchawp
Copy link
Copy Markdown
Contributor

If you disabled caching for logged in users then a warning about about using NULL showed in the PHP error log:

PHP Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in .../plugins/wp-super-cache/wp-cache-phase2.php on line 54

In this PR the code that defines $wp_cache_request_uri is moved up in wp-cache-phase1.php so it always runs. It wouldn't run if caching was disabled for logged in users due to the way checks were done.
Later on when that variable was used, functions received the value "NULL" and in the latest versions of PHP8 that caused a warning to show.

The linter complained the value was unsanitized but this code executes before WordPress is entirely loaded. It is eventually unslashed and sanitized in wp_cache_postload().

Related thread: https://wordpress.org/support/topic/php-deprecated-str_replace-passing-null/

Proposed changes:

  • Move code that defines $wp_cache_request_uri up so it runs all the time.
  • Sanitize that variable in wp_cache_postload()

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

Does this pull request change what data or activity we track or use?

No

Testing instructions:

  • Using a host with PHP8.2* apply this patch.
  • Disable caching for logged in users.
  • Visit your site and you shouldn't see that warning notice.

@github-actions github-actions Bot added [Plugin] Super Cache A fast caching plugin for WordPress. [Status] In Progress labels Aug 22, 2023
@github-actions
Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ All commits were linted before commit.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Once your PR is ready for review, check one last time that all required checks (other than "Required review") appearing at the bottom of this PR are passing or skipped.
Then, add the "[Status] Needs Team Review" label and ask someone from your team review the code. Once reviewed, it can then be merged.
If you need an extra review from someone familiar with the codebase, you can update the labels from "[Status] Needs Team Review" to "[Status] Needs Review", and in that case Jetpack Approvers will do a final review of your PR.


Super Cache plugin:

  • Next scheduled release: September 5, 2023.
  • Scheduled code freeze: August 28, 2023.

@donnchawp donnchawp requested a review from a team August 22, 2023 15:52
@donnchawp donnchawp marked this pull request as ready for review August 22, 2023 15:52
@donnchawp donnchawp self-assigned this Aug 22, 2023
Copy link
Copy Markdown
Contributor

@haqadn haqadn left a comment

Choose a reason for hiding this comment

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

Tested and it works nice 👍🏼

@donnchawp donnchawp merged commit c57e511 into trunk Aug 23, 2023
@donnchawp donnchawp deleted the fix/super_cache_wp_cache_request_uri_defined branch August 23, 2023 07:11
@github-actions github-actions Bot added this to the super-cache/1.10.1 milestone Aug 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Super Cache A fast caching plugin for WordPress.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants