Make WordPress Core

Changeset 49622


Ignore:
Timestamp:
11/17/2020 03:27:07 AM (4 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.

Location:
trunk
Files:
1 deleted
3 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.
  • trunk/src/wp-includes/link-template.php

    r49563 r49622  
    165165    $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
    166166
    167     if ( $permalink && ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ), true ) ) {
     167    if (
     168        $permalink &&
     169        ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future', 'trash' ), true )
     170    ) {
    168171
    169172        $category = '';
     
    419422    if ( $parent && ! in_array( $parent->post_type, get_post_types(), true ) ) {
    420423        $parent = false;
    421     }
    422 
    423     if ( $parent ) {
    424         $parent_status_obj = get_post_status_object( get_post_status( $post->post_parent ) );
    425         if (
    426             ! is_post_type_viewable( get_post_type( $post->post_parent ) ) ||
    427             $parent_status_obj->internal ||
    428             $parent_status_obj->protected
    429         ) {
    430             $parent = false;
    431         }
    432424    }
    433425
  • trunk/tests/phpunit/tests/media.php

    r49603 r49622  
    99    protected static $_sizes;
    1010    protected static $large_filename = 'test-image-large.jpg';
     11    protected static $post_ids;
    1112
    1213    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     
    1617        $filename       = DIR_TESTDATA . '/images/' . self::$large_filename;
    1718        self::$large_id = $factory->attachment->create_upload_object( $filename );
     19
     20        $post_statuses = array( 'publish', 'future', 'draft', 'auto-draft', 'trash' );
     21        foreach ( $post_statuses as $post_status ) {
     22            $date = '';
     23            if ( 'future' === $post_status ) {
     24                strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
     25            }
     26
     27            self::$post_ids[ $post_status ] = $factory->post->create(
     28                array(
     29                    'post_status' => 'trash' === $post_status ? 'publish' : $post_status,
     30                    'post_date'   => $date,
     31                    'post_name'   => "$post_status-post",
     32                )
     33            );
     34
     35            // Attachments without media.
     36            self::$post_ids[ "$post_status-attachment" ] = $factory->attachment->create_object(
     37                array(
     38                    'post_parent' => self::$post_ids[ $post_status ],
     39                    'post_status' => 'inherit',
     40                    'post_name'   => "$post_status-attachment",
     41                    'post_date'   => $date,
     42                )
     43            );
     44        }
     45
     46        // Trash the trash post.
     47        wp_trash_post( self::$post_ids['trash'] );
    1848    }
    1949
     
    30243054    }
    30253055
     3056    /**
     3057     * Test attachment permalinks based on parent post status.
     3058     *
     3059     * @dataProvider data_attachment_permalinks_based_on_parent_status
     3060     * @ticket 51776
     3061     *
     3062     * @param string $post_key     Post as keyed in the shared fixture array.
     3063     * @param string $expected     Expected result.
     3064     * @param bool   $expected_404 Whether the page is expected to return a 404 result.
     3065     *
     3066     */
     3067    function test_attachment_permalinks_based_on_parent_status( $post_key, $expected, $expected_404 ) {
     3068        $this->set_permalink_structure( '/%postname%' );
     3069        $post = get_post( self::$post_ids[ $post_key ] );
     3070
     3071        /*
     3072         * The dataProvider runs before the fixures are set up, therefore the
     3073         * post object IDs are placeholders that needs to be replaced.
     3074         */
     3075        $expected = home_url( str_replace( '%ID%', $post->ID, $expected ) );
     3076
     3077        $this->assertSame( $expected, get_permalink( $post ) );
     3078        $this->go_to( get_permalink( $post ) );
     3079        $this->assertSame( $expected_404, is_404() );
     3080    }
     3081
     3082    /**
     3083     * Data provider for test_attachment_permalinks_based_on_parent_status().
     3084     *
     3085     * @return array[] {
     3086     *     @type string $post_key     Post as keyed in the shared fixture array.
     3087     *     @type string $expected     Expected result.
     3088     *     $type bool   $expected_404 Whether the page is expected to return a 404 result.
     3089     * }
     3090     */
     3091    function data_attachment_permalinks_based_on_parent_status() {
     3092        return array(
     3093            array( 'draft-attachment', '/?attachment_id=%ID%', true ),
     3094            array( 'publish-attachment', '/publish-post/publish-attachment', false ),
     3095            array( 'future-attachment', '/future-post/future-attachment', false ),
     3096            array( 'auto-draft-attachment', '/?attachment_id=%ID%', true ),
     3097            array( 'trash-attachment', '/?attachment_id=%ID%', false ),
     3098        );
     3099    }
    30263100}
    30273101
Note: See TracChangeset for help on using the changeset viewer.