Make WordPress Core

Changeset 58854


Ignore:
Timestamp:
08/05/2024 06:58:49 PM (2 years ago)
Author:
SergeyBiryukov
Message:

Menus: Check if taxonomy term exists in wp_update_nav_menu_item().

When inserting a term from a non-existing taxonomy as a nav item, the post_title property should be empty, and the function should not throw a fatal error for wp_specialchars_decode().

Includes bringing some consistency to similar checks for post types and post type archives in the same code fragment.

Follow-up to [14283], [14450], [35382], [36095].

Props dd32, narenin, mukesh27, SergeyBiryukov.
Fixes #61799.

Location:
trunk
Files:
2 edited

Legend:

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

    r58119 r58854  
    492492
    493493                $original_title = '';
     494
    494495                if ( 'taxonomy' === $args['menu-item-type'] ) {
    495                         $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
    496                         $original_title  = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
     496                        $original_object = get_term( $args['menu-item-object-id'], $args['menu-item-object'] );
     497
     498                        if ( $original_object instanceof WP_Term ) {
     499                                $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
     500                                $original_title  = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
     501                        }
    497502                } elseif ( 'post_type' === $args['menu-item-type'] ) {
    498 
    499503                        $original_object = get_post( $args['menu-item-object-id'] );
    500                         $original_parent = (int) $original_object->post_parent;
    501                         $original_title  = $original_object->post_title;
     504
     505                        if ( $original_object instanceof WP_Post ) {
     506                                $original_parent = (int) $original_object->post_parent;
     507                                $original_title  = $original_object->post_title;
     508                        }
    502509                } elseif ( 'post_type_archive' === $args['menu-item-type'] ) {
    503510                        $original_object = get_post_type_object( $args['menu-item-object'] );
    504                         if ( $original_object ) {
     511
     512                        if ( $original_object instanceof WP_Post_Type ) {
    505513                                $original_title = $original_object->labels->archives;
    506514                        }
  • trunk/tests/phpunit/tests/post/nav-menu.php

    r57987 r58854  
    12101210
    12111211        /**
     1212         * Tests `wp_update_nav_menu_item()` with a non-existing taxonomy.
     1213         *
     1214         * When inserting a term from a non-existing taxonomy as a nav item,
     1215         * the `post_title` property should be empty, and the function
     1216         * should not throw a fatal error for `wp_specialchars_decode()`.
     1217         *
     1218         * @ticket 61799
     1219         */
     1220        public function test_wp_update_nav_menu_item_with_invalid_taxonomy() {
     1221                register_taxonomy( 'invalid', 'post' );
     1222                $term = self::factory()->term->create_and_get( array( 'taxonomy' => 'invalid' ) );
     1223                unregister_taxonomy( 'invalid' );
     1224
     1225                $menu_item_id = wp_update_nav_menu_item(
     1226                        $this->menu_id,
     1227                        0,
     1228                        array(
     1229                                'menu-item-type'      => 'taxonomy',
     1230                                'menu-item-object'    => 'invalid',
     1231                                'menu-item-object-id' => $term->term_id,
     1232                                'menu-item-status'    => 'publish',
     1233                        )
     1234                );
     1235
     1236                $menu_item = get_post( $menu_item_id );
     1237                $this->assertEmpty( $menu_item->post_title );
     1238        }
     1239
     1240        /**
    12121241         * Test passed post_date/post_date_gmt.
    12131242         *
Note: See TracChangeset for help on using the changeset viewer.