Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/wp-includes/feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
41 changes: 41 additions & 0 deletions tests/phpunit/tests/date/getFeedBuildDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading