Repository navigation
Conversation
Core's documented pre_http_request reply doesn't have to include the http_response object, so read the status and body with Core's response helpers instead.
Contributor
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
Contributor
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
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 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
Code Coverage SummaryCoverage changed in 1 file.
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes JETPACK-4037
Proposed changes
pre_http_requestfilter replaces the WordPress.com response. Core's documented reply is an array that doesn't have to includehttp_response, butStats::fetch_video_plays()calledget_status()on that key without checking it.wp_remote_retrieve_response_code()andwp_remote_retrieve_body(). Cast the status to an integer, asRest_Controller::get_stats_video_plays()already does for this endpoint. A string'200'therefore counts as a success, andcodeis still an integer.codeanddata. If the body is empty or isn't valid JSON,datais stillnull.WP_Erroris returned unchanged, and any other status still returnsvideopress_stats_error. A missing or non-numeric status is also an error.http_response: a 200, a string'200'and a 503. A fourth case covers aWP_Error.We haven't seen this on a real site. We found it in the CI logs of draft PR 55610, whose Stats e2e fixture sends this kind of reply. A plugin that replaces the response the same way could cause it too. The code that gets and updates VideoPress posters also uses
http_response. That code hasn't been tested and isn't changed here.Only the VideoPress package gets a changelog entry, because this is an edge case with no known reports from sites.
Related product discussion/links
Does this pull request change what data or activity we track or use?
No.
Testing instructions
Automated tests
jetpack test php packages/videopress -v. All tests should pass.git checkout trunk -- projects/packages/videopress/src/class-stats.php, then run the tests again. The 200, string'200'and 503 cases should fail withCall to a member function get_status() on null. Rungit checkout HEAD -- projects/packages/videopress/src/class-stats.phpto restore the fix.On a test site
Use a test site running this branch, connected to WordPress.com.
wp-content/mu-plugins/stats-reply.php. It replaces the stats response with an array that has nohttp_response:wp transient delete my-jetpack-videopress-statsso it requests them again.await wp.apiFetch( { path: '/my-jetpack/v1/site/videopress/data' } ).videoCountand nofeaturedStats. On trunk, the request returns a 500, and the PHP log shows theget_status()error.stats-reply.phpand run the request again. It should return a 200.Already tested
The automated tests use the real Connection client with a fake connection, and check that the filter handled the request. Before the fix, the three cases without
http_responsefailed with the error above. They pass with it.I also sent the My Jetpack request above on WordPress Playground (WordPress 7.1.5, PHP 8.4.28), on three new sites for each build. Both builds used the real Jetpack plugin's PHP from trunk at 231171d, without a JavaScript build. The fixed build's only difference was this PR's
class-stats.php. The e2e plugins were the unchanged files from PR 55610 at a57461d. They simulate the connection and WordPress.com's replies. A separate test plugin added the object and valid stats where the table says so. It also cleared My Jetpack's saved stats and logged the requests.{}get_status()error in the PHP log{"videoCount":0}{"videoCount":0}{"videoCount":0}The PHP log was empty after every request that returned a 200. I haven't tested this on a site with a real WordPress.com connection, or checked the My Jetpack screen in a browser.