Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableMetaQuery::generate_where_for_clause_value()privateWC 1.0

Generates an SQL WHERE clause for a given meta_query atomic clause based on its meta value. Adapted from WordPress' WP_Meta_Query::get_sql_for_clause() method.

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

Хуков нет.

Возвращает

Строку. An SQL WHERE clause or an empty string if $clause is invalid.

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

// private - только в коде основоного (родительского) класса
$result = $this->generate_where_for_clause_value( $clause ): string;
$clause(массив) (обязательный)
An atomic meta_query clause.

Код OrdersTableMetaQuery::generate_where_for_clause_value() WC 9.4.2

private function generate_where_for_clause_value( $clause ): string {
	global $wpdb;

	if ( ! array_key_exists( 'value', $clause ) ) {
		return '';
	}

	$meta_value = $clause['value'];

	if ( in_array( $clause['compare'], array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ), true ) ) {
		if ( ! is_array( $meta_value ) ) {
			$meta_value = preg_split( '/[,\s]+/', $meta_value );
		}
	} elseif ( is_string( $meta_value ) ) {
		$meta_value = trim( $meta_value );
	}

	$meta_compare = $clause['compare'];

	switch ( $meta_compare ) {
		case 'IN':
		case 'NOT IN':
			$where = $wpdb->prepare( '(' . substr( str_repeat( ',%s', count( (array) $meta_value ) ), 1 ) . ')', $meta_value ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
			break;

		case 'BETWEEN':
		case 'NOT BETWEEN':
			$where = $wpdb->prepare( '%s AND %s', $meta_value[0], $meta_value[1] );
			break;

		case 'LIKE':
		case 'NOT LIKE':
			$where = $wpdb->prepare( '%s', '%' . $wpdb->esc_like( $meta_value ) . '%' );
			break;

		// EXISTS with a value is interpreted as '='.
		case 'EXISTS':
			$meta_compare = '=';
			$where        = $wpdb->prepare( '%s', $meta_value );
			break;

		// 'value' is ignored for NOT EXISTS.
		case 'NOT EXISTS':
			$where = '';
			break;

		default:
			$where = $wpdb->prepare( '%s', $meta_value );
			break;
	}

	if ( $where ) {
		if ( 'CHAR' === $clause['cast'] ) {
			return "{$clause['alias']}.meta_value {$meta_compare} {$where}";
		} else {
			return "CAST({$clause['alias']}.meta_value AS {$clause['cast']}) {$meta_compare} {$where}";
		}
	}
}