From a84227aab786dcad8356c945aba0f4ed8bc84933 Mon Sep 17 00:00:00 2001 From: Eric Andrew Lewis Date: Sun, 6 Dec 2015 21:57:59 +0000 Subject: [PATCH] Posts: Don't modify post_name if it wasn't supplied to `wp_insert_post()`. Previously when updating a post using wp_insert_post(), post_name was regenerated based on post_title every time if post_name was not passed in explicitly. This irons out the expectation that properties not passed into the function should not be modified. Props jason_the_adams. Fixes #34865. git-svn-id: https://develop.svn.wordpress.org/trunk@35800 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 6 +++++- tests/phpunit/tests/post.php | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index d0bc1a3d86..f13d34b823 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -2872,7 +2872,8 @@ function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { * @type string $ping_status Whether the post can accept pings. Accepts 'open' or 'closed'. * Default is the value of 'default_ping_status' option. * @type string $post_password The password to access the post. Default empty. - * @type string $post_name The post name. Default is the sanitized post title. + * @type string $post_name The post name. Default is the sanitized post title + * when creating a new post. * @type string $to_ping Space or carriage return-separated list of URLs to ping. * Default empty. * @type string $pinged Space or carriage return-separated list of URLs that have @@ -2953,6 +2954,9 @@ function wp_insert_post( $postarr, $wp_error = false ) { $post_excerpt = $postarr['post_excerpt']; if ( isset( $postarr['post_name'] ) ) { $post_name = $postarr['post_name']; + } elseif ( $update ) { + // For an update, don't modify the post_name if it wasn't supplied as an argument. + $post_name = $post_before->post_name; } $maybe_empty = 'attachment' !== $post_type diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index 71fe55efcc..025ef10c8a 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -1188,6 +1188,30 @@ class Tests_Post extends WP_UnitTestCase { $this->assertEquals( 'Updated', $saved_post->post_content ); } + /** + * If a post is updated without providing a post_name param, + * a new slug should not be generated. + * + * @ticket 34865 + */ + function test_post_updates_without_slug_provided() { + $post_id = self::factory()->post->create( array( + 'post_title' => 'Stuff', + 'post_status' => 'publish' + ) ); + + $data = array( + 'ID' => $post_id, + 'post_title' => 'Stuff and Things' + ); + + wp_insert_post( $data ); + + $updated_post = get_post( $post_id ); + // Ensure changing the post_title didn't modify the post_name. + $this->assertEquals('stuff', $updated_post->post_name); + } + /** * @ticket 32585 */