preview ) && method_exists( $elementor->preview, 'is_preview_mode' ) && $elementor->preview->is_preview_mode() ) {
return true;
}
if ( isset( $elementor->editor ) && method_exists( $elementor->editor, 'is_edit_mode' ) && $elementor->editor->is_edit_mode() ) {
return true;
}
return false;
}
function acewpf_late_inline_style_blocks( $style_block = null, $flush = false ) {
static $blocks = array();
if ( $flush ) {
$pending = $blocks;
$blocks = array();
return $pending;
}
$style_block = trim( (string) $style_block );
if ( '' !== $style_block ) {
$blocks[ md5( $style_block ) ] = $style_block;
}
return $blocks;
}
function acewpf_print_late_styles() {
if ( ! wp_style_is( 'acewpf-filter-style', 'done' ) ) {
if ( ! wp_style_is( 'acewpf-filter-style', 'registered' ) ) {
acewpf_register_assets();
}
if ( ! wp_style_is( 'acewpf-filter-style', 'enqueued' ) ) {
wp_enqueue_style( 'acewpf-filter-style' );
}
wp_print_styles( array( 'acewpf-filter-style' ) );
}
foreach ( acewpf_late_inline_style_blocks( null, true ) as $hash => $css ) {
printf(
'',
esc_attr( 'acewpf-filter-inline-' . substr( (string) $hash, 0, 12 ) ),
wp_strip_all_tags( $css )
);
}
}
function acewpf_should_print_filter_styles_immediately() {
if ( wp_doing_ajax() ) {
return true;
}
if ( did_action( 'wp_head' ) ) {
return true;
}
return acewpf_is_elementor_edit_or_preview() && did_action( 'admin_head' );
}
/**
* Strip auto-inserted
tags that break flex layouts.
*
* Some themes/editors run wpautop/nl2br on shortcode or block output. Those
* breaks become flex items between labels, counts, and filter buttons.
*
* @param string $html Filter markup.
* @return string
*/
function acewpf_sanitize_filter_markup( $html ) {
$html = (string) $html;
if ( '' === $html ) {
return $html;
}
// Remove breaks and collapse inter-tag whitespace so nl2br/wpautop cannot re-inject them.
$html = preg_replace( '#
#i', '', $html );
$html = preg_replace( '/>\s+', '><', $html );
return $html;
}
function acewpf_prefix_late_filter_styles( $html ) {
$html = acewpf_sanitize_filter_markup( $html );
if ( '' === $html || ! acewpf_should_print_filter_styles_immediately() ) {
return $html;
}
ob_start();
acewpf_print_late_styles();
return ob_get_clean() . $html;
}
function acewpf_enqueue_assets() {
static $assets_enqueued = false;
if ( ! wp_style_is( 'acewpf-filter-style', 'registered' ) || ! wp_script_is( 'acewpf-filter-script', 'registered' ) ) {
acewpf_register_assets();
}
if ( $assets_enqueued ) {
return;
}
$assets_enqueued = true;
wp_enqueue_style( 'acewpf-filter-style' );
wp_enqueue_script( 'acewpf-filter-script' );
wp_localize_script(
'acewpf-filter-script',
'acewpfFilterI18n',
array(
'noResultsMessage' => __( 'No products were found matching your selection.', 'ace-products-filters' ),
)
);
}
function acewpf_enqueue_google_font_stylesheet( $styles, $style_keys ) {
static $loaded_urls = array();
if ( ! acewpf_google_fonts_are_enabled() ) {
return;
}
$styles = is_array( $styles ) ? $styles : array();
$style_keys = is_array( $style_keys ) ? $style_keys : array();
$families = array();
foreach ( $style_keys as $style_key ) {
if ( empty( $styles[ $style_key ] ) ) {
continue;
}
$family = acewpf_get_google_font_family_name( $styles[ $style_key ] );
if ( '' !== $family ) {
$families[] = $family;
}
}
$url = acewpf_get_google_fonts_url( $families );
if ( '' === $url || isset( $loaded_urls[ $url ] ) ) {
return;
}
$loaded_urls[ $url ] = true;
$handle = 'cpf-fonts-' . substr( md5( $url ), 0, 12 );
wp_enqueue_style(
$handle,
$url,
array(),
substr( md5( $url ), 0, 12 )
);
if ( did_action( 'wp_head' ) ) {
add_action(
'wp_footer',
static function () use ( $handle ) {
if ( ! wp_style_is( $handle, 'done' ) ) {
wp_print_styles( array( $handle ) );
}
},
1
);
}
}
function acewpf_enqueue_filter_font_stylesheet( $filter ) {
$styles = isset( $filter['styles'] ) && is_array( $filter['styles'] ) ? $filter['styles'] : array();
acewpf_enqueue_google_font_stylesheet(
$styles,
array( 'label_font_family', 'panel_heading_font_family', 'count_font_family', 'button_font_family', 'term_search_font_family' )
);
}
function acewpf_enqueue_panel_font_stylesheet( $panel ) {
$styles = isset( $panel['styles'] ) && is_array( $panel['styles'] ) ? $panel['styles'] : array();
acewpf_enqueue_google_font_stylesheet(
$styles,
array( 'mobile_toggle_font_family', 'group_header_font_family' )
);
}
function acewpf_enqueue_inline_style_block( $style_block ) {
static $enqueued_blocks = array();
$style_block = trim( (string) $style_block );
if ( '' === $style_block ) {
return;
}
acewpf_enqueue_assets();
$style_hash = md5( $style_block );
if ( isset( $enqueued_blocks[ $style_hash ] ) ) {
return;
}
$enqueued_blocks[ $style_hash ] = true;
if ( wp_style_is( 'acewpf-filter-style', 'done' ) ) {
acewpf_late_inline_style_blocks( $style_block );
return;
}
wp_add_inline_style( 'acewpf-filter-style', $style_block );
}
function acewpf_build_css_rule( $selector, $declarations ) {
if ( empty( $declarations ) ) {
return '';
}
return $selector . '{' . implode( ';', $declarations ) . ';}';
}
function acewpf_style_value_is_set( $value ) {
return '' !== trim( (string) $value );
}
function acewpf_append_panel_button_style_rules( &$rules, $scope, $selector, $hover_selector, $styles, $prefix ) {
$styles = is_array( $styles ) ? $styles : array();
$base_declarations = array();
$hover_declarations = array();
$base_declarations[] = 'font-style:normal';
if ( ! empty( $styles[ $prefix . '_text_color' ] ) ) {
$base_declarations[] = 'color:' . $styles[ $prefix . '_text_color' ];
}
if ( ! empty( $styles[ $prefix . '_background_color' ] ) ) {
$base_declarations[] = 'background:' . $styles[ $prefix . '_background_color' ];
}
if ( ! empty( $styles[ $prefix . '_border_color' ] ) ) {
$base_declarations[] = 'border-color:' . $styles[ $prefix . '_border_color' ];
}
if ( isset( $styles[ $prefix . '_border_width' ] ) && acewpf_style_value_is_set( $styles[ $prefix . '_border_width' ] ) ) {
$base_declarations[] = 'border-width:' . absint( $styles[ $prefix . '_border_width' ] ) . 'px';
}
if ( isset( $styles[ $prefix . '_padding_y' ] ) && acewpf_style_value_is_set( $styles[ $prefix . '_padding_y' ] ) ) {
$base_declarations[] = 'padding-top:' . absint( $styles[ $prefix . '_padding_y' ] ) . 'px';
$base_declarations[] = 'padding-bottom:' . absint( $styles[ $prefix . '_padding_y' ] ) . 'px';
}
if ( isset( $styles[ $prefix . '_padding_x' ] ) && acewpf_style_value_is_set( $styles[ $prefix . '_padding_x' ] ) ) {
$base_declarations[] = 'padding-left:' . absint( $styles[ $prefix . '_padding_x' ] ) . 'px';
$base_declarations[] = 'padding-right:' . absint( $styles[ $prefix . '_padding_x' ] ) . 'px';
}
if ( isset( $styles[ $prefix . '_border_radius' ] ) && acewpf_style_value_is_set( $styles[ $prefix . '_border_radius' ] ) ) {
$base_declarations[] = 'border-radius:' . absint( $styles[ $prefix . '_border_radius' ] ) . 'px';
}
acewpf_append_typography_declarations( $base_declarations, $styles, $prefix );
$base_rule = acewpf_build_css_rule( $scope . ' ' . $selector, $base_declarations );
if ( '' !== $base_rule ) {
$rules[] = $base_rule;
}
if ( ! empty( $styles[ $prefix . '_hover_text_color' ] ) ) {
$hover_declarations[] = 'color:' . $styles[ $prefix . '_hover_text_color' ];
}
if ( ! empty( $styles[ $prefix . '_hover_background_color' ] ) ) {
$hover_declarations[] = 'background:' . $styles[ $prefix . '_hover_background_color' ];
}
if ( ! empty( $styles[ $prefix . '_hover_border_color' ] ) ) {
$hover_declarations[] = 'border-color:' . $styles[ $prefix . '_hover_border_color' ];
}
$hover_rule = acewpf_build_css_rule( $scope . ' ' . $hover_selector, $hover_declarations );
if ( '' !== $hover_rule ) {
$rules[] = $hover_rule;
}
}
function acewpf_append_typography_declarations( &$declarations, $styles, $prefix ) {
$font_family = isset( $styles[ $prefix . '_font_family' ] ) ? acewpf_get_font_family_css_value( $styles[ $prefix . '_font_family' ] ) : '';
$font_weight = isset( $styles[ $prefix . '_font_weight' ] ) ? trim( (string) $styles[ $prefix . '_font_weight' ] ) : '';
$font_size = isset( $styles[ $prefix . '_font_size' ] ) ? trim( (string) $styles[ $prefix . '_font_size' ] ) : '';
$letter_spacing = isset( $styles[ $prefix . '_letter_spacing' ] ) ? trim( (string) $styles[ $prefix . '_letter_spacing' ] ) : '';
$text_transform = isset( $styles[ $prefix . '_text_transform' ] ) ? trim( (string) $styles[ $prefix . '_text_transform' ] ) : '';
if ( '' !== $font_family ) {
$declarations[] = 'font-family:' . $font_family;
}
if ( '' !== $font_weight ) {
$declarations[] = 'font-weight:' . $font_weight;
}
if ( '' !== $font_size ) {
$declarations[] = 'font-size:' . $font_size . 'px';
}
if ( '' !== $letter_spacing ) {
$declarations[] = 'letter-spacing:' . $letter_spacing . 'px';
}
if ( '' !== $text_transform ) {
$declarations[] = 'text-transform:' . $text_transform;
}
}
function acewpf_get_filter_responsive_style_block( $filter ) {
$styles = isset( $filter['styles'] ) && is_array( $filter['styles'] ) ? $filter['styles'] : array();
$responsive = isset( $styles['responsive'] ) && is_array( $styles['responsive'] ) ? $styles['responsive'] : array();
$scope = '.cpf-filter--' . sanitize_html_class( $filter['id'] );
$responsive_rules = array();
foreach ( acewpf_get_responsive_style_modes() as $mode_key => $mode ) {
$mode_styles = isset( $responsive[ $mode_key ] ) && is_array( $responsive[ $mode_key ] ) ? $responsive[ $mode_key ] : array();
$mode_rules = array();
$container_declarations = array();
if ( isset( $mode_styles['gap'] ) && acewpf_style_value_is_set( $mode_styles['gap'] ) ) {
$container_declarations[] = 'gap:' . absint( $mode_styles['gap'] ) . 'px';
}
$container_rule = acewpf_build_css_rule( $scope, $container_declarations );
if ( '' !== $container_rule ) {
$mode_rules[] = $container_rule;
}
$button_declarations = array();
if ( isset( $mode_styles['button_padding_y'] ) && acewpf_style_value_is_set( $mode_styles['button_padding_y'] ) ) {
$button_declarations[] = 'padding-top:' . absint( $mode_styles['button_padding_y'] ) . 'px';
$button_declarations[] = 'padding-bottom:' . absint( $mode_styles['button_padding_y'] ) . 'px';
}
if ( isset( $mode_styles['button_padding_x'] ) && acewpf_style_value_is_set( $mode_styles['button_padding_x'] ) ) {
$button_declarations[] = 'padding-left:' . absint( $mode_styles['button_padding_x'] ) . 'px';
$button_declarations[] = 'padding-right:' . absint( $mode_styles['button_padding_x'] ) . 'px';
}
$button_rule = acewpf_build_css_rule( $scope . ' .cpf-filter__button, ' . $scope . ' .cpf-filter__mobile-toggle', $button_declarations );
if ( '' !== $button_rule ) {
$mode_rules[] = $button_rule;
}
$label_declarations = array();
if ( isset( $mode_styles['label_font_size'] ) && acewpf_style_value_is_set( $mode_styles['label_font_size'] ) ) {
$label_declarations[] = 'font-size:' . absint( $mode_styles['label_font_size'] ) . 'px';
}
$label_rule = acewpf_build_css_rule( $scope . ' .cpf-filter__label', $label_declarations );
if ( '' !== $label_rule ) {
$mode_rules[] = $label_rule;
}
$panel_heading_declarations = array();
if ( isset( $mode_styles['panel_heading_font_size'] ) && acewpf_style_value_is_set( $mode_styles['panel_heading_font_size'] ) ) {
$panel_heading_declarations[] = 'font-size:' . absint( $mode_styles['panel_heading_font_size'] ) . 'px';
} elseif ( isset( $mode_styles['label_font_size'] ) && acewpf_style_value_is_set( $mode_styles['label_font_size'] ) ) {
$panel_heading_declarations[] = 'font-size:' . absint( $mode_styles['label_font_size'] ) . 'px';
}
$panel_heading_rule = acewpf_build_css_rule( $scope . ' .cpf-filter__panel-heading', $panel_heading_declarations );
if ( '' !== $panel_heading_rule ) {
$mode_rules[] = $panel_heading_rule;
}
$count_declarations = array();
if ( isset( $mode_styles['count_font_size'] ) && acewpf_style_value_is_set( $mode_styles['count_font_size'] ) ) {
$count_declarations[] = 'font-size:' . absint( $mode_styles['count_font_size'] ) . 'px';
}
$count_rule = acewpf_build_css_rule( $scope . ' .cpf-filter__count', $count_declarations );
if ( '' !== $count_rule ) {
$mode_rules[] = $count_rule;
}
if ( empty( $mode_rules ) ) {
continue;
}
$responsive_rules[] = '@media (max-width: ' . absint( $mode['max_width'] ) . 'px){' . implode( '', $mode_rules ) . '}';
}
return implode( "\n", $responsive_rules );
}
function acewpf_get_filter_style_block( $filter ) {
$styles = isset( $filter['styles'] ) && is_array( $filter['styles'] ) ? $filter['styles'] : array();
$scope = '.cpf-filter--' . sanitize_html_class( $filter['id'] );
$rules = array();
$container_declarations = array();
if ( isset( $styles['gap'] ) && acewpf_style_value_is_set( $styles['gap'] ) ) {
$container_declarations[] = 'gap:' . absint( $styles['gap'] ) . 'px';
}
$button_declarations = array();
if ( ! empty( $styles['label_color'] ) ) {
$button_declarations[] = 'color:' . $styles['label_color'];
} elseif ( ! empty( $styles['count_color'] ) ) {
$button_declarations[] = 'color:' . $styles['count_color'];
}
if ( ! empty( $styles['button_background_color'] ) ) {
$button_declarations[] = 'background:' . $styles['button_background_color'];
}
if ( ! empty( $styles['button_border_color'] ) ) {
$button_declarations[] = 'border-color:' . $styles['button_border_color'];
}
if ( isset( $styles['button_border_width'] ) && acewpf_style_value_is_set( $styles['button_border_width'] ) ) {
$button_declarations[] = 'border-width:' . absint( $styles['button_border_width'] ) . 'px';
}
if ( isset( $styles['checkbox_size'] ) && acewpf_style_value_is_set( $styles['checkbox_size'] ) ) {
$container_declarations[] = '--cpf-checkbox-size:' . absint( $styles['checkbox_size'] ) . 'px';
}
if ( isset( $styles['checkbox_border_width'] ) && acewpf_style_value_is_set( $styles['checkbox_border_width'] ) ) {
$container_declarations[] = '--cpf-checkbox-border-width:' . absint( $styles['checkbox_border_width'] ) . 'px';
}
if ( isset( $styles['checkbox_border_radius'] ) && acewpf_style_value_is_set( $styles['checkbox_border_radius'] ) ) {
$container_declarations[] = '--cpf-checkbox-radius:' . absint( $styles['checkbox_border_radius'] ) . 'px';
}
$container_rule = acewpf_build_css_rule( $scope, $container_declarations );
if ( '' !== $container_rule ) {
$rules[] = $container_rule;
}
if ( isset( $styles['border_radius'] ) && acewpf_style_value_is_set( $styles['border_radius'] ) ) {
$button_declarations[] = 'border-radius:' . absint( $styles['border_radius'] ) . 'px';
}
if ( isset( $styles['button_padding_y'] ) && acewpf_style_value_is_set( $styles['button_padding_y'] ) ) {
$button_declarations[] = 'padding-top:' . absint( $styles['button_padding_y'] ) . 'px';
$button_declarations[] = 'padding-bottom:' . absint( $styles['button_padding_y'] ) . 'px';
}
if ( isset( $styles['button_padding_x'] ) && acewpf_style_value_is_set( $styles['button_padding_x'] ) ) {
$button_declarations[] = 'padding-left:' . absint( $styles['button_padding_x'] ) . 'px';
$button_declarations[] = 'padding-right:' . absint( $styles['button_padding_x'] ) . 'px';
}
$button_rule = acewpf_build_css_rule( $scope . ' .cpf-filter__button, ' . $scope . ' .cpf-filter__mobile-toggle', $button_declarations );
if ( '' !== $button_rule ) {
$rules[] = $button_rule;
}
$active_declarations = array();
if ( ! empty( $styles['active_text_color'] ) ) {
$active_declarations[] = 'color:' . $styles['active_text_color'];
}
if ( ! empty( $styles['active_background_color'] ) ) {
$active_declarations[] = 'background:' . $styles['active_background_color'];
}
if ( ! empty( $styles['active_border_color'] ) ) {
$active_declarations[] = 'border-color:' . $styles['active_border_color'];
}
$active_rule = acewpf_build_css_rule( $scope . ' .cpf-filter__button.is-active, ' . $scope . ' .cpf-filter__choice.is-active .cpf-filter__button, ' . $scope . ' .cpf-filter__mobile-toggle.is-selected', $active_declarations );
if ( '' !== $active_rule ) {
$rules[] = $active_rule;
}
if ( ! empty( $styles['active_text_color'] ) ) {
$active_text_rule = acewpf_build_css_rule(
$scope . ' .cpf-filter__button.is-active .cpf-filter__label, ' . $scope . ' .cpf-filter__button.is-active .cpf-filter__count, ' . $scope . ' .cpf-filter__choice.is-active .cpf-filter__label, ' . $scope . ' .cpf-filter__choice.is-active .cpf-filter__count, ' . $scope . ' .cpf-filter__mobile-toggle.is-selected .cpf-filter__label, ' . $scope . ' .cpf-filter__mobile-toggle.is-selected .cpf-filter__count',
array( 'color:' . $styles['active_text_color'] )
);
if ( '' !== $active_text_rule ) {
$rules[] = $active_text_rule;
}
}
$hover_declarations = array();
if ( ! empty( $styles['button_hover_text_color'] ) ) {
$hover_declarations[] = 'color:' . $styles['button_hover_text_color'];
}
if ( ! empty( $styles['button_hover_background_color'] ) ) {
$hover_declarations[] = 'background:' . $styles['button_hover_background_color'];
}
if ( ! empty( $styles['button_hover_border_color'] ) ) {
$hover_declarations[] = 'border-color:' . $styles['button_hover_border_color'];
}
$hover_rule = acewpf_build_css_rule( $scope . ' .cpf-filter__button:not(.is-active):hover, ' . $scope . ' .cpf-filter__choice:not(.is-active):hover .cpf-filter__button, ' . $scope . ' .cpf-filter__mobile-toggle:not(.is-selected):hover', $hover_declarations );
if ( '' !== $hover_rule ) {
$rules[] = $hover_rule;
}
if ( ! empty( $styles['button_hover_text_color'] ) ) {
$hover_text_rule = acewpf_build_css_rule(
$scope . ' .cpf-filter__button:not(.is-active):hover .cpf-filter__label, ' . $scope . ' .cpf-filter__button:not(.is-active):hover .cpf-filter__count, ' . $scope . ' .cpf-filter__choice:not(.is-active):hover .cpf-filter__label, ' . $scope . ' .cpf-filter__choice:not(.is-active):hover .cpf-filter__count, ' . $scope . ' .cpf-filter__mobile-toggle:not(.is-selected):hover .cpf-filter__label, ' . $scope . ' .cpf-filter__mobile-toggle:not(.is-selected):hover .cpf-filter__count',
array( 'color:' . $styles['button_hover_text_color'] )
);
if ( '' !== $hover_text_rule ) {
$rules[] = $hover_text_rule;
}
}
$label_declarations = array();
if ( ! empty( $styles['label_color'] ) ) {
$label_declarations[] = 'color:' . $styles['label_color'];
}
acewpf_append_typography_declarations( $label_declarations, $styles, 'label' );
$label_rule = acewpf_build_css_rule( $scope . ' .cpf-filter__label', $label_declarations );
if ( '' !== $label_rule ) {
$rules[] = $label_rule;
}
$panel_heading_declarations = array();
$panel_heading_styles = array(
'panel_heading_font_family' => ! empty( $styles['panel_heading_font_family'] ) ? $styles['panel_heading_font_family'] : ( isset( $styles['label_font_family'] ) ? $styles['label_font_family'] : '' ),
'panel_heading_font_weight' => ! empty( $styles['panel_heading_font_weight'] ) ? $styles['panel_heading_font_weight'] : ( isset( $styles['label_font_weight'] ) ? $styles['label_font_weight'] : '' ),
'panel_heading_font_size' => ( isset( $styles['panel_heading_font_size'] ) && acewpf_style_value_is_set( $styles['panel_heading_font_size'] ) ) ? $styles['panel_heading_font_size'] : ( isset( $styles['label_font_size'] ) ? $styles['label_font_size'] : '' ),
'panel_heading_letter_spacing' => ! empty( $styles['panel_heading_letter_spacing'] ) ? $styles['panel_heading_letter_spacing'] : ( isset( $styles['label_letter_spacing'] ) ? $styles['label_letter_spacing'] : '' ),
'panel_heading_text_transform' => ! empty( $styles['panel_heading_text_transform'] ) ? $styles['panel_heading_text_transform'] : ( isset( $styles['label_text_transform'] ) ? $styles['label_text_transform'] : '' ),
);
if ( ! empty( $styles['panel_heading_color'] ) ) {
$panel_heading_declarations[] = 'color:' . $styles['panel_heading_color'];
} elseif ( ! empty( $styles['label_color'] ) ) {
$panel_heading_declarations[] = 'color:' . $styles['label_color'];
}
acewpf_append_typography_declarations( $panel_heading_declarations, $panel_heading_styles, 'panel_heading' );
$panel_heading_rule = acewpf_build_css_rule( $scope . ' .cpf-filter__panel-heading', $panel_heading_declarations );
if ( '' !== $panel_heading_rule ) {
$rules[] = $panel_heading_rule;
}
$panel_toggle_declarations = array();
if ( ! empty( $styles['panel_toggle_icon_color'] ) ) {
$panel_toggle_declarations[] = 'color:' . $styles['panel_toggle_icon_color'];
} elseif ( ! empty( $styles['panel_heading_color'] ) ) {
$panel_toggle_declarations[] = 'color:' . $styles['panel_heading_color'];
}
if ( isset( $styles['panel_toggle_icon_size'] ) && acewpf_style_value_is_set( $styles['panel_toggle_icon_size'] ) ) {
$panel_toggle_declarations[] = '--cpf-panel-toggle-size:' . absint( $styles['panel_toggle_icon_size'] ) . 'px';
}
if ( ! empty( $styles['panel_toggle_hover_icon_color'] ) ) {
$panel_toggle_declarations[] = '--cpf-panel-toggle-hover-color:' . $styles['panel_toggle_hover_icon_color'];
}
if ( ! empty( $styles['panel_toggle_hover_background_color'] ) ) {
$panel_toggle_declarations[] = '--cpf-panel-toggle-hover-background:' . $styles['panel_toggle_hover_background_color'];
}
if ( ! empty( $styles['panel_toggle_active_icon_color'] ) ) {
$panel_toggle_declarations[] = '--cpf-panel-toggle-active-color:' . $styles['panel_toggle_active_icon_color'];
}
if ( ! empty( $styles['panel_toggle_active_background_color'] ) ) {
$panel_toggle_declarations[] = '--cpf-panel-toggle-active-background:' . $styles['panel_toggle_active_background_color'];
}
$panel_toggle_rule = acewpf_build_css_rule( $scope . ' .cpf-filter__panel-toggle', $panel_toggle_declarations );
if ( '' !== $panel_toggle_rule ) {
$rules[] = $panel_toggle_rule;
}
$count_declarations = array();
if ( ! empty( $styles['count_color'] ) ) {
$count_declarations[] = 'color:' . $styles['count_color'];
}
acewpf_append_typography_declarations( $count_declarations, $styles, 'count' );
$count_rule = acewpf_build_css_rule( $scope . ' .cpf-filter__count', $count_declarations );
if ( '' !== $count_rule ) {
$rules[] = $count_rule;
}
if ( ! acewpf_is_price_filter( $filter ) ) {
$term_search_declarations = array();
$term_search_placeholder_declarations = array();
$term_search_focus_declarations = array();
if ( ! empty( $styles['term_search_text_color'] ) ) {
$term_search_declarations[] = 'color:' . $styles['term_search_text_color'];
}
if ( ! empty( $styles['term_search_background_color'] ) ) {
$term_search_declarations[] = 'background:' . $styles['term_search_background_color'];
}
if ( ! empty( $styles['term_search_border_color'] ) ) {
$term_search_declarations[] = 'border-color:' . $styles['term_search_border_color'];
}
if ( isset( $styles['term_search_border_width'] ) && acewpf_style_value_is_set( $styles['term_search_border_width'] ) ) {
$term_search_declarations[] = 'border-width:' . absint( $styles['term_search_border_width'] ) . 'px';
}
if ( isset( $styles['term_search_border_radius'] ) && acewpf_style_value_is_set( $styles['term_search_border_radius'] ) ) {
$term_search_declarations[] = 'border-radius:' . absint( $styles['term_search_border_radius'] ) . 'px';
}
if ( isset( $styles['term_search_padding_y'] ) && acewpf_style_value_is_set( $styles['term_search_padding_y'] ) ) {
$term_search_declarations[] = 'padding-top:' . absint( $styles['term_search_padding_y'] ) . 'px';
$term_search_declarations[] = 'padding-bottom:' . absint( $styles['term_search_padding_y'] ) . 'px';
}
if ( isset( $styles['term_search_padding_x'] ) && acewpf_style_value_is_set( $styles['term_search_padding_x'] ) ) {
$term_search_declarations[] = 'padding-left:' . absint( $styles['term_search_padding_x'] ) . 'px';
$term_search_declarations[] = 'padding-right:' . absint( $styles['term_search_padding_x'] ) . 'px';
}
acewpf_append_typography_declarations( $term_search_declarations, $styles, 'term_search' );
$term_search_rule = acewpf_build_css_rule( $scope . ' .cpf-filter__term-search-input', $term_search_declarations );
if ( '' !== $term_search_rule ) {
$rules[] = $term_search_rule;
}
if ( ! empty( $styles['term_search_text_color'] ) ) {
$term_search_clear_rule = acewpf_build_css_rule(
$scope . ' .cpf-filter__term-search-clear',
array( 'color:' . $styles['term_search_text_color'] )
);
if ( '' !== $term_search_clear_rule ) {
$rules[] = $term_search_clear_rule;
}
}
if ( ! empty( $styles['term_search_placeholder_color'] ) ) {
$term_search_placeholder_declarations[] = 'color:' . $styles['term_search_placeholder_color'];
$term_search_placeholder_rule = acewpf_build_css_rule(
$scope . ' .cpf-filter__term-search-input::placeholder, ' . $scope . ' .cpf-filter__term-search-input::-webkit-input-placeholder',
$term_search_placeholder_declarations
);
if ( '' !== $term_search_placeholder_rule ) {
$rules[] = $term_search_placeholder_rule;
}
}
if ( ! empty( $styles['term_search_focus_border_color'] ) ) {
$term_search_focus_declarations[] = 'border-color:' . $styles['term_search_focus_border_color'];
$term_search_focus_rule = acewpf_build_css_rule( $scope . ' .cpf-filter__term-search-input:focus', $term_search_focus_declarations );
if ( '' !== $term_search_focus_rule ) {
$rules[] = $term_search_focus_rule;
}
}
if ( isset( $styles['term_search_list_max_height'] ) && acewpf_style_value_is_set( $styles['term_search_list_max_height'] ) ) {
$list_max_height = absint( $styles['term_search_list_max_height'] );
$term_search_list_rule = acewpf_build_css_rule(
$scope,
array( '--cpf-term-search-list-max-height:' . ( $list_max_height > 0 ? $list_max_height . 'px' : 'none' ) )
);
if ( '' !== $term_search_list_rule ) {
$rules[] = $term_search_list_rule;
}
}
}
if ( acewpf_is_price_filter( $filter ) ) {
$price_declarations = array();
if ( ! empty( $styles['label_color'] ) ) {
$price_declarations[] = '--cpf-price-text-color:' . $styles['label_color'];
} elseif ( ! empty( $styles['button_text_color'] ) ) {
$price_declarations[] = '--cpf-price-text-color:' . $styles['button_text_color'];
}
if ( ! empty( $styles['active_background_color'] ) ) {
$price_declarations[] = '--cpf-price-accent-color:' . $styles['active_background_color'];
} elseif ( ! empty( $styles['active_border_color'] ) ) {
$price_declarations[] = '--cpf-price-accent-color:' . $styles['active_border_color'];
} elseif ( ! empty( $styles['button_border_color'] ) ) {
$price_declarations[] = '--cpf-price-accent-color:' . $styles['button_border_color'];
} elseif ( ! empty( $styles['button_text_color'] ) ) {
$price_declarations[] = '--cpf-price-accent-color:' . $styles['button_text_color'];
}
if ( ! empty( $styles['button_background_color'] ) ) {
$price_declarations[] = '--cpf-price-input-background:' . $styles['button_background_color'];
}
if ( ! empty( $styles['button_text_color'] ) ) {
$price_declarations[] = '--cpf-price-input-text-color:' . $styles['button_text_color'];
}
if ( ! empty( $styles['button_border_color'] ) ) {
$price_declarations[] = '--cpf-price-input-border-color:' . $styles['button_border_color'];
}
if ( ! empty( $styles['price_track_color'] ) ) {
$price_declarations[] = '--cpf-price-track-color:' . $styles['price_track_color'];
}
if ( isset( $styles['price_track_height'] ) && acewpf_style_value_is_set( $styles['price_track_height'] ) ) {
$price_declarations[] = '--cpf-price-track-height:' . absint( $styles['price_track_height'] ) . 'px';
}
if ( isset( $styles['price_handle_size'] ) && acewpf_style_value_is_set( $styles['price_handle_size'] ) ) {
$price_declarations[] = '--cpf-price-handle-size:' . absint( $styles['price_handle_size'] ) . 'px';
}
if ( isset( $styles['price_input_width'] ) && acewpf_style_value_is_set( $styles['price_input_width'] ) ) {
$price_declarations[] = '--cpf-price-input-width:' . absint( $styles['price_input_width'] ) . 'px';
}
if ( isset( $styles['price_input_font_size'] ) && acewpf_style_value_is_set( $styles['price_input_font_size'] ) ) {
$price_declarations[] = '--cpf-price-input-font-size:' . absint( $styles['price_input_font_size'] ) . 'px';
}
if ( isset( $styles['price_input_padding_y'] ) && acewpf_style_value_is_set( $styles['price_input_padding_y'] ) ) {
$price_declarations[] = '--cpf-price-input-padding-y:' . absint( $styles['price_input_padding_y'] ) . 'px';
}
if ( isset( $styles['price_input_padding_x'] ) && acewpf_style_value_is_set( $styles['price_input_padding_x'] ) ) {
$price_declarations[] = '--cpf-price-input-padding-x:' . absint( $styles['price_input_padding_x'] ) . 'px';
}
if ( isset( $styles['border_radius'] ) && acewpf_style_value_is_set( $styles['border_radius'] ) ) {
$price_declarations[] = '--cpf-price-radius:' . absint( $styles['border_radius'] ) . 'px';
}
$price_rule = acewpf_build_css_rule( $scope . '.cpf-filter--price', $price_declarations );
if ( '' !== $price_rule ) {
$rules[] = $price_rule;
}
}
if ( function_exists( 'acewpf_filter_uses_rating_stars' ) && acewpf_filter_uses_rating_stars( $filter ) ) {
$rating_declarations = array();
$star_url = acewpf_get_rating_star_mask_url( $filter );
$star_url = is_string( $star_url ) ? esc_url_raw( $star_url ) : '';
$star_url = str_replace( array( '"', "'", '(', ')' ), '', $star_url );
if ( '' !== $star_url ) {
$rating_declarations[] = '--cpf-rating-star-image:url("' . $star_url . '")';
}
if ( ! empty( $styles['rating_star_color'] ) ) {
$rating_declarations[] = '--cpf-rating-star-color:' . $styles['rating_star_color'];
}
if ( ! empty( $styles['rating_star_empty_color'] ) ) {
$rating_declarations[] = '--cpf-rating-star-empty-color:' . $styles['rating_star_empty_color'];
}
if ( isset( $styles['rating_star_size'] ) && acewpf_style_value_is_set( $styles['rating_star_size'] ) ) {
$rating_declarations[] = '--cpf-rating-star-size:' . absint( $styles['rating_star_size'] ) . 'px';
}
if ( isset( $styles['rating_star_gap'] ) && acewpf_style_value_is_set( $styles['rating_star_gap'] ) ) {
$rating_declarations[] = '--cpf-rating-star-gap:' . absint( $styles['rating_star_gap'] ) . 'px';
}
$rating_rule = acewpf_build_css_rule( $scope . '.cpf-filter--rating-stars', $rating_declarations );
if ( '' !== $rating_rule ) {
$rules[] = $rating_rule;
}
}
if ( acewpf_filter_uses_swatches( $filter ) ) {
$swatch_declarations = array(
'min-height:0',
'width:auto',
'padding:4px',
'background:transparent',
'border-color:transparent',
'box-shadow:none',
);
if ( isset( $styles['swatch_size'] ) && acewpf_style_value_is_set( $styles['swatch_size'] ) ) {
$swatch_declarations[] = '--cpf-swatch-size:' . absint( $styles['swatch_size'] ) . 'px';
}
$swatch_area_declarations = array();
if ( ! empty( $styles['swatch_background_color'] ) ) {
$swatch_area_declarations[] = '--cpf-swatch-area-background:' . $styles['swatch_background_color'];
$swatch_area_declarations[] = 'background:' . $styles['swatch_background_color'];
$swatch_area_declarations[] = 'padding:10px';
if ( isset( $styles['border_radius'] ) && acewpf_style_value_is_set( $styles['border_radius'] ) ) {
$swatch_area_declarations[] = 'border-radius:' . absint( $styles['border_radius'] ) . 'px';
}
}
$swatch_area_rule = acewpf_build_css_rule(
$scope . '.cpf-filter--swatches .cpf-filter__panel-body',
$swatch_area_declarations
);
if ( '' !== $swatch_area_rule ) {
$rules[] = $swatch_area_rule;
}
$swatch_button_rule = acewpf_build_css_rule(
$scope . '.cpf-filter--swatches .cpf-filter__button, ' . $scope . '.cpf-filter--swatches .cpf-filter__choice .cpf-filter__button',
$swatch_declarations
);
if ( '' !== $swatch_button_rule ) {
$rules[] = $swatch_button_rule;
}
$swatch_active_color = '';
if ( ! empty( $styles['active_border_color'] ) ) {
$swatch_active_color = $styles['active_border_color'];
} elseif ( ! empty( $styles['active_background_color'] ) ) {
$swatch_active_color = $styles['active_background_color'];
} elseif ( ! empty( $styles['button_border_color'] ) ) {
$swatch_active_color = $styles['button_border_color'];
}
$swatch_text_color = '';
if ( ! empty( $styles['label_color'] ) ) {
$swatch_text_color = $styles['label_color'];
} elseif ( ! empty( $styles['button_text_color'] ) ) {
$swatch_text_color = $styles['button_text_color'];
}
$swatch_reset_declarations = array(
'background:transparent',
'border-color:transparent',
'box-shadow:none',
);
if ( '' !== $swatch_text_color ) {
$swatch_reset_declarations[] = 'color:' . $swatch_text_color;
}
$swatch_reset_rule = acewpf_build_css_rule(
$scope . '.cpf-filter--swatches .cpf-filter__button.is-active, ' . $scope . '.cpf-filter--swatches .cpf-filter__choice.is-active .cpf-filter__button, ' . $scope . '.cpf-filter--swatches .cpf-filter__checkbox:checked + .cpf-filter__button, ' . $scope . '.cpf-filter--swatches .cpf-filter__button:not(.is-active):hover, ' . $scope . '.cpf-filter--swatches .cpf-filter__choice:not(.is-active):hover .cpf-filter__button',
$swatch_reset_declarations
);
if ( '' !== $swatch_reset_rule ) {
$rules[] = $swatch_reset_rule;
}
if ( '' !== $swatch_text_color ) {
$swatch_label_rule = acewpf_build_css_rule(
$scope . '.cpf-filter--swatches .cpf-filter__button.is-active .cpf-filter__label, ' . $scope . '.cpf-filter--swatches .cpf-filter__button.is-active .cpf-filter__count, ' . $scope . '.cpf-filter--swatches .cpf-filter__choice.is-active .cpf-filter__label, ' . $scope . '.cpf-filter--swatches .cpf-filter__choice.is-active .cpf-filter__count',
array( 'color:' . $swatch_text_color )
);
if ( '' !== $swatch_label_rule ) {
$rules[] = $swatch_label_rule;
}
}
if ( '' !== $swatch_active_color ) {
$swatch_ring_gap = ! empty( $styles['swatch_background_color'] ) ? $styles['swatch_background_color'] : '#ffffff';
$swatch_ring_rule = acewpf_build_css_rule(
$scope . '.cpf-filter--swatches .cpf-filter__button.is-active .cpf-filter__swatch, ' . $scope . '.cpf-filter--swatches .cpf-filter__choice.is-active .cpf-filter__swatch, ' . $scope . '.cpf-filter--swatches .cpf-filter__checkbox:checked + .cpf-filter__button .cpf-filter__swatch',
array( 'box-shadow:0 0 0 2px ' . $swatch_ring_gap . ', 0 0 0 4px ' . $swatch_active_color . ', inset 0 0 0 1px rgba(16, 24, 40, 0.16)' )
);
if ( '' !== $swatch_ring_rule ) {
$rules[] = $swatch_ring_rule;
}
}
}
$responsive_rule_block = acewpf_get_filter_responsive_style_block( $filter );
if ( '' !== $responsive_rule_block ) {
$rules[] = $responsive_rule_block;
}
return implode( "\n", $rules );
}
function acewpf_get_filter_panel_style_block( $panel ) {
$styles = isset( $panel['styles'] ) && is_array( $panel['styles'] ) ? $panel['styles'] : array();
$scope = '.cpf-filter-panel--' . sanitize_html_class( $panel['id'] );
$rules = array();
if ( empty( $styles ) ) {
return '';
}
$button_declarations = array();
if ( ! empty( $styles['clear_all_text_color'] ) ) {
$button_declarations[] = 'color:' . $styles['clear_all_text_color'];
}
if ( ! empty( $styles['clear_all_background_color'] ) ) {
$button_declarations[] = 'background:' . $styles['clear_all_background_color'];
}
if ( ! empty( $styles['clear_all_border_color'] ) ) {
$button_declarations[] = 'border-color:' . $styles['clear_all_border_color'];
}
$panel_declarations = array();
if ( ! empty( $styles['separator_color'] ) ) {
$panel_declarations[] = '--cpf-panel-separator-color:' . $styles['separator_color'];
}
if ( isset( $styles['separator_width'] ) && acewpf_style_value_is_set( $styles['separator_width'] ) ) {
$panel_declarations[] = '--cpf-panel-separator-width:' . absint( $styles['separator_width'] ) . 'px';
}
if ( isset( $styles['separator_spacing'] ) && acewpf_style_value_is_set( $styles['separator_spacing'] ) ) {
$panel_declarations[] = '--cpf-panel-separator-spacing:' . absint( $styles['separator_spacing'] ) . 'px';
}
if ( ! empty( $styles['group_header_color'] ) ) {
$panel_declarations[] = '--cpf-group-header-color:' . $styles['group_header_color'];
}
if ( isset( $styles['group_header_font_size'] ) && acewpf_style_value_is_set( $styles['group_header_font_size'] ) ) {
$panel_declarations[] = '--cpf-group-header-font-size:' . absint( $styles['group_header_font_size'] ) . 'px';
}
if ( isset( $styles['group_header_letter_spacing'] ) && acewpf_style_value_is_set( $styles['group_header_letter_spacing'] ) ) {
$panel_declarations[] = '--cpf-group-header-letter-spacing:' . $styles['group_header_letter_spacing'] . 'px';
}
// Safe fallback colors for mobile toggle to prevent transient theme colors on first render.
$panel_declarations[] = '--cpf-mobile-toggle-fallback-text-color:' . ( ! empty( $styles['mobile_toggle_text_color'] ) ? sanitize_hex_color( $styles['mobile_toggle_text_color'] ) : '#ffffff' );
$panel_declarations[] = '--cpf-mobile-toggle-fallback-background-color:' . ( ! empty( $styles['mobile_toggle_background_color'] ) ? sanitize_hex_color( $styles['mobile_toggle_background_color'] ) : '#1d2327' );
$panel_declarations[] = '--cpf-mobile-toggle-fallback-border-color:' . ( ! empty( $styles['mobile_toggle_border_color'] ) ? sanitize_hex_color( $styles['mobile_toggle_border_color'] ) : '#1d2327' );
$panel_rule = acewpf_build_css_rule( $scope, $panel_declarations );
if ( '' !== $panel_rule ) {
$rules[] = $panel_rule;
}
if ( isset( $styles['clear_all_font_size'] ) && acewpf_style_value_is_set( $styles['clear_all_font_size'] ) ) {
$button_declarations[] = 'font-size:' . absint( $styles['clear_all_font_size'] ) . 'px';
}
if ( isset( $styles['clear_all_padding_y'] ) && acewpf_style_value_is_set( $styles['clear_all_padding_y'] ) ) {
$button_declarations[] = 'padding-top:' . absint( $styles['clear_all_padding_y'] ) . 'px';
$button_declarations[] = 'padding-bottom:' . absint( $styles['clear_all_padding_y'] ) . 'px';
}
if ( isset( $styles['clear_all_padding_x'] ) && acewpf_style_value_is_set( $styles['clear_all_padding_x'] ) ) {
$button_declarations[] = 'padding-left:' . absint( $styles['clear_all_padding_x'] ) . 'px';
$button_declarations[] = 'padding-right:' . absint( $styles['clear_all_padding_x'] ) . 'px';
}
if ( isset( $styles['clear_all_border_radius'] ) && acewpf_style_value_is_set( $styles['clear_all_border_radius'] ) ) {
$button_declarations[] = 'border-radius:' . absint( $styles['clear_all_border_radius'] ) . 'px';
}
$button_rule = acewpf_build_css_rule( $scope . ' .cpf-filter-panel__clear', $button_declarations );
if ( '' !== $button_rule ) {
$rules[] = $button_rule;
}
$hover_declarations = array();
if ( ! empty( $styles['clear_all_hover_text_color'] ) ) {
$hover_declarations[] = 'color:' . $styles['clear_all_hover_text_color'];
}
if ( ! empty( $styles['clear_all_hover_background_color'] ) ) {
$hover_declarations[] = 'background:' . $styles['clear_all_hover_background_color'];
}
if ( ! empty( $styles['clear_all_hover_border_color'] ) ) {
$hover_declarations[] = 'border-color:' . $styles['clear_all_hover_border_color'];
}
$hover_rule = acewpf_build_css_rule( $scope . ' .cpf-filter-panel__clear:hover, ' . $scope . ' .cpf-filter-panel__clear:focus-visible', $hover_declarations );
if ( '' !== $hover_rule ) {
$rules[] = $hover_rule;
}
acewpf_append_panel_button_style_rules(
$rules,
$scope,
'.cpf-filter-panel__mobile-toggle',
'.cpf-filter-panel__mobile-toggle:hover, .cpf-filter-panel__mobile-toggle:focus, .cpf-filter-panel__mobile-toggle:focus-visible, .cpf-filter-panel__mobile-toggle:active',
$styles,
'mobile_toggle'
);
$group_header_declarations = array();
if ( ! empty( $styles['group_header_color'] ) ) {
$group_header_declarations[] = 'color:' . $styles['group_header_color'];
}
acewpf_append_typography_declarations( $group_header_declarations, $styles, 'group_header' );
$group_header_rule = acewpf_build_css_rule( $scope . ' .cpf-filter-panel__group-header > span', $group_header_declarations );
if ( '' !== $group_header_rule ) {
$rules[] = $group_header_rule;
}
return implode( "\n", $rules );
}
function acewpf_get_ajax_empty_results_config( $source ) {
$source = is_array( $source ) ? $source : array();
$styles = isset( $source['styles'] ) && is_array( $source['styles'] ) ? $source['styles'] : array();
$config = array(
'message' => isset( $source['empty_results_message'] ) ? acewpf_sanitize_empty_results_message( $source['empty_results_message'] ) : '',
'textColor' => isset( $styles['empty_results_text_color'] ) ? sanitize_hex_color( $styles['empty_results_text_color'] ) : '',
'backgroundColor' => isset( $styles['empty_results_background_color'] ) ? sanitize_hex_color( $styles['empty_results_background_color'] ) : '',
'borderColor' => isset( $styles['empty_results_border_color'] ) ? sanitize_hex_color( $styles['empty_results_border_color'] ) : '',
'fontSize' => isset( $styles['empty_results_font_size'] ) ? acewpf_sanitize_size_value( $styles['empty_results_font_size'] ) : '',
'paddingY' => isset( $styles['empty_results_padding_y'] ) ? acewpf_sanitize_size_value( $styles['empty_results_padding_y'] ) : '',
'paddingX' => isset( $styles['empty_results_padding_x'] ) ? acewpf_sanitize_size_value( $styles['empty_results_padding_x'] ) : '',
'borderRadius' => isset( $styles['empty_results_border_radius'] ) ? acewpf_sanitize_size_value( $styles['empty_results_border_radius'] ) : '',
);
return array_filter(
$config,
static function ( $value ) {
return '' !== (string) $value;
}
);
}
function acewpf_get_current_product_archive_count_context() {
static $context = null;
if ( null !== $context ) {
return $context;
}
$context = array();
$queried_object = get_queried_object();
if ( ! ( $queried_object instanceof WP_Term ) ) {
return $context;
}
$taxonomy = sanitize_key( $queried_object->taxonomy );
if ( ! acewpf_is_supported_product_filter_taxonomy( $taxonomy ) ) {
return $context;
}
$slug = sanitize_title( $queried_object->slug );
if ( '' !== $slug ) {
$context[ $taxonomy ] = array( $slug );
}
return $context;
}
function acewpf_get_filter_count_context_tax_clauses( $filter ) {
static $contexts = array();
$filter = is_array( $filter ) ? $filter : array();
$filter_id = isset( $filter['id'] ) ? sanitize_key( $filter['id'] ) : '';
$filter_taxonomy = acewpf_get_filter_taxonomy( $filter );
$cache_key = $filter_id . ':' . $filter_taxonomy;
if ( isset( $contexts[ $cache_key ] ) ) {
return $contexts[ $cache_key ];
}
$contexts[ $cache_key ] = acewpf_get_all_active_filter_tax_clauses( $filter_id );
foreach ( acewpf_get_current_product_archive_count_context() as $taxonomy => $archive_slugs ) {
if ( $taxonomy === $filter_taxonomy ) {
continue;
}
$contexts[ $cache_key ][] = array(
'filter_id' => 'archive-' . $taxonomy,
'taxonomy' => $taxonomy,
'slugs' => $archive_slugs,
);
}
$contexts[ $cache_key ] = acewpf_normalize_active_filter_tax_clauses( $contexts[ $cache_key ] );
return $contexts[ $cache_key ];
}
function acewpf_get_filter_count_context_slugs_by_filter( $filter ) {
return acewpf_get_filter_count_context_tax_clauses( $filter );
}
function acewpf_get_filter_context_price_ranges( $filter ) {
static $contexts = array();
$filter = is_array( $filter ) ? $filter : array();
$filter_id = isset( $filter['id'] ) ? sanitize_key( $filter['id'] ) : '';
if ( isset( $contexts[ $filter_id ] ) ) {
return $contexts[ $filter_id ];
}
$contexts[ $filter_id ] = array();
foreach ( acewpf_get_all_active_price_filters() as $active_filter_id => $price_range ) {
if ( $active_filter_id === $filter_id ) {
continue;
}
$contexts[ $filter_id ][ $active_filter_id ] = $price_range;
}
return $contexts[ $filter_id ];
}
function acewpf_get_product_count_query_base_args() {
static $query_args = null;
if ( null !== $query_args ) {
return $query_args;
}
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => 1,
'paged' => 1,
'acewpf_skip_request_filters' => true,
'ignore_sticky_posts' => true,
'no_found_rows' => false,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
);
if ( function_exists( 'WC' ) && null !== WC() && isset( WC()->query ) && is_object( WC()->query ) ) {
if ( method_exists( WC()->query, 'get_meta_query' ) ) {
$meta_query = WC()->query->get_meta_query();
if ( is_array( $meta_query ) && ! empty( $meta_query ) ) {
$query_args['meta_query'] = $meta_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- WooCommerce visibility and stock filtering relies on meta_query clauses.
}
}
if ( method_exists( WC()->query, 'get_tax_query' ) ) {
$tax_query = WC()->query->get_tax_query();
if ( is_array( $tax_query ) && ! empty( $tax_query ) ) {
$query_args['tax_query'] = $tax_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- WooCommerce catalog visibility and layered navigation rely on tax_query clauses.
}
}
}
return $query_args;
}
function acewpf_append_price_range_meta_query( &$meta_query, $price_filters ) {
$meta_query = is_array( $meta_query ) ? $meta_query : array();
$price_filters = is_array( $price_filters ) ? $price_filters : array();
foreach ( $price_filters as $price_range ) {
if ( isset( $price_range['min'] ) && null !== $price_range['min'] ) {
$meta_query[] = array(
'acewpf_price_filter' => true,
'key' => '_price',
'value' => $price_range['min'],
'compare' => '>=',
'type' => 'NUMERIC',
);
}
if ( isset( $price_range['max'] ) && null !== $price_range['max'] ) {
$meta_query[] = array(
'acewpf_price_filter' => true,
'key' => '_price',
'value' => $price_range['max'],
'compare' => '<=',
'type' => 'NUMERIC',
);
}
}
}
function acewpf_is_active_price_meta_query_clause( $clause, $price_filters ) {
if ( ! is_array( $clause ) ) {
return false;
}
if ( ! empty( $clause['acewpf_price_filter'] ) ) {
return true;
}
if ( ! isset( $clause['key'], $clause['compare'] ) || '_price' !== $clause['key'] ) {
return false;
}
$compare = strtoupper( (string) $clause['compare'] );
$clause_value = isset( $clause['value'] ) ? acewpf_sanitize_price_filter_value( $clause['value'] ) : null;
if ( null === $clause_value || ( '>=' !== $compare && '<=' !== $compare ) ) {
return false;
}
foreach ( (array) $price_filters as $price_range ) {
if ( '>=' === $compare && isset( $price_range['min'] ) && null !== $price_range['min'] ) {
$min_value = acewpf_sanitize_price_filter_value( $price_range['min'] );
if ( null !== $min_value && abs( $clause_value - $min_value ) < 0.00001 ) {
return true;
}
}
if ( '<=' === $compare && isset( $price_range['max'] ) && null !== $price_range['max'] ) {
$max_value = acewpf_sanitize_price_filter_value( $price_range['max'] );
if ( null !== $max_value && abs( $clause_value - $max_value ) < 0.00001 ) {
return true;
}
}
}
return false;
}
function acewpf_strip_active_price_filter_meta_query( $meta_query, $price_filters = null ) {
$meta_query = is_array( $meta_query ) ? $meta_query : array();
$price_filters = null === $price_filters ? acewpf_get_all_active_price_filters() : $price_filters;
$price_filters = is_array( $price_filters ) ? $price_filters : array();
if ( empty( $meta_query ) || empty( $price_filters ) ) {
return $meta_query;
}
$filtered_query = array();
foreach ( $meta_query as $key => $clause ) {
if ( 'relation' === $key ) {
$filtered_query['relation'] = $clause;
continue;
}
if ( acewpf_is_active_price_meta_query_clause( $clause, $price_filters ) ) {
continue;
}
$filtered_query[] = $clause;
}
return $filtered_query;
}
function acewpf_get_contextual_product_query_args( $active_tax_clauses, $active_price_filters = array(), $active_property_filters = array() ) {
$query_args = acewpf_get_product_count_query_base_args();
$tax_query = isset( $query_args['tax_query'] ) && is_array( $query_args['tax_query'] ) ? $query_args['tax_query'] : array();
$meta_query = isset( $query_args['meta_query'] ) && is_array( $query_args['meta_query'] ) ? $query_args['meta_query'] : array();
$active_tax_clauses = acewpf_normalize_active_filter_tax_clauses( $active_tax_clauses );
$active_price_filters = is_array( $active_price_filters ) ? $active_price_filters : array();
$active_property_filters = is_array( $active_property_filters ) ? $active_property_filters : array();
$meta_query = acewpf_strip_active_price_filter_meta_query( $meta_query );
foreach ( $active_tax_clauses as $filter_clause ) {
if ( empty( $filter_clause['taxonomy'] ) || empty( $filter_clause['slugs'] ) ) {
continue;
}
$tax_query[] = array(
'taxonomy' => $filter_clause['taxonomy'],
'field' => 'slug',
'terms' => $filter_clause['slugs'],
'operator' => ( isset( $filter_clause['operator'] ) && 'and' === $filter_clause['operator'] && count( $filter_clause['slugs'] ) > 1 ) ? 'AND' : 'IN',
);
}
if ( ! empty( $tax_query ) ) {
$query_args['tax_query'] = $tax_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Contextual filter state requires taxonomy constraints.
}
acewpf_append_price_range_meta_query( $meta_query, $active_price_filters );
if ( ! empty( $meta_query ) ) {
$query_args['meta_query'] = $meta_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Contextual filter state requires price and visibility meta constraints.
}
if ( function_exists( 'acewpf_apply_product_property_filters_to_query_args' ) ) {
acewpf_apply_product_property_filters_to_query_args( $query_args, $active_property_filters );
}
return $query_args;
}
function acewpf_get_contextual_filter_term_count_map_for_terms( $filter_taxonomy, $terms, $active_tax_clauses, $active_price_filters = array(), $selected_slugs = array(), $active_property_filters = array() ) {
static $maps = array();
$filter_taxonomy = sanitize_key( (string) $filter_taxonomy );
$terms = is_array( $terms ) ? $terms : array();
$active_tax_clauses = acewpf_normalize_active_filter_tax_clauses( $active_tax_clauses );
$active_price_filters = is_array( $active_price_filters ) ? $active_price_filters : array();
$active_property_filters = is_array( $active_property_filters ) ? $active_property_filters : array();
$selected_slugs = array_map( 'sanitize_title', is_array( $selected_slugs ) ? $selected_slugs : array() );
$selected_slugs = array_values( array_filter( array_unique( $selected_slugs ) ) );
$count_map = array();
$term_slugs = array();
foreach ( $terms as $term ) {
if ( ! ( $term instanceof WP_Term ) ) {
continue;
}
if ( $filter_taxonomy !== sanitize_key( $term->taxonomy ) ) {
continue;
}
$term_id = (int) $term->term_id;
if ( $term_id <= 0 ) {
continue;
}
$count_map[ $term_id ] = 0;
$term_slugs[ $term_id ] = sanitize_title( $term->slug );
}
if ( '' === $filter_taxonomy || empty( $count_map ) ) {
return $count_map;
}
$cache_key = md5(
wp_json_encode(
array(
'filter_taxonomy' => $filter_taxonomy,
'terms' => array_keys( $count_map ),
'active' => $active_tax_clauses,
'price' => $active_price_filters,
'property' => $active_property_filters,
'selected' => $selected_slugs,
)
)
);
if ( isset( $maps[ $cache_key ] ) ) {
return $maps[ $cache_key ];
}
$product_ids_by_term = array();
$query_args = acewpf_get_contextual_product_query_args( $active_tax_clauses, $active_price_filters, $active_property_filters );
$query_args['posts_per_page'] = -1;
$query_args['no_found_rows'] = true;
$query_args['paged'] = 1;
$query_args['orderby'] = 'none';
$query_args['cache_results'] = false;
$query = new WP_Query( $query_args );
$product_ids = ! empty( $query->posts ) ? array_map( 'absint', $query->posts ) : array();
wp_reset_postdata();
if ( empty( $product_ids ) ) {
$maps[ $cache_key ] = $count_map;
return $maps[ $cache_key ];
}
$term_relationships = wp_get_object_terms(
$product_ids,
$filter_taxonomy,
array(
'fields' => 'all_with_object_id',
'orderby' => 'none',
)
);
if ( is_wp_error( $term_relationships ) || empty( $term_relationships ) ) {
$maps[ $cache_key ] = $count_map;
return $maps[ $cache_key ];
}
foreach ( $term_relationships as $term_relationship ) {
if ( ! is_object( $term_relationship ) || ! isset( $term_relationship->object_id, $term_relationship->term_id, $term_relationship->slug ) ) {
continue;
}
$object_id = absint( $term_relationship->object_id );
$term_id = (int) $term_relationship->term_id;
if ( $object_id <= 0 || $term_id <= 0 ) {
continue;
}
if ( isset( $count_map[ $term_id ] ) ) {
if ( ! isset( $product_ids_by_term[ $term_id ] ) ) {
$product_ids_by_term[ $term_id ] = array();
}
$product_ids_by_term[ $term_id ][ $object_id ] = true;
}
}
foreach ( $count_map as $term_id => $count ) {
$term_product_ids = isset( $product_ids_by_term[ $term_id ] ) ? $product_ids_by_term[ $term_id ] : array();
$count_map[ $term_id ] = count( $term_product_ids );
}
$maps[ $cache_key ] = $count_map;
return $maps[ $cache_key ];
}
function acewpf_get_contextual_filter_term_count( $filter_taxonomy, $term_slug, $active_slugs_by_taxonomy, $active_price_filters = array() ) {
$filter_taxonomy = sanitize_key( (string) $filter_taxonomy );
$term_slug = sanitize_title( $term_slug );
$active_slugs_by_taxonomy = is_array( $active_slugs_by_taxonomy ) ? $active_slugs_by_taxonomy : array();
$active_price_filters = is_array( $active_price_filters ) ? $active_price_filters : array();
$term = get_term_by( 'slug', $term_slug, $filter_taxonomy );
if ( ! ( $term instanceof WP_Term ) ) {
return 0;
}
$count_map = acewpf_get_contextual_filter_term_count_map_for_terms(
$filter_taxonomy,
array( $term ),
$active_slugs_by_taxonomy,
$active_price_filters
);
return isset( $count_map[ $term->term_id ] ) ? (int) $count_map[ $term->term_id ] : 0;
}
function acewpf_get_filter_term_count_map( $filter, $force_contextual = false ) {
static $maps = array();
$filter = is_array( $filter ) ? $filter : array();
$filter_id = isset( $filter['id'] ) ? sanitize_key( $filter['id'] ) : '';
$taxonomy = acewpf_get_filter_taxonomy( $filter );
$active_tax_clauses = acewpf_get_filter_count_context_tax_clauses( $filter );
$selected_slugs = acewpf_parse_filter_request_slugs( $filter_id );
$active_price_filters = acewpf_get_filter_context_price_ranges( $filter );
$active_property_filters = function_exists( 'acewpf_get_filter_count_context_property_filters' )
? acewpf_get_filter_count_context_property_filters( $filter )
: array();
$force_contextual = (bool) $force_contextual;
if ( '' === $filter_id || ( empty( $active_tax_clauses ) && empty( $active_price_filters ) && empty( $active_property_filters ) && empty( $selected_slugs ) && ! $force_contextual ) ) {
return array();
}
$cache_key = $filter_id . ':' . md5( wp_json_encode( array( 'tax' => $active_tax_clauses, 'price' => $active_price_filters, 'property' => $active_property_filters, 'selected' => $selected_slugs, 'force_contextual' => $force_contextual ) ) );
if ( isset( $maps[ $cache_key ] ) ) {
return $maps[ $cache_key ];
}
$terms = acewpf_get_filter_terms( $filter_id );
if ( empty( $terms ) ) {
$maps[ $cache_key ] = array();
return $maps[ $cache_key ];
}
if ( acewpf_is_product_property_filter( $filter ) && function_exists( 'acewpf_get_product_property_filter_count_map' ) ) {
$maps[ $cache_key ] = acewpf_get_product_property_filter_count_map( $filter, $active_tax_clauses, $active_price_filters, $active_property_filters );
return $maps[ $cache_key ];
}
$maps[ $cache_key ] = acewpf_get_contextual_filter_term_count_map_for_terms( $taxonomy, $terms, $active_tax_clauses, $active_price_filters, $selected_slugs, $active_property_filters );
return $maps[ $cache_key ];
}
function acewpf_format_price_filter_number( $value ) {
$value = acewpf_sanitize_price_filter_value( $value );
if ( null === $value ) {
return '';
}
$formatted = number_format( $value, acewpf_get_price_filter_decimals(), '.', '' );
if ( acewpf_get_price_filter_decimals() > 0 ) {
$formatted = rtrim( rtrim( $formatted, '0' ), '.' );
}
return '' === $formatted ? '0' : $formatted;
}
function acewpf_get_price_filter_step() {
$decimals = acewpf_get_price_filter_decimals();
if ( $decimals <= 0 ) {
return '1';
}
return number_format( pow( 10, -1 * $decimals ), $decimals, '.', '' );
}
function acewpf_get_price_filter_percentage( $value, $min_value, $max_value ) {
$value = acewpf_sanitize_price_filter_value( $value );
$min_value = acewpf_sanitize_price_filter_value( $min_value );
$max_value = acewpf_sanitize_price_filter_value( $max_value );
if ( null === $value || null === $min_value || null === $max_value || $max_value <= $min_value ) {
return 0;
}
$percentage = ( ( $value - $min_value ) / ( $max_value - $min_value ) ) * 100;
if ( $percentage < 0 ) {
$percentage = 0;
} elseif ( $percentage > 100 ) {
$percentage = 100;
}
return round( $percentage, 4 );
}
function acewpf_render_price_filter_amount( $value ) {
$value = acewpf_sanitize_price_filter_value( $value );
if ( null === $value ) {
return '';
}
if ( function_exists( 'wc_price' ) ) {
return wc_price( $value );
}
return esc_html( number_format_i18n( $value, acewpf_get_price_filter_decimals() ) );
}
function acewpf_get_price_bounds_from_query_args( $query_args ) {
$query_args = is_array( $query_args ) ? $query_args : array();
$meta_query = isset( $query_args['meta_query'] ) && is_array( $query_args['meta_query'] ) ? $query_args['meta_query'] : array();
$meta_query[] = array(
'key' => '_price',
'compare' => 'EXISTS',
);
$meta_query[] = array(
'key' => '_price',
'value' => '',
'compare' => '!=',
);
$query_args['meta_query'] = $meta_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Price-bound lookups require existence and non-empty price meta constraints.
$query_args['posts_per_page'] = 1;
$query_args['fields'] = 'ids';
$query_args['no_found_rows'] = true;
$query_args['update_post_meta_cache'] = false;
$query_args['update_post_term_cache'] = false;
$query_args['meta_key'] = '_price'; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Price-bound lookups intentionally order by the WooCommerce _price meta key.
$query_args['orderby'] = 'meta_value_num';
$min_query_args = $query_args;
$min_query_args['order'] = 'ASC';
$min_query = new WP_Query( $min_query_args );
$min_product_ids = ! empty( $min_query->posts ) ? $min_query->posts : array();
$min_value = ! empty( $min_product_ids ) ? acewpf_sanitize_price_filter_value( get_post_meta( (int) $min_product_ids[0], '_price', true ) ) : null;
wp_reset_postdata();
if ( null === $min_value ) {
return array(
'min' => null,
'max' => null,
);
}
$max_query_args = $query_args;
$max_query_args['order'] = 'DESC';
$max_query = new WP_Query( $max_query_args );
$max_product_ids = ! empty( $max_query->posts ) ? $max_query->posts : array();
$max_value = ! empty( $max_product_ids ) ? acewpf_sanitize_price_filter_value( get_post_meta( (int) $max_product_ids[0], '_price', true ) ) : $min_value;
wp_reset_postdata();
if ( null === $max_value ) {
$max_value = $min_value;
}
if ( $max_value < $min_value ) {
$swap_value = $min_value;
$min_value = $max_value;
$max_value = $swap_value;
}
return array(
'min' => $min_value,
'max' => $max_value,
);
}
function acewpf_get_contextual_price_filter_bounds( $filter ) {
static $bounds = array();
$filter = is_array( $filter ) ? $filter : array();
$filter_id = isset( $filter['id'] ) ? sanitize_key( $filter['id'] ) : '';
$active_tax_filters = acewpf_get_filter_count_context_tax_clauses( $filter );
$active_price_filters = acewpf_get_filter_context_price_ranges( $filter );
$active_property_filters = function_exists( 'acewpf_get_filter_count_context_property_filters' )
? acewpf_get_filter_count_context_property_filters( $filter )
: array();
$cache_key = $filter_id . ':' . md5( wp_json_encode( array( 'tax' => $active_tax_filters, 'price' => $active_price_filters, 'property' => $active_property_filters ) ) );
if ( isset( $bounds[ $cache_key ] ) ) {
return $bounds[ $cache_key ];
}
$query_args = acewpf_get_contextual_product_query_args( $active_tax_filters, $active_price_filters, $active_property_filters );
$resolved_bounds = acewpf_get_price_bounds_from_query_args( $query_args );
if ( null === $resolved_bounds['min'] ) {
$fallback_query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => true,
);
$base_query_args = acewpf_get_product_count_query_base_args();
$fallback_meta_query = isset( $base_query_args['meta_query'] ) && is_array( $base_query_args['meta_query'] ) ? $base_query_args['meta_query'] : array();
$fallback_meta_query = acewpf_strip_active_price_filter_meta_query( $fallback_meta_query );
if ( ! empty( $fallback_meta_query ) ) {
$fallback_query_args['meta_query'] = $fallback_meta_query; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Fallback price bounds still require WooCommerce visibility constraints.
}
$resolved_bounds = acewpf_get_price_bounds_from_query_args( $fallback_query_args );
}
$bounds[ $cache_key ] = $resolved_bounds;
return $bounds[ $cache_key ];
}
function acewpf_get_price_filter_state( $filter ) {
$bounds = acewpf_get_contextual_price_filter_bounds( $filter );
$price_range = acewpf_parse_filter_request_price_range( isset( $filter['id'] ) ? $filter['id'] : '' );
if ( null === $bounds['min'] || null === $bounds['max'] ) {
return array(
'min_bound' => null,
'max_bound' => null,
'current_min' => null,
'current_max' => null,
'selected_min' => null,
'selected_max' => null,
);
}
$current_min = null !== $price_range['min'] ? max( $bounds['min'], min( $price_range['min'], $bounds['max'] ) ) : $bounds['min'];
$current_max = null !== $price_range['max'] ? min( $bounds['max'], max( $price_range['max'], $bounds['min'] ) ) : $bounds['max'];
if ( $current_min > $current_max ) {
$current_min = $bounds['min'];
$current_max = $bounds['max'];
}
return array(
'min_bound' => $bounds['min'],
'max_bound' => $bounds['max'],
'current_min' => $current_min,
'current_max' => $current_max,
'selected_min' => $price_range['min'],
'selected_max' => $price_range['max'],
);
}
function acewpf_get_filter_legacy_query_var_key( $filter ) {
$filter = is_array( $filter ) ? $filter : array();
if ( empty( $filter ) || acewpf_is_price_filter( $filter ) || 'product_cat' !== acewpf_get_filter_taxonomy( $filter ) ) {
return '';
}
$first_filter = acewpf_get_filter_set();
if ( empty( $first_filter ) || empty( $first_filter['id'] ) || empty( $filter['id'] ) ) {
return '';
}
return $first_filter['id'] === $filter['id'] ? 'filter_cat' : '';
}
function acewpf_panel_has_active_filters( $panel ) {
$panel_filter_ids = acewpf_parse_filter_id_list( isset( $panel['filter_ids'] ) ? $panel['filter_ids'] : array() );
if ( empty( $panel_filter_ids ) ) {
return false;
}
foreach ( $panel_filter_ids as $filter_id ) {
$filter = acewpf_get_filter_set( $filter_id );
if ( empty( $filter ) ) {
continue;
}
if ( acewpf_is_price_filter( $filter ) ) {
$price_range = acewpf_parse_filter_request_price_range( $filter['id'] );
if ( null !== $price_range['min'] || null !== $price_range['max'] ) {
return true;
}
continue;
}
if ( ! empty( acewpf_parse_filter_request_slugs( $filter['id'] ) ) ) {
return true;
}
}
return false;
}
function acewpf_get_filter_panel_clear_all_url( $panel ) {
$panel_filter_ids = acewpf_parse_filter_id_list( isset( $panel['filter_ids'] ) ? $panel['filter_ids'] : array() );
$query_args_to_remove = array( 'paged' );
$base_url = acewpf_get_filter_render_base_url();
if ( '' === $base_url ) {
$base_url = acewpf_get_filter_base_url();
}
foreach ( $panel_filter_ids as $filter_id ) {
$filter = acewpf_get_filter_set( $filter_id );
if ( empty( $filter ) ) {
continue;
}
if ( acewpf_is_price_filter( $filter ) ) {
$query_args_to_remove[] = acewpf_get_filter_price_min_query_var( $filter['id'] );
$query_args_to_remove[] = acewpf_get_filter_price_max_query_var( $filter['id'] );
continue;
}
$query_args_to_remove[] = acewpf_get_filter_query_var( $filter['id'] );
$legacy_query_var = acewpf_get_filter_legacy_query_var_key( $filter );
if ( '' !== $legacy_query_var ) {
$query_args_to_remove[] = $legacy_query_var;
}
}
return remove_query_arg( array_values( array_unique( $query_args_to_remove ) ), $base_url );
}
function acewpf_should_render_panel_clear_all( $panel ) {
$panel_filter_ids = acewpf_parse_filter_id_list( isset( $panel['filter_ids'] ) ? $panel['filter_ids'] : array() );
$clear_all_mode = isset( $panel['clear_all_mode'] ) ? acewpf_sanitize_filter_panel_clear_all_mode( $panel['clear_all_mode'] ) : 'auto';
if ( 'off' === $clear_all_mode || empty( $panel_filter_ids ) ) {
return false;
}
if ( 'always' === $clear_all_mode ) {
return true;
}
return count( $panel_filter_ids ) > 1;
}
function acewpf_render_filter_choice_content( $category, $filter, $count = null ) {
$selection_mode = isset( $filter['selection_mode'] ) ? $filter['selection_mode'] : 'single';
$display_count = null === $count ? absint( $category->count ) : absint( $count );
$uses_swatches = acewpf_filter_uses_swatches( $filter );
$uses_stars = function_exists( 'acewpf_filter_uses_rating_stars' ) && acewpf_filter_uses_rating_stars( $filter );
$swatch_color = $uses_swatches ? acewpf_get_filter_term_color( $filter, isset( $category->term_id ) ? $category->term_id : 0 ) : '';
$filled_stars = $uses_stars && isset( $category->slug ) ? acewpf_get_rating_star_threshold( $category->slug ) : 0;
ob_start();
?>
aria-hidden="true">
0 ) : ?>
name ); ?>
count ) ) {
return (int) $term->count;
}
return 0;
}
function acewpf_resolve_hide_zero_count_terms( $filter, $override = '' ) {
if ( acewpf_is_price_filter( $filter ) ) {
return false;
}
$override = sanitize_key( (string) $override );
if ( 'hide' === $override ) {
return true;
}
if ( 'show' === $override ) {
return false;
}
return ! empty( $filter['hide_zero_count_terms'] );
}
function acewpf_resolve_term_search_mode( $filter, $override = '' ) {
if ( acewpf_is_price_filter( $filter ) ) {
return 'terms';
}
$override = sanitize_key( (string) $override );
$choices = acewpf_get_filter_term_search_mode_choices();
if ( isset( $choices[ $override ] ) ) {
return $override;
}
return acewpf_sanitize_filter_term_search_mode( isset( $filter['term_search_mode'] ) ? $filter['term_search_mode'] : 'terms' );
}
function acewpf_filter_uses_swatches( $filter ) {
if ( acewpf_is_price_filter( $filter ) || acewpf_is_product_property_filter( $filter ) ) {
return false;
}
return 'swatches' === acewpf_sanitize_filter_display_mode( isset( $filter['display_mode'] ) ? $filter['display_mode'] : 'buttons' );
}
function acewpf_get_filter_term_color( $filter, $term_id ) {
$colors = isset( $filter['term_colors'] ) && is_array( $filter['term_colors'] ) ? $filter['term_colors'] : array();
$term_id = absint( $term_id );
if ( ! $term_id ) {
return '';
}
if ( ! empty( $colors[ $term_id ] ) ) {
return acewpf_normalize_hex_color( $colors[ $term_id ] );
}
$key = (string) $term_id;
if ( ! empty( $colors[ $key ] ) ) {
return acewpf_normalize_hex_color( $colors[ $key ] );
}
return '';
}
function acewpf_get_filter_term_search_placeholder( $filter ) {
$placeholder = isset( $filter['term_search_placeholder'] ) ? trim( (string) $filter['term_search_placeholder'] ) : '';
return '' !== $placeholder ? $placeholder : __( 'Search…', 'ace-products-filters' );
}
function acewpf_get_filter_term_search_empty_message( $filter ) {
$message = isset( $filter['term_search_empty_message'] ) ? trim( (string) $filter['term_search_empty_message'] ) : '';
return '' !== $message ? $message : __( 'No matching terms', 'ace-products-filters' );
}
function acewpf_get_filter_term_search_text( $term ) {
$parts = array();
if ( is_object( $term ) && isset( $term->name ) && '' !== (string) $term->name ) {
$parts[] = (string) $term->name;
}
if ( is_object( $term ) && isset( $term->slug ) && '' !== (string) $term->slug ) {
$parts[] = (string) $term->slug;
}
return strtolower( implode( ' ', $parts ) );
}
function acewpf_get_visible_filter_terms( $terms, $count_map, $active_slugs, $preserve_ancestors = false ) {
$terms = is_array( $terms ) ? $terms : array();
$count_map = is_array( $count_map ) ? $count_map : array();
$active_slugs = is_array( $active_slugs ) ? $active_slugs : array();
if ( empty( $terms ) ) {
return array();
}
$selected_ids = array();
$keep = array();
foreach ( $terms as $term ) {
if ( ! ( $term instanceof WP_Term ) ) {
continue;
}
$term_id = (int) $term->term_id;
$slug = isset( $term->slug ) ? (string) $term->slug : '';
$selected_ids[ $term_id ] = true;
$keep[ $term_id ] = ( '' !== $slug && in_array( $slug, $active_slugs, true ) ) || acewpf_get_filter_term_display_count( $term, $count_map ) > 0;
}
if ( $preserve_ancestors ) {
$changed = true;
$guard = 0;
while ( $changed && $guard < 20 ) {
$changed = false;
$guard++;
foreach ( $terms as $term ) {
if ( ! ( $term instanceof WP_Term ) ) {
continue;
}
$term_id = (int) $term->term_id;
if ( empty( $keep[ $term_id ] ) ) {
continue;
}
$ancestor_id = acewpf_get_selected_term_ancestor_id( $term, $selected_ids );
if ( $ancestor_id > 0 && empty( $keep[ $ancestor_id ] ) ) {
$keep[ $ancestor_id ] = true;
$changed = true;
}
}
}
}
$visible = array();
foreach ( $terms as $term ) {
if ( $term instanceof WP_Term && ! empty( $keep[ (int) $term->term_id ] ) ) {
$visible[] = $term;
}
}
return $visible;
}
function acewpf_get_filter_choice_count_key( $choice ) {
if ( is_object( $choice ) && isset( $choice->term_id ) ) {
return (int) $choice->term_id;
}
if ( is_array( $choice ) && isset( $choice['term_id'] ) ) {
return (int) $choice['term_id'];
}
if ( is_object( $choice ) && isset( $choice->slug ) ) {
return sanitize_title( (string) $choice->slug );
}
if ( is_array( $choice ) && isset( $choice['slug'] ) ) {
return sanitize_title( (string) $choice['slug'] );
}
return '';
}
function acewpf_get_current_filter_category( $categories, $active_slugs ) {
if ( empty( $categories ) || ! is_array( $categories ) ) {
return null;
}
foreach ( $categories as $category ) {
if ( in_array( $category->slug, $active_slugs, true ) ) {
return $category;
}
}
return reset( $categories );
}
function acewpf_render_filter_mobile_toggle( $category, $filter, $is_selected, $count = null ) {
if ( empty( $category ) ) {
return '';
}
ob_start();
?>
'',
'ajax' => '',
'results_selector' => '',
'show_panel_heading' => '',
'count_display_mode' => '',
'hide_zero_count_terms' => '',
'term_search_mode' => '',
),
$atts,
'acewpf_filter'
);
$filter = acewpf_get_filter_set( $atts['id'] );
if ( empty( $filter ) ) {
return '';
}
$ajax_override = isset( $atts['ajax'] ) ? trim( (string) $atts['ajax'] ) : '';
$selector_override = isset( $atts['results_selector'] ) ? trim( (string) $atts['results_selector'] ) : '';
$force_contextual_counts = ! empty( $atts['force_contextual_counts'] );
$count_display_override = isset( $atts['count_display_mode'] ) ? sanitize_key( (string) $atts['count_display_mode'] ) : '';
$hide_zero_count_override = isset( $atts['hide_zero_count_terms'] ) ? sanitize_key( (string) $atts['hide_zero_count_terms'] ) : '';
$term_search_override = isset( $atts['term_search_mode'] ) ? sanitize_key( (string) $atts['term_search_mode'] ) : '';
$show_panel_heading = ! empty( $atts['show_panel_heading'] );
$panel_heading_enabled = ! empty( $filter['show_panel_heading'] );
$filter_ajax_enabled = ! empty( $filter['ajax_enabled'] );
$filter_results_selector = isset( $filter['results_selector'] ) ? acewpf_sanitize_results_selector( $filter['results_selector'] ) : '';
$ajax_enabled = '' === $ajax_override ? $filter_ajax_enabled : acewpf_shortcode_truthy( $ajax_override );
$results_selector = '' !== $selector_override ? acewpf_sanitize_results_selector( $selector_override ) : $filter_results_selector;
$ajax_enabled = $ajax_enabled && '' !== $results_selector;
$panel_heading = '';
$price_show_inputs = ! isset( $filter['price_show_inputs'] ) || ! empty( $filter['price_show_inputs'] );
if ( $show_panel_heading && $panel_heading_enabled ) {
$panel_heading = isset( $filter['panel_heading'] ) ? trim( sanitize_text_field( (string) $filter['panel_heading'] ) ) : '';
}
acewpf_enqueue_assets();
acewpf_enqueue_filter_font_stylesheet( $filter );
$style_block = acewpf_get_filter_style_block( $filter );
acewpf_enqueue_inline_style_block( $style_block );
$empty_results_config = acewpf_get_ajax_empty_results_config( $filter );
$empty_results_config_json = ! empty( $empty_results_config ) ? wp_json_encode( $empty_results_config ) : '';
$orientation = isset( $filter['orientation'] ) ? acewpf_sanitize_filter_orientation( $filter['orientation'] ) : 'horizontal';
$base_url = acewpf_get_filter_render_base_url();
$legacy_param_key = acewpf_get_filter_legacy_query_var_key( $filter );
if ( acewpf_is_price_filter( $filter ) ) {
$price_state = acewpf_get_price_filter_state( $filter );
$min_bound = $price_state['min_bound'];
$max_bound = $price_state['max_bound'];
$current_min = $price_state['current_min'];
$current_max = $price_state['current_max'];
$price_step = acewpf_get_price_filter_step();
$is_fixed_range = null !== $min_bound && null !== $max_bound && $max_bound <= $min_bound;
$container_class = 'cpf-filter cpf-filter--' . sanitize_html_class( $filter['id'] ) . ' cpf-filter--' . sanitize_html_class( $orientation ) . ' cpf-filter--price';
if ( null === $min_bound || null === $max_bound || null === $current_min || null === $current_max ) {
return '';
}
ob_start();
?>