WP_REST_Menus_Controller::handle_locations()protectedWP 5.9.0

Updates the menu's locations from a REST request.

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

Хуков нет.

Возвращает

true|WP_Error. True on success, a WP_Error on an error updating any of the locations.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->handle_locations( $menu_id, $request );
$menu_id(int) (обязательный)
The menu id to update.
$request(WP_REST_Request) (обязательный)
Full details about the request.

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

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

Код WP_REST_Menus_Controller::handle_locations() WP 6.0.2

protected function handle_locations( $menu_id, $request ) {
	if ( ! isset( $request['locations'] ) ) {
		return true;
	}

	$menu_locations = get_registered_nav_menus();
	$menu_locations = array_keys( $menu_locations );
	$new_locations  = array();
	foreach ( $request['locations'] as $location ) {
		if ( ! in_array( $location, $menu_locations, true ) ) {
			return new WP_Error(
				'rest_invalid_menu_location',
				__( 'Invalid menu location.' ),
				array(
					'status'   => 400,
					'location' => $location,
				)
			);
		}
		$new_locations[ $location ] = $menu_id;
	}
	$assigned_menu = get_nav_menu_locations();
	foreach ( $assigned_menu as $location => $term_id ) {
		if ( $term_id === $menu_id ) {
			unset( $assigned_menu[ $location ] );
		}
	}
	$new_assignments = array_merge( $assigned_menu, $new_locations );
	set_theme_mod( 'nav_menu_locations', $new_assignments );

	return true;
}