wp/wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
--- a/wp/wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php	Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/rest-api/endpoints/class-wp-rest-menu-locations-controller.php	Fri Sep 05 18:40:08 2025 +0200
@@ -120,7 +120,7 @@
 	 * @since 5.9.0
 	 *
 	 * @param WP_REST_Request $request Full details about the request.
-	 * @return WP_Error|bool True if the request has read access for the item, WP_Error object otherwise.
+	 * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
 	 */
 	public function get_item_permissions_check( $request ) {
 		if ( ! current_user_can( 'edit_theme_options' ) ) {
@@ -168,7 +168,8 @@
 	 */
 	public function prepare_item_for_response( $item, $request ) {
 		// Restores the more descriptive, specific name for use within this method.
-		$location  = $item;
+		$location = $item;
+
 		$locations = get_nav_menu_locations();
 		$menu      = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
 
@@ -193,7 +194,9 @@
 
 		$response = rest_ensure_response( $data );
 
-		$response->add_links( $this->prepare_links( $location ) );
+		if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
+			$response->add_links( $this->prepare_links( $location ) );
+		}
 
 		/**
 		 * Filters menu location data returned from the REST API.
@@ -208,6 +211,44 @@
 	}
 
 	/**
+	 * Prepares links for the request.
+	 *
+	 * @since 5.9.0
+	 *
+	 * @param stdClass $location Menu location.
+	 * @return array Links for the given menu location.
+	 */
+	protected function prepare_links( $location ) {
+		$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
+
+		// Entity meta.
+		$links = array(
+			'self'       => array(
+				'href' => rest_url( trailingslashit( $base ) . $location->name ),
+			),
+			'collection' => array(
+				'href' => rest_url( $base ),
+			),
+		);
+
+		$locations = get_nav_menu_locations();
+		$menu      = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
+		if ( $menu ) {
+			$path = rest_get_route_for_term( $menu );
+			if ( $path ) {
+				$url = rest_url( $path );
+
+				$links['https://api.w.org/menu'][] = array(
+					'href'       => $url,
+					'embeddable' => true,
+				);
+			}
+		}
+
+		return $links;
+	}
+
+	/**
 	 * Retrieves the menu location's schema, conforming to JSON Schema.
 	 *
 	 * @since 5.9.0
@@ -260,42 +301,4 @@
 			'context' => $this->get_context_param( array( 'default' => 'view' ) ),
 		);
 	}
-
-	/**
-	 * Prepares links for the request.
-	 *
-	 * @since 5.9.0
-	 *
-	 * @param stdClass $location Menu location.
-	 * @return array Links for the given menu location.
-	 */
-	protected function prepare_links( $location ) {
-		$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
-
-		// Entity meta.
-		$links = array(
-			'self'       => array(
-				'href' => rest_url( trailingslashit( $base ) . $location->name ),
-			),
-			'collection' => array(
-				'href' => rest_url( $base ),
-			),
-		);
-
-		$locations = get_nav_menu_locations();
-		$menu      = isset( $locations[ $location->name ] ) ? $locations[ $location->name ] : 0;
-		if ( $menu ) {
-			$path = rest_get_route_for_term( $menu );
-			if ( $path ) {
-				$url = rest_url( $path );
-
-				$links['https://api.w.org/menu'][] = array(
-					'href'       => $url,
-					'embeddable' => true,
-				);
-			}
-		}
-
-		return $links;
-	}
 }