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.



git-svn-id: https://develop.svn.wordpress.org/trunk@49622 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2020-11-17 03:27:07 +00:00
parent 828ee6c1ed
commit 64ad8eb332
4 changed files with 78 additions and 1022 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

@ -1,970 +0,0 @@
<?php
/**
* @group canonical
* @group rewrite
* @group query
*/
class Tests_Canonical_PostStatus extends WP_Canonical_UnitTestCase {
/**
* User IDs.
*
* @var array
*/
public static $users;
/**
* Post Objects.
*
* @var array
*/
public static $posts;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::setup_custom_types();
self::$users = array(
'anon' => 0,
'subscriber' => $factory->user->create( array( 'role' => 'subscriber' ) ),
'content_author' => $factory->user->create( array( 'role' => 'author' ) ),
'editor' => $factory->user->create( array( 'role' => 'editor' ) ),
);
$post_statuses = array( 'publish', 'future', 'draft', 'pending', 'private', 'auto-draft', 'a-private-status' );
foreach ( $post_statuses as $post_status ) {
$post_date = '';
if ( 'future' === $post_status ) {
$post_date = strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
}
self::$posts[ $post_status ] = $factory->post->create_and_get(
array(
'post_type' => 'post',
'post_title' => "$post_status post",
'post_name' => "$post_status-post",
'post_status' => $post_status,
'post_content' => "Prevent canonical redirect exposing post slugs.\n\n<!--nextpage-->Page 2",
'post_author' => self::$users['content_author'],
'post_date' => $post_date,
)
);
// Add fake attachment to the post (file upload not needed).
self::$posts[ "$post_status-attachment" ] = $factory->post->create_and_get(
array(
'post_type' => 'attachment',
'post_title' => "$post_status inherited attachment",
'post_name' => "$post_status-inherited-attachment",
'post_status' => 'inherit',
'post_content' => "Prevent canonical redirect exposing post via attachments.\n\n<!--nextpage-->Page 2",
'post_author' => self::$users['content_author'],
'post_parent' => self::$posts[ $post_status ]->ID,
'post_date' => $post_date,
)
);
// Set up a page with same.
self::$posts[ "$post_status-page" ] = $factory->post->create_and_get(
array(
'post_type' => 'page',
'post_title' => "$post_status page",
'post_name' => "$post_status-page",
'post_status' => $post_status,
'post_content' => "Prevent canonical redirect exposing page slugs.\n\n<!--nextpage-->Page 2",
'post_author' => self::$users['content_author'],
'post_date' => $post_date,
)
);
}
// Create a public CPT using a private status.
self::$posts['a-public-cpt'] = $factory->post->create_and_get(
array(
'post_type' => 'a-public-cpt',
'post_title' => 'a-public-cpt',
'post_name' => 'a-public-cpt',
'post_status' => 'private',
'post_content' => 'Prevent canonical redirect exposing a-public-cpt titles.',
'post_author' => self::$users['content_author'],
)
);
// Add fake attachment to the public cpt (file upload not needed).
self::$posts['a-public-cpt-attachment'] = $factory->post->create_and_get(
array(
'post_type' => 'attachment',
'post_title' => 'a-public-cpt post inherited attachment',
'post_name' => 'a-public-cpt-inherited-attachment',
'post_status' => 'inherit',
'post_content' => "Prevent canonical redirect exposing post via attachments.\n\n<!--nextpage-->Page 2",
'post_author' => self::$users['content_author'],
'post_parent' => self::$posts['a-public-cpt']->ID,
)
);
// Create a private CPT with a public status.
self::$posts['a-private-cpt'] = $factory->post->create_and_get(
array(
'post_type' => 'a-private-cpt',
'post_title' => 'a-private-cpt',
'post_name' => 'a-private-cpt',
'post_status' => 'publish',
'post_content' => 'Prevent canonical redirect exposing a-private-cpt titles.',
'post_author' => self::$users['content_author'],
)
);
// Add fake attachment to the private cpt (file upload not needed).
self::$posts['a-private-cpt-attachment'] = $factory->post->create_and_get(
array(
'post_type' => 'attachment',
'post_title' => 'a-private-cpt post inherited attachment',
'post_name' => 'a-private-cpt-inherited-attachment',
'post_status' => 'inherit',
'post_content' => "Prevent canonical redirect exposing post via attachments.\n\n<!--nextpage-->Page 2",
'post_author' => self::$users['content_author'],
'post_parent' => self::$posts['a-private-cpt']->ID,
)
);
// Post for trashing.
self::$posts['trash'] = $factory->post->create_and_get(
array(
'post_type' => 'post',
'post_title' => 'trash post',
'post_name' => 'trash-post',
'post_status' => 'publish',
'post_content' => "Prevent canonical redirect exposing post slugs.\n\n<!--nextpage-->Page 2",
'post_author' => self::$users['content_author'],
)
);
self::$posts['trash-attachment'] = $factory->post->create_and_get(
array(
'post_type' => 'attachment',
'post_title' => 'trash post inherited attachment',
'post_name' => 'trash-post-inherited-attachment',
'post_status' => 'inherit',
'post_content' => "Prevent canonical redirect exposing post via attachments.\n\n<!--nextpage-->Page 2",
'post_author' => self::$users['content_author'],
'post_parent' => self::$posts['trash']->ID,
)
);
// Page for trashing.
self::$posts['trash-page'] = $factory->post->create_and_get(
array(
'post_type' => 'page',
'post_title' => 'trash page',
'post_name' => 'trash-page',
'post_status' => 'publish',
'post_content' => "Prevent canonical redirect exposing page slugs.\n\n<!--nextpage-->Page 2",
'post_author' => self::$users['content_author'],
)
);
wp_trash_post( self::$posts['trash']->ID );
wp_trash_post( self::$posts['trash-page']->ID );
}
function setUp() {
parent::setUp();
self::setup_custom_types();
}
/**
* Set up a custom post type and private status.
*
* This needs to be called both in the class setup and
* test setup.
*/
public static function setup_custom_types() {
// Register public custom post type.
register_post_type(
'a-public-cpt',
array(
'public' => true,
'rewrite' => array(
'slug' => 'a-public-cpt',
),
)
);
// Register private custom post type.
register_post_type(
'a-private-cpt',
array(
'public' => false,
'rewrite' => array(
'slug' => 'a-private-cpt',
),
)
);
// Register custom private post status.
register_post_status(
'a-private-status',
array(
'private' => true,
)
);
}
/**
* Test canonical redirect does not reveal private posts presence.
*
* @ticket 5272
* @dataProvider data_canonical_redirects_to_ugly_permalinks
*
* @param string $post_key Post key used for creating fixtures.
* @param string $user_role User role.
* @param string $requested Requested URL.
* @param string $expected Expected URL.
*/
public function test_canonical_redirects_to_ugly_permalinks( $post_key, $user_role, $requested, $expected ) {
wp_set_current_user( self::$users[ $user_role ] );
$this->set_permalink_structure( '' );
$post = self::$posts[ $post_key ];
clean_post_cache( $post->ID );
/*
* The dataProvider runs before the fixures are set up, therefore the
* post object IDs are placeholders that needs to be replaced.
*/
$requested = str_replace( '%ID%', $post->ID, $requested );
$expected = str_replace( '%ID%', $post->ID, $expected );
$this->assertCanonical( $requested, $expected );
}
/**
* Data provider for test_canonical_redirects_to_ugly_permalinks.
*
* @return array[] Array of arguments for tests {
* @type string $post_key Post key used for creating fixtures.
* @type string $user_role User role.
* @type string $requested Requested URL.
* @type string $expected Expected URL.
* }
*/
function data_canonical_redirects_to_ugly_permalinks() {
$data = array();
$all_user_list = array( 'anon', 'subscriber', 'content_author', 'editor' );
$select_allow_list = array( 'content_author', 'editor' );
$select_block_list = array( 'anon', 'subscriber' );
// All post/page keys
$all_user_post_status_keys = array( 'publish' );
$select_user_post_status_keys = array( 'private', 'a-private-status' );
$no_user_post_status_keys = array( 'future', 'draft', 'pending', 'auto-draft' ); // Excludes trash for attachment rules.
$select_user_post_type_keys = array( 'a-public-cpt' );
$no_user_post_type_keys = array( 'a-private-cpt' );
foreach ( $all_user_post_status_keys as $post_key ) {
foreach ( $all_user_list as $user ) {
/*
* In the event `redirect_canonical()` is updated to redirect ugly permalinks
* to a canonical ugly version, these expected values can be changed.
*/
$data[] = array(
"$post_key-page",
$user,
'/?post_type=page&p=%ID%',
'/?post_type=page&p=%ID%',
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key-post",
"/?name=$post_key-post",
);
// Ensure rss redirects to rss2.
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
'/?feed=rss2&p=%ID%',
);
// Ensure rss redirects to rss2.
$data[] = array(
"$post_key-page",
$user,
'/?feed=rss&page_id=%ID%',
'/?feed=rss2&page_id=%ID%',
);
}
}
foreach ( $select_user_post_status_keys as $post_key ) {
foreach ( $select_allow_list as $user ) {
/*
* In the event `redirect_canonical()` is updated to redirect ugly permalinks
* to a canonical ugly version, these expected values can be changed.
*/
$data[] = array(
"$post_key-page",
$user,
'/?post_type=page&p=%ID%',
'/?post_type=page&p=%ID%',
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key-post",
"/?name=$post_key-post",
);
// Ensure rss redirects to rss2.
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
'/?feed=rss2&p=%ID%',
);
// Ensure rss redirects to rss2.
$data[] = array(
"$post_key-page",
$user,
'/?feed=rss&page_id=%ID%',
'/?feed=rss2&page_id=%ID%',
);
}
foreach ( $select_block_list as $user ) {
/*
* In the event `redirect_canonical()` is updated to redirect ugly permalinks
* to a canonical ugly version, these expected values MUST NOT be changed.
*/
$data[] = array(
"$post_key-page",
$user,
'/?post_type=page&p=%ID%',
'/?post_type=page&p=%ID%',
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key-post",
"/?name=$post_key-post",
);
// Ensure post's existence is not demonstrated by changing rss to rss2.
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
'/?feed=rss&p=%ID%',
);
// Ensure post's existence is not demonstrated by changing rss to rss2.
$data[] = array(
"$post_key-page",
$user,
'/?feed=rss&page_id=%ID%',
'/?feed=rss&page_id=%ID%',
);
}
}
foreach ( $no_user_post_status_keys as $post_key ) {
foreach ( $all_user_list as $user ) {
/*
* In the event `redirect_canonical()` is updated to redirect ugly permalinks
* to a canonical ugly version, these expected values MUST NOT be changed.
*/
$data[] = array(
"$post_key-page",
$user,
'/?post_type=page&p=%ID%',
'/?post_type=page&p=%ID%',
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key-post",
"/?name=$post_key-post",
);
// Ensure post's existence is not demonstrated by changing rss to rss2.
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
'/?feed=rss&p=%ID%',
);
// Ensure post's existence is not demonstrated by changing rss to rss2.
$data[] = array(
"$post_key-page",
$user,
'/?feed=rss&page_id=%ID%',
'/?feed=rss&page_id=%ID%',
);
}
}
foreach ( array( 'trash' ) as $post_key ) {
foreach ( $all_user_list as $user ) {
/*
* In the event `redirect_canonical()` is updated to redirect ugly permalinks
* to a canonical ugly version, these expected values MUST NOT be changed.
*/
$data[] = array(
"$post_key-page",
$user,
'/?post_type=page&p=%ID%',
'/?post_type=page&p=%ID%',
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key-post",
"/?name=$post_key-post",
);
// Ensure post's existence is not demonstrated by changing rss to rss2.
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
'/?feed=rss&p=%ID%',
);
// Ensure post's existence is not demonstrated by changing rss to rss2.
$data[] = array(
"$post_key-page",
$user,
'/?feed=rss&page_id=%ID%',
'/?feed=rss&page_id=%ID%',
);
}
}
foreach ( $select_user_post_type_keys as $post_key ) {
foreach ( $select_allow_list as $user ) {
$data[] = array(
$post_key,
$user,
'/?p=%ID%',
'/?a-public-cpt=a-public-cpt',
);
$data[] = array(
"$post_key-attachment",
$user,
'/?attachment_id=%ID%',
'/?attachment_id=%ID%',
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key&post_type=$post_key",
"/?name=$post_key&post_type=$post_key",
);
// Ensure rss is replaced by rss2.
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
'/?a-public-cpt=a-public-cpt&feed=rss2',
);
}
foreach ( $select_block_list as $user ) {
$data[] = array(
$post_key,
$user,
'/?p=%ID%',
'/?p=%ID%',
);
$data[] = array(
"$post_key-attachment",
$user,
'/?attachment_id=%ID%',
'/?attachment_id=%ID%',
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key&post_type=$post_key",
"/?name=$post_key&post_type=$post_key",
);
// Ensure rss is not replaced with rss2.
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
'/?feed=rss&p=%ID%',
);
}
}
foreach ( $no_user_post_type_keys as $post_key ) {
foreach ( $all_user_list as $user ) {
$data[] = array(
$post_key,
$user,
'/?p=%ID%',
'/?p=%ID%',
);
$data[] = array(
"$post_key-attachment",
$user,
'/?attachment_id=%ID%',
'/?attachment_id=%ID%',
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key&post_type=$post_key",
"/?name=$post_key&post_type=$post_key",
);
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
'/?feed=rss&p=%ID%',
);
}
}
return $data;
}
/**
* Test canonical redirect does not reveal private slugs.
*
* @ticket 5272
* @dataProvider data_canonical_redirects_to_pretty_permalinks
*
* @param string $post_key Post key used for creating fixtures.
* @param string $user_role User role.
* @param string $requested Requested URL.
* @param string $expected Expected URL.
*/
public function test_canonical_redirects_to_pretty_permalinks( $post_key, $user_role, $requested, $expected ) {
wp_set_current_user( self::$users[ $user_role ] );
$this->set_permalink_structure( '/%postname%/' );
$post = self::$posts[ $post_key ];
clean_post_cache( $post->ID );
/*
* The dataProvider runs before the fixures are set up, therefore the
* post object IDs are placeholders that needs to be replaced.
*/
$requested = str_replace( '%ID%', $post->ID, $requested );
$expected = str_replace( '%ID%', $post->ID, $expected );
$this->assertCanonical( $requested, $expected );
}
/**
* Data provider for test_canonical_redirects_to_pretty_permalinks.
*
* @return array[] Array of arguments for tests {
* @type string $post_key Post key used for creating fixtures.
* @type string $user_role User role.
* @type string $requested Requested URL.
* @type string $expected Expected URL.
* }
*/
function data_canonical_redirects_to_pretty_permalinks() {
$data = array();
$all_user_list = array( 'anon', 'subscriber', 'content_author', 'editor' );
$select_allow_list = array( 'content_author', 'editor' );
$select_block_list = array( 'anon', 'subscriber' );
// All post/page keys
$all_user_post_status_keys = array( 'publish' );
$select_user_post_status_keys = array( 'private', 'a-private-status' );
$no_user_post_status_keys = array( 'future', 'draft', 'pending', 'auto-draft' ); // Excludes trash for attachment rules.
$select_user_post_type_keys = array( 'a-public-cpt' );
$no_user_post_type_keys = array( 'a-private-cpt' );
foreach ( $all_user_post_status_keys as $post_key ) {
foreach ( $all_user_list as $user ) {
$data[] = array(
$post_key,
$user,
'/?p=%ID%',
"/$post_key-post/",
);
$data[] = array(
"$post_key-attachment",
$user,
'/?attachment_id=%ID%',
"/$post_key-post/$post_key-inherited-attachment/",
);
$data[] = array(
"$post_key-page",
$user,
'/?post_type=page&p=%ID%',
"/$post_key-page/",
);
$data[] = array(
"$post_key-page",
$user,
'/?page_id=%ID%',
"/$post_key-page/",
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key-post",
"/$post_key-post/",
);
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
"/$post_key-post/feed/",
);
$data[] = array(
"$post_key-page",
$user,
'/?feed=rss&page_id=%ID%',
"/$post_key-page/feed/",
);
}
}
foreach ( $select_user_post_status_keys as $post_key ) {
foreach ( $select_allow_list as $user ) {
$data[] = array(
$post_key,
$user,
'/?p=%ID%',
"/$post_key-post/",
);
$data[] = array(
"$post_key-attachment",
$user,
'/?attachment_id=%ID%',
"/$post_key-post/$post_key-inherited-attachment/",
);
$data[] = array(
"$post_key-page",
$user,
'/?post_type=page&p=%ID%',
"/$post_key-page/",
);
$data[] = array(
"$post_key-page",
$user,
'/?page_id=%ID%',
"/$post_key-page/",
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key-post",
"/$post_key-post/",
);
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
"/$post_key-post/feed/",
);
$data[] = array(
"$post_key-page",
$user,
'/?feed=rss&page_id=%ID%',
"/$post_key-page/feed/",
);
}
foreach ( $select_block_list as $user ) {
$data[] = array(
$post_key,
$user,
'/?p=%ID%',
'/?p=%ID%',
);
$data[] = array(
"$post_key-attachment",
$user,
'/?attachment_id=%ID%',
'/?attachment_id=%ID%',
);
$data[] = array(
"$post_key-page",
$user,
'/?post_type=page&p=%ID%',
'/?post_type=page&p=%ID%',
);
$data[] = array(
"$post_key-page",
$user,
'/?page_id=%ID%',
'/?page_id=%ID%',
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key-post",
"/?name=$post_key-post",
);
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
'/?feed=rss&p=%ID%',
);
$data[] = array(
"$post_key-page",
$user,
'/?feed=rss&page_id=%ID%',
'/?feed=rss&page_id=%ID%',
);
}
}
foreach ( $select_user_post_type_keys as $post_key ) {
foreach ( $select_allow_list as $user ) {
$data[] = array(
$post_key,
$user,
'/?p=%ID%',
"/$post_key/$post_key/",
);
$data[] = array(
"$post_key-attachment",
$user,
'/?attachment_id=%ID%',
"/$post_key/$post_key/$post_key-inherited-attachment/",
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key&post_type=$post_key",
"/$post_key/$post_key/?post_type=$post_key",
);
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
"/$post_key/$post_key/feed/",
);
}
foreach ( $select_block_list as $user ) {
$data[] = array(
$post_key,
$user,
'/?p=%ID%',
'/?p=%ID%',
);
$data[] = array(
"$post_key-attachment",
$user,
'/?attachment_id=%ID%',
'/?attachment_id=%ID%',
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key&post_type=$post_key",
"/?name=$post_key&post_type=$post_key",
);
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
'/?feed=rss&p=%ID%',
);
}
}
foreach ( $no_user_post_type_keys as $post_key ) {
foreach ( $all_user_list as $user ) {
$data[] = array(
$post_key,
$user,
'/?p=%ID%',
'/?p=%ID%',
);
$data[] = array(
"$post_key-attachment",
$user,
'/?attachment_id=%ID%',
"/$post_key-inherited-attachment/",
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key&post_type=$post_key",
"/?name=$post_key&post_type=$post_key",
);
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
'/?feed=rss&p=%ID%',
);
}
}
foreach ( $no_user_post_status_keys as $post_key ) {
foreach ( $all_user_list as $user ) {
$data[] = array(
$post_key,
$user,
'/?p=%ID%',
'/?p=%ID%',
);
$data[] = array(
"$post_key-attachment",
$user,
'/?attachment_id=%ID%',
'/?attachment_id=%ID%',
);
$data[] = array(
"$post_key-page",
$user,
'/?post_type=page&p=%ID%',
'/?post_type=page&p=%ID%',
);
$data[] = array(
"$post_key-page",
$user,
'/?page_id=%ID%',
'/?page_id=%ID%',
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key-post",
"/?name=$post_key-post",
);
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
'/?feed=rss&p=%ID%',
);
$data[] = array(
"$post_key-page",
$user,
'/?feed=rss&page_id=%ID%',
'/?feed=rss&page_id=%ID%',
);
}
}
foreach ( array( 'trash' ) as $post_key ) {
foreach ( $all_user_list as $user ) {
$data[] = array(
$post_key,
$user,
'/?p=%ID%',
'/?p=%ID%',
);
$data[] = array(
"$post_key-attachment",
$user,
'/?attachment_id=%ID%',
'/trash-post-inherited-attachment/',
);
$data[] = array(
"$post_key",
$user,
'/trash-post/trash-post-inherited-attachment/',
'/trash-post-inherited-attachment/',
);
$data[] = array(
"$post_key",
$user,
'/trash-post__trashed/trash-post-inherited-attachment/',
'/trash-post-inherited-attachment/',
);
$data[] = array(
"$post_key-page",
$user,
'/?post_type=page&p=%ID%',
'/?post_type=page&p=%ID%',
);
$data[] = array(
"$post_key-page",
$user,
'/?page_id=%ID%',
'/?page_id=%ID%',
);
$data[] = array(
$post_key,
$user,
"/?name=$post_key-post",
"/?name=$post_key-post",
);
$data[] = array(
$post_key,
$user,
'/?feed=rss&p=%ID%',
'/?feed=rss&p=%ID%',
);
$data[] = array(
"$post_key-page",
$user,
'/?feed=rss&page_id=%ID%',
'/?feed=rss&page_id=%ID%',
);
}
}
return $data;
}
}

