WordPress.org

Make WordPress Core

Changeset 35800


Ignore:
Timestamp:
12/06/2015 09:57:59 PM (5 years ago)
Author:
ericlewis
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

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

    r35725 r35800  
    28732873 *                                         Default is the value of 'default_ping_status' option.
    28742874 *     @type string $post_password         The password to access the post. Default empty.
    2875  *     @type string $post_name             The post name. Default is the sanitized post title.
     2875 *     @type string $post_name             The post name. Default is the sanitized post title
     2876 *                                         when creating a new post.
    28762877 *     @type string $to_ping               Space or carriage return-separated list of URLs to ping.
    28772878 *                                         Default empty.
     
    29542955    if ( isset( $postarr['post_name'] ) ) {
    29552956        $post_name = $postarr['post_name'];
     2957    } elseif ( $update ) {
     2958        // For an update, don't modify the post_name if it wasn't supplied as an argument.
     2959        $post_name = $post_before->post_name;
    29562960    }
    29572961
  • trunk/tests/phpunit/tests/post.php

    r35705 r35800  
    11901190
    11911191    /**
     1192     * If a post is updated without providing a post_name param,
     1193     * a new slug should not be generated.
     1194     *
     1195     * @ticket 34865
     1196     */
     1197    function test_post_updates_without_slug_provided() {
     1198        $post_id = self::factory()->post->create( array(
     1199            'post_title'   => 'Stuff',
     1200            'post_status'  => 'publish'
     1201        ) );
     1202
     1203        $data = array(
     1204            'ID'         => $post_id,
     1205            'post_title' => 'Stuff and Things'
     1206        );
     1207
     1208        wp_insert_post( $data );
     1209
     1210        $updated_post = get_post( $post_id );
     1211        // Ensure changing the post_title didn't modify the post_name.
     1212        $this->assertEquals('stuff', $updated_post->post_name);
     1213    }
     1214
     1215    /**
    11921216     * @ticket 32585
    11931217     */
Note: See TracChangeset for help on using the changeset viewer.