Make WordPress Core

Changeset 50013


Ignore:
Timestamp:
01/25/2021 01:22:06 AM (6 years ago)
Author:
pento
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/nav-menu.php

    r49963 r50013  
    437437
    438438        $defaults = array(
    439                 'menu-item-db-id'       => $menu_item_db_id,
    440                 'menu-item-object-id'   => 0,
    441                 'menu-item-object'      => '',
    442                 'menu-item-parent-id'   => 0,
    443                 'menu-item-position'    => 0,
    444                 'menu-item-type'        => 'custom',
    445                 'menu-item-title'       => '',
    446                 'menu-item-url'         => '',
    447                 'menu-item-description' => '',
    448                 'menu-item-attr-title'  => '',
    449                 'menu-item-target'      => '',
    450                 'menu-item-classes'     => '',
    451                 'menu-item-xfn'         => '',
    452                 'menu-item-status'      => '',
     439                'menu-item-db-id'         => $menu_item_db_id,
     440                'menu-item-object-id'     => 0,
     441                'menu-item-object'        => '',
     442                'menu-item-parent-id'     => 0,
     443                'menu-item-position'      => 0,
     444                'menu-item-type'          => 'custom',
     445                'menu-item-title'         => '',
     446                'menu-item-url'           => '',
     447                'menu-item-description'   => '',
     448                'menu-item-attr-title'    => '',
     449                'menu-item-target'        => '',
     450                'menu-item-classes'       => '',
     451                'menu-item-xfn'           => '',
     452                'menu-item-status'        => '',
     453                'menu-item-post-date'     => '',
     454                'menu-item-post-date-gmt' => '',
    453455        );
    454456
     
    513515                'post_type'    => 'nav_menu_item',
    514516        );
     517
     518        $post_date = wp_resolve_post_date( $args['menu-item-post-date'], $args['menu-item-post-date-gmt'] );
     519        if ( $post_date ) {
     520                $post['post_date'] = $post_date;
     521        }
    515522
    516523        $update = 0 != $menu_item_db_id;
  • trunk/tests/phpunit/tests/post/nav-menu.php

    r49327 r50013  
    993993                $this->assertEmpty( $category_item->post_title );
    994994        }
     995
     996        /**
     997         * Test passed post_date/post_date_gmt.
     998         *
     999         * When inserting a nav menu item, it should be possible to set the post_date
     1000         * of it to ensure that this data is maintained during an import.
     1001         *
     1002         * @ticket 52189
     1003         */
     1004        function test_wp_update_nav_menu_item_with_post_date() {
     1005                $post_date     = '2020-12-28 11:26:35';
     1006                $post_date_gmt = '2020-12-29 10:11:45';
     1007                $invalid_date  = '2020-12-41 14:15:27';
     1008
     1009                $post_id = self::factory()->post->create(
     1010                        array(
     1011                                'post_status' => 'publish',
     1012                        )
     1013                );
     1014
     1015                $menu_item_id = wp_update_nav_menu_item(
     1016                        $this->menu_id,
     1017                        0,
     1018                        array(
     1019                                'menu-item-type'      => 'post_type',
     1020                                'menu-item-object'    => 'post',
     1021                                'menu-item-object-id' => $post_id,
     1022                                'menu-item-status'    => 'publish',
     1023                        )
     1024                );
     1025                $post = get_post( $menu_item_id );
     1026                $this->assertEqualsWithDelta( strtotime( date( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
     1027
     1028                $menu_item_id = wp_update_nav_menu_item(
     1029                        $this->menu_id,
     1030                        0,
     1031                        array(
     1032                                'menu-item-type'          => 'post_type',
     1033                                'menu-item-object'        => 'post',
     1034                                'menu-item-object-id'     => $post_id,
     1035                                'menu-item-status'        => 'publish',
     1036                                'menu-item-post-date-gmt' => $post_date_gmt,
     1037                        )
     1038                );
     1039                $post = get_post( $menu_item_id );
     1040                $this->assertEquals( get_date_from_gmt( $post_date_gmt ), $post->post_date );
     1041
     1042                $menu_item_id = wp_update_nav_menu_item(
     1043                        $this->menu_id,
     1044                        0,
     1045                        array(
     1046                                'menu-item-type'          => 'post_type',
     1047                                'menu-item-object'        => 'post',
     1048                                'menu-item-object-id'     => $post_id,
     1049                                'menu-item-status'        => 'publish',
     1050                                'menu-item-post-date-gmt' => $invalid_date,
     1051                        )
     1052                );
     1053                $post = get_post( $menu_item_id );
     1054                $this->assertEquals( '1970-01-01 00:00:00', $post->post_date );
     1055
     1056                $menu_item_id = wp_update_nav_menu_item(
     1057                        $this->menu_id,
     1058                        0,
     1059                        array(
     1060                                'menu-item-type'      => 'post_type',
     1061                                'menu-item-object'    => 'post',
     1062                                'menu-item-object-id' => $post_id,
     1063                                'menu-item-status'    => 'publish',
     1064                                'menu-item-post-date' => $post_date,
     1065                        )
     1066                );
     1067                $post = get_post( $menu_item_id );
     1068                $this->assertEquals( $post_date, $post->post_date );
     1069
     1070                $menu_item_id = wp_update_nav_menu_item(
     1071                        $this->menu_id,
     1072                        0,
     1073                        array(
     1074                                'menu-item-type'          => 'post_type',
     1075                                'menu-item-object'        => 'post',
     1076                                'menu-item-object-id'     => $post_id,
     1077                                'menu-item-status'        => 'publish',
     1078                                'menu-item-post-date'     => $post_date,
     1079                                'menu-item-post-date-gmt' => $post_date_gmt,
     1080                        )
     1081                );
     1082                $post = get_post( $menu_item_id );
     1083                $this->assertEquals( $post_date, $post->post_date );
     1084
     1085                $menu_item_id = wp_update_nav_menu_item(
     1086                        $this->menu_id,
     1087                        0,
     1088                        array(
     1089                                'menu-item-type'          => 'post_type',
     1090                                'menu-item-object'        => 'post',
     1091                                'menu-item-object-id'     => $post_id,
     1092                                'menu-item-status'        => 'publish',
     1093                                'menu-item-post-date'     => $post_date,
     1094                                'menu-item-post-date-gmt' => $invalid_date,
     1095                        )
     1096                );
     1097                $post = get_post( $menu_item_id );
     1098                $this->assertEquals( $post_date, $post->post_date );
     1099
     1100                $menu_item_id = wp_update_nav_menu_item(
     1101                        $this->menu_id,
     1102                        0,
     1103                        array(
     1104                                'menu-item-type'      => 'post_type',
     1105                                'menu-item-object'    => 'post',
     1106                                'menu-item-object-id' => $post_id,
     1107                                'menu-item-status'    => 'publish',
     1108                                'menu-item-post-date' => $invalid_date,
     1109                        )
     1110                );
     1111                $post = get_post( $menu_item_id );
     1112                $this->assertEqualsWithDelta( strtotime( date( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
     1113
     1114                $menu_item_id = wp_update_nav_menu_item(
     1115                        $this->menu_id,
     1116                        0,
     1117                        array(
     1118                                'menu-item-type'          => 'post_type',
     1119                                'menu-item-object'        => 'post',
     1120                                'menu-item-object-id'     => $post_id,
     1121                                'menu-item-status'        => 'publish',
     1122                                'menu-item-post-date'     => $invalid_date,
     1123                                'menu-item-post-date-gmt' => $post_date_gmt,
     1124                        )
     1125                );
     1126                $post = get_post( $menu_item_id );
     1127                $this->assertEqualsWithDelta( strtotime( date( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
     1128
     1129                $menu_item_id = wp_update_nav_menu_item(
     1130                        $this->menu_id,
     1131                        0,
     1132                        array(
     1133                                'menu-item-type'          => 'post_type',
     1134                                'menu-item-object'        => 'post',
     1135                                'menu-item-object-id'     => $post_id,
     1136                                'menu-item-status'        => 'publish',
     1137                                'menu-item-post-date'     => $invalid_date,
     1138                                'menu-item-post-date-gmt' => $invalid_date,
     1139                        )
     1140                );
     1141                $post = get_post( $menu_item_id );
     1142                $this->assertEqualsWithDelta( strtotime( date( 'Y-m-d H:i:s' ) ), strtotime( $post->post_date ), 2, 'The dates should be equal' );
     1143        }
    9951144}
Note: See TracChangeset for help on using the changeset viewer.