Skip to content

Commit

Permalink
Menus: Make use of wp_resolve_post_date() when updating menu items.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pento committed Jan 25, 2021
1 parent e2bb95a commit 6840112
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/wp-includes/nav-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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.
Expand Down
149 changes: 149 additions & 0 deletions tests/phpunit/tests/post/nav-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}
}

0 comments on commit 6840112

Please sign in to comment.