Make WordPress Core

Changeset 49985


Ignore:
Timestamp:
01/20/2021 04:39:24 AM (3 years ago)
Author:
peterwilsoncc
Message:

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.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r49965 r49985  
    902902    }
    903903
    904     if ( 'attachment' === $post->post_type ) {
    905         if ( 'private' === $post->post_status ) {
    906             return 'private';
    907         }
    908 
    909         // Unattached attachments are assumed to be published.
    910         if ( ( 'inherit' === $post->post_status ) && ( 0 == $post->post_parent ) ) {
    911             return 'publish';
    912         }
    913 
    914         // Inherit status from the parent.
    915         if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) {
    916             $parent_post_status = get_post_status( $post->post_parent );
    917             if ( 'trash' === $parent_post_status ) {
    918                 return get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );
    919             } else {
    920                 return $parent_post_status;
     904    $post_status = $post->post_status;
     905
     906    if (
     907        'attachment' === $post->post_type &&
     908        'inherit' === $post_status
     909    ) {
     910        // Attachment permitted statuses: 'inherit', , see wp_insert_post().
     911
     912        if (
     913            0 === $post->post_parent ||
     914            ! get_post( $post->post_parent ) ||
     915            $post->ID === $post->post_parent
     916        ) {
     917            // Unattached attachments with inherit status are assumed to be published.
     918            $post_status = 'publish';
     919        } elseif ( 'trash' === get_post_status( $post->post_parent ) ) {
     920            // Get parent status prior to trashing.
     921            $post_status = get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );
     922            if ( ! $post_status ) {
     923                // Assume publish as above.
     924                $post_status = 'publish';
    921925            }
    922         }
     926        } else {
     927            $post_status = get_post_status( $post->post_parent );
     928        }
     929    } elseif (
     930        'attachment' === $post->post_type &&
     931        ! in_array( $post_status, array( 'private', 'trash', 'auto-draft' ), true )
     932    ) {
     933        /*
     934         * Ensure uninherited attachments have a permitted status either 'private', 'trash', 'auto-draft'.
     935         * This is to match the logic in wp_insert_post().
     936         *
     937         * Note: 'inherit' is excluded from this check as it is resolved to the parent post's
     938         * status in the logic block above.
     939         */
     940        $post_status = 'publish';
    923941    }
    924942
     
    927945     *
    928946     * @since 4.4.0
     947     * @since 5.7.0 The attachment post type is now passed through this filter.
    929948     *
    930949     * @param string  $post_status The post status.
    931950     * @param WP_Post $post        The post object.
    932951     */
    933     return apply_filters( 'get_post_status', $post->post_status, $post );
     952    return apply_filters( 'get_post_status', $post_status, $post );
    934953}
    935954
Note: See TracChangeset for help on using the changeset viewer.