Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableMetaQuery::generate_where_for_clause_key()privateWC 1.0

Generates an SQL WHERE clause for a given meta_query atomic clause based on its meta key. 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_key( $clause ): string;
$clause(массив) (обязательный)
An atomic meta_query clause.

Код OrdersTableMetaQuery::generate_where_for_clause_key() WC 6.9.4

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

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

	if ( 'NOT EXISTS' === $clause['compare'] ) {
		return "{$clause['alias']}.order_id IS NULL";
	}

	$alias = $clause['alias'];

	if ( in_array( $clause['compare_key'], array( '!=', 'NOT IN', 'NOT LIKE', 'NOT EXISTS', 'NOT REGEXP' ), true ) ) {
		$i                     = count( $this->table_aliases );
		$subquery_alias        = self::ALIAS_PREFIX . $i;
		$this->table_aliases[] = $subquery_alias;

		$meta_compare_string_start  = 'NOT EXISTS (';
		$meta_compare_string_start .= "SELECT 1 FROM {$this->meta_table} {$subquery_alias} ";
		$meta_compare_string_start .= "WHERE {$subquery_alias}.order_id = {$alias}.order_id ";
		$meta_compare_string_end    = 'LIMIT 1';
		$meta_compare_string_end   .= ')';
	}

	switch ( $clause['compare_key'] ) {
		case '=':
		case 'EXISTS':
			$where = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			break;
		case 'LIKE':
			$meta_compare_value = '%' . $wpdb->esc_like( trim( $clause['key'] ) ) . '%';
			$where              = $wpdb->prepare( "$alias.meta_key LIKE %s", $meta_compare_value ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			break;
		case 'IN':
			$meta_compare_string = "$alias.meta_key IN (" . substr( str_repeat( ',%s', count( $clause['key'] ) ), 1 ) . ')';
			$where               = $wpdb->prepare( $meta_compare_string, $clause['key'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
			break;
		case 'RLIKE':
		case 'REGEXP':
			$operator = $clause['compare_key'];
			if ( isset( $clause['type_key'] ) && 'BINARY' === strtoupper( $clause['type_key'] ) ) {
				$cast = 'BINARY';
			} else {
				$cast = '';
			}
			$where = $wpdb->prepare( "$alias.meta_key $operator $cast %s", trim( $clause['key'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			break;
		case '!=':
		case 'NOT EXISTS':
			$meta_compare_string = $meta_compare_string_start . "AND $subquery_alias.meta_key = %s " . $meta_compare_string_end;
			$where               = $wpdb->prepare( $meta_compare_string, $clause['key'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
			break;
		case 'NOT LIKE':
			$meta_compare_string = $meta_compare_string_start . "AND $subquery_alias.meta_key LIKE %s " . $meta_compare_string_end;

			$meta_compare_value = '%' . $wpdb->esc_like( trim( $clause['key'] ) ) . '%';
			$where              = $wpdb->prepare( $meta_compare_string, $meta_compare_value ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
			break;
		case 'NOT IN':
			$array_subclause     = '(' . substr( str_repeat( ',%s', count( $clause['key'] ) ), 1 ) . ') ';
			$meta_compare_string = $meta_compare_string_start . "AND $subquery_alias.meta_key IN " . $array_subclause . $meta_compare_string_end;
			$where               = $wpdb->prepare( $meta_compare_string, $clause['key'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
			break;
		case 'NOT REGEXP':
			$operator = $clause['compare_key'];
			if ( isset( $clause['type_key'] ) && 'BINARY' === strtoupper( $clause['type_key'] ) ) {
				$cast = 'BINARY';
			} else {
				$cast = '';
			}

			$meta_compare_string = $meta_compare_string_start . "AND $subquery_alias.meta_key REGEXP $cast %s " . $meta_compare_string_end;
			$where               = $wpdb->prepare( $meta_compare_string, $clause['key'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
			break;
		default:
			$where = '';
			break;
	}

	return $where;
}