diff --git a/src/wp-includes/feed.php b/src/wp-includes/feed.php index 821a3eb9be804..7b6045ce9c2a9 100644 --- a/src/wp-includes/feed.php +++ b/src/wp-includes/feed.php @@ -733,7 +733,9 @@ function get_feed_build_date( $format ) { } // Determine the maximum modified time. - $datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', max( $modified_times ), $utc ); + if ( ! empty( $modified_times ) ) { + $datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', max( $modified_times ), $utc ); + } } if ( false === $datetime ) { diff --git a/tests/phpunit/tests/date/getFeedBuildDate.php b/tests/phpunit/tests/date/getFeedBuildDate.php index d884c0baef05c..9889508dc91e6 100644 --- a/tests/phpunit/tests/date/getFeedBuildDate.php +++ b/tests/phpunit/tests/date/getFeedBuildDate.php @@ -40,6 +40,47 @@ public function test_should_return_correct_feed_build_date() { $this->assertSame( '2018-07-23T03:13:23+00:00', get_feed_build_date( DATE_RFC3339 ) ); } + /** + * Test that get_feed_build_date() does not throw a ValueError + * when wp_list_pluck() returns an empty array because the posts + * array contains non-object, non-array values. + * + * @ticket 59956 + */ + public function test_should_not_error_when_modified_times_is_empty() { + global $wp_query; + + $datetime = new DateTimeImmutable( 'now', wp_timezone() ); + $datetime_utc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) ); + + // Create a real post so the fallback has something to return. + self::factory()->post->create( + array( + 'post_date' => $datetime->format( 'Y-m-d H:i:s' ), + ) + ); + + // Simulate a query where have_posts() is true but wp_list_pluck() + // returns an empty array because the posts array contains scalar + // values (neither objects nor arrays). This triggers the _doing_it_wrong + // notice in WP_List_Util::pluck() and produces an empty result. + $wp_query = new WP_Query(); + + $wp_query->posts = array( 1 ); + $wp_query->post_count = 1; + + $this->setExpectedIncorrectUsage( 'WP_List_Util::pluck' ); + + $result = get_feed_build_date( DATE_RFC3339 ); + + $this->assertEqualsWithDelta( + strtotime( $datetime_utc->format( DATE_RFC3339 ) ), + strtotime( $result ), + 2, + 'Should fall back to last post modified when modified_times is empty.' + ); + } + /** * Test that get_feed_build_date() works with invalid post dates. *