Skip to content
Permalink
Browse files

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
  • Loading branch information...
SergeyBiryukov committed Aug 25, 2019
1 parent 7ef2679 commit 252e14ac27345eaaff75de44555403c1798db3ee
Showing with 38 additions and 19 deletions.
  1. +7 −4 src/wp-admin/includes/class-walker-nav-menu-edit.php
  2. +31 −15 src/wp-includes/nav-menu.php
@@ -71,14 +71,17 @@ public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0
);
$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 ) {
@@ -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' );

0 comments on commit 252e14a

Please sign in to comment.
You can’t perform that action at this time.