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.
Built from https://develop.svn.wordpress.org/trunk@45891


git-svn-id: http://core.svn.wordpress.org/trunk@45702 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2019-08-25 21:32:59 +00:00
parent fe0b22cd67
commit b1847ef3e2
3 changed files with 39 additions and 20 deletions

View File

@ -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 ) {

View File

@ -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' );

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.3-alpha-45890';
$wp_version = '5.3-alpha-45891';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.