Automattic\WooCommerce\Internal\Products

ProductsOrderingMoveService::apply │ private │ WC 1.0

Applies the move to the database and returns updated positions of all affected products.

Метод класса: ProductsOrderingMoveService{}

Хуков нет.

Возвращает

array<int,int>.

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

// private - только в коде основоного (родительского) класса
$result = $this->apply( $map, $reindexed ): array;
$map(объект) (обязательный)
Map with the move route.
$reindexed(array<int,int>) (обязательный)
Reindexed positions map.

Код ProductsOrderingMoveService::apply() WC 11.2.2

private function apply( object $map, array $reindexed ): array {
	global $wpdb;

	$range_ids          = array_merge( array( $map->product_id ), $this->compose_range_ids( $map, $reindexed ) );
	$range_placeholders = implode( ', ', array_fill( 0, count( $range_ids ), '%d' ) );

	// Shift the affected range (including the moved product) then pin it to the exact target position; update by PK is nearly instant.
	$updated_count  = 0;
	$updated_count += (int) $wpdb->query(
		// phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
		$wpdb->prepare(
			"UPDATE {$wpdb->posts} SET menu_order = menu_order + %d WHERE ID IN ( {$range_placeholders} )", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			$map->range_delta,
			...$range_ids
		)
	);
	$updated_count += (int) $wpdb->update( $wpdb->posts, array( 'menu_order' => $map->new_position ), array( 'ID' => $map->product_id ) );
	if ( $updated_count > 0 ) {
		// Performance note: fires `clean_post_cache` action per product for extensions compatibility (WooCommerce v11.2).
		// Targeted wp_cache_delete_multiple + wp_cache_set_posts_last_changed is fast, but insufficient for extensibility surface.
		array_walk( $range_ids, 'clean_post_cache' );
	}

	// Fetch updated positions for cache invalidation, hooks, and response; fetch by PK is nearly instant.
	$updated_positions = array_column(
		$wpdb->get_results(
			$wpdb->prepare(
				"SELECT ID, menu_order FROM {$wpdb->posts} WHERE ID IN ( {$range_placeholders} ) ORDER BY menu_order ASC", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
				$range_ids
			)
		),
		'menu_order',
		'ID'
	);

	return array_map( 'intval', $updated_positions );
}