Posting: Improve the invalid date protection code based on feedback from nacin.

* Introduce a wp_checkdate() function with a single filter to centralise the code that validates dates.
 * Improve the error message
 * Correctly handle the return value of wp_insert_post which is not always a WP_Error on failure

Fixes #17180


git-svn-id: http://core.svn.wordpress.org/trunk@21922 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Westwood 2012-09-20 10:46:50 +00:00
parent c05f6dc6dd
commit fa8a509ff3
3 changed files with 19 additions and 6 deletions

View File

@ -122,11 +122,11 @@ function _wp_translate_postdata( $update = false, $post_data = null ) {
$hh = ($hh > 23 ) ? $hh -24 : $hh;
$mn = ($mn > 59 ) ? $mn -60 : $mn;
$ss = ($ss > 59 ) ? $ss -60 : $ss;
$valid_date = apply_filters( '_wp_translate_postdata_valid_date', checkdate( $mm, $jj, $aa ), $post_data );
if ( !$valid_date ) {
return new WP_Error( 'invalid_date', __( 'Woops, the provided date is invalid.' ) );
}
$post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
$valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
if ( !$valid_date ) {
return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
}
$post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
}

View File

@ -3703,3 +3703,13 @@ function _device_can_upload() {
return true;
}
/**
* Test if the supplied date is valid for the Gregorian calendar
*
* @since 3.5.0
*
* @return bool true|false
*/
function wp_checkdate( $month, $day, $year, $source_date ) {
return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date );
}

View File

@ -2637,9 +2637,12 @@ function wp_insert_post($postarr, $wp_error = false) {
$mm = substr( $post_date, 5, 2 );
$jj = substr( $post_date, 8, 2 );
$aa = substr( $post_date, 0, 4 );
$valid_date = apply_filters( 'wp_insert_post_validate_date', checkdate( $mm, $jj, $aa ), $post_date );
$valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
if ( !$valid_date ) {
return new WP_Error( 'invalid_date', __( 'Woops, the provided date is invalid.' ) );
if ( $wp_error )
return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
else
return 0;
}
if ( empty($post_date_gmt) || '0000-00-00 00:00:00' == $post_date_gmt ) {