wp/wp-includes/nav-menu.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
--- a/wp/wp-includes/nav-menu.php	Tue Oct 22 16:11:46 2019 +0200
+++ b/wp/wp-includes/nav-menu.php	Tue Dec 15 13:49:49 2020 +0100
@@ -70,7 +70,7 @@
 		$menu_obj &&
 		! is_wp_error( $menu_obj ) &&
 		! empty( $menu_obj->taxonomy ) &&
-		'nav_menu' == $menu_obj->taxonomy
+		'nav_menu' === $menu_obj->taxonomy
 	) {
 		return true;
 	}
@@ -92,6 +92,13 @@
 
 	add_theme_support( 'menus' );
 
+	foreach ( $locations as $key => $value ) {
+		if ( is_int( $key ) ) {
+			_doing_it_wrong( __FUNCTION__, __( 'Nav menu locations must be strings.' ), '5.3.0' );
+			break;
+		}
+	}
+
 	$_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations );
 }
 
@@ -99,6 +106,7 @@
  * Unregisters a navigation menu location for a theme.
  *
  * @since 3.1.0
+ *
  * @global array $_wp_registered_nav_menus
  *
  * @param string $location The menu location identifier.
@@ -228,7 +236,7 @@
  * @return bool Whether the given ID is that of a nav menu item.
  */
 function is_nav_menu_item( $menu_item_id = 0 ) {
-	return ( ! is_wp_error( $menu_item_id ) && ( 'nav_menu_item' == get_post_type( $menu_item_id ) ) );
+	return ( ! is_wp_error( $menu_item_id ) && ( 'nav_menu_item' === get_post_type( $menu_item_id ) ) );
 }
 
 /**
@@ -317,7 +325,7 @@
 		'slug'        => null,
 	);
 
-	// double-check that we're not going to have one menu take the name of another
+	// Double-check that we're not going to have one menu take the name of another.
 	$_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
 	if (
 		$_possible_existing &&
@@ -327,23 +335,23 @@
 	) {
 		return new WP_Error(
 			'menu_exists',
-			/* translators: %s: menu name */
 			sprintf(
+				/* translators: %s: Menu name. */
 				__( 'The menu name %s conflicts with another menu name. Please try another.' ),
 				'<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
 			)
 		);
 	}
 
