From d86f9f140f826a64e32b3a00a4f1cefcbaa41a15 Mon Sep 17 00:00:00 2001 From: Peter Westwood Date: Thu, 20 Sep 2012 10:46:50 +0000 Subject: [PATCH] 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: https://develop.svn.wordpress.org/trunk@21922 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/includes/post.php | 8 ++++---- wp-includes/functions.php | 10 ++++++++++ wp-includes/post.php | 7 +++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index 223f1b9728..8296607968 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -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'] ); } diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 631be64dc1..e78be346b0 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -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 ); +} \ No newline at end of file diff --git a/wp-includes/post.php b/wp-includes/post.php index ff544404f9..b2c3c8f5d1 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -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 ) {