Automattic\WooCommerce\Internal\Admin\Settings
Utils::order_map_move_at_order
Move an id at a specific order in an order map.
This method is used to simulate the behavior of a drag&drop sorting UI:
- When moving an id down, all the ids with an order equal or lower than the desired order but equal or higher than the current order are decreased by 1.
- When moving an id up, all the ids with an order equal or higher than the desired order but equal or lower than the current order are increased by 1.
Метод класса: Utils{}
Хуков нет.
Возвращает
Массив. The updated order map. This map is not normalized.
Использование
$result = Utils::order_map_move_at_order( $order_map, $id, $order ): array;
- $order_map(массив) (обязательный)
- The order map.
- $id(строка) (обязательный)
- The id to place.
- $order(int) (обязательный)
- The order at which to place the id.
Код Utils::order_map_move_at_order() Utils::order map move at order WC 10.5.0
public static function order_map_move_at_order( array $order_map, string $id, int $order ): array {
// If the id is not in the order map, return the order map as is.
if ( ! isset( $order_map[ $id ] ) ) {
return $order_map;
}
// If the id is already at the desired order, return the order map as is.
if ( $order_map[ $id ] === $order ) {
return $order_map;
}
// If there is no id at the desired order, just place the id there.
if ( ! in_array( $order, $order_map, true ) ) {
$order_map[ $id ] = $order;
return $order_map;
}
// We apply the normal behavior of a drag&drop sorting UI.
$existing_order = $order_map[ $id ];
if ( $order > $existing_order ) {
// Moving down.
foreach ( $order_map as $key => $value ) {
if ( $value <= $order && $value >= $existing_order ) {
--$order_map[ $key ];
}
}
} else {
// Moving up.
foreach ( $order_map as $key => $value ) {
if ( $value >= $order && $value <= $existing_order ) {
++$order_map[ $key ];
}
}
}
// Place the id at the desired order.
$order_map[ $id ] = $order;
return $order_map;
}