From 48beba441b24959ff4e6d2d1580d669c1bf3f187 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 18 Aug 2015 02:50:23 +0000 Subject: [PATCH] When generating a fallback `post_name` using the post ID, `wp_insert_post()` should clear the post cache immediately. If the post cache is not cleared at this point, the cache can become stale for operations performed before the cache is cleared later in the function. Specifically, the generation of a `guid` for new posts can use stale data, resulting in non-unique values. [33262] introduced a call to `get_post()` that introduced just such an invalidation problem. Fixes #5305. git-svn-id: https://develop.svn.wordpress.org/trunk@33630 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 1 + tests/phpunit/tests/post.php | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index f15c9e23a5..912a03c8c8 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -3455,6 +3455,7 @@ function wp_insert_post( $postarr, $wp_error = false ) { if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) { $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent ); $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); + clean_post_cache( $post_ID ); } if ( is_object_in_taxonomy( $post_type, 'category' ) ) { diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 22310c0aad..d51b5462b4 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -434,6 +434,24 @@ class Tests_Post extends WP_UnitTestCase { $this->assertEquals( "$p-2", $post->post_name ); } + /** + * @ticket 5305 + * @ticket 33392 + */ + public function test_wp_insert_post_should_invalidate_post_cache_before_generating_guid_when_post_name_is_empty_and_is_generated_from_the_post_ID(){ + register_post_type( 'wptests_pt' ); + + $p = wp_insert_post( array( + 'post_title' => '', + 'post_type' => 'wptests_pt', + 'post_status' => 'publish', + ) ); + + $post = get_post( $p ); + + $this->assertContains( 'wptests_pt=' . $p, $post->guid ); + } + /** * @ticket 5364 */