Rewrite: Redirect attachment URLs when their slug changes.

Using the same logic that we use to redirect posts when their slug changes, we can provide the same functionality for attachments. Attachment pages are posts, too.

Props swissspdy.

Fixes #34043.



git-svn-id: https://develop.svn.wordpress.org/trunk@34685 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2015-09-29 04:57:02 +00:00
parent 0ab0525691
commit 070b65dcfc
4 changed files with 48 additions and 15 deletions

View File

@ -294,8 +294,9 @@ add_action( 'begin_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_
add_action( 'end_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_remove' );
// Redirect Old Slugs
add_action( 'template_redirect', 'wp_old_slug_redirect' );
add_action( 'post_updated', 'wp_check_for_changed_slugs', 12, 3 );
add_action( 'template_redirect', 'wp_old_slug_redirect' );
add_action( 'post_updated', 'wp_check_for_changed_slugs', 12, 3 );
add_action( 'attachment_updated', 'wp_check_for_changed_slugs', 12, 3 );
// Nonce check for Post Previews
add_action( 'init', '_show_post_preview' );

View File

@ -1330,7 +1330,7 @@ function _post_type_meta_capabilities( $capabilities = null ) {
* @since 4.3.0 Added the `featured_image`, `set_featured_image`, `remove_featured_image`,
* and `use_featured_image` labels.
* @since 4.4.0 Added the `insert_into_item` and `uploaded_to_this_item` labels.
*
*
* @access private
*
* @param object $post_type_object Post type object.
@ -3280,6 +3280,18 @@ function wp_insert_post( $postarr, $wp_error = false ) {
* @param int $post_ID Attachment ID.
*/
do_action( 'edit_attachment', $post_ID );
$post_after = get_post( $post_ID );
/**
* Fires once an existing attachment has been updated.
*
* @since 4.4.0
*
* @param int $post_ID Post ID.
* @param WP_Post $post_after Post object following the update.
* @param WP_Post $post_before Post object before the update.
*/
do_action( 'attachment_updated', $post_ID, $post_after, $post_before );
} else {
/**
@ -5133,23 +5145,27 @@ function wp_mime_type_icon( $mime = 0 ) {
* @param WP_Post $post_before The Previous Post Object
*/
function wp_check_for_changed_slugs( $post_id, $post, $post_before ) {
// Don't bother if it hasnt changed.
if ( $post->post_name == $post_before->post_name )
// Don't bother if it hasn't changed.
if ( $post->post_name == $post_before->post_name ) {
return;
}
// We're only concerned with published, non-hierarchical objects.
if ( $post->post_status != 'publish' || is_post_type_hierarchical( $post->post_type ) )
if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) {
return;
}
$old_slugs = (array) get_post_meta($post_id, '_wp_old_slug');
$old_slugs = (array) get_post_meta( $post_id, '_wp_old_slug' );
// If we haven't added this old slug before, add it now.
if ( !empty( $post_before->post_name ) && !in_array($post_before->post_name, $old_slugs) )
add_post_meta($post_id, '_wp_old_slug', $post_before->post_name);
if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs ) ) {
add_post_meta( $post_id, '_wp_old_slug', $post_before->post_name );
}
// If the new slug was used previously, delete it from the list.
if ( in_array($post->post_name, $old_slugs) )
delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
if ( in_array( $post->post_name, $old_slugs ) ) {
delete_post_meta( $post_id, '_wp_old_slug', $post->post_name );
}
}
/**

View File

@ -4736,12 +4736,15 @@ function wp_old_slug_redirect() {
global $wpdb;
// Guess the current post_type based on the query vars.
if ( get_query_var('post_type') )
$post_type = get_query_var('post_type');
elseif ( !empty($wp_query->query_vars['pagename']) )
if ( get_query_var( 'post_type' ) ) {
$post_type = get_query_var( 'post_type' );
} elseif ( get_query_var( 'attachment' ) ) {
$post_type = 'attachment';
} elseif ( ! empty( $wp_query->query_vars['pagename'] ) ) {
$post_type = 'page';
else
} else {
$post_type = 'post';
}
if ( is_array( $post_type ) ) {
if ( count( $post_type ) > 1 )

View File

@ -121,6 +121,19 @@ class Tests_Rewrite_OldSlugRedirect extends WP_UnitTestCase {
wp_old_slug_redirect();
$this->assertNull( $this->old_slug_redirect_url );
$this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' );
$old_permalink = get_attachment_link( $attachment_id );
wp_update_post( array(
'ID' => $attachment_id,
'post_name' => 'the-attachment',
) );
$permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'the-attachment' );
$this->go_to( $old_permalink );
wp_old_slug_redirect();
$this->assertEquals( $permalink, $this->old_slug_redirect_url );
}
public function test_old_slug_redirect_paged() {