Make WordPress Core

Changeset 34690


Ignore:
Timestamp:
09/29/2015 09:41:14 AM (9 years ago)
Author:
pento
Message:

Permalinks: Add pretty permalinks for unattached attachments.

Previously, unattached attachments would have unsightly /?attachment_id=1 URLs. As we've moved away from attachments being specifically attached to posts, instead being Media items, this has made the unattached URLs a more common occurrence.

We can breath easy once more, knowing that the world is a little bit safer from the horror of unnecessarily ugly URLs.

Props SergeyBiryukov, wonderboymusic, pento.

Fixes #1914.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/post.php

    r34670 r34690  
    12971297            $preview_target = " target='wp-preview-{$post->ID}'";
    12981298        } else {
    1299             if ( 'publish' === $post->post_status ) {
     1299            if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) {
    13001300                $view_link = get_permalink( $post );
    13011301            } else {
     
    13111311
    13121312        if ( false !== $view_link ) {
    1313             $return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $permalink . "</a>\n";
     1313            $return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $view_link . "</a>\n";
    13141314        } else {
    13151315            $return .= '<span id="sample-permalink">' . $permalink . "</span>\n";
  • trunk/src/wp-includes/link-template.php

    r34561 r34690  
    387387    $post = get_post( $post );
    388388    $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
     389    if ( $parent && ! in_array( $parent->post_type, get_post_types() ) ) {
     390        $parent = false;
     391    }
    389392
    390393    if ( $wp_rewrite->using_permalinks() && $parent ) {
     
    404407        if ( ! $leavename )
    405408            $link = str_replace( '%postname%', $name, $link );
     409    } elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) {
     410        $link = home_url( user_trailingslashit( $post->post_name ) );
    406411    }
    407412
  • trunk/src/wp-includes/query.php

    r34685 r34690  
    35813581        if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
    35823582            $status = get_post_status($this->posts[0]);
     3583            if ( 'attachment' === $this->posts[0]->post_type && 0 === (int) $this->posts[0]->post_parent ) {
     3584                $this->is_page = false;
     3585                $this->is_single = true;
     3586                $this->is_attachment = true;
     3587            }
    35833588            $post_status_obj = get_post_status_object($status);
    35843589            //$type = get_post_type($this->posts[0]);
  • trunk/tests/phpunit/tests/link.php

    r34088 r34690  
    396396        $this->assertEquals( $non_pretty_permalink, get_permalink( $p ) );
    397397    }
     398
     399    /**
     400     * @ticket 1914
     401     */
     402    public function test_unattached_attachment_has_a_pretty_permalink() {
     403        global $wp_rewrite;
     404        $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
     405        $wp_rewrite->flush_rules();
     406
     407        $attachment_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(
     408            'post_mime_type' => 'image/jpeg',
     409            'post_type' => 'attachment',
     410            'post_title' => 'An Attachment!',
     411            'post_status' => 'inherit',
     412        ) );
     413
     414        $attachment = get_post( $attachment_id );
     415
     416        $this->assertSame( home_url( user_trailingslashit( $attachment->post_name ) ), get_permalink( $attachment_id ) );
     417    }
     418
     419    /**
     420     * @ticket 1914
     421     */
     422    public function test_attachment_attached_to_non_existent_post_type_has_a_pretty_permalink() {
     423        global $wp_rewrite, $wp_post_types;
     424        $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
     425
     426        register_post_type( 'not_a_post_type', array( 'public' => true ) );
     427
     428        $wp_rewrite->flush_rules();
     429
     430        $post_id = $this->factory->post->create( array( 'post_type' => 'not_a_post_type' ) );
     431
     432        $attachment_id = $this->factory->attachment->create_object( 'image.jpg', $post_id, array(
     433            'post_mime_type' => 'image/jpeg',
     434            'post_type' => 'attachment',
     435            'post_title' => 'An Attachment!',
     436            'post_status' => 'inherit',
     437        ) );
     438
     439        $attachment = get_post( $attachment_id );
     440
     441        $this->assertSame( get_permalink( $post_id ) . user_trailingslashit( $attachment->post_name ), get_permalink( $attachment_id ) );
     442
     443        foreach( $wp_post_types as $id => $pt ) {
     444            if ( 'not_a_post_type' === $pt->name ) {
     445                unset( $wp_post_types[ $id ] );
     446                break;
     447            }
     448        }
     449
     450        $this->assertSame( home_url( user_trailingslashit( $attachment->post_name ) ), get_permalink( $attachment_id ) );
     451    }
    398452}
Note: See TracChangeset for help on using the changeset viewer.