diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index c50a9d949c..6897c2ed53 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -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' ); diff --git a/src/wp-includes/post-functions.php b/src/wp-includes/post-functions.php index cbe464f967..17d3d8ce0a 100644 --- a/src/wp-includes/post-functions.php +++ b/src/wp-includes/post-functions.php @@ -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 ); + } } /** diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 28cf56f74a..cc48edda86 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -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 ) diff --git a/tests/phpunit/tests/rewrite/oldSlugRedirect.php b/tests/phpunit/tests/rewrite/oldSlugRedirect.php index a166d432be..5e8cdda2ab 100644 --- a/tests/phpunit/tests/rewrite/oldSlugRedirect.php +++ b/tests/phpunit/tests/rewrite/oldSlugRedirect.php @@ -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() {