Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1. **wp_checkdate() in functions.php:** #8400

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7383,6 +7383,11 @@
* @return bool True if valid date, false if not valid date.
*/
function wp_checkdate( $month, $day, $year, $source_date ) {

$checkdate = false;
if ( is_numeric( $month ) && is_numeric( $day ) && is_numeric( $year ) ) {

Check failure on line 7388 in src/wp-includes/functions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Tabs must be used to indent lines; spaces are not allowed
$checkdate = checkdate( intval( $month ), intval( $day ), intval( $year ) );

Check failure on line 7389 in src/wp-includes/functions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Tabs must be used to indent lines; spaces are not allowed
}

Check failure on line 7390 in src/wp-includes/functions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Tabs must be used to indent lines; spaces are not allowed

Check failure on line 7390 in src/wp-includes/functions.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Line indented incorrectly; expected 1 tabs, found 1 tabs and 1 spaces
/**
* Filters whether the given date is valid for the Gregorian calendar.
*
Expand All @@ -7391,7 +7396,7 @@
* @param bool $checkdate Whether the given date is valid.
* @param string $source_date Date to check.
*/
return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date );
return apply_filters( 'wp_checkdate', $checkdate, $source_date );
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -5348,11 +5348,12 @@
}

// Validate the date.
$month = (int) substr( $post_date, 5, 2 );
$day = (int) substr( $post_date, 8, 2 );
$year = (int) substr( $post_date, 0, 4 );
preg_match( "/^(?P<year>[0-9]{4})-(?P<month>0[1-9]|1[0-2])-(?P<day>0[1-9]|[1-2][0-9]|3[0-1])/", $post_date, $matches );

Check failure on line 5351 in src/wp-includes/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

String "/^(?P<year>[0-9]{4})-(?P<month>0[1-9]|1[0-2])-(?P<day>0[1-9]|[1-2][0-9]|3[0-1])/" does not require double quotes; use single quotes instead

$valid_date = wp_checkdate( $month, $day, $year, $post_date );
if ( empty( $matches ) || ! is_array( $matches ) || count( $matches ) < 4 ) {
return false;
}
$valid_date = wp_checkdate( $matches['month'], $matches['day'], $matches['year'], $post_date );

if ( ! $valid_date ) {
return false;
Expand Down
233 changes: 233 additions & 0 deletions tests/phpunit/tests/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,4 +756,237 @@
$this->assertTrue( use_block_editor_for_post( $restless_post_id ) );
remove_filter( 'use_block_editor_for_post', '__return_true' );
}

/**
* @ticket 26798
*
* @dataProvider data_wp_insert_post_handle_malformed_post_date
*
* The purpose of this test is to ensure that invalid dates do not
* cause PHP errors when wp_insert_post() is called, and that the
* posts are not actually "inserted" (created).
*/
public function test_wp_insert_post_handle_malformed_post_date( $input, $expected ) {
$post = array(
'post_author' => self::$editor_id,
'post_status' => 'publish',
'post_content' => 'content',
'post_title' => 'title',
'post_date' => $input,
);
// Inserting the post should fail gracefully.
$id = wp_insert_post( $post );
// Check if the result is "0" or not.
$result = ! empty( $id );
$this->assertEquals( $result, $expected );
}

/**
* @ticket 26798
*/
public function data_wp_insert_post_handle_malformed_post_date() {
return array(
array(
'2012-01-01',
true,
),
// 24-hour time format.
array(
'2012-01-01 13:00:00',
true,
),
// ISO8601 date with timezone.
array(
'2016-01-16T00:00:00Z',
true,
),
// ISO8601 date with timezone offset.
array(
'2016-01-16T00:00:00+0100',
true,
),
// RFC3339 Format.
array(
'1970-01-01T01:00:00+01:00',
true,
),
// RSS Format
array(
'1970-01-01T01:00:00+0100',
true,
),
// Leap year.
array(
'2012-02-29',
true,
),
// Strange formats.
array(
'2012-01-01 0',
true,
),
array(
'2012-01-01 25:00:00',
true,
),
array(
'2012-01-01 00:60:00',
true,
),
// Failures.
array(
'2012-08-0z',
false,
),
array(
'2012-08-1',
false,
),
array(
'201-01-08 00:00:00',
false,
),
array(
'201-01-08 00:60:00',
false,
),
array(
'201a-01-08 00:00:00',
false,
),
array(
'2012-1-08 00:00:00',
false,
),
array(
'2012-31-08 00:00:00',
false,
),
array(
'2012-01-8 00:00:00',
false,
),
array(
'2012-01-48 00:00:00',
false,
),
// Not a leap year.
array(
'2011-02-29',
false,
),
);
}

/**
* @ticket 26798
*
* @dataProvider data_wp_resolve_post_date_regex
*
* Tests the regex inside of wp_resolve_post_date(), with
* the emphasis on the date format (not the time).
*/
public function test_wp_resolve_post_date_regex( $date, $expected ) {
$out = wp_resolve_post_date( $date );
$this->assertEquals( $out, $expected );
}

/**
* @ticket 26798
*/
public function data_wp_resolve_post_date_regex() {
return array(
array(
'2012-01-01',
'2012-01-01',
),
array(
'2012-01-01 00:00:00',
'2012-01-01 00:00:00',
),
// ISO8601 date with timezone.
array(
'2016-01-16T00:00:00Z',
'2016-01-16T00:00:00Z',
),
// ISO8601 date with timezone offset.
array(
'2016-01-16T00:00:00+0100',
'2016-01-16T00:00:00+0100',
),
// RFC3339 Format.
array(
'1970-01-01T01:00:00+01:00',
'1970-01-01T01:00:00+01:00',
),
// RSS Format
array(
'1970-01-01T01:00:00+0100',
'1970-01-01T01:00:00+0100',
),
// 24-hour time format.
array(
'2012-01-01 13:00:00',
'2012-01-01 13:00:00',
),
array(
'2016-01-16T00:0',
'2016-01-16T00:0',
),
array(
'2012-01-01 0',
'2012-01-01 0',
),
array(
'2012-01-01 00:00',
'2012-01-01 00:00',
),
array(
'2012-01-01 25:00:00',
'2012-01-01 25:00:00',
),
array(
'2012-01-01 00:60:00',
'2012-01-01 00:60:00',
),
array(
'2012-01-01 00:00:60',
'2012-01-01 00:00:60',
),
array(
'201-01-08',
false,
),
array(
'201a-01-08',
false,
),
array(
'2012-1-08',
false,
),
array(
'2012-31-08',
false,
),
array(
'2012-01-8',
false,
),
array(
'2012-01-48 00:00:00',
false,
),
// Leap year.
array(
'2012-02-29',
'2012-02-29',
),
array(
'2011-02-29',
false,
),
);
}

}

Check failure on line 992 in tests/phpunit/tests/post.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

The closing brace for the class must go on the next line after the body
Loading