Feeds: Guard against empty array passed to max() in get_feed_build_date()#11387
Feeds: Guard against empty array passed to max() in get_feed_build_date()#11387chubes4 wants to merge 3 commits intoWordPress:trunkfrom
Conversation
…te(). When `wp_list_pluck()` returns an empty array from posts that lack the `post_modified_gmt` property, `max()` throws a `ValueError` on PHP 8+. This adds an emptiness check before calling `max()`, allowing `$datetime` to remain `false` and fall through to the existing `get_lastpostmodified()` fallback. Includes a unit test that simulates the condition. Fixes #59956.
|
Hi @chubes4! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Aligns variable assignments in the test to satisfy the Generic.Formatting.MultipleStatementAlignment sniff.
…_times. The production failure occurs when the posts array contains scalar values (e.g. post IDs) rather than post objects. wp_list_pluck() emits a _doing_it_wrong notice and returns an empty array, which then triggers the max() ValueError. Update the test to use an integer in the posts array and call setExpectedIncorrectUsage() to properly handle the expected notice. See #59956.
Trac Ticket
See https://core.trac.wordpress.org/ticket/59956
Description
get_feed_build_date()passes the result ofwp_list_pluck()directly tomax()without checking for an empty array. When the query's post objects lack thepost_modified_gmtproperty,wp_list_pluck()returns an empty array andmax()throws aValueErroron PHP 8.0+.This happens in production when bots request RSS feed URLs (e.g.
/author/{name}/feed/,/taxonomy/{term}/feed/) for terms or authors with zero published posts. Thehave_posts()guard on line 722 passes (the query object exists and has a truthypost_count), but the underlying post objects are incomplete — sowp_list_pluck( $wp_query->posts, 'post_modified_gmt' )returns[].On PHP 7.x this was a silent warning. On PHP 8.0+ it is a fatal
ValueErrorthat returns HTTP 500 to the crawler.The Fix
Wrap the
max()call inif ( ! empty( $modified_times ) ). When the array is empty,$datetimeremainsfalseand falls through to the existingget_lastpostmodified( 'GMT' )fallback on line 741. No behavioral change for feeds that have valid post data.Testing
Automated: Added
test_should_not_error_when_modified_times_is_emptytotests/phpunit/tests/date/getFeedBuildDate.php. The test simulates a query wherehave_posts()is true but posts lackpost_modified_gmt, and verifies the function falls back toget_lastpostmodified()instead of throwing.Manual (production): Verified on a WordPress 6.9.4 multisite (PHP 8.4) with 1,175 artist taxonomy terms that had zero published posts on the main site. Before the fix, these feed URLs returned HTTP 500 with
ValueError: max(): Argument #1 ($value) must contain at least one element. After the fix, all return HTTP 200 with valid empty RSS feeds. Normal feeds with posts are unaffected.debug.log— fatal errors stopped immediately after applying the fix (were firing every 10–20 seconds prior)<item>elements and<lastBuildDate>AI Disclosure
AI assistance: Yes
Tool(s): Claude Code (Claude Opus 4.6 directly on a VPS via OpenCode)
Used for: Identifying the root cause from production debug logs, drafting the guard condition, and scaffolding the unit test. Final implementation, testing, and PR description were reviewed and edited by a human contributor.