Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::validate_db_row_lengths │ private │ WC 1.0

Throws before any order table is written when a value will not fit its column.

Rows saved with $wpdb->insert() or $wpdb->update() (the ones with a where clause) are refused by wpdb when a value is too long, which would otherwise happen after the order row was already written. The other rows use a raw query, where MySQL truncates instead of failing, so they are not checked.

Method of the class: OrdersTableDataStore{}

No Hooks.

Returns

void. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->validate_db_row_lengths( $db_updates ): void;
$db_updates(array) (required)
Rows to persist, as returned by get_db_rows_for_order().

OrdersTableDataStore::validate_db_row_lengths() code WC 11.3.0

private function validate_db_row_lengths( array $db_updates ): void {
	global $wpdb;

	foreach ( $db_updates as $update ) {
		if ( ! isset( $update['where'] ) ) {
			continue;
		}

		foreach ( $update['data'] as $column => $value ) {
			if ( ! is_string( $value ) || in_array( $update['format'][ $column ] ?? '%s', array( '%d', '%f' ), true ) ) {
				continue;
			}

			$column_length = $wpdb->get_col_length( $update['table'], $column );
			if ( ! is_array( $column_length ) || empty( $column_length['length'] ) ) {
				continue;
			}

			// Same measure wpdb uses: bytes for binary and text columns, characters for char and varchar.
			$value_length = 'byte' === $column_length['type'] ? strlen( $value ) : mb_strlen( $value, 'UTF-8' );
			if ( $value_length > $column_length['length'] ) {
				throw new \Exception(
					esc_html(
						sprintf(
							/* translators: 1: database table name, 2: column name. */
							__( 'Could not persist order to database table "%1$s": the value for "%2$s" is too long.', 'woocommerce' ),
							$update['table'],
							$column
						)
					)
				);
			}
		}
	}
}