Automattic\WooCommerce\Blocks

QueryFilters::get_chosen_attributes()privateWC 1.0

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

Method of the class: QueryFilters{}

No Hooks.

Return

Array.

Usage

// private - for code of main (parent) class only
$result = $this->get_chosen_attributes( $query_vars );
$query_vars(array) (required)
The WP_Query arguments.

QueryFilters::get_chosen_attributes() code WC 9.7.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;
}