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.


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


git-svn-id: http://core.svn.wordpress.org/trunk@49360 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2020-11-17 03:29:12 +00:00
parent 03eba7beb2
commit 30b79ebd28
3 changed files with 5 additions and 53 deletions

View File

@ -77,7 +77,6 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
$redirect = $original;
$redirect_url = false;
$redirect_obj = false;
// Notice fixing.
if ( ! isset( $redirect['path'] ) ) {
@ -103,7 +102,6 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
if ( is_feed() && $post_id ) {
$redirect_url = get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
$redirect_obj = get_post( $post_id );
if ( $redirect_url ) {
$redirect['query'] = _remove_qs_args_if_not_in_url(
@ -128,7 +126,6 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
}
$redirect_url = get_permalink( $post_id );
$redirect_obj = get_post( $post_id );
if ( $redirect_url ) {
$redirect['query'] = _remove_qs_args_if_not_in_url(
@ -153,7 +150,6 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
if ( $post_type_obj->public && 'auto-draft' !== $redirect_post->post_status ) {
$redirect_url = get_permalink( $redirect_post );
$redirect_obj = get_post( $redirect_post );
$redirect['query'] = _remove_qs_args_if_not_in_url(
$redirect['query'],
@ -201,7 +197,6 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
if ( $post_id ) {
$redirect_url = get_permalink( $post_id );
$redirect_obj = get_post( $post_id );
$redirect['path'] = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
$redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
@ -228,32 +223,27 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
) {
if ( ! empty( $_GET['attachment_id'] ) ) {
$redirect_url = get_attachment_link( get_query_var( 'attachment_id' ) );
$redirect_obj = get_post( get_query_var( 'attachment_id' ) );
if ( $redirect_url ) {
$redirect['query'] = remove_query_arg( 'attachment_id', $redirect['query'] );
}
} else {
$redirect_url = get_attachment_link();
$redirect_obj = get_post();
}
} elseif ( is_single() && ! empty( $_GET['p'] ) && ! $redirect_url ) {
$redirect_url = get_permalink( get_query_var( 'p' ) );
$redirect_obj = get_post( get_query_var( 'p' ) );
if ( $redirect_url ) {
$redirect['query'] = remove_query_arg( array( 'p', 'post_type' ), $redirect['query'] );
}
} elseif ( is_single() && ! empty( $_GET['name'] ) && ! $redirect_url ) {
$redirect_url = get_permalink( $wp_query->get_queried_object_id() );
$redirect_obj = get_post( $wp_query->get_queried_object_id() );
if ( $redirect_url ) {
$redirect['query'] = remove_query_arg( 'name', $redirect['query'] );
}
} elseif ( is_page() && ! empty( $_GET['page_id'] ) && ! $redirect_url ) {
$redirect_url = get_permalink( get_query_var( 'page_id' ) );
$redirect_obj = get_post( get_query_var( 'page_id' ) );
if ( $redirect_url ) {
$redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
@ -266,7 +256,6 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
&& 'page' === get_option( 'show_on_front' ) && get_query_var( 'page_id' ) === (int) get_option( 'page_for_posts' )
) {
$redirect_url = get_permalink( get_option( 'page_for_posts' ) );
$redirect_obj = get_post( get_option( 'page_for_posts' ) );
if ( $redirect_url ) {
$redirect['query'] = remove_query_arg( 'page_id', $redirect['query'] );
@ -321,7 +310,6 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
&& $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE $wpdb->posts.post_author = %d AND $wpdb->posts.post_status = 'publish' LIMIT 1", $author->ID ) )
) {
$redirect_url = get_author_posts_url( $author->ID, $author->user_nicename );
$redirect_obj = $author;
if ( $redirect_url ) {
$redirect['query'] = remove_query_arg( 'author', $redirect['query'] );
@ -397,7 +385,6 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
|| ! has_term( $category->term_id, 'category', $wp_query->get_queried_object_id() )
) {
$redirect_url = get_permalink( $wp_query->get_queried_object_id() );
$redirect_obj = get_post( $wp_query->get_queried_object_id() );
}
}
}
@ -408,7 +395,6 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
if ( ! $redirect_url ) {
$redirect_url = get_permalink( get_queried_object_id() );
$redirect_obj = get_post( get_queried_object_id() );
}
if ( $page > 1 ) {
@ -754,32 +740,6 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
$requested_url = preg_replace_callback( '|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url );
}
if (
$redirect_obj &&
is_a( $redirect_obj, 'WP_Post' )
) {
$post_status_obj = get_post_status_object( get_post_status( $redirect_obj ) );
if (
// Unviewable post types are never redirected.
! is_post_type_viewable( $redirect_obj->post_type ) ||
// Internal or protected posts never redirect.
$post_status_obj->internal ||
$post_status_obj->protected ||
(
// Don't redirect a non-public post...
! $post_status_obj->public &&
(
// ...unless it's private and the logged in user has access.
$post_status_obj->private &&
! current_user_can( 'read_post', $redirect_obj->ID )
)
)
) {
$redirect_obj = false;
$redirect_url = false;
}
}
/**
* Filters the canonical redirect URL.
*

View File

@ -164,7 +164,10 @@ function get_permalink( $post = 0, $leavename = false ) {
*/
$permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename );
if ( $permalink && ! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future' ), true ) ) {
if (
$permalink &&
! in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft', 'future', 'trash' ), true )
) {
$category = '';
if ( strpos( $permalink, '%category%' ) !== false ) {
@ -420,17 +423,6 @@ function get_attachment_link( $post = null, $leavename = false ) {
$parent = false;
}
if ( $parent ) {
$parent_status_obj = get_post_status_object( get_post_status( $post->post_parent ) );
if (
! is_post_type_viewable( get_post_type( $post->post_parent ) ) ||
$parent_status_obj->internal ||
$parent_status_obj->protected
) {
$parent = false;
}
}
if ( $wp_rewrite->using_permalinks() && $parent ) {
if ( 'page' === $parent->post_type ) {
$parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front.

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.6-beta4-49621';
$wp_version = '5.6-beta4-49622';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.