WP_REST_Menu_Items_Controller::update_item()publicWP 5.9.0

Updates a single nav menu item.

{} It's a method of the class: WP_REST_Menu_Items_Controller{}

Return

WP_REST_Response|WP_Error. Response object on success, or WP_Error object on failure.

Usage

$WP_REST_Menu_Items_Controller = new WP_REST_Menu_Items_Controller();
$WP_REST_Menu_Items_Controller->update_item( $request );
$request(WP_REST_Request) (required)
Full details about the request.

Changelog

Since 5.9.0 Introduced.

Code of WP_REST_Menu_Items_Controller::update_item() WP 6.0

public function update_item( $request ) {
	$valid_check = $this->get_nav_menu_item( $request['id'] );
	if ( is_wp_error( $valid_check ) ) {
		return $valid_check;
	}
	$post_before       = get_post( $request['id'] );
	$prepared_nav_item = $this->prepare_item_for_database( $request );

	if ( is_wp_error( $prepared_nav_item ) ) {
		return $prepared_nav_item;
	}

	$prepared_nav_item = (array) $prepared_nav_item;

	$nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id'], $prepared_nav_item['menu-item-db-id'], wp_slash( $prepared_nav_item ), false );

	if ( is_wp_error( $nav_menu_item_id ) ) {
		if ( 'db_update_error' === $nav_menu_item_id->get_error_code() ) {
			$nav_menu_item_id->add_data( array( 'status' => 500 ) );
		} else {
			$nav_menu_item_id->add_data( array( 'status' => 400 ) );
		}

		return $nav_menu_item_id;
	}

	$nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
	if ( is_wp_error( $nav_menu_item ) ) {
		$nav_menu_item->add_data( array( 'status' => 404 ) );

		return $nav_menu_item;
	}

	/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php */
	do_action( 'rest_insert_nav_menu_item', $nav_menu_item, $request, false );

	$schema = $this->get_item_schema();

	if ( ! empty( $schema['properties']['meta'] ) && isset( $request['meta'] ) ) {
		$meta_update = $this->meta->update_value( $request['meta'], $nav_menu_item->ID );

		if ( is_wp_error( $meta_update ) ) {
			return $meta_update;
		}
	}

	$post          = get_post( $nav_menu_item_id );
	$nav_menu_item = $this->get_nav_menu_item( $nav_menu_item_id );
	$fields_update = $this->update_additional_fields_for_object( $nav_menu_item, $request );

	if ( is_wp_error( $fields_update ) ) {
		return $fields_update;
	}

	$request->set_param( 'context', 'edit' );

	/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php */
	do_action( 'rest_after_insert_nav_menu_item', $nav_menu_item, $request, false );

	wp_after_insert_post( $post, true, $post_before );

	$response = $this->prepare_item_for_response( get_post( $nav_menu_item_id ), $request );

	return rest_ensure_response( $response );
}