Automattic\WooCommerce\Blocks

QueryFilters::get_chosen_attributes()privateWC 1.0

Get an array of attributes and terms selected from query arguments.

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

Хуков нет.

Возвращает

Массив.

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

// private - только в коде основоного (родительского) класса
$result = $this->get_chosen_attributes( $query_vars );
$query_vars(массив) (обязательный)
The WP_Query arguments.

Код QueryFilters::get_chosen_attributes() WC 9.5.1

private function get_chosen_attributes( $query_vars ) {
	$chosen_attributes = array();

	if ( empty( $query_vars ) ) {
		return $chosen_attributes;
	}

	foreach ( $query_vars as $key => $value ) {
		if ( 0 === strpos( $key, 'filter_' ) ) {
			$attribute    = wc_sanitize_taxonomy_name( str_replace( 'filter_', '', $key ) );
			$taxonomy     = wc_attribute_taxonomy_name( $attribute );
			$filter_terms = ! empty( $value ) ? explode( ',', wc_clean( wp_unslash( $value ) ) ) : array();

			if ( empty( $filter_terms ) || ! taxonomy_exists( $taxonomy ) || ! wc_attribute_taxonomy_id_by_name( $attribute ) ) {
				continue;
			}

			$query_type                                   = ! empty( $query_vars[ 'query_type_' . $attribute ] ) && in_array( $query_vars[ 'query_type_' . $attribute ], array( 'and', 'or' ), true ) ? wc_clean( wp_unslash( $query_vars[ 'query_type_' . $attribute ] ) ) : '';
			$chosen_attributes[ $taxonomy ]['terms']      = array_map( 'sanitize_title', $filter_terms ); // Ensures correct encoding.
			$chosen_attributes[ $taxonomy ]['query_type'] = $query_type ? $query_type : 'and';
		}
	}

	return $chosen_attributes;
}