From 252e14ac27345eaaff75de44555403c1798db3ee Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 25 Aug 2019 21:31:43 +0000 Subject: [PATCH] Menus: In `wp_setup_nav_menu_item()` and `Walker_Nav_Menu_Edit::start_el()`, check if the post or term associated with the menu item still exists to avoid a PHP notice. If the associated post or term no longer exists, mark the menu item as invalid. Props mehulkaklotar, kamrankhorsandi, cristiano.zanca, SergeyBiryukov. Fixes #31703. git-svn-id: https://develop.svn.wordpress.org/trunk@45891 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-walker-nav-menu-edit.php | 11 +++-- src/wp-includes/nav-menu.php | 46 +++++++++++++------ 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/wp-admin/includes/class-walker-nav-menu-edit.php b/src/wp-admin/includes/class-walker-nav-menu-edit.php index 9782618a7e..29f579ab93 100644 --- a/src/wp-admin/includes/class-walker-nav-menu-edit.php +++ b/src/wp-admin/includes/class-walker-nav-menu-edit.php @@ -71,14 +71,17 @@ class Walker_Nav_Menu_Edit extends Walker_Nav_Menu { ); $original_title = false; + if ( 'taxonomy' == $item->type ) { - $original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' ); - if ( is_wp_error( $original_title ) ) { - $original_title = false; + $original_object = get_term( (int) $item->object_id, $item->object ); + if ( $original_object && ! is_wp_error( $original_title ) ) { + $original_title = $original_object->name; } } elseif ( 'post_type' == $item->type ) { $original_object = get_post( $item->object_id ); - $original_title = get_the_title( $original_object->ID ); + if ( $original_object ) { + $original_title = get_the_title( $original_object->ID ); + } } elseif ( 'post_type_archive' == $item->type ) { $original_object = get_post_type_object( $item->object ); if ( $original_object ) { diff --git a/src/wp-includes/nav-menu.php b/src/wp-includes/nav-menu.php index 98584eac8a..62bdbbf939 100644 --- a/src/wp-includes/nav-menu.php +++ b/src/wp-includes/nav-menu.php @@ -819,33 +819,40 @@ function wp_setup_nav_menu_item( $menu_item ) { $menu_item->_invalid = true; } - $menu_item->url = get_permalink( $menu_item->object_id ); - $original_object = get_post( $menu_item->object_id ); - /** This filter is documented in wp-includes/post-template.php */ - $original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID ); + + if ( $original_object ) { + $menu_item->url = get_permalink( $original_object->ID ); + /** This filter is documented in wp-includes/post-template.php */ + $original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID ); + } else { + $menu_item->url = ''; + $original_title = ''; + $menu_item->_invalid = true; + } if ( '' === $original_title ) { /* translators: %d: ID of a post */ - $original_title = sprintf( __( '#%d (no title)' ), $original_object->ID ); + $original_title = sprintf( __( '#%d (no title)' ), $menu_item->object_id ); } - $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title; + $menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title; } elseif ( 'post_type_archive' == $menu_item->type ) { $object = get_post_type_object( $menu_item->object ); if ( $object ) { - $menu_item->title = '' == $menu_item->post_title ? $object->labels->archives : $menu_item->post_title; + $menu_item->title = ( '' === $menu_item->post_title ) ? $object->labels->archives : $menu_item->post_title; $post_type_description = $object->description; } else { - $menu_item->_invalid = true; $post_type_description = ''; + $menu_item->_invalid = true; } $menu_item->type_label = __( 'Post Type Archive' ); $post_content = wp_trim_words( $menu_item->post_content, 200 ); - $post_type_description = '' == $post_content ? $post_type_description : $post_content; + $post_type_description = ( '' === $post_content ) ? $post_type_description : $post_content; $menu_item->url = get_post_type_archive_link( $menu_item->object ); + } elseif ( 'taxonomy' == $menu_item->type ) { $object = get_taxonomy( $menu_item->object ); if ( $object ) { @@ -855,14 +862,23 @@ function wp_setup_nav_menu_item( $menu_item ) { $menu_item->_invalid = true; } - $term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object ); - $menu_item->url = ! is_wp_error( $term_url ) ? $term_url : ''; + $original_object = get_term( (int) $menu_item->object_id, $menu_item->object ); - $original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' ); - if ( is_wp_error( $original_title ) ) { - $original_title = false; + if ( $original_object && ! is_wp_error( $original_object ) ) { + $menu_item->url = get_term_link( (int) $menu_item->object_id, $menu_item->object ); + $original_title = $original_object->name; + } else { + $menu_item->url = ''; + $original_title = ''; + $menu_item->_invalid = true; } - $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title; + + if ( '' === $original_title ) { + /* translators: %d: ID of a term */ + $original_title = sprintf( __( '#%d (no title)' ), $menu_item->object_id ); + } + + $menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title; } else { $menu_item->type_label = __( 'Custom Link' );