Skip to content

VideoPress: Fix a stats error when a filter replaces the response - #55617

Open
kraftbj wants to merge 1 commit into
trunkfrom
fix/videopress-stats-http-response-JETPACK-4037
Open

kraftbj wants to merge 1 commit into
trunkfrom
fix/videopress-stats-http-response-JETPACK-4037

Conversation

@kraftbj

@kraftbj kraftbj commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Fixes JETPACK-4037

Proposed changes

  • Fix a fatal error in VideoPress stats when a pre_http_request filter replaces the WordPress.com response. Core's documented reply is an array that doesn't have to include http_response, but Stats::fetch_video_plays() called get_status() on that key without checking it.
  • Read the status and body with wp_remote_retrieve_response_code() and wp_remote_retrieve_body(). Cast the status to an integer, as Rest_Controller::get_stats_video_plays() already does for this endpoint. A string '200' therefore counts as a success, and code is still an integer.
  • Results for normal responses don't change:
    • A 200 returns the same code and data. If the body is empty or isn't valid JSON, data is still null.
    • A WP_Error is returned unchanged, and any other status still returns videopress_stats_error. A missing or non-numeric status is also an error.
  • Add test cases for replies without http_response: a 200, a string '200' and a 503. A fourth case covers a WP_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

  • Run jetpack test php packages/videopress -v. All tests should pass.
  • To see the error, run git checkout trunk -- projects/packages/videopress/src/class-stats.php, then run the tests again. The 200, string '200' and 503 cases should fail with Call to a member function get_status() on null. Run git checkout HEAD -- projects/packages/videopress/src/class-stats.php to restore the fix.

On a test site

Use a test site running this branch, connected to WordPress.com.

  • Add this as wp-content/mu-plugins/stats-reply.php. It replaces the stats response with an array that has no http_response:
<?php
add_filter(
	'pre_http_request',
	function ( $reply, $args, $url ) {
		if ( false === strpos( $url, '/stats/video-plays' ) ) {
			return $reply;
		}
		return array(
			'headers'  => array(),
			'body'     => '{}',
			'response' => array(
				'code'    => 200,
				'message' => 'OK',
			),
			'cookies'  => array(),
			'filename' => null,
		);
	},
	10,
	3
);
  • My Jetpack keeps successful stats for a day. Run wp transient delete my-jetpack-videopress-stats so it requests them again.
  • Log in as an administrator and open My Jetpack. In the browser console, run await wp.apiFetch( { path: '/my-jetpack/v1/site/videopress/data' } ).
  • It should return an object with videoCount and no featuredStats. On trunk, the request returns a 500, and the PHP log shows the get_status() error.
  • Delete stats-reply.php and 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_response failed 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.

Reply Trunk, 3 sites This PR, 3 sites
Unchanged fixture: no object, body {} 500, get_status() error in the PHP log 200, {"videoCount":0}
Fixture plus the object 200, {"videoCount":0} 200, {"videoCount":0}
Valid stats plus the object 200 with stats 200 with the same stats
Valid stats, no object 500, same error 200 with the same stats

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.

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.
@kraftbj
kraftbj requested a review from a team as a code owner November 30, 2026 23:45
@kraftbj kraftbj added Bug When a feature is broken and / or not performing as intended [Status] Needs Review This PR is ready for review. [Pri] Normal [Package] VideoPress labels Nov 30, 2026
@kraftbj kraftbj self-assigned this Nov 30, 2026
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the fix/videopress-stats-http-response-JETPACK-4037 branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack fix/videopress-stats-http-response-JETPACK-4037

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@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.
  • ✅ Add a "[Status]" label (In Progress, Needs 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 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@jp-launch-control

Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/packages/videopress/src/class-stats.php 30/82 (36.59%) 13.69% -12 💚

Full summary · PHP report · JS report

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug When a feature is broken and / or not performing as intended [Package] VideoPress [Pri] Normal [Status] Needs Review This PR is ready for review. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant