=term1,term2` in the URL (read here and by Zadex\Loop\LoopRenderer on reload). * * @var array $settings * @var string $attrs * @var array $element * @var \Zadex\Frontend\Renderer $renderer */ if (!defined('ABSPATH')) { exit; } // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template variables are local to Zadex\Frontend\Renderer::include_template(). $element_id = (string) ($element['id'] ?? ''); $target = preg_replace('/[^A-Za-z0-9_-]/', '', (string) ($settings['target'] ?? '')) ?? ''; $zx_taxonomy = \Zadex\Loop\Query::taxonomy((string) ($settings['taxonomy'] ?? 'category')); $multiple = !empty($settings['multiple']); $layout = in_array($settings['layout'] ?? 'horizontal', ['horizontal', 'vertical', 'dropdown'], true) ? (string) $settings['layout'] : 'horizontal'; $show_count = !empty($settings['show_count']); $include = array_values(array_filter(array_map(static fn(string $zx_s): string => sanitize_title_for_query(trim($zx_s)), explode(',', (string) ($settings['include'] ?? ''))), 'strlen')); $zx_orderby = in_array($settings['orderby'] ?? 'name', ['name', 'count', 'include'], true) ? (string) $settings['orderby'] : 'name'; $terms = []; if ($zx_taxonomy !== '' && function_exists('get_terms') && (!function_exists('taxonomy_exists') || taxonomy_exists($zx_taxonomy))) { $args = ['taxonomy' => $zx_taxonomy, 'hide_empty' => true, 'number' => 100, 'orderby' => $zx_orderby === 'include' ? 'name' : $zx_orderby, 'order' => $zx_orderby === 'count' ? 'DESC' : 'ASC']; if ($include) { $args['slug'] = $include; } $found = get_terms($args); $terms = is_array($found) ? array_values(array_filter($found, 'is_object')) : []; if ($include && $zx_orderby === 'include') { usort($terms, static fn($a, $b): int => array_search($a->slug, $include, true) <=> array_search($b->slug, $include, true)); } } // URL state: ?zx-filter-=a,b $active = []; // Read-only filter state from a shareable URL, sanitized by LoopRenderer (wp_strip_all_tags + sanitize_title_for_query per slug). $param = \Zadex\Loop\LoopRenderer::url_filter_value((string) $element_id); if ($param !== '') { $active = \Zadex\Loop\LoopRenderer::terms_param($param); if (!$multiple && $active) { $active = [$active[0]]; } } $attrs = preg_replace('/class="/', 'class="zx-loop-filter--' . esc_attr($layout) . ' ', $attrs, 1); $attrs .= ' data-zx-target="' . esc_attr($target) . '" data-zx-taxonomy="' . esc_attr($zx_taxonomy) . '" data-zx-multiple="' . esc_attr($multiple ? 'true' : 'false') . '"'; $all_text = trim((string) ($settings['all_text'] ?? '')) !== '' ? (string) $settings['all_text'] : __('All', 'zadex'); $label = __('Filter posts', 'zadex'); $zx_html = '
'; if ($target === '' && defined('REST_REQUEST') && REST_REQUEST) { $zx_html .= '
' . esc_html__('Choose the loop to filter.', 'zadex') . '
'; } if ($layout === 'dropdown') { $zx_html .= ''; } else { $zx_html .= ''; } $zx_html .= '
'; echo wp_kses($zx_html, \Zadex\Frontend\Kses::allowed_html()) . "\n";