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
18 changes: 18 additions & 0 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -7998,6 +7998,24 @@ function get_posts_by_author_sql( $post_type, $full = true, $post_author = null,
* @return string The date of the last post, or false on failure.
*/
function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) {
/**
* Pre-filter the return value of get_lastpostdate() before the query is run.
*
* @since 7.3.0
*
* @param string|false $lastpostdate The most recent time that a post was published,
* in 'Y-m-d H:i:s' format, or false. Returning anything
* other than false will short-circuit the function.
* @param string $timezone Location to use for getting the post published date.
* See get_lastpostdate() for accepted `$timezone` values.
* @param string $post_type The post type to check.
*/
$lastpostdate = apply_filters( 'pre_get_lastpostdate', false, $timezone, $post_type );

if ( false !== $lastpostdate ) {
return $lastpostdate;
}

$lastpostdate = _get_last_post_time( $timezone, 'date', $post_type );

/**
Expand Down
47 changes: 47 additions & 0 deletions tests/phpunit/tests/post/getLastPostDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,51 @@ public function test_get_lastpostdate() {
$this->assertSame( $post_post_date_last, get_lastpostdate( 'blog', 'post' ) );
$this->assertSame( $book_post_date_last, get_lastpostdate( 'blog', 'book' ) );
}

/**
* @ticket 63614
*/
public function test_pre_get_lastpostdate_filter_short_circuits_query() {
global $wpdb;

self::factory()->post->create(
array(
'post_date' => '2020-02-28 16:09:28',
)
);

$filter = new MockAction();
add_filter( 'pre_get_lastpostdate', array( $filter, 'filter' ), 10, 3 );
add_filter(
'pre_get_lastpostdate',
static function () {
return '2001-01-01 00:00:00';
}
);

$num_queries = $wpdb->num_queries;
$result = get_lastpostdate( 'blog', 'post' );

$this->assertSame( '2001-01-01 00:00:00', $result, 'The filtered value should be returned.' );
$this->assertSame( $num_queries, $wpdb->num_queries, 'No database queries should be run.' );
$this->assertSame( 1, $filter->get_call_count(), 'The filter should be called once.' );
$this->assertSame( array( false, 'blog', 'post' ), $filter->get_args()[0], 'The filter should receive the default value, timezone, and post type.' );
}

/**
* @ticket 63614
*/
public function test_pre_get_lastpostdate_filter_returning_false_does_not_short_circuit() {
$post_date = '2020-02-28 16:09:28';

self::factory()->post->create(
array(
'post_date' => $post_date,
)
);

add_filter( 'pre_get_lastpostdate', '__return_false' );

$this->assertSame( $post_date, get_lastpostdate( 'blog', 'post' ) );
}
}
Loading