Make WordPress Core


Ignore:
Timestamp:
11/17/2020 03:27:07 AM (6 years ago)
Author:
peterwilsoncc
Message:

Permalinks: Prevent attachment pages 404ing following [49563].

This largely reverts [49563] due to attachment pages returning 404: File not found errors when they use the inherit status.

Permalink changes to attachment pages are retained when they are descendants of trashed or deleted posts.

Props Toro_Unit, helen, johnbillion, peterwilsoncc.
Fixes #51776.
See #5272.

File:
1 edited

Legend:

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

    r49563 r49622  
    7878        $redirect     = $original;
    7979        $redirect_url = false;
    80         $redirect_obj = false;
    8180
    8281        // Notice fixing.
     
    104103        if ( is_feed() && $post_id ) {
    105104                $redirect_url = get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
    106                 $redirect_obj = get_post( $post_id );
    107105
    108106                if ( $redirect_url ) {
     
    129127
    130128                        $redirect_url = get_permalink( $post_id );
    131                         $redirect_obj = get_post( $post_id );
    132129
    133130                        if ( $redirect_url ) {
     
    154151                        if ( $post_type_obj->public && 'auto-draft' !== $redirect_post->post_status ) {
    155152                                $redirect_url = get_permalink( $redirect_post );
    156                                 $redirect_obj = get_post( $redirect_post );
    157153
    158154                                $redirect['query'] = _remove_qs_args_if_not_in_url(
     
    202198                        if ( $post_id ) {
    203199                                $redirect_url = get_permalink( $post_id );
    204                                 $redirect_obj = get_post( $post_id );
    205200
    206201                                $redirect['path']  = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
     
    229224                        if ( ! empty( $_GET['attachment_id'] ) ) {
    230225                                $redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) );
    231                                 $redirect_obj = get_post( get_query_var( 'attachment_id' ) );
    232226
    233227                                if ( $redirect_url ) {
     
    236230                        } else {
    237231                                $redirect_url = get_attachment_link();
    238                                 $redirect_obj = get_post();
    239232                        }
    240233                } elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) {
    241234                        $redirect_url = get_permalink( get_query_var( 'p' ) );
    242                         $redirect_obj = get_post( get_query_var( 'p' ) );
    243235
    244236                        if ( $redirect_url ) {
     
    247239                } elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) {
    248240                        $redirect_url = get_permalink( $wp_query->get_queried_object_id() );
    249                         $redirect_obj = get_post( $wp_query->get_queried_object_id() );
    250241
    251242                        if ( $redirect_url ) {
     
    254245                } elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) {
    255246                        $redirect_url = get_permalink( get_query_var( 'page_id' ) );
    256                         $redirect_obj = get_post( get_query_var( 'page_id' ) );
    257247
    258248                        if ( $redirect_url ) {
     
    267257                ) {
    268258                        $redirect_url = get_permalink( get_option( 'page_for_posts' ) );
    269                         $redirect_obj = get_post( get_option( 'page_for_posts' ) );
    270259
    271260                        if ( $redirect_url ) {
     
    322311                        ) {
    323312                                $redirect_url = get_author_posts_url( $author->ID, $author->user_nicename );
    324                                 $redirect_obj = $author;
    325313
    326314                                if ( $redirect_url ) {
     
    398386                                ) {
    399387                                        $redirect_url = get_permalink( $wp_query->get_queried_object_id() );
    400                                         $redirect_obj = get_post( $wp_query->get_queried_object_id() );
    401388                                }
    402389                        }
     
    409396                        if ( ! $redirect_url ) {
    410397                                $redirect_url = get_permalink( get_queried_object_id() );
    411                                 $redirect_obj = get_post( get_queried_object_id() );
    412398                        }
    413399
     
    755741        }
    756742
    757         if (
    758                 $redirect_obj &&
    759                 is_a( $redirect_obj, 'WP_Post' )
    760         ) {
    761                 $post_status_obj = get_post_status_object( get_post_status( $redirect_obj ) );
    762                 if (
    763                         // Unviewable post types are never redirected.
    764                         ! is_post_type_viewable( $redirect_obj->post_type ) ||
    765                         // Internal or protected posts never redirect.
    766                         $post_status_obj->internal ||
    767                         $post_status_obj->protected ||
    768                         (
    769                                 // Don't redirect a non-public post...
    770                                 ! $post_status_obj->public &&
    771                                 (
    772                                         // ...unless it's private and the logged in user has access.
    773                                         $post_status_obj->private &&
    774                                         ! current_user_can( 'read_post', $redirect_obj->ID )
    775                                 )
    776                         )
    777                 ) {
    778                         $redirect_obj = false;
    779                         $redirect_url = false;
    780                 }
    781         }
    782 
    783743        /**
    784744         * Filters the canonical redirect URL.
Note: See TracChangeset for help on using the changeset viewer.