Media: Ensure get_post_status() returns correct result for attachments.

Prevent `get_post_status()` returning `false` for attachments if the parent post has been deleted. The returned attachment post status is now passed through the `get_post_status` filter.

Add tests for `get_post_status()`.

Props peterwilsoncc, timothyblynjacobs for review.
Fixes #52326.


Built from https://develop.svn.wordpress.org/trunk@49985


git-svn-id: http://core.svn.wordpress.org/trunk@49686 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2021-01-20 04:41:03 +00:00
parent 777f752c2d
commit 9c2c78b6c8
2 changed files with 36 additions and 17 deletions

View File

@ -901,36 +901,55 @@ function get_post_status( $post = null ) {
return false;
}
if ( 'attachment' === $post->post_type ) {
if ( 'private' === $post->post_status ) {
return 'private';
}
$post_status = $post->post_status;
// Unattached attachments are assumed to be published.
if ( ( 'inherit' === $post->post_status ) && ( 0 == $post->post_parent ) ) {
return 'publish';
}
if (
'attachment' === $post->post_type &&
'inherit' === $post_status
) {
// Attachment permitted statuses: 'inherit', , see wp_insert_post().
// Inherit status from the parent.
if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) {
$parent_post_status = get_post_status( $post->post_parent );
if ( 'trash' === $parent_post_status ) {
return get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );
} else {
return $parent_post_status;
if (
0 === $post->post_parent ||
! get_post( $post->post_parent ) ||
$post->ID === $post->post_parent
) {
// Unattached attachments with inherit status are assumed to be published.
$post_status = 'publish';
} elseif ( 'trash' === get_post_status( $post->post_parent ) ) {
// Get parent status prior to trashing.
$post_status = get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );
if ( ! $post_status ) {
// Assume publish as above.
$post_status = 'publish';
}
} else {
$post_status = get_post_status( $post->post_parent );
}
} elseif (
'attachment' === $post->post_type &&
! in_array( $post_status, array( 'private', 'trash', 'auto-draft' ), true )
) {
/*
* Ensure uninherited attachments have a permitted status either 'private', 'trash', 'auto-draft'.
* This is to match the logic in wp_insert_post().
*
* Note: 'inherit' is excluded from this check as it is resolved to the parent post's
* status in the logic block above.
*/
$post_status = 'publish';
}
/**
* Filters the post status.
*
* @since 4.4.0
* @since 5.7.0 The attachment post type is now passed through this filter.
*
* @param string $post_status The post status.
* @param WP_Post $post The post object.
*/
return apply_filters( 'get_post_status', $post->post_status, $post );
return apply_filters( 'get_post_status', $post_status, $post );
}
/**

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.7-alpha-49984';
$wp_version = '5.7-alpha-49985';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.