-	// menu doesn't already exist, so create a new menu
+	// Menu doesn't already exist, so create a new menu.
 	if ( ! $_menu || is_wp_error( $_menu ) ) {
 		$menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
 
 		if ( $menu_exists ) {
 			return new WP_Error(
 				'menu_exists',
-				/* translators: %s: menu name */
 				sprintf(
+					/* translators: %s: Menu name. */
 					__( 'The menu name %s conflicts with another menu name. Please try another.' ),
 					'<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
 				)
@@ -412,7 +420,7 @@
 	$menu_id         = (int) $menu_id;
 	$menu_item_db_id = (int) $menu_item_db_id;
 
-	// make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects
+	// Make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects.
 	if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) ) {
 		return new WP_Error( 'update_nav_menu_item_failed', __( 'The given object ID is not that of a menu item.' ) );
 	}
@@ -469,32 +477,32 @@
 		$args['menu-item-url'] = '';
 
 		$original_title = '';
-		if ( 'taxonomy' == $args['menu-item-type'] ) {
+		if ( 'taxonomy' === $args['menu-item-type'] ) {
 			$original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
 			$original_title  = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
-		} elseif ( 'post_type' == $args['menu-item-type'] ) {
+		} elseif ( 'post_type' === $args['menu-item-type'] ) {
 
 			$original_object = get_post( $args['menu-item-object-id'] );
 			$original_parent = (int) $original_object->post_parent;
 			$original_title  = $original_object->post_title;
-		} elseif ( 'post_type_archive' == $args['menu-item-type'] ) {
+		} elseif ( 'post_type_archive' === $args['menu-item-type'] ) {
 			$original_object = get_post_type_object( $args['menu-item-object'] );
 			if ( $original_object ) {
 				$original_title = $original_object->labels->archives;
 			}
 		}
 
-		if ( $args['menu-item-title'] == $original_title ) {
+		if ( wp_unslash( $args['menu-item-title'] ) === wp_specialchars_decode( $original_title ) ) {
 			$args['menu-item-title'] = '';
 		}
 
-		// hack to get wp to create a post object when too many properties are empty
-		if ( '' == $args['menu-item-title'] && '' == $args['menu-item-description'] ) {
+		// Hack to get wp to create a post object when too many properties are empty.
+		if ( '' === $args['menu-item-title'] && '' === $args['menu-item-description'] ) {
 			$args['menu-item-description'] = ' ';
 		}
 	}
 
-	// Populate the menu item object
+	// Populate the menu item object.
 	$post = array(
 		'menu_order'   => $args['menu-item-position'],
 		'ping_status'  => 0,
@@ -507,10 +515,10 @@
 
 	$update = 0 != $menu_item_db_id;
 
-	// New menu item. Default is draft status
+	// New menu item. Default is draft status.
 	if ( ! $update ) {
 		$post['ID']          = 0;
-		$post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
+		$post['post_status'] = 'publish' === $args['menu-item-status'] ? 'publish' : 'draft';
 		$menu_item_db_id     = wp_insert_post( $post );
 		if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) {
 			return $menu_item_db_id;
@@ -530,13 +538,13 @@
 		do_action( 'wp_add_nav_menu_item', $menu_id, $menu_item_db_id, $args );
 	}
 
-	// Associate the menu item with the menu term
-	// Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms()
+	// Associate the menu item with the menu term.
+	// Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms().
 	if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
 		wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
 	}
 
-	if ( 'custom' == $args['menu-item-type'] ) {
+	if ( 'custom' === $args['menu-item-type'] ) {
 		$args['menu-item-object-id'] = $menu_item_db_id;
 		$args['menu-item-object']    = 'custom';
 	}
@@ -561,10 +569,10 @@
 		delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' );
 	}
 
-	// Update existing menu item. Default is publish status
+	// Update existing menu item. Default is publish status.
 	if ( $update ) {
 		$post['ID']          = $menu_item_db_id;
-		$post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish';
+		$post['post_status'] = ( 'draft' === $args['menu-item-status'] ) ? 'draft' : 'publish';
 		wp_update_post( $post );
 	}
 
@@ -593,10 +601,11 @@
  *
  * @param array $args Optional. Array of arguments passed on to get_terms().
  *                    Default empty array.
- * @return array Menu objects.
+ * @return WP_Term[] An array of menu objects.
  */
 function wp_get_nav_menus( $args = array() ) {
 	$defaults = array(
+		'taxonomy'   => 'nav_menu',
 		'hide_empty' => false,
 		'orderby'    => 'name',
 	);
@@ -609,10 +618,10 @@
 	 *
 	 * @see get_terms()
 	 *
-	 * @param array $menus An array of menu objects.
-	 * @param array $args  An array of arguments used to retrieve menu objects.
+	 * @param WP_Term[] $menus An array of menu objects.
+	 * @param array     $args  An array of arguments used to retrieve menu objects.
 	 */
-	return apply_filters( 'wp_get_nav_menus', get_terms( 'nav_menu', $args ), $args );
+	return apply_filters( 'wp_get_nav_menus', get_terms( $args ), $args );
 }
 
 /**
@@ -640,8 +649,6 @@
  *
  * @since 3.0.0
  *
- * @staticvar array $fetched
- *
  * @param int|string|WP_Term $menu Menu ID, slug, name, or object.
  * @param array              $args {
  *     Optional. Arguments to pass to get_posts().
@@ -659,7 +666,7 @@
  *                               processed in this function. Default 'menu_order'.
  *     @type bool   $nopaging    Whether to retrieve all menu items (true) or paginate (false). Default true.
  * }
- * @return false|array $items Array of menu items, otherwise false.
+ * @return array|false Array of menu items, otherwise false.
  */
 function wp_get_nav_menu_items( $menu, $args = array() ) {
 	$menu = wp_get_nav_menu_object( $menu );
@@ -693,7 +700,7 @@
 		$items = array();
 	}
 
-	// Get all posts and terms at once to prime the caches
+	// Get all posts and terms at once to prime the caches.
 	if ( empty( $fetched[ $menu->term_id ] ) && ! wp_using_ext_object_cache() ) {
 		$fetched[ $menu->term_id ] = true;
 		$posts                     = array();
@@ -703,9 +710,9 @@
 			$object    = get_post_meta( $item->ID, '_menu_item_object', true );
 			$type      = get_post_meta( $item->ID, '_menu_item_type', true );
 
-			if ( 'post_type' == $type ) {
+			if ( 'post_type' === $type ) {
 				$posts[ $object ][] = $object_id;
-			} elseif ( 'taxonomy' == $type ) {
+			} elseif ( 'taxonomy' === $type ) {
 				$terms[ $object ][] = $object_id;
 			}
 		}
@@ -727,8 +734,8 @@
 		if ( ! empty( $terms ) ) {
 			foreach ( array_keys( $terms ) as $taxonomy ) {
 				get_terms(
-					$taxonomy,
 					array(
+						'taxonomy'     => $taxonomy,
 						'include'      => $terms[ $taxonomy ],
 						'hierarchical' => false,
 					)
@@ -740,7 +747,7 @@
 
 	$items = array_map( 'wp_setup_nav_menu_item', $items );
 
-	if ( ! is_admin() ) { // Remove invalid items only in front end
+	if ( ! is_admin() ) { // Remove invalid items only on front end.
 		$items = array_filter( $items, '_is_valid_nav_menu_item' );
 	}
 
@@ -779,13 +786,13 @@
  * - db_id:            The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist).
  * - description:      The description of this menu item.
  * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise.
- * - object:           The type of object originally represented, such as "category," "post", or "attachment."
+ * - object:           The type of object originally represented, such as 'category', 'post', or 'attachment'.
  * - object_id:        The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
  * - post_parent:      The DB ID of the original object's parent object, if any (0 otherwise).
  * - post_title:       A "no title" label if menu item represents a post that lacks a title.
  * - target:           The target attribute of the link element for this menu item.
  * - title:            The title of this menu item.
- * - type:             The family of objects originally represented, such as "post_type" or "taxonomy."
+ * - type:             The family of objects originally represented, such as 'post_type' or 'taxonomy'.
  * - type_label:       The singular label used to describe this type of menu item.
  * - url:              The URL to which this menu item points.
  * - xfn:              The XFN relationship expressed in the link of this menu item.
@@ -794,21 +801,29 @@
  * @since 3.0.0
  *
  * @param object $menu_item The menu item to modify.
- * @return object $menu_item The menu item with standard menu item properties.
+ * @return object The menu item with standard menu item properties.
  */
 function wp_setup_nav_menu_item( $menu_item ) {
 	if ( isset( $menu_item->post_type ) ) {
-		if ( 'nav_menu_item' == $menu_item->post_type ) {
+		if ( 'nav_menu_item' === $menu_item->post_type ) {
 			$menu_item->db_id            = (int) $menu_item->ID;
 			$menu_item->menu_item_parent = ! isset( $menu_item->menu_item_parent ) ? get_post_meta( $menu_item->ID, '_menu_item_menu_item_parent', true ) : $menu_item->menu_item_parent;
 			$menu_item->object_id        = ! isset( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id;
 			$menu_item->object           = ! isset( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object;
 			$menu_item->type             = ! isset( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type;
 
-			if ( 'post_type' == $menu_item->type ) {
+			if ( 'post_type' === $menu_item->type ) {
 				$object = get_post_type_object( $menu_item->object );
 				if ( $object ) {
 					$menu_item->type_label = $object->labels->singular_name;
+					// Denote post states for special pages (only in the admin).
+					if ( function_exists( 'get_post_states' ) ) {
+						$menu_post   = get_post( $menu_item->object_id );
+						$post_states = get_post_states( $menu_post );
+						if ( $post_states ) {
+							$menu_item->type_label = wp_strip_all_tags( implode( ', ', $post_states ) );
+						}
+					}
 				} else {
 					$menu_item->type_label = $menu_item->object;
 					$menu_item->_invalid   = true;
@@ -818,34 +833,41 @@
 					$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_title ) {
-					/* translators: %d: ID of a post */
-					$original_title = sprintf( __( '#%d (no 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;
 				}
 
-				$menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
+				if ( '' === $original_title ) {
+					/* translators: %d: ID of a post. */
+					$original_title = sprintf( __( '#%d (no title)' ), $menu_item->object_id );
+				}
 
-			} elseif ( 'post_type_archive' == $menu_item->type ) {
+				$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 {
+					$post_type_description = '';
 					$menu_item->_invalid   = true;
-					$post_type_description = '';
 				}
 
 				$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 ) {
+
+			} elseif ( 'taxonomy' === $menu_item->type ) {
 				$object = get_taxonomy( $menu_item->object );
 				if ( $object ) {
 					$menu_item->type_label = $object->labels->singular_name;
@@ -854,14 +876,23 @@
 					$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' );
@@ -904,7 +935,7 @@
 			$menu_item->type_label = $object->labels->singular_name;
 
 			if ( '' === $menu_item->post_title ) {
-				/* translators: %d: ID of a post */
+				/* translators: %d: ID of a post. */
 				$menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID );
 			}
 
@@ -958,9 +989,10 @@
  * @since 3.0.0
  *
  * @param int    $object_id   The ID of the original object.
- * @param string $object_type The type of object, such as "taxonomy" or "post_type."
- * @param string $taxonomy    If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
- * @return array The array of menu item IDs; empty array if none;
+ * @param string $object_type The type of object, such as 'taxonomy' or 'post_type'.
+ * @param string $taxonomy    If $object_type is 'taxonomy', $taxonomy is the name of the tax
+ *                            that $object_id belongs to.
+ * @return int[] The array of menu item IDs; empty array if none;
  */
 function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
 	$object_id     = (int) $object_id;
@@ -980,13 +1012,13 @@
 		if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
 			$menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
 			if (
-				'post_type' == $object_type &&
-				'post_type' == $menu_item_type
+				'post_type' === $object_type &&
+				'post_type' === $menu_item_type
 			) {
 				$menu_item_ids[] = (int) $menu_item->ID;
 			} elseif (
-				'taxonomy' == $object_type &&
-				'taxonomy' == $menu_item_type &&
+				'taxonomy' === $object_type &&
+				'taxonomy' === $menu_item_type &&
 				get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy
 			) {
 				$menu_item_ids[] = (int) $menu_item->ID;
@@ -1041,12 +1073,12 @@
  * @since 3.0.0
  * @access private
  *
- * @param string $new_status The new status of the post object.
- * @param string $old_status The old status of the post object.
- * @param object $post       The post object being transitioned from one status to another.
+ * @param string  $new_status The new status of the post object.
+ * @param string  $old_status The old status of the post object.
+ * @param WP_Post $post       The post object being transitioned from one status to another.
  */
 function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
-	if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type ) {
+	if ( 'publish' !== $new_status || 'publish' === $old_status || 'page' !== $post->post_type ) {
 		return;
 	}
 	if ( ! empty( $post->post_parent ) ) {
@@ -1221,11 +1253,11 @@
 							// Go back and check the next new menu location.
 							continue 3;
 						}
-					} // endforeach ( $slug_group as $slug )
-				} // endforeach ( $old_nav_menu_locations as $location => $menu_id )
-			} // endforeach foreach ( $registered_nav_menus as $new_location => $name )
-		} // endforeach ( $slug_group as $slug )
-	} // endforeach ( $common_slug_groups as $slug_group )
+					} // End foreach ( $slug_group as $slug ).
+				} // End foreach ( $old_nav_menu_locations as $location => $menu_id ).
+			} // End foreach foreach ( $registered_nav_menus as $new_location => $name ).
+		} // End foreach ( $slug_group as $slug ).
+	} // End foreach ( $common_slug_groups as $slug_group ).
 
 	return $new_nav_menu_locations;
 }