[wp-trac] [WordPress Trac] #26798: While inserting a post some values for 'post_date' throw a PHP exception

WordPress Trac noreply at wordpress.org
Wed Jan 8 23:30:54 UTC 2014


#26798: While inserting a post some values for 'post_date' throw a PHP exception
--------------------------+-----------------------------
 Reporter:  mobius5150    |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Validation    |    Version:  trunk
 Severity:  normal        |   Keywords:
--------------------------+-----------------------------
 If you try and insert a post with a date that does not have leading zeros
 (eg `2012-01-8` or `2012-1-08` or `2012-1-8`) a PHP error is thrown in
 [source:/trunk/src/wp-includes/functions.php#L4015]: `A non well formed
 numeric value encountered` due to the checkdate() call.

 Minimum code to reproduce:
 {{{
 #!php
 $post_data = array(
     'post_title' => 'some title',
     'post_content' => 'some content',
     'post_status' => 'publish',
     'post_date' => '2012-01-8 12:00:00',
 );

 wp_insert_post( $post_data );
 }}}

 Two possible solutions:
 1. Get strict about date formatting: ie: require leading zeros. If a date
 comes in without leading zeros a WP_Error() should be returned, an error
 should not get thrown.
 2. Allow non-leading zeros.

 Either way using `substr()` in [source:/trunk/src/wp-
 includes/post.php#L2805] is at fault.

 I've attached a diff that uses a regex to parse the date using capture
 groups. Its much cleaner and will detect a badly formatted date and handle
 it gracefully. The regex does not require leading zeros. If leading zeros
 are required the regex should be changed to:
 `/^(?P<year>\d{4})-(?P<month>0[1-9]|1[012])-(?P<day>0[1-9]|[12][0-9]|3[01])/`

--
Ticket URL: <https://core.trac.wordpress.org/ticket/26798>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list