diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 606d2669ab..fd5a48b9e0 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -2169,7 +2169,18 @@ function stick_post( $post_id ) { if ( ! in_array($post_id, $stickies) ) $stickies[] = $post_id; - update_option('sticky_posts', $stickies); + $updated = update_option( 'sticky_posts', $stickies ); + + if ( $updated ) { + /** + * Fires once a post has been added to the sticky list. + * + * @since 4.6.0 + * + * @param int $post_id ID of the post that was stuck. + */ + do_action( 'post_stuck', $post_id ); + } } /** @@ -2196,7 +2207,18 @@ function unstick_post( $post_id ) { array_splice($stickies, $offset, 1); - update_option('sticky_posts', $stickies); + $updated = update_option( 'sticky_posts', $stickies ); + + if ( $updated ) { + /** + * Fires once a post has been removed from the sticky list. + * + * @since 4.6.0 + * + * @param int $post_id ID of the post that was unstuck. + */ + do_action( 'post_unstuck', $post_id ); + } } /** diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index a1ec373be8..de11a34542 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -1181,6 +1181,33 @@ class Tests_Post extends WP_UnitTestCase { $this->assertEquals( 'Updated', $saved_post->post_content ); } + /** + * Test that hooks are fired when post gets stuck and unstuck. + * + * @ticket 35600 + */ + function test_hooks_fire_when_post_gets_stuck_and_unstuck() { + $post_id = self::factory()->post->create(); + $a1 = new MockAction(); + $a2 = new MockAction(); + + $this->assertFalse( is_sticky( $post_id ) ); + + add_action( 'post_stuck', array( $a1, 'action' ) ); + add_action( 'post_unstuck', array( $a2, 'action' ) ); + + stick_post( $post_id ); + $this->assertTrue( is_sticky( $post_id ) ); + unstick_post( $post_id ); + $this->assertFalse( is_sticky( $post_id ) ); + + remove_action( 'post_stuck', array( $a1, 'action' ) ); + remove_action( 'post_unstuck', array( $a2, 'action' ) ); + + $this->assertEquals( 1, $a1->get_call_count() ); + $this->assertEquals( 1, $a2->get_call_count() ); + } + /** * If a post is updated without providing a post_name param, * a new slug should not be generated.