WordPress.org

Make WordPress Core

Ticket #35880: refactor-insert-post-date-handling.patch

File refactor-insert-post-date-handling.patch, 3.2 KB (added by redsweater, 5 years ago)

Refactor wp_insert_post()'s handling of dates to be more readable and maintainable

  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 6360735..e03b4a4 100644
    function wp_insert_post( $postarr, $wp_error = false ) { 
    31303130                }
    31313131        }
    31323132
    3133         /*
    3134          * If the post date is empty (due to having been new or a draft) and status
    3135          * is not 'draft' or 'pending', set date to now.
    3136          */
    3137         if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' == $postarr['post_date'] ) {
    3138                 if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) {
    3139                         $post_date = current_time( 'mysql' );
    3140                 } else {
    3141                         $post_date = get_date_from_gmt( $postarr['post_date_gmt'] );
    3142                 }
    3143         } else {
     3133        // By default we respect a provided post_date
     3134        $post_date = '';
     3135        if ( ! empty( $postarr['post_date'] ) ) {
    31443136                $post_date = $postarr['post_date'];
    31453137        }
    31463138
    3147         // Validate the date.
    3148         $mm = substr( $post_date, 5, 2 );
    3149         $jj = substr( $post_date, 8, 2 );
    3150         $aa = substr( $post_date, 0, 4 );
    3151         $valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
    3152         if ( ! $valid_date ) {
    3153                 if ( $wp_error ) {
    3154                         return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
     3139        // Use the provided post_date_gmt if we didn't get anything from post_date
     3140        if ( empty( $post_date ) && ! empty( $postarr['post_date_gmt'] ) && '0000-00-00 00:00:00' !== $postarr['post_date_gmt'] ) {
     3141                $post_date_gmt = $postarr['post_date_gmt'];
     3142                $post_date = get_date_from_gmt( $post_date_gmt );
     3143        }
     3144
     3145        // We require a valid date unless the post is pending or draft
     3146        $needs_date = ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) );
     3147
     3148        // Assign the current date if we need a date and don't have one, otherwise
     3149        // standardize to empty value.
     3150        if ( empty( $post_date ) || '0000-00-00 00:00:00' == $post_date ) {
     3151                if ( $needs_date ) {
     3152                        $post_date = current_time( 'mysql' );
    31553153                } else {
    3156                         return 0;
     3154                        $post_date = '';
    31573155                }
    31583156        }
    31593157
    3160         if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) {
    3161                 if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
    3162                         $post_date_gmt = get_gmt_from_date( $post_date );
    3163                 } else {
    3164                         $post_date_gmt = '0000-00-00 00:00:00';
     3158        // Validate the date when it's not empty
     3159        if ( ! empty ( $post_date ) ) {
     3160                $mm = substr( $post_date, 5, 2 );
     3161                $jj = substr( $post_date, 8, 2 );
     3162                $aa = substr( $post_date, 0, 4 );
     3163                $valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
     3164                if ( ! $valid_date ) {
     3165                        if ( $wp_error ) {
     3166                                return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
     3167                        } else {
     3168                                return 0;
     3169                        }
    31653170                }
     3171        }
     3172
     3173        // Finally, set $post_date_gmt based on post_date if we have it, otherwise to
     3174        // canonical empty date value to guarantee the math works below
     3175        if ( ! empty( $post_date ) ) {
     3176                $post_date_gmt = get_gmt_from_date( $post_date );
    31663177        } else {
    3167                 $post_date_gmt = $postarr['post_date_gmt'];
     3178                $post_date_gmt = '0000-00-00 00:00:00';
    31683179        }
    31693180
    3170         if ( $update || '0000-00-00 00:00:00' == $post_date ) {
     3181        if ( $update || empty( $post_date ) ) {
    31713182                $post_modified     = current_time( 'mysql' );
    31723183                $post_modified_gmt = current_time( 'mysql', 1 );
    31733184        } else {