Make WordPress Core

Changeset 53238


Ignore:
Timestamp:
04/21/2022 06:02:21 AM (4 years ago)
Author:
peterwilsoncc
Message:

Posts, Post Types: Fix option validation in stick_post().

Normalize an invalid sticky_posts option to an empty array within stick_post(). This fixes a bug in which an unexpected option value would prevent new posts from being made sticky.

Follow up to [50380].

Props azouamauriac, denishua, kajalgohel, sergeybiryukov, costdev.
Fixes #55176.

Location:
trunk
Files:
2 edited

Legend:

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

    r53170 r53238  
    28962896
    28972897        if ( ! is_array( $stickies ) ) {
    2898                 $stickies = array( $post_id );
     2898                $stickies = array();
    28992899        } else {
    29002900                $stickies = array_unique( array_map( 'intval', $stickies ) );
  • trunk/tests/phpunit/tests/post.php

    r52389 r53238  
    16871687
    16881688        /**
     1689         * Ensures sticking a post succeeds after deleting the 'sticky_posts' option.
     1690         *
     1691         * @ticket 52007
     1692         * @ticket 55176
     1693         * @covers ::stick_post
     1694         */
     1695        public function test_stick_post_after_delete_sticky_posts_option() {
     1696                delete_option( 'sticky_posts' );
     1697
     1698                stick_post( 1 );
     1699                $this->assertSameSets( array( 1 ), get_option( 'sticky_posts' ) );
     1700        }
     1701
     1702        /**
     1703         * Ensures sticking works with an unexpected option value.
     1704         *
     1705         * @ticket 52007
     1706         * @ticket 55176
     1707         * @covers ::stick_post
     1708         * @dataProvider data_stick_post_with_unexpected_sticky_posts_option
     1709         *
     1710         * @param mixed $starting_option Starting value for sticky_posts option.
     1711         */
     1712        public function test_stick_post_with_unexpected_sticky_posts_option( $starting_option ) {
     1713                update_option( 'sticky_posts', $starting_option );
     1714
     1715                stick_post( 1 );
     1716                $this->assertSameSets( array( 1 ), get_option( 'sticky_posts' ) );
     1717        }
     1718
     1719        /**
     1720         * Data provider.
     1721         *
     1722         * @return array
     1723         */
     1724        public function data_stick_post_with_unexpected_sticky_posts_option() {
     1725                return array(
     1726                        'false'     => array( false ),
     1727                        'a string'  => array( 'string' ),
     1728                        '1 int'     => array( 1 ),
     1729                        'null'      => array( null ),
     1730                        'true'      => array( true ),
     1731                        'an object' => array( new stdClass ),
     1732                );
     1733        }
     1734
     1735        /**
    16891736         * Ensure sticking a post removes other duplicate post IDs from the option.
    16901737         *
Note: See TracChangeset for help on using the changeset viewer.