Make WordPress Core


Ignore:
Timestamp:
01/25/2021 01:06:25 AM (5 years ago)
Author:
pento
Message:

Posts: Create a new function for resolving the post date.

wp_insert_post() has a few checks using post_date and post_date_gmt, to determine the correct post date. This functionality is now extracted out into a new wp_resolve_post_date() function, allowing the checks to be reused elsewhere.

Props jmdodd.
Fixes #52187.

File:
1 edited

Legend:

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

    r49986 r50012  
    37033703        'import_id'             => 0,
    37043704        'context'               => '',
     3705        'post_date'             => '',
     3706        'post_date_gmt'         => '',
    37053707    );
    37063708
     
    38363838
    38373839    /*
    3838      * If the post date is empty (due to having been new or a draft) and status
    3839      * is not 'draft' or 'pending', set date to now.
     3840     * Resolve the post date from any provided post date or post date GMT strings;
     3841     * if none are provided, the date will be set to now.
    38403842     */
    3841     if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' === $postarr['post_date'] ) {
    3842         if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' === $postarr['post_date_gmt'] ) {
    3843             $post_date = current_time( 'mysql' );
    3844         } else {
    3845             $post_date = get_date_from_gmt( $postarr['post_date_gmt'] );
    3846         }
    3847     } else {
    3848         $post_date = $postarr['post_date'];
    3849     }
    3850 
    3851     // Validate the date.
    3852     $mm         = substr( $post_date, 5, 2 );
    3853     $jj         = substr( $post_date, 8, 2 );
    3854     $aa         = substr( $post_date, 0, 4 );
    3855     $valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
    3856     if ( ! $valid_date ) {
     3843    $post_date = wp_resolve_post_date( $postarr['post_date'], $postarr['post_date_gmt'] );
     3844    if ( ! $post_date ) {
    38573845        if ( $wp_error ) {
    38583846            return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
     
    45374525    // wp_publish_post() returns no meaningful value.
    45384526    wp_publish_post( $post_id );
     4527}
     4528
     4529/**
     4530 * Uses wp_checkdate to return a valid Gregorian-calendar value for post_date.
     4531 * If post_date is not provided, this first checks post_date_gmt if provided,
     4532 * then falls back to use the current time.
     4533 *
     4534 * For back-compat purposes in wp_insert_post, an empty post_date and an invalid
     4535 * post_date_gmt will continue to return '1970-01-01 00:00:00' rather than false.
     4536 *
     4537 * @since 5.7.0
     4538 *
     4539 * @param string $post_date     The date in mysql format.
     4540 * @param string $post_date_gmt The GMT date in mysql format.
     4541 * @return string|false A valid Gregorian-calendar date string, or false on failure.
     4542 */
     4543function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) {
     4544    // If the date is empty, set the date to now.
     4545    if ( empty( $post_date ) || '0000-00-00 00:00:00' === $post_date ) {
     4546        if ( empty( $post_date_gmt ) || '0000-00-00 00:00:00' === $post_date_gmt ) {
     4547            $post_date = current_time( 'mysql' );
     4548        } else {
     4549            $post_date = get_date_from_gmt( $post_date_gmt );
     4550        }
     4551    }
     4552
     4553    // Validate the date.
     4554    $month = substr( $post_date, 5, 2 );
     4555    $day   = substr( $post_date, 8, 2 );
     4556    $year  = substr( $post_date, 0, 4 );
     4557
     4558    $valid_date = wp_checkdate( $month, $day, $year, $post_date );
     4559
     4560    if ( ! $valid_date ) {
     4561        return false;
     4562    }
     4563    return $post_date;
    45394564}
    45404565
Note: See TracChangeset for help on using the changeset viewer.