Posting: Make it much harder to create posts with invalid dates by enforcing the post date tests in the UI and the backend code.

Previously you could quite easily send a new post into the back of beyond by specifying an invalid date like the 30th Feb and this was very confusing.
Sometimes it would seem to work and sometimes the post would end up very far in the past - depending on the mysql version and other factors.

Fixes #17180 props jkudish.


git-svn-id: http://core.svn.wordpress.org/trunk@21921 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Westwood 2012-09-19 21:43:35 +00:00
parent fabcc374fd
commit c05f6dc6dd
3 changed files with 23 additions and 0 deletions

View File

@ -122,6 +122,10 @@ 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 );
$post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
}

View File

@ -528,6 +528,16 @@ jQuery(document).ready( function($) {
return false;
});
$('#post').submit(function(e){
if ( !updateText() ) {
e.preventDefault();
$('#timestampdiv').show();
$('#publishing-action .ajax-loading').css('visibility', 'hidden');
$('#publish').prop('disabled', false).removeClass('button-primary-disabled');
return false;
}
});
$('#post-status-select').siblings('a.edit-post-status').click(function() {
if ($('#post-status-select').is(":hidden")) {
$('#post-status-select').slideDown('fast');

View File

@ -2633,6 +2633,15 @@ function wp_insert_post($postarr, $wp_error = false) {
if ( empty($post_date) || '0000-00-00 00:00:00' == $post_date )
$post_date = current_time('mysql');
// validate the date
$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 );
if ( !$valid_date ) {
return new WP_Error( 'invalid_date', __( 'Woops, the provided date is invalid.' ) );
}
if ( empty($post_date_gmt) || '0000-00-00 00:00:00' == $post_date_gmt ) {
if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
$post_date_gmt = get_gmt_from_date($post_date);