From cf173408cd4069978c7d71ccd7164ceaa479624e Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Tue, 29 Sep 2015 09:42:26 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@34690 git-svn-id: http://core.svn.wordpress.org/trunk@34654 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/post.php | 4 ++-- wp-includes/link-template.php | 5 +++++ wp-includes/query.php | 5 +++++ wp-includes/version.php | 2 +- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index 218fdc42f8..7ed2a66112 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -1296,7 +1296,7 @@ function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) { $view_link = get_preview_post_link( $post, array(), $draft_link ); $preview_target = " target='wp-preview-{$post->ID}'"; } else { - if ( 'publish' === $post->post_status ) { + if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) { $view_link = get_permalink( $post ); } else { // Allow non-published (private, future) to be viewed at a pretty permalink. @@ -1310,7 +1310,7 @@ function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) { $return = '' . __( 'Permalink:' ) . "\n"; if ( false !== $view_link ) { - $return .= '' . $permalink . "\n"; + $return .= '' . $view_link . "\n"; } else { $return .= '' . $permalink . "\n"; } diff --git a/wp-includes/link-template.php b/wp-includes/link-template.php index 4861478d0f..9244cff8a3 100644 --- a/wp-includes/link-template.php +++ b/wp-includes/link-template.php @@ -386,6 +386,9 @@ function get_attachment_link( $post = null, $leavename = false ) { $post = get_post( $post ); $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false; + if ( $parent && ! in_array( $parent->post_type, get_post_types() ) ) { + $parent = false; + } if ( $wp_rewrite->using_permalinks() && $parent ) { if ( 'page' == $parent->post_type ) @@ -403,6 +406,8 @@ function get_attachment_link( $post = null, $leavename = false ) { if ( ! $leavename ) $link = str_replace( '%postname%', $name, $link ); + } elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) { + $link = home_url( user_trailingslashit( $post->post_name ) ); } if ( ! $link ) diff --git a/wp-includes/query.php b/wp-includes/query.php index cc48edda86..fa59b0f55f 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -3580,6 +3580,11 @@ class WP_Query { // Check post status to determine if post should be displayed. if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) { $status = get_post_status($this->posts[0]); + if ( 'attachment' === $this->posts[0]->post_type && 0 === (int) $this->posts[0]->post_parent ) { + $this->is_page = false; + $this->is_single = true; + $this->is_attachment = true; + } $post_status_obj = get_post_status_object($status); //$type = get_post_type($this->posts[0]); diff --git a/wp-includes/version.php b/wp-includes/version.php index 3522918471..c08e6bdb32 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.4-alpha-34689'; +$wp_version = '4.4-alpha-34690'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.