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. git-svn-id: https://develop.svn.wordpress.org/trunk@34690 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
133b204471
commit
ce14a63214
@ -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 );
|
$view_link = get_preview_post_link( $post, array(), $draft_link );
|
||||||
$preview_target = " target='wp-preview-{$post->ID}'";
|
$preview_target = " target='wp-preview-{$post->ID}'";
|
||||||
} else {
|
} else {
|
||||||
if ( 'publish' === $post->post_status ) {
|
if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) {
|
||||||
$view_link = get_permalink( $post );
|
$view_link = get_permalink( $post );
|
||||||
} else {
|
} else {
|
||||||
// Allow non-published (private, future) to be viewed at a pretty permalink.
|
// 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 = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
|
$return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
|
||||||
|
|
||||||
if ( false !== $view_link ) {
|
if ( false !== $view_link ) {
|
||||||
$return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $permalink . "</a>\n";
|
$return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $view_link . "</a>\n";
|
||||||
} else {
|
} else {
|
||||||
$return .= '<span id="sample-permalink">' . $permalink . "</span>\n";
|
$return .= '<span id="sample-permalink">' . $permalink . "</span>\n";
|
||||||
}
|
}
|
||||||
|
@ -386,6 +386,9 @@ function get_attachment_link( $post = null, $leavename = false ) {
|
|||||||
|
|
||||||
$post = get_post( $post );
|
$post = get_post( $post );
|
||||||
$parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false;
|
$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 ( $wp_rewrite->using_permalinks() && $parent ) {
|
||||||
if ( 'page' == $parent->post_type )
|
if ( 'page' == $parent->post_type )
|
||||||
@ -403,6 +406,8 @@ function get_attachment_link( $post = null, $leavename = false ) {
|
|||||||
|
|
||||||
if ( ! $leavename )
|
if ( ! $leavename )
|
||||||
$link = str_replace( '%postname%', $name, $link );
|
$link = str_replace( '%postname%', $name, $link );
|
||||||
|
} elseif ( $wp_rewrite->using_permalinks() && ! $leavename ) {
|
||||||
|
$link = home_url( user_trailingslashit( $post->post_name ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! $link )
|
if ( ! $link )
|
||||||
|
@ -3580,6 +3580,11 @@ class WP_Query {
|
|||||||
// Check post status to determine if post should be displayed.
|
// Check post status to determine if post should be displayed.
|
||||||
if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
|
if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
|
||||||
$status = get_post_status($this->posts[0]);
|
$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);
|
$post_status_obj = get_post_status_object($status);
|
||||||
//$type = get_post_type($this->posts[0]);
|
//$type = get_post_type($this->posts[0]);
|
||||||
|
|
||||||
|
@ -395,4 +395,58 @@ class Tests_Link extends WP_UnitTestCase {
|
|||||||
|
|
||||||
$this->assertEquals( $non_pretty_permalink, get_permalink( $p ) );
|
$this->assertEquals( $non_pretty_permalink, get_permalink( $p ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 1914
|
||||||
|
*/
|
||||||
|
public function test_unattached_attachment_has_a_pretty_permalink() {
|
||||||
|
global $wp_rewrite;
|
||||||
|
$wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
|
||||||
|
$wp_rewrite->flush_rules();
|
||||||
|
|
||||||
|
$attachment_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(
|
||||||
|
'post_mime_type' => 'image/jpeg',
|
||||||
|
'post_type' => 'attachment',
|
||||||
|
'post_title' => 'An Attachment!',
|
||||||
|
'post_status' => 'inherit',
|
||||||
|
) );
|
||||||
|
|
||||||
|
$attachment = get_post( $attachment_id );
|
||||||
|
|
||||||
|
$this->assertSame( home_url( user_trailingslashit( $attachment->post_name ) ), get_permalink( $attachment_id ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 1914
|
||||||
|
*/
|
||||||
|
public function test_attachment_attached_to_non_existent_post_type_has_a_pretty_permalink() {
|
||||||
|
global $wp_rewrite, $wp_post_types;
|
||||||
|
$wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
|
||||||
|
|
||||||
|
register_post_type( 'not_a_post_type', array( 'public' => true ) );
|
||||||
|
|
||||||
|
$wp_rewrite->flush_rules();
|
||||||
|
|
||||||
|
$post_id = $this->factory->post->create( array( 'post_type' => 'not_a_post_type' ) );
|
||||||
|
|
||||||
|
$attachment_id = $this->factory->attachment->create_object( 'image.jpg', $post_id, array(
|
||||||
|
'post_mime_type' => 'image/jpeg',
|
||||||
|
'post_type' => 'attachment',
|
||||||
|
'post_title' => 'An Attachment!',
|
||||||
|
'post_status' => 'inherit',
|
||||||
|
) );
|
||||||
|
|
||||||
|
$attachment = get_post( $attachment_id );
|
||||||
|
|
||||||
|
$this->assertSame( get_permalink( $post_id ) . user_trailingslashit( $attachment->post_name ), get_permalink( $attachment_id ) );
|
||||||
|
|
||||||
|
foreach( $wp_post_types as $id => $pt ) {
|
||||||
|
if ( 'not_a_post_type' === $pt->name ) {
|
||||||
|
unset( $wp_post_types[ $id ] );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertSame( home_url( user_trailingslashit( $attachment->post_name ) ), get_permalink( $attachment_id ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user