Skip to content

Commit 6b4c4c3

Browse files
committed
Feeds: Address review feedback on get_feed_build_date() empty array fix.
- Replace `! empty( $modified_times )` with truthy check `if ( $modified_times )`, aligning with the ongoing effort to avoid empty() in new core code. - Rewrite the test setup to use the real query codepath: self::factory()->post->create() + query_posts( [ 'posts__in' => [ $id ], 'fields' => 'ids' ] ). 'fields' => 'ids' makes WP_Query->posts an array of integer IDs, which causes wp_list_pluck() to emit _doing_it_wrong and return an empty array naturally — no more manual $wp_query property mutation. - Add $this->assertIsString( $result ) to narrow the string|false return type before it flows into strtotime(), keeping PHPStan happy at higher analysis levels. Addresses review feedback from @westonruter. Props westonruter. See #59956.
1 parent c0fe548 commit 6b4c4c3

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

src/wp-includes/feed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ function get_feed_build_date( $format ) {
733733
}
734734

735735
// Determine the maximum modified time.
736-
if ( ! empty( $modified_times ) ) {
736+
if ( $modified_times ) {
737737
$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', max( $modified_times ), $utc );
738738
}
739739
}

tests/phpunit/tests/date/getFeedBuildDate.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,30 @@ public function test_should_return_correct_feed_build_date() {
4848
* @ticket 59956
4949
*/
5050
public function test_should_not_error_when_modified_times_is_empty() {
51-
global $wp_query;
52-
5351
$datetime = new DateTimeImmutable( 'now', wp_timezone() );
5452
$datetime_utc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
5553

56-
// Create a real post so the fallback has something to return.
57-
self::factory()->post->create(
54+
$id = self::factory()->post->create(
5855
array(
5956
'post_date' => $datetime->format( 'Y-m-d H:i:s' ),
6057
)
6158
);
6259

63-
// Simulate a query where have_posts() is true but wp_list_pluck()
60+
// Use a query where have_posts() is true but wp_list_pluck()
6461
// returns an empty array because the posts array contains scalar
6562
// values (neither objects nor arrays). This triggers the _doing_it_wrong
6663
// notice in WP_List_Util::pluck() and produces an empty result.
67-
$wp_query = new WP_Query();
68-
69-
$wp_query->posts = array( 1 );
70-
$wp_query->post_count = 1;
64+
query_posts(
65+
array(
66+
'posts__in' => array( $id ),
67+
'fields' => 'ids',
68+
)
69+
);
7170

7271
$this->setExpectedIncorrectUsage( 'WP_List_Util::pluck' );
7372

7473
$result = get_feed_build_date( DATE_RFC3339 );
74+
$this->assertIsString( $result );
7575

7676
$this->assertEqualsWithDelta(
7777
strtotime( $datetime_utc->format( DATE_RFC3339 ) ),

0 commit comments

Comments
 (0)