Make WordPress Core

Changeset 60108


Ignore:
Timestamp:
03/30/2025 11:32:38 PM (17 months ago)
Author:
audrasjb
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r59865 r60108  
    40644064        }
    40654065
    4066         if ( 'publish' !== $post->post_status ) {
     4066        if ( 'publish' !== get_post_status( $post ) ) {
    40674067                return false;
    40684068        }
  • trunk/tests/phpunit/tests/link/wpGetCanonicalUrl.php

    r51125 r60108  
    11<?php
    2 
    32/**
     3 * Tests for the wp_get_canonical_url() function.
     4 *
     5 * @package WordPress
     6 * @subpackage Link
     7 */
     8
     9/**
     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 {
     16class 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(
     
    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
     
    139172
    140173                $this->assertSame( $expected, wp_get_canonical_url( self::$post_id ) );
     174        }
     175
     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' );
    141187        }
    142188
Note: See TracChangeset for help on using the changeset viewer.