Make WordPress Core

Changeset 53043


Ignore:
Timestamp:
04/01/2022 03:23:07 AM (3 years ago)
Author:
peterwilsoncc
Message:

Canonical: Include all public status in 404 redirects.

In redirect_guess_404_permalink() search for posts using all publicly queryable statuses rather than limiting options to the publish status.

Props goaroundagain, costdev, htdat, audrasjb, chaion07.
Fixes #47911.

Location:
trunk
Files:
2 edited

Legend:

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

    r52833 r53043  
    958958        }
    959959
     960        $publicly_viewable_statuses = array_filter( get_post_stati(), 'is_post_status_viewable' );
    960961        // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    961         $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'" );
     962        $post_id = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE $where AND post_status IN ('" . implode( "', '", esc_sql( $publicly_viewable_statuses ) ) . "')" );
    962963
    963964        if ( ! $post_id ) {
  • trunk/tests/phpunit/tests/canonical.php

    r52010 r53043  
    277277
    278278    /**
     279     * Ensure public posts with custom public statuses are guessed.
     280     *
     281     * @ticket 47911
     282     * @dataProvider data_redirect_guess_404_permalink_with_custom_statuses
     283     *
     284     * @covers ::redirect_guess_404_permalink
     285     */
     286    public function test_redirect_guess_404_permalink_with_custom_statuses( $status_args, $redirects ) {
     287        register_post_status( 'custom', $status_args );
     288
     289        $post = self::factory()->post->create(
     290            array(
     291                'post_title'  => 'custom-status-public-guess-404-permalink',
     292                'post_status' => 'custom',
     293            )
     294        );
     295
     296        $this->go_to( 'custom-status-public-guess-404-permalink' );
     297
     298        $expected = $redirects ? get_permalink( $post ) : false;
     299
     300        $this->assertSame( $expected, redirect_guess_404_permalink() );
     301    }
     302
     303    /**
     304     * Data provider for test_redirect_guess_404_permalink_with_custom_statuses().
     305     *
     306     * return array[] {
     307     *    array Arguments used to register custom status
     308     *    bool  Whether the 404 link is expected to redirect
     309     * }
     310     */
     311    public function data_redirect_guess_404_permalink_with_custom_statuses() {
     312        return array(
     313            'public status'                      => array(
     314                'status_args' => array( 'public' => true ),
     315                'redirects'   => true,
     316            ),
     317            'private status'                     => array(
     318                'status_args' => array( 'public' => false ),
     319                'redirects'   => false,
     320            ),
     321            'internal status'                    => array(
     322                'status_args' => array( 'internal' => true ),
     323                'redirects'   => false,
     324            ),
     325            'protected status'                   => array(
     326                'status_args' => array( 'protected' => true ),
     327                'redirects'   => false,
     328            ),
     329            'protected status flagged as public' => array(
     330                'status_args' => array(
     331                    'protected' => true,
     332                    'public'    => true,
     333                ),
     334                'redirects'   => false,
     335            ),
     336        );
     337    }
     338
     339    /**
    279340     * Ensure multiple post types do not throw a notice.
    280341     *
Note: See TracChangeset for help on using the changeset viewer.