diff --git a/src/wp-includes/nav-menu.php b/src/wp-includes/nav-menu.php index 9261a4a5c5713..547ca7f709ac2 100644 --- a/src/wp-includes/nav-menu.php +++ b/src/wp-includes/nav-menu.php @@ -436,20 +436,22 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item } $defaults = array( - 'menu-item-db-id' => $menu_item_db_id, - 'menu-item-object-id' => 0, - 'menu-item-object' => '', - 'menu-item-parent-id' => 0, - 'menu-item-position' => 0, - 'menu-item-type' => 'custom', - 'menu-item-title' => '', - 'menu-item-url' => '', - 'menu-item-description' => '', - 'menu-item-attr-title' => '', - 'menu-item-target' => '', - 'menu-item-classes' => '', - 'menu-item-xfn' => '', - 'menu-item-status' => '', + 'menu-item-db-id' => $menu_item_db_id, + 'menu-item-object-id' => 0, + 'menu-item-object' => '', + 'menu-item-parent-id' => 0, + 'menu-item-position' => 0, + 'menu-item-type' => 'custom', + 'menu-item-title' => '', + 'menu-item-url' => '', + 'menu-item-description' => '', + 'menu-item-attr-title' => '', + 'menu-item-target' => '', + 'menu-item-classes' => '', + 'menu-item-xfn' => '', + 'menu-item-status' => '', + 'menu-item-post-date' => '', + 'menu-item-post-date-gmt' => '', ); $args = wp_parse_args( $menu_item_data, $defaults ); @@ -513,6 +515,11 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item 'post_type' => 'nav_menu_item', ); + $post_date = wp_resolve_post_date( $args['menu-item-post-date'], $args['menu-item-post-date-gmt'] ); + if ( $post_date ) { + $post['post_date'] = $post_date; + } + $update = 0 != $menu_item_db_id; // New menu item. Default is draft status. diff --git a/tests/phpunit/tests/post/nav-menu.php b/tests/phpunit/tests/post/nav-menu.php index e242066fb2cd1..284267f60d3d7 100644 --- a/tests/phpunit/tests/post/nav-menu.php +++ b/tests/phpunit/tests/post/nav-menu.php @@ -992,4 +992,153 @@ function test_wp_update_nav_menu_item_with_special_characters_in_category_name() $category_item = get_post( $category_item_id ); $this->assertEmpty( $category_item->post_title ); } + + /** + * Test passed post_date/post_date_gmt. + * + * When inserting a nav menu item, it should be possible to set the post_date + * of it to ensure that this data is maintained during an import. + * + * @ticket 52189 + */ + function test_wp_update_nav_menu_item_with_post_date() { + $post_date = '2020-12-28 11:26:35'; + $post_date_gmt = '2020-12-29 10:11:45'; + $invalid_date = '2020-12-41 14:15:27'; + + $post_id = self::factory()->post->create( + array( + 'post_status' => 'publish', + ) + ); + + $menu_item_id = wp_update_nav_menu_item( + $this->menu_id, + 0, + array( + 'menu-item-type' => 'post_type', + 'menu-item-object' => 'post', + 'menu-item-object-id' => $post_id, + 'menu-item-status' => 'publish', + ) + ); + $post = get_post( $menu_item_id ); + $this->assertEqualsWithDelta( strtotime( date( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' ); + + $menu_item_id = wp_update_nav_menu_item( + $this->menu_id, + 0, + array( + 'menu-item-type' => 'post_type', + 'menu-item-object' => 'post', + 'menu-item-object-id' => $post_id, + 'menu-item-status' => 'publish', + 'menu-item-post-date-gmt' => $post_date_gmt, + ) + ); + $post = get_post( $menu_item_id ); + $this->assertEquals( get_date_from_gmt( $post_date_gmt ), $post->post_date ); + + $menu_item_id = wp_update_nav_menu_item( + $this->menu_id, + 0, + array( + 'menu-item-type' => 'post_type', + 'menu-item-object' => 'post', + 'menu-item-object-id' => $post_id, + 'menu-item-status' => 'publish', + 'menu-item-post-date-gmt' => $invalid_date, + ) + ); + $post = get_post( $menu_item_id ); + $this->assertEquals( '1970-01-01 00:00:00', $post->post_date ); + + $menu_item_id = wp_update_nav_menu_item( + $this->menu_id, + 0, + array( + 'menu-item-type' => 'post_type', + 'menu-item-object' => 'post', + 'menu-item-object-id' => $post_id, + 'menu-item-status' => 'publish', + 'menu-item-post-date' => $post_date, + ) + ); + $post = get_post( $menu_item_id ); + $this->assertEquals( $post_date, $post->post_date ); + + $menu_item_id = wp_update_nav_menu_item( + $this->menu_id, + 0, + array( + 'menu-item-type' => 'post_type', + 'menu-item-object' => 'post', + 'menu-item-object-id' => $post_id, + 'menu-item-status' => 'publish', + 'menu-item-post-date' => $post_date, + 'menu-item-post-date-gmt' => $post_date_gmt, + ) + ); + $post = get_post( $menu_item_id ); + $this->assertEquals( $post_date, $post->post_date ); + + $menu_item_id = wp_update_nav_menu_item( + $this->menu_id, + 0, + array( + 'menu-item-type' => 'post_type', + 'menu-item-object' => 'post', + 'menu-item-object-id' => $post_id, + 'menu-item-status' => 'publish', + 'menu-item-post-date' => $post_date, + 'menu-item-post-date-gmt' => $invalid_date, + ) + ); + $post = get_post( $menu_item_id ); + $this->assertEquals( $post_date, $post->post_date ); + + $menu_item_id = wp_update_nav_menu_item( + $this->menu_id, + 0, + array( + 'menu-item-type' => 'post_type', + 'menu-item-object' => 'post', + 'menu-item-object-id' => $post_id, + 'menu-item-status' => 'publish', + 'menu-item-post-date' => $invalid_date, + ) + ); + $post = get_post( $menu_item_id ); + $this->assertEqualsWithDelta( strtotime( date( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' ); + + $menu_item_id = wp_update_nav_menu_item( + $this->menu_id, + 0, + array( + 'menu-item-type' => 'post_type', + 'menu-item-object' => 'post', + 'menu-item-object-id' => $post_id, + 'menu-item-status' => 'publish', + 'menu-item-post-date' => $invalid_date, + 'menu-item-post-date-gmt' => $post_date_gmt, + ) + ); + $post = get_post( $menu_item_id ); + $this->assertEqualsWithDelta( strtotime( date( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' ); + + $menu_item_id = wp_update_nav_menu_item( + $this->menu_id, + 0, + array( + 'menu-item-type' => 'post_type', + 'menu-item-object' => 'post', + 'menu-item-object-id' => $post_id, + 'menu-item-status' => 'publish', + 'menu-item-post-date' => $invalid_date, + 'menu-item-post-date-gmt' => $invalid_date, + ) + ); + $post = get_post( $menu_item_id ); + $this->assertEqualsWithDelta( strtotime( date( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' ); + } }