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.



git-svn-id: https://develop.svn.wordpress.org/trunk@53238 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2022-04-21 06:02:21 +00:00
parent d80d27b4cc
commit f341d73ad9
2 changed files with 48 additions and 1 deletions

View File

@ -2895,7 +2895,7 @@ function stick_post( $post_id ) {
$updated = false;
if ( ! is_array( $stickies ) ) {
$stickies = array( $post_id );
$stickies = array();
} else {
$stickies = array_unique( array_map( 'intval', $stickies ) );
}

View File

@ -1685,6 +1685,53 @@ class Tests_Post extends WP_UnitTestCase {
);
}
/**
* Ensures sticking a post succeeds after deleting the 'sticky_posts' option.
*
* @ticket 52007
* @ticket 55176
* @covers ::stick_post
*/
public function test_stick_post_after_delete_sticky_posts_option() {
delete_option( 'sticky_posts' );
stick_post( 1 );
$this->assertSameSets( array( 1 ), get_option( 'sticky_posts' ) );
}
/**
* Ensures sticking works with an unexpected option value.
*
* @ticket 52007
* @ticket 55176
* @covers ::stick_post
* @dataProvider data_stick_post_with_unexpected_sticky_posts_option
*
* @param mixed $starting_option Starting value for sticky_posts option.
*/
public function test_stick_post_with_unexpected_sticky_posts_option( $starting_option ) {
update_option( 'sticky_posts', $starting_option );
stick_post( 1 );
$this->assertSameSets( array( 1 ), get_option( 'sticky_posts' ) );
}
/**
* Data provider.
*
* @return array
*/
public function data_stick_post_with_unexpected_sticky_posts_option() {
return array(
'false' => array( false ),
'a string' => array( 'string' ),
'1 int' => array( 1 ),
'null' => array( null ),
'true' => array( true ),
'an object' => array( new stdClass ),
);
}
/**
* Ensure sticking a post removes other duplicate post IDs from the option.
*