Skip to content

Commit ca8b2f5

Browse files
committed
Canonical: Use get_post_status() to determine whether the post has a publish post status in wp_get_canonical_url().
This changeset fixes an issue where `wp_get_canonical_url()` always returned `false` for attachments. Attachments have a `inherit` post status rather than `publish`. The `wp_get_canonical_url()` function was directly checking `$post->post_status === 'publish'`, causing it to always return `false` for attachments. Using `get_post_status()` fixes the issue as if the post is an attachment, then the parent post status will be given instead. Follow-up to [37685]. Props othernoel, ankitkumarshah, SirLouen, Fixes #63041. git-svn-id: https://develop.svn.wordpress.org/trunk@60108 602fd350-edb4-49c9-b593-d223f7449a82
1 parent fc1990d commit ca8b2f5

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

src/wp-includes/link-template.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4063,7 +4063,7 @@ function wp_get_canonical_url( $post = null ) {
40634063
return false;
40644064
}
40654065

4066-
if ( 'publish' !== $post->post_status ) {
4066+
if ( 'publish' !== get_post_status( $post ) ) {
40674067
return false;
40684068
}
40694069

tests/phpunit/tests/link/wpGetCanonicalUrl.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,53 @@
11
<?php
2+
/**
3+
* Tests for the wp_get_canonical_url() function.
4+
*
5+
* @package WordPress
6+
* @subpackage Link
7+
*/
28

39
/**
10+
* Class for Testing the wp_get_canonical_url() function.
11+
*
412
* @group link
513
* @group canonical
614
* @covers ::wp_get_canonical_url
715
*/
8-
class Tests_Link_wpGetCanonicalUrl extends WP_UnitTestCase {
16+
class Tests_Link_WpGetCanonicalUrl extends WP_UnitTestCase {
17+
/**
18+
* The ID of the post.
19+
*
20+
* @var int
21+
*/
922
public static $post_id;
1023

24+
/**
25+
* The ID of the attachment.
26+
*
27+
* @var int
28+
*/
29+
public static $attachment_id;
30+
31+
/**
32+
* Sets up the test environment before any tests are run.
33+
*
34+
* @param WP_UnitTest_Factory $factory The factory object.
35+
*/
1136
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
1237
self::$post_id = $factory->post->create(
1338
array(
1439
'post_content' => 'Page 1 <!--nextpage--> Page 2 <!--nextpage--> Page 3',
1540
'post_status' => 'publish',
1641
)
1742
);
43+
44+
self::$attachment_id = $factory->attachment->create_object(
45+
array(
46+
'file' => DIR_TESTDATA . '/images/canola.jpg',
47+
'post_parent' => self::$post_id,
48+
'post_status' => 'inherit',
49+
)
50+
);
1851
}
1952

2053
/**
@@ -140,6 +173,19 @@ public function test_comments_paged_with_pretty_permalink_structure() {
140173
$this->assertSame( $expected, wp_get_canonical_url( self::$post_id ) );
141174
}
142175

176+
/**
177+
* This test ensures that attachments with 'inherit' status properly receive a canonical URL.
178+
*
179+
* @ticket 63041
180+
*/
181+
public function test_attachment_canonical_url() {
182+
$this->go_to( get_attachment_link( self::$attachment_id ) );
183+
$canonical_url = wp_get_canonical_url( self::$attachment_id );
184+
185+
$this->assertNotFalse( $canonical_url, 'Attachment should have a canonical URL' );
186+
$this->assertSame( get_attachment_link( self::$attachment_id ), $canonical_url, 'Canonical URL should match the attachment permalink' );
187+
}
188+
143189
/**
144190
* Test calling of filter.
145191
*/

0 commit comments

Comments
 (0)