Make WordPress Core

Ticket #35874: fix-and-test-update-post.patch

File fix-and-test-update-post.patch, 2.3 KB (added by redsweater, 8 years ago)

Fixes to wp_update_post to prevent accidentally stripping the date from posts.

  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 6360735..4997766 100644
    function wp_update_post( $postarr = array(), $wp_error = false ) { 
    35383538                $post_cats = $post['post_category'];
    35393539
    35403540        // Drafts shouldn't be assigned a date unless explicitly done so by the user.
    3541         if ( isset( $post['post_status'] ) && in_array($post['post_status'], array('draft', 'pending', 'auto-draft')) && empty($postarr['edit_date']) &&
    3542                          ('0000-00-00 00:00:00' == $post['post_date_gmt']) )
    3543                 $clear_date = true;
    3544         else
    3545                 $clear_date = false;
     3541        $clear_date = false;
     3542        if ( isset( $post['post_status'] ) && in_array($post['post_status'], array('draft', 'pending', 'auto-draft')) ) {
     3543                // edit_date will only be set for date edits made throug wp-admin, not through XMRLPC API
     3544                $missing_admin_edit_flag = empty( $postarr['edit_date'] );
     3545                $missing_post_date = ( '0000-00-00 00:00:00' == $post['post_date_gmt'] );
     3546                $missing_post_date_gmt = ( '0000-00-00 00:00:00' == $post['post_date'] );
     3547                if ( $missing_admin_edit_flag && $missing_post_date && $missing_post_date_gmt ) {
     3548                        $clear_date = true;
     3549                }
     3550        }
    35463551
    35473552        // Merge old and new fields with new fields overwriting old ones.
    35483553        $postarr = array_merge($post, $postarr);
  • tests/phpunit/tests/post.php

    diff --git tests/phpunit/tests/post.php tests/phpunit/tests/post.php
    index 4f5a294..636b82b 100644
    class Tests_Post extends WP_UnitTestCase { 
    12581258                $this->assertEquals( 0, get_post( $page_id )->post_parent );
    12591259        }
    12601260
     1261        /**
     1262         * @ticket 35874
     1263         */
     1264        function test_wp_update_post_should_respect_post_date() {
     1265                $post = array(
     1266                        'post_author' => self::$editor_id,
     1267                        'post_status' => 'draft',
     1268                        'post_content' => rand_str(),
     1269                        'post_title' => rand_str(),
     1270                );
     1271
     1272                $id = wp_insert_post($post);
     1273
     1274                $out = get_post($id);
     1275
     1276                // Change the post status to publish while also supplying a date
     1277                $specific_date = strftime("%Y-%m-%d %H:%M:%S", strtotime('+1 day'));
     1278
     1279                $post['ID'] = $id;
     1280                $post['post_date'] = $specific_date;
     1281                $post['post_status'] = 'publish';
     1282                wp_update_post( $post );
     1283
     1284                // Updated post should still possess the specified date
     1285                $out = get_post($id);
     1286                $this->assertEquals($specific_date, $out->post_date);
     1287        }
     1288
    12611289}