diff --git src/wp-includes/post.php src/wp-includes/post.php
index 4ceb8dd3a6..b94b692b71 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 date-formatted string (YYYY-MM-DD H:i:s). |
| 4916 | preg_match( "/^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])[\s](?:[0-1][0-9]|2[1-3]):[0-5][0-9]:[0-5][0-9]$/", $post_date, $matches ); |
| 4917 | if ( empty( $matches ) || ! is_array( $matches ) || count( $matches ) < 4 ) { |
| 4918 | return false; |
| 4919 | } |
4919 | 4920 | |
4920 | | $valid_date = wp_checkdate( $month, $day, $year, $post_date ); |
| 4921 | $valid_date = wp_checkdate( $matches[2], $matches[3], $matches[1], $post_date ); |
4921 | 4922 | |
4922 | 4923 | if ( ! $valid_date ) { |
4923 | 4924 | return false; |
4924 | 4925 | } |
| 4926 | |
4925 | 4927 | return $post_date; |
4926 | 4928 | } |
4927 | 4929 | |
diff --git tests/phpunit/tests/post.php tests/phpunit/tests/post.php
index 108e6ef4a6..82a62cb3a4 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 12:00:00', |
| 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 | * Tests the regex inside of wp_resolve_post_date(). |
| 1665 | */ |
| 1666 | public function test_wp_resolve_post_date_regex() { |
| 1667 | $invalid_dates = array( |
| 1668 | '2012-01-08', |
| 1669 | '201-01-08 00:00:00', |
| 1670 | '201a-01-08 00:00:00', |
| 1671 | '2012-1-08 00:00:00', |
| 1672 | '2012-31-08 00:00:00', |
| 1673 | '2012-01-8 00:00:00', |
| 1674 | '2012-01-48 00:00:00', |
| 1675 | '2012-01-08 0:00:00', |
| 1676 | '2012-01-08 24:00:00', |
| 1677 | '2012-01-08 00:0:00', |
| 1678 | '2012-01-08 00:60:00', |
| 1679 | '2012-01-08 00:00:0', |
| 1680 | '2012-01-08 00:00:60', |
| 1681 | ); |
| 1682 | foreach( $invalid_dates as $date ) { |
| 1683 | $out = wp_resolve_post_date( $date ); |
| 1684 | $this->assertFalse( $out ); |
| 1685 | } |
| 1686 | } |
| 1687 | |
1642 | 1688 | /** |
1643 | 1689 | * Ensure sticking a post updates the `sticky_posts` option. |
1644 | 1690 | * |