mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
Menus: Make use of wp_resolve_post_date() when updating menu items.
This allows a menu item `post_date` to be set to particular value, rather than only allowing it to be set to "now". In particular, the WordPress Importer can use this to perform faster, more accurate duplicate checks. Props jmdodd. Fixes #52189. git-svn-id: https://develop.svn.wordpress.org/trunk@50013 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -992,4 +992,153 @@ class Tests_Post_Nav_Menu extends WP_UnitTestCase {
|
||||
$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' );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user