diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index fcaa834886..49dcdf5309 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -622,22 +622,35 @@ function get_post_comments_feed_link($post_id = 0, $feed = '') { if ( empty( $feed ) ) $feed = get_default_feed(); + $post = get_post( $post_id ); + $unattached = 'attachment' === $post->post_type && 0 === (int) $post->post_parent; + if ( '' != get_option('permalink_structure') ) { if ( 'page' == get_option('show_on_front') && $post_id == get_option('page_on_front') ) $url = _get_page_link( $post_id ); else $url = get_permalink($post_id); - $url = trailingslashit($url) . 'feed'; - if ( $feed != get_default_feed() ) - $url .= "/$feed"; - $url = user_trailingslashit($url, 'single_feed'); + if ( $unattached ) { + $url = home_url( '/feed/' ); + if ( $feed !== get_default_feed() ) { + $url .= "$feed/"; + } + $url = add_query_arg( 'attachment_id', $post_id, $url ); + } else { + $url = trailingslashit($url) . 'feed'; + if ( $feed != get_default_feed() ) + $url .= "/$feed"; + $url = user_trailingslashit($url, 'single_feed'); + } } else { - $type = get_post_field('post_type', $post_id); - if ( 'page' == $type ) + if ( $unattached ) { + $url = add_query_arg( array( 'feed' => $feed, 'attachment_id' => $post_id ), home_url( '/' ) ); + } elseif ( 'page' == $post->post_type ) { $url = add_query_arg( array( 'feed' => $feed, 'page_id' => $post_id ), home_url( '/' ) ); - else + } else { $url = add_query_arg( array( 'feed' => $feed, 'p' => $post_id ), home_url( '/' ) ); + } } /** diff --git a/tests/phpunit/tests/link/getPostCommentsFeedLink.php b/tests/phpunit/tests/link/getPostCommentsFeedLink.php new file mode 100644 index 0000000000..cd05ae6646 --- /dev/null +++ b/tests/phpunit/tests/link/getPostCommentsFeedLink.php @@ -0,0 +1,139 @@ +permalink_structure = get_option( 'permalink_structure' ); + } + + function tearDown() { + $this->remove_added_uploads(); + + parent::tearDown(); + + $this->set_permalink_structure( $this->permalink_structure ); + } + + public function set_permalink_structure( $permalink_structure ) { + global $wp_rewrite; + $wp_rewrite->set_permalink_structure( $permalink_structure ); + $wp_rewrite->flush_rules(); + } + + public function test_post_link() { + $this->set_permalink_structure( '' ); + + $post_id = $this->factory->post->create(); + + $link = get_post_comments_feed_link( $post_id ); + $expected = add_query_arg( array( + 'feed' => get_default_feed(), + 'p' => $post_id + ), home_url( '/' ) ); + + $this->assertEquals( $expected, $link ); + } + + public function test_post_pretty_link() { + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); + + $post_id = $this->factory->post->create(); + + $link = get_post_comments_feed_link( $post_id ); + $expected = get_permalink( $post_id ) . 'feed/'; + + $this->assertEquals( $expected, $link ); + } + + public function test_attachment_link() { + $this->set_permalink_structure( '' ); + + $post_id = $this->factory->post->create(); + $attachment_id = $this->factory->attachment->create_object( 'image.jpg', $post_id, array( + 'post_mime_type' => 'image/jpeg', + 'post_type' => 'attachment' + ) ); + + $link = get_post_comments_feed_link( $attachment_id ); + $expected = add_query_arg( array( + 'feed' => get_default_feed(), + 'p' => $attachment_id + ), home_url( '/' ) ); + + $this->assertEquals( $expected, $link ); + } + + public function test_attachment_pretty_link() { + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); + + $post_id = $this->factory->post->create( array( + 'post_status' => 'publish' + ) ); + $attachment_id = $this->factory->attachment->create_object( 'image.jpg', $post_id, array( + 'post_mime_type' => 'image/jpeg', + 'post_type' => 'attachment', + 'post_title' => 'Burrito' + ) ); + + $p = get_post( $post_id ); + + $link = get_post_comments_feed_link( $attachment_id ); + $expected = get_permalink( $post_id ) . 'burrito/feed/'; + + $this->assertEquals( $expected, $link ); + } + + public function test_attachment_no_name_pretty_link() { + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); + + $post_id = $this->factory->post->create(); + $attachment_id = $this->factory->attachment->create_object( 'image.jpg', $post_id, array( + 'post_mime_type' => 'image/jpeg', + 'post_type' => 'attachment' + ) ); + + $link = get_post_comments_feed_link( $attachment_id ); + $expected = add_query_arg( array( + 'feed' => get_default_feed(), + 'p' => $attachment_id + ), home_url( '/' ) ); + + $this->assertEquals( $expected, $link ); + } + + public function test_unattached_link() { + $this->set_permalink_structure( '' ); + + $attachment_id = $this->factory->attachment->create_object( 'image.jpg', 0, array( + 'post_mime_type' => 'image/jpeg', + 'post_type' => 'attachment' + ) ); + + $link = get_post_comments_feed_link( $attachment_id ); + $expected = add_query_arg( array( + 'feed' => get_default_feed(), + 'attachment_id' => $attachment_id + ), home_url( '/' ) ); + + $this->assertEquals( $expected, $link ); + } + + public function test_unattached_pretty_link() { + $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); + + $attachment_id = $this->factory->attachment->create_object( 'image.jpg', 0, array( + 'post_mime_type' => 'image/jpeg', + 'post_type' => 'attachment' + ) ); + + $link = get_post_comments_feed_link( $attachment_id ); + $expected = add_query_arg( 'attachment_id', $attachment_id, home_url( '/feed/' ) ); + + $this->assertEquals( $expected, $link ); + } +} \ No newline at end of file