Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableQuery::where()publicWC 1.0

Generates a properly escaped and sanitized WHERE condition for a given field.

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

Хуков нет.

Возвращает

Строку. The resulting WHERE condition.

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

$OrdersTableQuery = new OrdersTableQuery();
$OrdersTableQuery->where( $table, $field, $operator, $value, $type ): string;
$table(строка) (обязательный)
The table the field belongs to.
$field(строка) (обязательный)
The field or column name.
$operator(строка) (обязательный)
The operator to use in the condition. or 'IN' depending on $value.
По умолчанию: '='
$value(разное) (обязательный)
The value.
$type(строка) (обязательный)
The column type as specified in OrdersTableDataStore{} column mappings.

Код OrdersTableQuery::where() WC 9.4.2

public function where( string $table, string $field, string $operator, $value, string $type ): string {
	global $wpdb;

	$db_util  = wc_get_container()->get( DatabaseUtil::class );
	$operator = strtoupper( '' !== $operator ? $operator : '=' );

	try {
		$format = $db_util->get_wpdb_format_for_type( $type );
	} catch ( \Exception $e ) {
		$format = '%s';
	}

	// = and != can be shorthands for IN and NOT in for array values.
	if ( is_array( $value ) && '=' === $operator ) {
		$operator = 'IN';
	} elseif ( is_array( $value ) && '!=' === $operator ) {
		$operator = 'NOT IN';
	}

	if ( ! in_array( $operator, array( '=', '!=', 'IN', 'NOT IN' ), true ) ) {
		return false;
	}

	if ( is_array( $value ) ) {
		$value = array_map( array( $db_util, 'format_object_value_for_db' ), $value, array_fill( 0, count( $value ), $type ) );
	} else {
		$value = $db_util->format_object_value_for_db( $value, $type );
	}

	if ( is_array( $value ) ) {
		$placeholder = array_fill( 0, count( $value ), $format );
		$placeholder = '(' . implode( ',', $placeholder ) . ')';
	} else {
		$placeholder = $format;
	}

	$sql = $wpdb->prepare( "{$table}.{$field} {$operator} {$placeholder}", $value ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare

	return $sql;
}