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
This commit is contained in:
Sergey Biryukov 2019-08-25 21:31:43 +00:00
parent 7ef2679ec7
commit 252e14ac27
2 changed files with 38 additions and 19 deletions

View File

@ -71,14 +71,17 @@ class Walker_Nav_Menu_Edit extends Walker_Nav_Menu {
); );
$original_title = false; $original_title = false;
if ( 'taxonomy' == $item->type ) { if ( 'taxonomy' == $item->type ) {
$original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' ); $original_object = get_term( (int) $item->object_id, $item->object );
if ( is_wp_error( $original_title ) ) { if ( $original_object && ! is_wp_error( $original_title ) ) {
$original_title = false; $original_title = $original_object->name;
} }
} elseif ( 'post_type' == $item->type ) { } elseif ( 'post_type' == $item->type ) {
$original_object = get_post( $item->object_id ); $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 ) { } elseif ( 'post_type_archive' == $item->type ) {
$original_object = get_post_type_object( $item->object ); $original_object = get_post_type_object( $item->object );
if ( $original_object ) { if ( $original_object ) {

View File

@ -819,33 +819,40 @@ function wp_setup_nav_menu_item( $menu_item ) {
$menu_item->_invalid = true; $menu_item->_invalid = true;
} }
$menu_item->url = get_permalink( $menu_item->object_id );
$original_object = get_post( $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 ) { if ( '' === $original_title ) {
/* translators: %d: ID of a post */ /* 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 ) { } elseif ( 'post_type_archive' == $menu_item->type ) {
$object = get_post_type_object( $menu_item->object ); $object = get_post_type_object( $menu_item->object );
if ( $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; $post_type_description = $object->description;
} else { } else {
$menu_item->_invalid = true;
$post_type_description = ''; $post_type_description = '';
$menu_item->_invalid = true;
} }
$menu_item->type_label = __( 'Post Type Archive' ); $menu_item->type_label = __( 'Post Type Archive' );
$post_content = wp_trim_words( $menu_item->post_content, 200 ); $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 ); $menu_item->url = get_post_type_archive_link( $menu_item->object );
} elseif ( 'taxonomy' == $menu_item->type ) { } elseif ( 'taxonomy' == $menu_item->type ) {
$object = get_taxonomy( $menu_item->object ); $object = get_taxonomy( $menu_item->object );
if ( $object ) { if ( $object ) {
@ -855,14 +862,23 @@ function wp_setup_nav_menu_item( $menu_item ) {
$menu_item->_invalid = true; $menu_item->_invalid = true;
} }
$term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object ); $original_object = get_term( (int) $menu_item->object_id, $menu_item->object );
$menu_item->url = ! is_wp_error( $term_url ) ? $term_url : '';
$original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' ); if ( $original_object && ! is_wp_error( $original_object ) ) {
if ( is_wp_error( $original_title ) ) { $menu_item->url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
$original_title = false; $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 { } else {
$menu_item->type_label = __( 'Custom Link' ); $menu_item->type_label = __( 'Custom Link' );