Make WordPress Core

Changeset 34685


Ignore:
Timestamp:
09/29/2015 04:57:02 AM (11 years ago)
Author:
pento
Message:

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.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/default-filters.php

    r34561 r34685  
    295295
    296296// Redirect Old Slugs
    297 add_action( 'template_redirect', 'wp_old_slug_redirect'              );
    298 add_action( 'post_updated',      'wp_check_for_changed_slugs', 12, 3 );
     297add_action( 'template_redirect',  'wp_old_slug_redirect'              );
     298add_action( 'post_updated',       'wp_check_for_changed_slugs', 12, 3 );
     299add_action( 'attachment_updated', 'wp_check_for_changed_slugs', 12, 3 );
    299300
    300301// Nonce check for Post Previews
  • trunk/src/wp-includes/post-functions.php

    r34668 r34685  
    13311331 *              and `use_featured_image` labels.
    13321332 * @since 4.4.0 Added the `insert_into_item` and `uploaded_to_this_item` labels.
    1333  * 
     1333 *
    13341334 * @access private
    13351335 *
     
    32813281                         */
    32823282                        do_action( 'edit_attachment', $post_ID );
     3283                        $post_after = get_post( $post_ID );
     3284
     3285                        /**
     3286                         * Fires once an existing attachment has been updated.
     3287                         *
     3288                         * @since 4.4.0
     3289                         *
     3290                         * @param int     $post_ID      Post ID.
     3291                         * @param WP_Post $post_after   Post object following the update.
     3292                         * @param WP_Post $post_before  Post object before the update.
     3293                         */
     3294                        do_action( 'attachment_updated', $post_ID, $post_after, $post_before );
    32833295                } else {
    32843296
     
    51345146 */
    51355147function wp_check_for_changed_slugs( $post_id, $post, $post_before ) {
    5136         // Don't bother if it hasnt changed.
    5137         if ( $post->post_name == $post_before->post_name )
     5148        // Don't bother if it hasn't changed.
     5149        if ( $post->post_name == $post_before->post_name ) {
    51385150                return;
     5151        }
    51395152
    51405153        // We're only concerned with published, non-hierarchical objects.
    5141         if ( $post->post_status != 'publish' || is_post_type_hierarchical( $post->post_type ) )
     5154        if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) {
    51425155                return;
    5143 
    5144         $old_slugs = (array) get_post_meta($post_id, '_wp_old_slug');
     5156        }
     5157
     5158        $old_slugs = (array) get_post_meta( $post_id, '_wp_old_slug' );
    51455159
    51465160        // If we haven't added this old slug before, add it now.
    5147         if ( !empty( $post_before->post_name ) && !in_array($post_before->post_name, $old_slugs) )
    5148                 add_post_meta($post_id, '_wp_old_slug', $post_before->post_name);
     5161        if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs ) ) {
     5162                add_post_meta( $post_id, '_wp_old_slug', $post_before->post_name );
     5163        }
    51495164
    51505165        // If the new slug was used previously, delete it from the list.
    5151         if ( in_array($post->post_name, $old_slugs) )
    5152                 delete_post_meta($post_id, '_wp_old_slug', $post->post_name);
     5166        if ( in_array( $post->post_name, $old_slugs ) ) {
     5167                delete_post_meta( $post_id, '_wp_old_slug', $post->post_name );
     5168        }
    51535169}
    51545170
  • trunk/src/wp-includes/query.php

    r34659 r34685  
    47374737
    47384738                // Guess the current post_type based on the query vars.
    4739                 if ( get_query_var('post_type') )
    4740                         $post_type = get_query_var('post_type');
    4741                 elseif ( !empty($wp_query->query_vars['pagename']) )
     4739                if ( get_query_var( 'post_type' ) ) {
     4740                        $post_type = get_query_var( 'post_type' );
     4741                } elseif ( get_query_var( 'attachment' ) ) {
     4742                        $post_type = 'attachment';
     4743                } elseif ( ! empty( $wp_query->query_vars['pagename'] ) ) {
    47424744                        $post_type = 'page';
    4743                 else
     4745                } else {
    47444746                        $post_type = 'post';
     4747                }
    47454748
    47464749                if ( is_array( $post_type ) ) {
  • trunk/tests/phpunit/tests/rewrite/oldSlugRedirect.php

    r34659 r34685  
    122122                $this->assertNull( $this->old_slug_redirect_url );
    123123                $this->assertQueryTrue( 'is_attachment', 'is_singular', 'is_single' );
     124
     125                $old_permalink = get_attachment_link( $attachment_id );
     126
     127                wp_update_post( array(
     128                        'ID' => $attachment_id,
     129                        'post_name' => 'the-attachment',
     130                ) );
     131
     132                $permalink = user_trailingslashit( trailingslashit( get_permalink( $this->post_id ) ) . 'the-attachment' );
     133
     134                $this->go_to( $old_permalink );
     135                wp_old_slug_redirect();
     136                $this->assertEquals( $permalink, $this->old_slug_redirect_url );
    124137        }
    125138
Note: See TracChangeset for help on using the changeset viewer.