WP_REST_Menus_Controller::update_item()publicWP 5.9.0

Updates a single term from a taxonomy.

{} Это метод класса: WP_REST_Menus_Controller{}

Возвращает

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

Использование

$WP_REST_Menus_Controller = new WP_REST_Menus_Controller();
$WP_REST_Menus_Controller->update_item( $request );
$request(WP_REST_Request) (обязательный)
Full details about the request.

Список изменений

С версии 5.9.0 Введена.

Код WP_REST_Menus_Controller::update_item() WP 6.0.2

public function update_item( $request ) {
	$term = $this->get_term( $request['id'] );
	if ( is_wp_error( $term ) ) {
		return $term;
	}

	if ( isset( $request['parent'] ) ) {
		if ( ! is_taxonomy_hierarchical( $this->taxonomy ) ) {
			return new WP_Error( 'rest_taxonomy_not_hierarchical', __( 'Cannot set parent term, taxonomy is not hierarchical.' ), array( 'status' => 400 ) );
		}

		$parent = get_term( (int) $request['parent'], $this->taxonomy );

		if ( ! $parent ) {
			return new WP_Error( 'rest_term_invalid', __( 'Parent term does not exist.' ), array( 'status' => 400 ) );
		}
	}

	$prepared_term = $this->prepare_item_for_database( $request );

	// Only update the term if we have something to update.
	if ( ! empty( $prepared_term ) ) {
		if ( ! isset( $prepared_term->{'menu-name'} ) ) {
			// wp_update_nav_menu_object() requires that the menu-name is always passed.
			$prepared_term->{'menu-name'} = $term->name;
		}

		$update = wp_update_nav_menu_object( $term->term_id, wp_slash( (array) $prepared_term ) );

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

	$term = get_term( $term->term_id, $this->taxonomy );

	/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
	do_action( "rest_insert_{$this->taxonomy}", $term, $request, false );

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

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

	$locations_update = $this->handle_locations( $term->term_id, $request );

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

	$this->handle_auto_add( $term->term_id, $request );

	$fields_update = $this->update_additional_fields_for_object( $term, $request );

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

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

	/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php */
	do_action( "rest_after_insert_{$this->taxonomy}", $term, $request, false );

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

	return rest_ensure_response( $response );
}