wp/wp-includes/nav-menu.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
--- a/wp/wp-includes/nav-menu.php	Wed Sep 21 18:19:35 2022 +0200
+++ b/wp/wp-includes/nav-menu.php	Tue Sep 27 16:37:53 2022 +0200
@@ -50,7 +50,7 @@
 }
 
 /**
- * Check if the given ID is a navigation menu.
+ * Determines whether the given ID is a navigation menu.
  *
  * Returns true if it is; false otherwise.
  *
@@ -159,7 +159,7 @@
  *
  * @since 3.0.0
  *
- * @return int[] Associative array of egistered navigation menu IDs keyed by their
+ * @return int[] Associative array of registered navigation menu IDs keyed by their
  *               location name. If none are registered, an empty array.
  */
 function get_nav_menu_locations() {
@@ -255,7 +255,7 @@
 }
 
 /**
- * Delete a Navigation Menu.
+ * Deletes a navigation menu.
  *
  * @since 3.0.0
  *
@@ -302,7 +302,7 @@
 }
 
 /**
- * Save the properties of a menu or create a new menu with those properties.
+ * Saves the properties of a menu or create a new menu with those properties.
  *
  * Note that `$menu_data` is expected to be pre-slashed.
  *
@@ -404,19 +404,21 @@
 }
 
 /**
- * Save the properties of a menu item or create a new one.
+ * Saves the properties of a menu item or create a new one.
  *
- * The menu-item-title, menu-item-description, and menu-item-attr-title are expected
- * to be pre-slashed since they are passed directly into `wp_insert_post()`.
+ * The menu-item-title, menu-item-description and menu-item-attr-title are expected
+ * to be pre-slashed since they are passed directly to APIs that expect slashed data.
  *
  * @since 3.0.0
+ * @since 5.9.0 Added the `$fire_after_hooks` parameter.
  *
- * @param int   $menu_id         The ID of the menu. Required. If "0", makes the menu item a draft orphan.
- * @param int   $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
- * @param array $menu_item_data  The menu item's data.
+ * @param int   $menu_id          The ID of the menu. If 0, makes the menu item a draft orphan.
+ * @param int   $menu_item_db_id  The ID of the menu item. If 0, creates a new menu item.
+ * @param array $menu_item_data   The menu item's data.
+ * @param bool  $fire_after_hooks Whether to fire the after insert hooks. Default true.
  * @return int|WP_Error The menu item's database ID or WP_Error object on failure.
  */
-function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) {
+function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array(), $fire_after_hooks = true ) {
 	$menu_id         = (int) $menu_id;
 	$menu_item_db_id = (int) $menu_item_db_id;
 
@@ -526,7 +528,7 @@
 	if ( ! $update ) {
 		$post['ID']          = 0;
 		$post['post_status'] = 'publish' === $args['menu-item-status'] ? 'publish' : 'draft';
-		$menu_item_db_id     = wp_insert_post( $post );
+		$menu_item_db_id     = wp_insert_post( $post, true, $fire_after_hooks );
 		if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) ) {
 			return $menu_item_db_id;
 		}
@@ -548,7 +550,10 @@
 	// 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' );
