diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index 24c713d89dbd4..e96c2d137882b 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -4063,7 +4063,7 @@ function wp_get_canonical_url( $post = null ) { return false; } - if ( 'publish' !== $post->post_status ) { + if ( 'publish' !== get_post_status( $post ) ) { return false; } diff --git a/tests/phpunit/tests/link/wpGetCanonicalUrl.php b/tests/phpunit/tests/link/wpGetCanonicalUrl.php index 5b7eddaadaafb..c1dad22e4d128 100644 --- a/tests/phpunit/tests/link/wpGetCanonicalUrl.php +++ b/tests/phpunit/tests/link/wpGetCanonicalUrl.php @@ -1,13 +1,39 @@ <?php +/** + * Tests for the Get Canonical Url function. + * + * @package WordPress + * @subpackage Link + */ /** + * Class for Testing the Get Canonical Url function. + * * @group link * @group canonical * @covers ::wp_get_canonical_url */ -class Tests_Link_wpGetCanonicalUrl extends WP_UnitTestCase { +class Tests_Link_WpGetCanonicalUrl extends WP_UnitTestCase { + + /** + * The ID of the post. + * + * @var int + */ public static $post_id; + /** + * The ID of the attachment. + * + * @var int + */ + public static $attachment_id; + + /** + * Sets up the test environment before any tests are run. + * + * @param WP_UnitTest_Factory $factory The factory object. + */ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { self::$post_id = $factory->post->create( array( @@ -15,6 +41,14 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'post_status' => 'publish', ) ); + + self::$attachment_id = $factory->attachment->create_object( + array( + 'file' => DIR_TESTDATA . '/images/canola.jpg', + 'post_parent' => self::$post_id, + 'post_status' => 'inherit', + ) + ); } /** @@ -140,6 +174,19 @@ public function test_comments_paged_with_pretty_permalink_structure() { $this->assertSame( $expected, wp_get_canonical_url( self::$post_id ) ); } + /** + * This test ensures that attachments with 'inherit' status properly receive a canonical URL. + * + * @ticket 63041 + */ + public function test_attachment_canonical_url() { + $this->go_to( get_attachment_link( self::$attachment_id ) ); + $canonical_url = wp_get_canonical_url( self::$attachment_id ); + + $this->assertNotFalse( $canonical_url, 'Attachment should have a canonical URL' ); + $this->assertSame( get_attachment_link( self::$attachment_id ), $canonical_url, 'Canonical URL should match the attachment permalink' ); + } + /** * Test calling of filter. */