Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableMetaQuery::sanitize_meta_query()privateWC 1.0

Sanitizes the meta_query argument.

Method of the class: OrdersTableMetaQuery{}

No Hooks.

Return

Array. A sanitized meta query array.

Usage

// private - for code of main (parent) class only
$result = $this->sanitize_meta_query( $q ): array;
$q(array) (required)
A meta_query array.

OrdersTableMetaQuery::sanitize_meta_query() code WC 7.1.0

private function sanitize_meta_query( array $q ): array {
	$sanitized = array();

	foreach ( $q as $key => $arg ) {
		if ( 'relation' === $key ) {
			$relation = $arg;
		} elseif ( ! is_array( $arg ) ) {
			continue;
		} elseif ( $this->is_atomic( $arg ) ) {
			if ( isset( $arg['value'] ) && array() === $arg['value'] ) {
				unset( $arg['value'] );
			}

			$arg['compare']      = isset( $arg['compare'] ) ? strtoupper( $arg['compare'] ) : ( isset( $arg['value'] ) && is_array( $arg['value'] ) ? 'IN' : '=' );
			$arg['compare_key']  = isset( $arg['compare_key'] ) ? strtoupper( $arg['compare_key'] ) : ( isset( $arg['key'] ) && is_array( $arg['key'] ) ? 'IN' : '=' );

			if ( ! in_array( $arg['compare'], self::NON_NUMERIC_OPERATORS, true ) && ! in_array( $arg['compare'], self::NUMERIC_OPERATORS, true ) ) {
				$arg['compare'] = '=';
			}

			if ( ! in_array( $arg['compare_key'], self::NON_NUMERIC_OPERATORS, true ) ) {
				$arg['compare_key'] = '=';
			}

			$sanitized[ $key ] = $arg;
		} else {
			$sanitized_arg = $this->sanitize_meta_query( $arg );

			if ( $sanitized_arg ) {
				$sanitized[ $key ] = $sanitized_arg;
			}
		}
	}

	if ( $sanitized ) {
		$sanitized['relation'] = 1 === count( $sanitized ) ? 'OR' : $this->sanitize_relation( $relation ?? 'AND' );
	}

	return $sanitized;
}