fix(starter-content): stop generated post creation failing on PHP 8 - #1026
fix(starter-content): stop generated post creation failing on PHP 8#1026jason10lee wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
🟢 Approval recommended
The change is minimal, directly addresses the PHP 8 fatal, and includes targeted regression tests that exercise the corrected sideload behavior.
Pull request overview
Fixes a PHP 8 fatal in Starter_Content_Generated::download_random_image() that prevented generated starter content posts from being created (and could also affect the setup wizard path), by ensuring the wp_handle_sideload() $file argument is passed as a variable (required because it is passed by reference). Adds focused unit tests that stub the HTTP layer so sideloading executes against a real local image file and verifies both the success and failure behaviors.
Changes:
- Avoid PHP 8 “cannot be passed by reference” fatal by assigning the sideload
$filearray to a variable before callingwp_handle_sideload(). - Add unit coverage for the image download/sideload success case (file ends up in uploads) and the failure case (returns
nullon request error). - Ensure test cleanup removes only this test’s
pre_http_requestfilters (without disrupting the WP test suite’s own handlers).
File summaries
| File | Description |
|---|---|
| plugins/newspack-plugin/includes/starter_content/class-starter-content-generated.php | Fixes the PHP 8 by-reference argument fatal by passing a $file variable into wp_handle_sideload(). |
| plugins/newspack-plugin/tests/unit-tests/starter-content-generated.php | Adds regression tests covering the sideload success path and the “request fails” null-return path, with safe per-test HTTP filter teardown. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
All Submissions:
Changes proposed in this Pull Request:
Generated starter content creates its categories and then no posts at all.
wp_handle_sideload()takes its first argument by reference, and the call passed an array literal, which PHP 8 rejects outright withArgument #1 ($file) could not be passed by reference. The fatal lands on the first post, son setupfinishes with categories and an empty site. The setup wizard reaches the same call throughapi_starter_content_post, which is registered on every site.Two things kept this quiet. The fatal has nothing to do with the network, so it fires whether or not the image host answers, and it presents as one of the image-fetch failures being fixed elsewhere. And the E2E path branches to
copy_bundled_image(), added when starter-content images moved off the public internet, so nothing running in CI reaches the call.The array is not a constant expression, since it calls
mime_content_type()andfilesize(), so it is a temporary. Passing a temporary by reference was a notice on PHP 7 and the call went through; PHP 8 made it an error. That dates the break to the PHP 8 move rather than to when this code was written.The fix assigns the array to a variable. Two regression tests come with it, stubbing the HTTP layer so the sideload runs for real against the bundled image instead of being asserted around.
We weighed one alternative: pointing the non-E2E path at the bundled image too, dropping the network dependency entirely. We left it out because it changes what publishers get, one repeated image in place of a varied one, and that is a product decision separate from the fatal.
Clearing
pre_http_requestin the test teardown takes the WordPress test suite's own handlers with it. The first version of these tests did exactly that and turned 142 later tests into errors and failures, while still passing on its own under--filter. The teardown therefore removes its own filters by name, and the comment in the file says why.Found while building an environment for unrelated work; no Linear issue.
How to test the changes in this Pull Request:
NEWSPACK_IS_E2Eis not defined, runn setup --yes. Ten starter posts are created, each with a featured image.wp_handle_sideload()fatal.NEWSPACK_IS_E2Edefined. Posts still take the bundled image, unchanged from before.Other information:
Full newspack-plugin suite green (3899 tests, 11026 assertions), phpcs clean. Reverting the fix turns the new tests into the fatal above, so they fail without it.