diff --git src/wp-includes/functions.php src/wp-includes/functions.php
index 4ecad8e808..e0888653b3 100644
|
|
function wp_checkdate( $month, $day, $year, $source_date ) { |
7066 | 7066 | * @param bool $checkdate Whether the given date is valid. |
7067 | 7067 | * @param string $source_date Date to check. |
7068 | 7068 | */ |
7069 | | return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date ); |
| 7069 | return apply_filters( 'wp_checkdate', checkdate( intval( $month ), intval( $day ), intval( $year ) ), $source_date ); |
7070 | 7070 | } |
7071 | 7071 | |
7072 | 7072 | /** |
diff --git src/wp-includes/post.php src/wp-includes/post.php
index 4ceb8dd3a6..cb17ab504d 100644
|
|
function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) { |
4912 | 4912 | } |
4913 | 4913 | } |
4914 | 4914 | |
4915 | | // Validate the date. |
4916 | | $month = substr( $post_date, 5, 2 ); |
4917 | | $day = substr( $post_date, 8, 2 ); |
4918 | | $year = substr( $post_date, 0, 4 ); |
| 4915 | // Ensure we have a valid mysql-formatted date string (YYYY-MM-DD). |
| 4916 | if ( 10 === strlen( $post_date ) ) { |
| 4917 | preg_match( "/^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])/", $post_date, $matches ); |
| 4918 | } else { |
| 4919 | // Validate any other attempts at the (YYYY-MM-DD H:i:s) or ISO formats. |
| 4920 | preg_match( "/^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])(?:\s|T)(?:[0-1][0-9]|2[1-3]):[0-5][0-9]:[0-5][0-9]/", $post_date, $matches ); |
| 4921 | } |
| 4922 | |
| 4923 | if ( empty( $matches ) || ! is_array( $matches ) || count( $matches ) < 4 ) { |
| 4924 | return false; |
| 4925 | } |
4919 | 4926 | |
4920 | | $valid_date = wp_checkdate( $month, $day, $year, $post_date ); |
| 4927 | $valid_date = wp_checkdate( $matches[2], $matches[3], $matches[1], $post_date ); |
4921 | 4928 | |
4922 | 4929 | if ( ! $valid_date ) { |
4923 | 4930 | return false; |
4924 | 4931 | } |
| 4932 | |
4925 | 4933 | return $post_date; |
4926 | 4934 | } |
4927 | 4935 | |
diff --git tests/phpunit/tests/post.php tests/phpunit/tests/post.php
index 108e6ef4a6..ed77658c2d 100644
|
|
class Tests_Post extends WP_UnitTestCase { |
1220 | 1220 | $this->assertSame( $post['post_date_gmt'], $out->post_date_gmt ); |
1221 | 1221 | } |
1222 | 1222 | |
| 1223 | /** |
| 1224 | * @ticket 26798 |
| 1225 | * |
| 1226 | * On a deeper level, this is handled by test_wp_resolve_post_date. |
| 1227 | */ |
| 1228 | public function test_wp_insert_post_reject_malformed_post_date() { |
| 1229 | $post = array( |
| 1230 | 'post_author' => self::$editor_id, |
| 1231 | 'post_status' => 'publish', |
| 1232 | 'post_content' => 'content', |
| 1233 | 'post_title' => 'title', |
| 1234 | 'post_date' => '2012-01-8', |
| 1235 | ); |
| 1236 | |
| 1237 | // Inserting the post should fail gracefully. |
| 1238 | $id = wp_insert_post( $post ); |
| 1239 | $this->assertSame( 0, $id ); |
| 1240 | } |
| 1241 | |
1223 | 1242 | public function test_wp_delete_post_reassign_hierarchical_post_type() { |
1224 | 1243 | $grandparent_page_id = self::factory()->post->create( array( 'post_type' => 'page' ) ); |
1225 | 1244 | $parent_page_id = self::factory()->post->create( |
… |
… |
class Tests_Post extends WP_UnitTestCase { |
1639 | 1658 | $this->assertFalse( $resolved_post_date ); |
1640 | 1659 | } |
1641 | 1660 | |
| 1661 | /** |
| 1662 | * @ticket 26798 |
| 1663 | * |
| 1664 | * @dataProvider data_wp_resolve_post_date_regex |
| 1665 | * |
| 1666 | * Tests the regex inside of wp_resolve_post_date(). |
| 1667 | */ |
| 1668 | public function test_wp_resolve_post_date_regex( $date, $expected ) { |
| 1669 | $out = wp_resolve_post_date( $date ); |
| 1670 | $this->assertEquals( $out, $expected ); |
| 1671 | } |
| 1672 | |
| 1673 | public function data_wp_resolve_post_date_regex() { |
| 1674 | return array( |
| 1675 | array( |
| 1676 | '2012-01-01', |
| 1677 | '2012-01-01', |
| 1678 | ), |
| 1679 | array( |
| 1680 | '2012-01-01 00:00:00', |
| 1681 | '2012-01-01 00:00:00', |
| 1682 | ), |
| 1683 | array( |
| 1684 | '2016-01-16T00:00:00Z', |
| 1685 | '2016-01-16T00:00:00Z', |
| 1686 | ), |
| 1687 | array( |
| 1688 | '2016-01-16T00:0', |
| 1689 | false, |
| 1690 | ), |
| 1691 | array( |
| 1692 | '2012-01-01 0', |
| 1693 | false, |
| 1694 | ), |
| 1695 | array( |
| 1696 | '2012-01-01 00:00', |
| 1697 | false, |
| 1698 | ), |
| 1699 | array( |
| 1700 | '2012-01-01 25:00:00', |
| 1701 | false, |
| 1702 | ), |
| 1703 | array( |
| 1704 | '2012-01-01 00:60:00', |
| 1705 | false, |
| 1706 | ), |
| 1707 | array( |
| 1708 | '2012-01-01 00:00:60', |
| 1709 | false, |
| 1710 | ), |
| 1711 | array( |
| 1712 | '201-01-08 00:00:00', |
| 1713 | false, |
| 1714 | ), |
| 1715 | array( |
| 1716 | '201a-01-08 00:00:00', |
| 1717 | false, |
| 1718 | ), |
| 1719 | array( |
| 1720 | '2012-1-08 00:00:00', |
| 1721 | false, |
| 1722 | ), |
| 1723 | array( |
| 1724 | '2012-31-08 00:00:00', |
| 1725 | false, |
| 1726 | ), |
| 1727 | array( |
| 1728 | '2012-01-8 00:00:00', |
| 1729 | false, |
| 1730 | ), |
| 1731 | array( |
| 1732 | '2012-01-48 00:00:00', |
| 1733 | false, |
| 1734 | ), |
| 1735 | // Ensure leap years are handled correctly. |
| 1736 | array( |
| 1737 | '2012-02-29', |
| 1738 | '2012-02-29', |
| 1739 | ), |
| 1740 | array( |
| 1741 | '2011-02-29', |
| 1742 | false, |
| 1743 | ), |
| 1744 | |
| 1745 | ); |
| 1746 | } |
| 1747 | |
1642 | 1748 | /** |
1643 | 1749 | * Ensure sticking a post updates the `sticky_posts` option. |
1644 | 1750 | * |