+		$update_terms = wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
+		if ( is_wp_error( $update_terms ) ) {
+			return $update_terms;
+		}
 	}
 
 	if ( 'custom' === $args['menu-item-type'] ) {
@@ -580,7 +585,11 @@
 	if ( $update ) {
 		$post['ID']          = $menu_item_db_id;
 		$post['post_status'] = ( 'draft' === $args['menu-item-status'] ) ? 'draft' : 'publish';
-		wp_update_post( $post );
+
+		$update_post = wp_update_post( $post, true );
+		if ( is_wp_error( $update_post ) ) {
+			return $update_post;
+		}
 	}
 
 	/**
@@ -632,7 +641,7 @@
 }
 
 /**
- * Return if a menu item is valid.
+ * Determines whether a menu item is valid.
  *
  * @link https://core.trac.wordpress.org/ticket/13958
  *
@@ -684,12 +693,11 @@
 
 	static $fetched = array();
 
-	$items = get_objects_in_term( $menu->term_id, 'nav_menu' );
-	if ( is_wp_error( $items ) ) {
+	if ( ! taxonomy_exists( 'nav_menu' ) ) {
 		return false;
 	}
 
-	$defaults        = array(
+	$defaults = array(
 		'order'       => 'ASC',
 		'orderby'     => 'menu_order',
 		'post_type'   => 'nav_menu_item',
@@ -697,59 +705,46 @@
 		'output'      => ARRAY_A,
 		'output_key'  => 'menu_order',
 		'nopaging'    => true,
+		'tax_query'   => array(
+			array(
+				'taxonomy' => 'nav_menu',
+				'field'    => 'term_taxonomy_id',
+				'terms'    => $menu->term_taxonomy_id,
+			),
+		),
 	);
-	$args            = wp_parse_args( $args, $defaults );
-	$args['include'] = $items;
-
-	if ( ! empty( $items ) ) {
+	$args     = wp_parse_args( $args, $defaults );
+	if ( $menu->count > 0 ) {
 		$items = get_posts( $args );
 	} else {
 		$items = array();
 	}
 
-	// Get all posts and terms at once to prime the caches.
-	if ( empty( $fetched[ $menu->term_id ] ) && ! wp_using_ext_object_cache() ) {
+	// Prime posts and terms caches.
+	if ( empty( $fetched[ $menu->term_id ] ) ) {
 		$fetched[ $menu->term_id ] = true;
-		$posts                     = array();
-		$terms                     = array();
+		$post_ids                  = array();
+		$term_ids                  = array();
 		foreach ( $items as $item ) {
 			$object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
-			$object    = get_post_meta( $item->ID, '_menu_item_object', true );
 			$type      = get_post_meta( $item->ID, '_menu_item_type', true );
 
 			if ( 'post_type' === $type ) {
-				$posts[ $object ][] = $object_id;
+				$post_ids[] = (int) $object_id;
 			} elseif ( 'taxonomy' === $type ) {
-				$terms[ $object ][] = $object_id;
+				$term_ids[] = (int) $object_id;
 			}
 		}
 
-		if ( ! empty( $posts ) ) {
-			foreach ( array_keys( $posts ) as $post_type ) {
-				get_posts(
-					array(
-						'post__in'               => $posts[ $post_type ],
-						'post_type'              => $post_type,
-						'nopaging'               => true,
-						'update_post_term_cache' => false,
-					)
-				);
-			}
+		if ( ! empty( $post_ids ) ) {
+			_prime_post_caches( $post_ids, false );
 		}
-		unset( $posts );
+		unset( $post_ids );
 
-		if ( ! empty( $terms ) ) {
-			foreach ( array_keys( $terms ) as $taxonomy ) {
-				get_terms(
-					array(
-						'taxonomy'     => $taxonomy,
-						'include'      => $terms[ $taxonomy ],
-						'hierarchical' => false,
-					)
-				);
-			}
+		if ( ! empty( $term_ids ) ) {
+			_prime_term_caches( $term_ids );
 		}
-		unset( $terms );
+		unset( $term_ids );
 	}
 
 	$items = array_map( 'wp_setup_nav_menu_item', $items );
@@ -993,7 +988,7 @@
 }
 
 /**
- * Get the menu items associated with a particular object.
+ * Returns the menu items associated with a particular object.
  *
  * @since 3.0.0
  *
@@ -1125,7 +1120,7 @@
 }
 
 /**
- * Delete auto-draft posts associated with the supplied changeset.
+ * Deletes auto-draft posts associated with the supplied changeset.
  *
  * @since 4.8.0
  * @access private
@@ -1159,7 +1154,7 @@
 }
 
 /**
- * Handle menu config after theme change.
+ * Handles menu config after theme change.
  *
  * @access private
  * @since 4.9.0