View File

@ -8,6 +8,7 @@ class Tests_Media extends WP_UnitTestCase {
protected static $large_id;
protected static $_sizes;
protected static $large_filename = 'test-image-large.jpg';
protected static $post_ids;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$_sizes = wp_get_additional_image_sizes();
@ -15,6 +16,35 @@ class Tests_Media extends WP_UnitTestCase {
$filename = DIR_TESTDATA . '/images/' . self::$large_filename;
self::$large_id = $factory->attachment->create_upload_object( $filename );
$post_statuses = array( 'publish', 'future', 'draft', 'auto-draft', 'trash' );
foreach ( $post_statuses as $post_status ) {
$date = '';
if ( 'future' === $post_status ) {
strftime( '%Y-%m-%d %H:%M:%S', strtotime( '+1 year' ) );
}
self::$post_ids[ $post_status ] = $factory->post->create(
array(
'post_status' => 'trash' === $post_status ? 'publish' : $post_status,
'post_date' => $date,
'post_name' => "$post_status-post",
)
);
// Attachments without media.
self::$post_ids[ "$post_status-attachment" ] = $factory->attachment->create_object(
array(
'post_parent' => self::$post_ids[ $post_status ],
'post_status' => 'inherit',
'post_name' => "$post_status-attachment",
'post_date' => $date,
)
);
}
// Trash the trash post.
wp_trash_post( self::$post_ids['trash'] );
}
public static function wpTearDownAfterClass() {
@ -3023,6 +3053,50 @@ EOF;
$this->assertNotContains( '<a ', $actual );
}
/**
* Test attachment permalinks based on parent post status.
*
* @dataProvider data_attachment_permalinks_based_on_parent_status
* @ticket 51776
*
* @param string $post_key Post as keyed in the shared fixture array.
* @param string $expected Expected result.
* @param bool $expected_404 Whether the page is expected to return a 404 result.
*
*/
function test_attachment_permalinks_based_on_parent_status( $post_key, $expected, $expected_404 ) {
$this->set_permalink_structure( '/%postname%' );
$post = get_post( self::$post_ids[ $post_key ] );
/*
* The dataProvider runs before the fixures are set up, therefore the
* post object IDs are placeholders that needs to be replaced.
*/
$expected = home_url( str_replace( '%ID%', $post->ID, $expected ) );
$this->assertSame( $expected, get_permalink( $post ) );
$this->go_to( get_permalink( $post ) );
$this->assertSame( $expected_404, is_404() );
}
/**
* Data provider for test_attachment_permalinks_based_on_parent_status().
*
* @return array[] {
* @type string $post_key Post as keyed in the shared fixture array.
* @type string $expected Expected result.
* $type bool $expected_404 Whether the page is expected to return a 404 result.
* }
*/
function data_attachment_permalinks_based_on_parent_status() {
return array(
array( 'draft-attachment', '/?attachment_id=%ID%', true ),
array( 'publish-attachment', '/publish-post/publish-attachment', false ),
array( 'future-attachment', '/future-post/future-attachment', false ),
array( 'auto-draft-attachment', '/?attachment_id=%ID%', true ),
array( 'trash-attachment', '/?attachment_id=%ID%', false ),
);
}
}
/**