Posts, Post Types: Pass the post type to `get_lastpostdate()` in `get_lastpostmodified()`.

This ensures that the correct values are being compared when retrieving the result for custom post types.

Additionally, pass the `$post_type` parameter to `get_lastpostdate` and `get_lastpostmodified` filters.

Props mikaumoto, munyagu, donmhico, johnbillion, SergeyBiryukov.
Fixes #47777.

git-svn-id: https://develop.svn.wordpress.org/trunk@48631 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-07-27 02:16:46 +00:00
parent a1c40b346c
commit 581deff371
3 changed files with 119 additions and 6 deletions

View File

@ -6620,16 +6620,20 @@ 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' ) {
$date = _get_last_post_time( $timezone, 'date', $post_type );
/**
* Filters the most recent time that a post on the site was published.
*
* @since 2.3.0
* @since 5.5.0 Added the `$post_type` parameter.
*
* @param string|false $date Date the last post was published. False on failure.
* @param string $timezone Location to use for getting the post published date.
* See get_lastpostdate() for accepted `$timezone` values.
* @param string|false $date Date the last post was published. False on failure.
* @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.
*/
return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date', $post_type ), $timezone );
return apply_filters( 'get_lastpostdate', $date, $timezone, $post_type );
}
/**
@ -6661,13 +6665,14 @@ function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) {
* @param string $post_type The post type to check.
*/
$lastpostmodified = apply_filters( 'pre_get_lastpostmodified', false, $timezone, $post_type );
if ( false !== $lastpostmodified ) {
return $lastpostmodified;
}
$lastpostmodified = _get_last_post_time( $timezone, 'modified', $post_type );
$lastpostdate = get_lastpostdate( $timezone, $post_type );
$lastpostdate = get_lastpostdate( $timezone );
if ( $lastpostdate > $lastpostmodified ) {
$lastpostmodified = $lastpostdate;
}
@ -6676,13 +6681,15 @@ function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) {
* Filters the most recent time that a post was modified.
*
* @since 2.3.0
* @since 5.5.0 Added the `$post_type` parameter.
*
* @param string|false $lastpostmodified The most recent time that a post was modified, in 'Y-m-d H:i:s' format.
* False on failure.
* @param string $timezone Location to use for getting the post modified date.
* See get_lastpostdate() for accepted `$timezone` values.
* @param string $post_type The post type to check.
*/
return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone );
return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone, $post_type );
}
/**

View File

@ -0,0 +1,39 @@
<?php
/**
* @group post
*/
class Tests_Post_GetLastPostDate extends WP_UnitTestCase {
/**
* @ticket 47777
*/
public function test_get_lastpostdate() {
$post_post_date = '2020-01-30 16:09:28';
$book_post_date = '2019-02-28 18:11:30';
// Register book post type.
register_post_type( 'book', array( 'has_archive' => true ) );
// Create a simple post.
$simple_post_id = self::factory()->post->create(
array(
'post_title' => 'Simple Post',
'post_type' => 'post',
'post_date' => $post_post_date,
)
);
// Create custom type post.
$book_cpt_id = self::factory()->post->create(
array(
'post_title' => 'Book CPT',
'post_type' => 'book',
'post_date' => $book_post_date,
)
);
$this->assertEquals( $post_post_date, get_lastpostdate( 'blog', 'post' ) );
$this->assertEquals( $book_post_date, get_lastpostdate( 'blog', 'book' ) );
}
}

View File

@ -0,0 +1,67 @@
<?php
/**
* @group post
*/
class Tests_Post_GetLastPostModified extends WP_UnitTestCase {
/**
* @ticket 47777
*/
public function test_get_lastpostmodified() {
global $wpdb;
$post_post_date = '2020-01-30 16:09:28';
$post_post_modified = '2020-02-28 17:10:29';
$book_post_date = '2019-03-30 20:09:28';
$book_post_modified = '2019-04-30 21:10:29';
// Register book post type.
register_post_type( 'book', array( 'has_archive' => true ) );
// Create a simple post.
$simple_post_id = self::factory()->post->create(
array(
'post_title' => 'Simple Post',
'post_type' => 'post',
'post_date' => $post_post_date,
)
);
// Create custom type post.
$book_cpt_id = self::factory()->post->create(
array(
'post_title' => 'Book CPT',
'post_type' => 'book',
'post_date' => $book_post_date,
)
);
// Update `post_modified` and `post_modified_gmt`.
$wpdb->update(
$wpdb->posts,
array(
'post_modified' => $post_post_modified,
'post_modified_gmt' => $post_post_modified,
),
array(
'ID' => $simple_post_id,
)
);
$wpdb->update(
$wpdb->posts,
array(
'post_modified' => $book_post_modified,
'post_modified_gmt' => $book_post_modified,
),
array(
'ID' => $book_cpt_id,
)
);
$this->assertEquals( $post_post_modified, get_lastpostmodified( 'blog', 'post' ) );
$this->assertEquals( $book_post_modified, get_lastpostmodified( 'blog', 'book' ) );
}
}