__( 'Filter Sets', 'Ajax-smart-filter' ), 'singular_name' => __( 'Filter Set', 'Ajax-smart-filter' ), ]; $args = [ 'labels' => $labels, 'public' => false, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 26, 'menu_icon' => 'dashicons-filter', 'supports' => [ 'title', 'editor' ], ]; register_post_type( 'asfp_filter_set', $args ); } /** * Register custom query vars to prevent 404 errors */ public function register_query_vars( $vars ) { // Register default plugin query vars $vars[] = 'asfp_search'; $vars[] = 'asfp_cat'; $vars[] = 'asfp_tag'; $vars[] = 'asfp_sort'; $vars[] = 'asfp_page'; // Note: 'tag' and 'category' are already WordPress query vars // We handle them in prevent_404_on_filter_params to prevent conflicts return $vars; } /** * Get all custom query parameter names from all filter sets * This allows us to detect filter parameters dynamically */ private function get_all_filter_query_params() { static $cached_params = null; if ( $cached_params !== null ) { return $cached_params; } $cached_params = []; // Get all filter sets that have querystring enabled $filter_sets = get_posts( [ 'post_type' => 'asfp_filter_set', 'posts_per_page' => -1, 'post_status' => 'publish', 'meta_query' => [ [ 'key' => '_asfp_enable_querystring', 'value' => '1', 'compare' => '=' ] ] ] ); foreach ( $filter_sets as $filter_set ) { $qs_search = get_post_meta( $filter_set->ID, '_asfp_qs_search', true ); $qs_category = get_post_meta( $filter_set->ID, '_asfp_qs_category', true ); $qs_tag = get_post_meta( $filter_set->ID, '_asfp_qs_tag', true ); $qs_sort = get_post_meta( $filter_set->ID, '_asfp_qs_sort', true ); $qs_page = get_post_meta( $filter_set->ID, '_asfp_qs_page', true ); // Add to list if not empty (use defaults if empty) if ( ! empty( $qs_search ) ) { $cached_params[] = $qs_search; } else { $cached_params[] = 'asfp_search'; } if ( ! empty( $qs_category ) ) { $cached_params[] = $qs_category; } else { $cached_params[] = 'asfp_cat'; } if ( ! empty( $qs_tag ) ) { $cached_params[] = $qs_tag; } else { $cached_params[] = 'asfp_tag'; } if ( ! empty( $qs_sort ) ) { $cached_params[] = $qs_sort; } else { $cached_params[] = 'asfp_sort'; } if ( ! empty( $qs_page ) ) { $cached_params[] = $qs_page; } else { $cached_params[] = 'asfp_page'; } } // Remove duplicates and return $cached_params = array_unique( $cached_params ); return $cached_params; } /** * Check if current request has filter parameters */ private function has_filter_params() { // Get all possible filter parameter names $filter_params = $this->get_all_filter_query_params(); // Check if any filter parameter exists in $_GET foreach ( $filter_params as $param_name ) { if ( isset( $_GET[ $param_name ] ) ) { return true; } } // Also check for common WordPress query vars that might be used as filters // Only if they're numeric (IDs) - this indicates filter params, not archive requests if ( ( isset( $_GET['tag'] ) && is_numeric( $_GET['tag'] ) ) || ( isset( $_GET['category'] ) && is_numeric( $_GET['category'] ) ) || ( isset( $_GET['cat'] ) && is_numeric( $_GET['cat'] ) ) ) { return true; } return false; } /** * Handle filter query parameters early in the request parsing * This prevents WordPress from trying to match them to posts/pages */ public function handle_filter_query_params( $wp ) { // Only process on frontend, not admin if ( is_admin() ) { return; } // Check if we have filter parameters if ( ! $this->has_filter_params() ) { return; } // Only clear query vars that might cause WordPress to try matching to a post by name/slug // DO NOT clear pagename or page_id as these are needed to identify the current page // Only clear 'name' which WordPress uses to match posts by slug if ( isset( $wp->query_vars['name'] ) && empty( $wp->query_vars['pagename'] ) && empty( $wp->query_vars['page_id'] ) ) { // Only clear 'name' if we're not on a page (pagename/page_id would be set for pages) $wp->query_vars['name'] = ''; } // Clear post ID matching only if it's not a valid page if ( isset( $wp->query_vars['p'] ) && empty( $wp->query_vars['pagename'] ) && empty( $wp->query_vars['page_id'] ) ) { $wp->query_vars['p'] = 0; } // Ensure we're not trying to match a specific attachment unset( $wp->query_vars['attachment'] ); unset( $wp->query_vars['attachment_id'] ); } /** * Prevent 404 errors when filter parameters are in the URL * This allows URLs like ?tag=6&category=4 to work without causing 404s */ public function prevent_404_on_filter_params( $query_vars ) { // Only process on frontend, not admin if ( is_admin() ) { return $query_vars; } // Check if we have filter parameters (uses dynamic parameter names) if ( ! $this->has_filter_params() ) { return $query_vars; } // DO NOT clear pagename or page_id - these are needed to identify the current page // Only clear 'name' which WordPress uses to match posts by slug (not pages) // Only clear if we're not on a page (pagename/page_id would be set for pages) if ( isset( $query_vars['name'] ) && empty( $query_vars['pagename'] ) && empty( $query_vars['page_id'] ) ) { unset( $query_vars['name'] ); } // Only clear post ID if it's not a page if ( isset( $query_vars['p'] ) && empty( $query_vars['pagename'] ) && empty( $query_vars['page_id'] ) ) { unset( $query_vars['p'] ); } // Clear attachment matching unset( $query_vars['attachment'] ); unset( $query_vars['attachment_id'] ); return $query_vars; } /** * Prevent 404 errors when filter parameters are present in the URL * This is called after WordPress determines a 404, allowing us to override it */ public function prevent_404_for_filter_params( $preempt, $wp_query ) { // Only process on frontend, not admin if ( is_admin() ) { return $preempt; } // Only handle main query if ( ! $wp_query->is_main_query() ) { return $preempt; } // Check if we have filter parameters (uses dynamic parameter names) if ( ! $this->has_filter_params() ) { return $preempt; } // If we're not on an archive page and no posts were found, these are likely filter params // (Archive pages should still work normally) if ( ! $wp_query->is_archive() && ! $wp_query->is_singular() && empty( $wp_query->posts ) ) { // Get the current URL path (without query parameters) to find the original page global $wp; $request_path = trim( $wp->request, '/' ); // Try to find the page/post by URL path $page_id = 0; if ( empty( $request_path ) || $request_path === '/' ) { // We're on the front page - get the front page ID $page_id = get_option( 'page_on_front' ); if ( ! $page_id ) { // If no static front page, it's the blog page $page_id = get_option( 'page_for_posts' ); } } else { // Try to find the page by path $page_id = url_to_postid( home_url( '/' . $request_path . '/' ) ); } // If we found a page, restore it as the queried object if ( $page_id > 0 ) { $page = get_post( $page_id ); if ( $page && $page->post_status === 'publish' ) { // Restore the page context $wp_query->is_404 = false; $wp_query->is_page = true; $wp_query->is_singular = true; $wp_query->queried_object = $page; $wp_query->queried_object_id = $page_id; $wp_query->posts = [ $page ]; $wp_query->post_count = 1; $wp_query->found_posts = 1; $wp_query->max_num_pages = 1; // Set query vars to match the page $wp_query->query['page_id'] = $page_id; $wp_query->query_vars['page_id'] = $page_id; $wp_query->query_vars['p'] = $page_id; // Return true to indicate we've handled the 404 return true; } } // If no page found, just prevent 404 - the page should still load // This handles cases where the page might be loaded via other means $wp_query->is_404 = false; // Return true to indicate we've handled the 404 return true; } return $preempt; } public function enqueue_frontend_assets() { wp_register_style( 'asfp-frontend', ASFP_PLUGIN_URL . 'assets/css/frontend.css', [], ASFP_VERSION ); wp_register_script( 'asfp-frontend', ASFP_PLUGIN_URL . 'assets/js/frontend.js', [ 'jquery' ], ASFP_VERSION, true ); wp_localize_script( 'asfp-frontend', 'ASFP', [ 'ajax_url' => admin_url( 'admin-ajax.php' ), // Kept for backward compatibility 'endpoint_url' => ASFP_PLUGIN_URL . 'ajax-endpoint.php', // Lightweight endpoint (no plugins loaded) 'nonce' => wp_create_nonce( 'asfp_nonce' ), 'analytics_enabled' => (bool) get_option( 'asfp_analytics_enabled', true ), ] ); } public function shortcode( $atts ) { $atts = shortcode_atts( [ 'id' => 0, ], $atts, 'asfp_post_filter' ); $filter_set_id = absint( $atts['id'] ); if ( ! $filter_set_id || get_post_type( $filter_set_id ) !== 'asfp_filter_set' ) { return '
' . esc_html__( 'Invalid filter set ID.', 'Ajax-smart-filter' ) . '
'; } // Check if filter UI is enabled (results should still render if disabled) $enable_filter_meta = get_post_meta( $filter_set_id, '_asfp_enable_filter', true ); $enable_filter = $enable_filter_meta === '' ? true : (bool) $enable_filter_meta; $show_controls = $enable_filter; wp_enqueue_style( 'asfp-frontend' ); wp_enqueue_script( 'asfp-frontend' ); ob_start(); $this->render_filter_ui( $filter_set_id, $show_controls ); return ob_get_clean(); } private function render_filter_ui( $filter_set_id, $show_controls = true ) { $container_id = 'asfp-container-' . $filter_set_id; $post_type = get_post_meta( $filter_set_id, '_asfp_post_type', true ); if ( ! $post_type ) { $post_type = 'post'; } $show_search = (bool) get_post_meta( $filter_set_id, '_asfp_show_search', true ); $show_tax_cat = (bool) get_post_meta( $filter_set_id, '_asfp_show_tax_category', true ); $show_tax_tag = (bool) get_post_meta( $filter_set_id, '_asfp_show_tax_post_tag', true ); $show_sort = (bool) get_post_meta( $filter_set_id, '_asfp_show_sort', true ); $apply_mode = get_post_meta( $filter_set_id, '_asfp_apply_mode', true ); if ( ! in_array( $apply_mode, [ 'button', 'auto' ], true ) ) { $apply_mode = 'button'; } $tax_display = get_post_meta( $filter_set_id, '_asfp_tax_display', true ); if ( ! in_array( $tax_display, [ 'dropdown', 'tabs' ], true ) ) { $tax_display = 'dropdown'; } $hide_empty_terms = (bool) get_post_meta( $filter_set_id, '_asfp_hide_empty_terms', true ); $template_filter_bar = get_post_meta( $filter_set_id, '_asfp_template_filter_bar', true ); $template_result_item = get_post_meta( $filter_set_id, '_asfp_template_result_item', true ); $per_page = (int) get_post_meta( $filter_set_id, '_asfp_per_page', true ); if ( $per_page <= 0 ) { $per_page = 6; } $pagination_display = get_post_meta( $filter_set_id, '_asfp_pagination_display', true ); if ( ! in_array( $pagination_display, [ 'none', 'next_prev', 'numeric', 'both' ], true ) ) { $pagination_display = 'numeric'; } // Display settings - default to true if not set $show_title_meta = get_post_meta( $filter_set_id, '_asfp_show_title', true ); $show_title = $show_title_meta === '' ? true : (bool) $show_title_meta; $show_excerpt_meta = get_post_meta( $filter_set_id, '_asfp_show_excerpt', true ); $show_excerpt = $show_excerpt_meta === '' ? true : (bool) $show_excerpt_meta; $show_date_meta = get_post_meta( $filter_set_id, '_asfp_show_date', true ); $show_date = $show_date_meta === '' ? true : (bool) $show_date_meta; $show_author_meta = get_post_meta( $filter_set_id, '_asfp_show_author', true ); $show_author = $show_author_meta === '' ? false : (bool) $show_author_meta; $show_image_meta = get_post_meta( $filter_set_id, '_asfp_show_image', true ); $show_image = $show_image_meta === '' ? true : (bool) $show_image_meta; // Excerpt length setting $excerpt_length = (int) get_post_meta( $filter_set_id, '_asfp_excerpt_length', true ); if ( $excerpt_length <= 0 ) { $excerpt_length = 25; } // Query string setting $enable_querystring = get_post_meta( $filter_set_id, '_asfp_enable_querystring', true ); $enable_querystring = $enable_querystring === '' ? false : (bool) $enable_querystring; $querystring_format = get_post_meta( $filter_set_id, '_asfp_querystring_format', true ); if ( ! in_array( $querystring_format, [ 'id', 'name' ], true ) ) { $querystring_format = 'id'; } // Custom query string parameter names $qs_search = get_post_meta( $filter_set_id, '_asfp_qs_search', true ); if ( empty( $qs_search ) ) { $qs_search = 'asfp_search'; } $qs_category = get_post_meta( $filter_set_id, '_asfp_qs_category', true ); if ( empty( $qs_category ) ) { $qs_category = 'asfp_cat'; } $qs_tag = get_post_meta( $filter_set_id, '_asfp_qs_tag', true ); if ( empty( $qs_tag ) ) { $qs_tag = 'asfp_tag'; } $qs_sort = get_post_meta( $filter_set_id, '_asfp_qs_sort', true ); if ( empty( $qs_sort ) ) { $qs_sort = 'asfp_sort'; } $qs_page = get_post_meta( $filter_set_id, '_asfp_qs_page', true ); if ( empty( $qs_page ) ) { $qs_page = 'asfp_page'; } // Template setting $blog_template = get_post_meta( $filter_set_id, '_asfp_blog_template', true ); if ( ! in_array( $blog_template, [ 'template1', 'template2', 'template3', 'template4', 'template5', 'template6', 'template7' ], true ) ) { $blog_template = 'template1'; } // Filter design setting $filter_design = get_post_meta( $filter_set_id, '_asfp_filter_design', true ); if ( ! in_array( $filter_design, [ 'default', 'minimalist', 'sidebar', 'compact' ], true ) ) { $filter_design = 'default'; } // Custom colors $primary_color = get_post_meta( $filter_set_id, '_asfp_primary_color', true ); if ( ! $primary_color ) { $primary_color = '#3b82f6'; } $secondary_color = get_post_meta( $filter_set_id, '_asfp_secondary_color', true ); if ( ! $secondary_color ) { $secondary_color = '#2563eb'; } $accent_color = get_post_meta( $filter_set_id, '_asfp_accent_color', true ); if ( ! $accent_color ) { $accent_color = '#1d4ed8'; } $labels = [ 'global_show' => (bool) get_post_meta( $filter_set_id, '_asfp_global_label_show', true ), 'global_text' => (string) get_post_meta( $filter_set_id, '_asfp_global_label_text', true ), 'search_show' => (bool) get_post_meta( $filter_set_id, '_asfp_label_search_show', true ), 'search_text' => (string) get_post_meta( $filter_set_id, '_asfp_label_search_text', true ), 'category_show' => (bool) get_post_meta( $filter_set_id, '_asfp_label_category_show', true ), 'category_text' => (string) get_post_meta( $filter_set_id, '_asfp_label_category_text', true ), 'tag_show' => (bool) get_post_meta( $filter_set_id, '_asfp_label_tag_show', true ), 'tag_text' => (string) get_post_meta( $filter_set_id, '_asfp_label_tag_text', true ), 'sort_show' => (bool) get_post_meta( $filter_set_id, '_asfp_label_sort_show', true ), 'sort_text' => (string) get_post_meta( $filter_set_id, '_asfp_label_sort_text', true ), ]; $design_class = $filter_design !== 'default' ? ' asfp-design-' . $filter_design : ''; // Add critical inline CSS to prevent FOUC (Flash of Unstyled Content) $critical_css = " "; echo $critical_css; // Add custom color CSS using wp_add_inline_style $custom_css = " #{$container_id} .asfp-tab.active, #{$container_id} .asfp-pager button.active, #{$container_id} .asfp-apply { background-color: {$primary_color} !important; border-color: {$primary_color} !important; color: #fff !important; } #{$container_id} .asfp-apply:hover { background-color: {$accent_color} !important; border-color: {$accent_color} !important; } #{$container_id} .asfp-title a:hover, #{$container_id} .asfp-read-more a { color: {$secondary_color} !important; } #{$container_id} .asfp-read-more a:hover { color: {$accent_color} !important; } #{$container_id} .asfp-search:focus, #{$container_id} select:focus { border-color: {$primary_color} !important; box-shadow: 0 0 0 3px " . $this->hex_to_rgba( $primary_color, 0.1 ) . " !important; } #{$container_id} .asfp-pager button:hover:not([disabled]) { border-color: {$secondary_color} !important; } #{$container_id} .asfp-category-tag { background-color: {$primary_color} !important; color: #fff !important; } #{$container_id} .asfp-template1 .asfp-category-tag { background-color: {$primary_color}2e !important; color: #fff !important; border: 1px solid {$primary_color} !important; } #{$container_id} .asfp-category-badge{ background-color: {$primary_color} !important; } #{$container_id} .asfp-category-tag:hover { background-color: {$accent_color} !important; } "; wp_add_inline_style( 'asfp-frontend', $custom_css ); echo '
'; if ( $show_controls ) { if ( $template_filter_bar ) { $allowed_html = wp_kses_allowed_html( 'post' ); $allowed_html['input'] = [ 'type' => true, 'class' => true, 'placeholder' => true, 'value' => true, 'name' => true, ]; $allowed_html['select'] = [ 'class' => true, 'name' => true, 'data-tax' => true, ]; $allowed_html['option'] = [ 'value' => true, 'selected' => true, ]; $allowed_html['button'] = [ 'type' => true, 'class' => true, 'data-term' => true, 'data-page' => true, ]; // Add style tag support so CSS in templates works $allowed_html['style'] = [ 'type' => true, ]; // Add script tag support (for custom JavaScript if needed) $allowed_html['script'] = [ 'type' => true, ]; echo wp_kses( $this->replace_placeholders_filter_bar( $template_filter_bar, [ 'show_search' => $show_search, 'show_tax_cat' => $show_tax_cat, 'show_tax_tag' => $show_tax_tag, 'show_sort' => $show_sort, ], $labels, $post_type, $hide_empty_terms ), $allowed_html ); } else { $this->default_filter_bar( $show_search, $show_tax_cat, $show_tax_tag, $show_sort, $apply_mode, $tax_display, $labels, $filter_design, $hide_empty_terms, $post_type ); } } // Wrap results in content wrapper for sidebar design if ( $filter_design === 'sidebar' ) { echo '
'; } // Add template-specific layout classes $results_class = 'asfp-results'; if ( $blog_template === 'template5' ) { $results_class .= ' asfp-template5-layout'; } elseif ( $blog_template === 'template6' ) { $results_class .= ' asfp-template6-layout'; } elseif ( $blog_template === 'template7' ) { $results_class .= ' asfp-template7-layout'; } echo '
'; // Render initial results on page load (no AJAX) - check for query string filters $meta = $this->render_initial_results( $filter_set_id, $post_type, $per_page, $template_result_item, $blog_template, $show_title, $show_excerpt, $show_date, $show_author, $show_image, $excerpt_length, $enable_querystring, $querystring_format, $qs_search, $qs_category, $qs_tag, $qs_sort, $qs_page ); echo '
'; // Initial status $status_html = isset( $meta['status'] ) ? $meta['status'] : ''; $found_posts = isset( $meta['found_posts'] ) ? (int) $meta['found_posts'] : 0; echo ''; // Initial pagination $max_pages = isset( $meta['max_num_pages'] ) ? (int) $meta['max_num_pages'] : 1; $cur_page = isset( $meta['current_page'] ) ? (int) $meta['current_page'] : 1; echo '
'; if ( in_array( $pagination_display, [ 'next_prev', 'numeric', 'both' ], true ) && $max_pages > 1 ) { echo '
'; if ( in_array( $pagination_display, [ 'next_prev', 'both' ], true ) ) { echo ''; } if ( in_array( $pagination_display, [ 'numeric', 'both' ], true ) ) { echo '
'; for ( $p = 1; $p <= $max_pages; $p++ ) { $active = $p === $cur_page ? ' active' : ''; echo ''; } echo '
'; } if ( in_array( $pagination_display, [ 'next_prev', 'both' ], true ) ) { echo ''; } echo '
'; } echo '
'; if ( $filter_design === 'sidebar' ) { echo '
'; // Close apf-content-wrapper } echo '
'; // Close apf-container } private function default_filter_bar( $show_search, $show_tax_cat, $show_tax_tag, $show_sort, $apply_mode, $tax_display, $labels, $filter_design = 'default', $hide_empty_terms = false, $post_type = 'post' ) { echo '
'; if ( ! empty( $labels['global_show'] ) ) { $txt = $labels['global_text'] !== '' ? $labels['global_text'] : __( 'Filters', 'Ajax-smart-filter' ); echo '
' . esc_html( $txt ) . '
'; } if ( $show_search ) { echo '
'; if ( ! empty( $labels['search_show'] ) ) { $txt = $labels['search_text'] !== '' ? $labels['search_text'] : __( 'Search', 'Ajax-smart-filter' ); echo ''; } echo '
'; if ( in_array( $filter_design, [ 'minimalist', 'sidebar', 'compact' ], true ) ) { echo ''; } echo ''; echo '
'; echo '
'; echo '
'; } if ( $show_tax_cat ) { // Check if category taxonomy is registered for this post type if ( is_object_in_taxonomy( $post_type, 'category' ) ) { echo '
'; if ( ! empty( $labels['category_show'] ) ) { $txt = $labels['category_text'] !== '' ? $labels['category_text'] : __( 'Category', 'Ajax-smart-filter' ); echo ''; } $this->render_tax_control( 'category', $tax_display, $hide_empty_terms, $post_type ); echo '
'; } } if ( $show_tax_tag ) { // Check if post_tag taxonomy is registered for this post type if ( is_object_in_taxonomy( $post_type, 'post_tag' ) ) { echo '
'; if ( ! empty( $labels['tag_show'] ) ) { $txt = $labels['tag_text'] !== '' ? $labels['tag_text'] : __( 'Tag', 'Ajax-smart-filter' ); echo ''; } $this->render_tax_control( 'post_tag', $tax_display, $hide_empty_terms, $post_type ); echo '
'; } } if ( $show_sort ) { echo '
'; if ( ! empty( $labels['sort_show'] ) ) { $txt = $labels['sort_text'] !== '' ? $labels['sort_text'] : __( 'Sort', 'Ajax-smart-filter' ); echo ''; } if ( $filter_design === 'compact' ) { echo '
'; echo ''; echo ''; echo '
'; } else { echo ''; } echo '
'; } if ( $show_controls && $apply_mode === 'button' ) { echo '
'; echo ''; echo '
'; } echo '
'; } private function replace_placeholders_filter_bar( $html, $opts, $labels, $post_type = 'post', $hide_empty = false ) { // When custom HTML is used, don't automatically add labels - only add them if user explicitly includes label placeholders // Build category dropdown HTML (matching code snippet plugin structure) $cat_dropdown_html = ''; if ( is_object_in_taxonomy( $post_type, 'category' ) ) { $post_ids = get_posts( [ 'post_type' => $post_type, 'fields' => 'ids', 'posts_per_page' => -1 ] ); $categories = get_terms( [ 'taxonomy' => 'category', 'hide_empty' => $hide_empty, 'object_ids' => $post_ids ] ); if ( ! is_wp_error( $categories ) && ! empty( $categories ) ) { $cat_dropdown_html = '
'; $cat_dropdown_html .= '
'; } } // Build tag dropdown HTML (matching code snippet plugin structure) $tag_dropdown_html = ''; if ( is_object_in_taxonomy( $post_type, 'post_tag' ) ) { $post_ids = get_posts( [ 'post_type' => $post_type, 'fields' => 'ids', 'posts_per_page' => -1 ] ); $tags = get_terms( [ 'taxonomy' => 'post_tag', 'hide_empty' => $hide_empty, 'object_ids' => $post_ids ] ); if ( ! is_wp_error( $tags ) && ! empty( $tags ) ) { $tag_dropdown_html = '
'; $tag_dropdown_html .= '
'; } } // Build category tabs HTML (matching code snippet plugin structure) $cat_tabs_html = ''; if ( is_object_in_taxonomy( $post_type, 'category' ) ) { $post_ids = get_posts( [ 'post_type' => $post_type, 'fields' => 'ids', 'posts_per_page' => -1 ] ); $categories = get_terms( [ 'taxonomy' => 'category', 'hide_empty' => $hide_empty, 'object_ids' => $post_ids ] ); if ( ! is_wp_error( $categories ) ) { $cat_tabs_html = '
'; $cat_tabs_html .= '
'; $cat_tabs_html .= '
' . esc_html__( 'All', 'Ajax-smart-filter' ) . '
'; if ( ! empty( $categories ) ) { foreach ( $categories as $cat ) { $cat_tabs_html .= '
' . esc_html( $cat->name ) . '
'; } } $cat_tabs_html .= '
'; } } // Build tag tabs HTML (matching code snippet plugin structure) $tag_tabs_html = ''; if ( is_object_in_taxonomy( $post_type, 'post_tag' ) ) { $post_ids = get_posts( [ 'post_type' => $post_type, 'fields' => 'ids', 'posts_per_page' => -1 ] ); $tags = get_terms( [ 'taxonomy' => 'post_tag', 'hide_empty' => $hide_empty, 'object_ids' => $post_ids ] ); if ( ! is_wp_error( $tags ) ) { $tag_tabs_html = '
'; $tag_tabs_html .= '
'; $tag_tabs_html .= '
' . esc_html__( 'All', 'Ajax-smart-filter' ) . '
'; if ( ! empty( $tags ) ) { foreach ( $tags as $tag ) { $tag_tabs_html .= '
' . esc_html( $tag->name ) . '
'; } } $tag_tabs_html .= '
'; } } $map = [ // Search input with autocomplete (matching code snippet plugin) '[filter attribute="search"]' => '
', // Category dropdowns (both variations) '[filter attribute="category"]' => $cat_dropdown_html, '[filter attribute="category-dropdown"]' => $cat_dropdown_html, // Tag dropdowns (both variations) '[filter attribute="tag"]' => $tag_dropdown_html, '[filter attribute="tag-dropdown"]' => $tag_dropdown_html, // Category tabs (matching code snippet plugin structure) '[filter attribute="category-tabs"]' => $cat_tabs_html, // Tag tabs (matching code snippet plugin structure) '[filter attribute="tag-tabs"]' => $tag_tabs_html, // Sort dropdown (matching code snippet plugin structure) '[filter attribute="sort"]' => '
' . '
', // Apply button (matching code snippet plugin structure) '[filter attribute="apply-button"]' => '
', ]; // Label placeholders (only rendered if user explicitly includes them in custom HTML and labels are enabled) $label_map = [ '[filter attribute="label-global"]' => ! empty( $labels['global_show'] ) ? '
' . esc_html( $labels['global_text'] !== '' ? $labels['global_text'] : __( 'Filters', 'Ajax-smart-filter' ) ) . '
' : '', '[filter attribute="label-search"]' => ! empty( $labels['search_show'] ) ? '' : '', '[filter attribute="label-category"]' => ! empty( $labels['category_show'] ) ? '' : '', '[filter attribute="label-tag"]' => ! empty( $labels['tag_show'] ) ? '' : '', '[filter attribute="label-sort"]' => ! empty( $labels['sort_show'] ) ? '' : '', ]; // Process label placeholders first, then filter placeholders $html = strtr( $html, $label_map ); return strtr( $html, $map ); } private function render_tax_control( $taxonomy, $display, $hide_empty = false, $post_type = 'post' ) { if ( $display === 'tabs' ) { echo wp_kses_post( $this->get_tax_tabs_markup( $taxonomy, $hide_empty, $post_type ) ); // hidden input to store active term for JS to read $class = $taxonomy === 'category' ? 'asfp-category' : 'asfp-post_tag'; echo ''; } else { // Use template file for dropdown $post_ids = get_posts( [ 'post_type' => $post_type, 'fields' => 'ids', 'posts_per_page' => -1 ] ); $terms = get_terms( [ 'taxonomy' => $taxonomy, 'hide_empty' => $hide_empty, 'object_ids' => $post_ids ] ); if ( is_wp_error( $terms ) ) { $terms = []; } $label = $taxonomy === 'category' ? __( 'Categories', 'Ajax-smart-filter' ) : __( 'Tags', 'Ajax-smart-filter' ); $label_all = $taxonomy === 'category' ? __( 'All Categories', 'Ajax-smart-filter' ) : __( 'All Tags', 'Ajax-smart-filter' ); $tax_class = $taxonomy === 'category' ? 'category' : 'post_tag'; $hidden_name = $taxonomy === 'category' ? 'apf_cat' : 'apf_tag'; echo asfp_load_template( 'filter-bar', 'dropdown', [ 'taxonomy' => $taxonomy, 'tax_class' => $tax_class, 'label' => $label, 'label_all' => $label_all, 'terms' => $terms, 'hidden_name' => $hidden_name, ] ); } } private function get_tax_tabs_markup( $taxonomy, $hide_empty = false, $post_type = 'post' ) { // Get all post IDs of this post type to filter terms $post_ids = get_posts( [ 'post_type' => $post_type, 'fields' => 'ids', 'posts_per_page' => -1 ] ); $terms = get_terms( [ 'taxonomy' => $taxonomy, 'hide_empty' => $hide_empty, 'object_ids' => $post_ids ] ); if ( is_wp_error( $terms ) ) { $terms = []; } $label = $taxonomy === 'category' ? __( 'Categories', 'Ajax-smart-filter' ) : __( 'Tags', 'Ajax-smart-filter' ); $label_all = $taxonomy === 'category' ? __( 'All Categories', 'Ajax-smart-filter' ) : __( 'All Tags', 'Ajax-smart-filter' ); $tax_class = $taxonomy === 'category' ? 'category' : 'post_tag'; // Use template file return asfp_load_template( 'filter-bar', 'tabs', [ 'taxonomy' => $taxonomy, 'tax_class' => $tax_class, 'label' => $label, 'label_all' => $label_all, 'terms' => $terms, ] ); } private function hex_to_rgba( $hex, $alpha = 1 ) { $hex = str_replace( '#', '', $hex ); if ( strlen( $hex ) === 3 ) { $r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) ); $g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) ); $b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) ); } else { $r = hexdec( substr( $hex, 0, 2 ) ); $g = hexdec( substr( $hex, 2, 2 ) ); $b = hexdec( substr( $hex, 4, 2 ) ); } return "rgba({$r}, {$g}, {$b}, {$alpha})"; } private function render_initial_results( $filter_set_id, $post_type, $per_page, $template_result_item, $blog_template, $show_title, $show_excerpt, $show_date, $show_author, $show_image, $excerpt_length = 25, $enable_querystring = false, $querystring_format = 'id', $qs_search = 'asfp_search', $qs_category = 'asfp_cat', $qs_tag = 'asfp_tag', $qs_sort = 'asfp_sort', $qs_page = 'asfp_page' ) { // Check for query string parameters $search = ''; $cat = 0; $tag = 0; $sort = 'newest'; $paged = 1; if ( $enable_querystring && ! empty( $_GET ) ) { // Get search parameter using custom name if ( isset( $_GET[ $qs_search ] ) ) { $search = sanitize_text_field( wp_unslash( $_GET[ $qs_search ] ) ); } // Get category parameter using custom name if ( isset( $_GET[ $qs_category ] ) ) { $cat_param = sanitize_text_field( wp_unslash( $_GET[ $qs_category ] ) ); if ( $querystring_format === 'name' ) { $term = get_term_by( 'slug', $cat_param, 'category' ); $cat = $term ? $term->term_id : 0; } else { $cat = absint( $cat_param ); } } // Get tag parameter using custom name if ( isset( $_GET[ $qs_tag ] ) ) { $tag_param = sanitize_text_field( wp_unslash( $_GET[ $qs_tag ] ) ); if ( $querystring_format === 'name' ) { $term = get_term_by( 'slug', $tag_param, 'post_tag' ); $tag = $term ? $term->term_id : 0; } else { $tag = absint( $tag_param ); } } // Get sort parameter using custom name if ( isset( $_GET[ $qs_sort ] ) ) { $sort = sanitize_key( wp_unslash( $_GET[ $qs_sort ] ) ); } // Get page parameter using custom name if ( isset( $_GET[ $qs_page ] ) ) { $paged = max( 1, absint( wp_unslash( $_GET[ $qs_page ] ) ) ); } } // Build query args $args = [ 'post_type' => $post_type, 'post_status' => 'publish', 'posts_per_page' => $per_page, 'paged' => $paged, ]; // Apply sorting switch ( $sort ) { case 'oldest': $args['orderby'] = 'date'; $args['order'] = 'ASC'; break; case 'popular': // Use 'post_views' meta key to match AJAX endpoint and other handlers // Include posts with or without post_views meta (treat missing as 0) // Use custom orderby via filter to handle NULL values as 0 $args['orderby'] = 'post_views_num'; $args['order'] = 'DESC'; // Add filter to modify SQL to include all posts and treat NULL as 0 add_filter( 'posts_join', function( $join, $query ) { global $wpdb; static $added = false; if ( ! $added && ! strpos( $join, 'post_views_meta' ) ) { $join .= " LEFT JOIN {$wpdb->postmeta} AS post_views_meta ON ({$wpdb->posts}.ID = post_views_meta.post_id AND post_views_meta.meta_key = 'post_views')"; $added = true; } return $join; }, 10, 2 ); add_filter( 'posts_orderby', function( $orderby, $query ) { if ( strpos( $orderby, 'post_views_num' ) !== false ) { // Use COALESCE to treat NULL as 0, then cast to number $orderby = "CAST(COALESCE(post_views_meta.meta_value, '0') AS UNSIGNED) DESC"; } return $orderby; }, 10, 2 ); break; case 'az': $args['orderby'] = 'title'; $args['order'] = 'ASC'; break; case 'za': $args['orderby'] = 'title'; $args['order'] = 'DESC'; break; default: // newest $args['orderby'] = 'date'; $args['order'] = 'DESC'; } // Apply search if ( ! empty( $search ) ) { $args['s'] = $search; } // Apply taxonomy filters $tax_query = []; if ( $cat > 0 ) { $tax_query[] = [ 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => $cat, ]; } if ( $tag > 0 ) { $tax_query[] = [ 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $tag, ]; } if ( ! empty( $tax_query ) ) { $args['tax_query'] = $tax_query; } $query = new WP_Query( $args ); // Debug output - always show for troubleshooting echo ''; echo ''; echo ''; if ( ! $query->have_posts() ) { $no_results_message = get_post_meta( $filter_set_id, '_asfp_no_results_message', true ); if ( ! $no_results_message ) { $no_results_message = __( 'No posts found. Please try different filters.', 'Ajax-smart-filter' ); } echo '
' . esc_html( $no_results_message ) . '
'; return [ 'status' => '', 'found_posts' => 0, 'max_num_pages' => 0, 'current_page' => max( 1, (int) $args['paged'] ), ]; } // Render posts using template loader for consistent rendering $display_settings = [ 'show_title' => $show_title, 'show_excerpt' => $show_excerpt, 'show_date' => $show_date, 'show_author' => $show_author, 'show_image' => $show_image, ]; // Reset post index for template7 featured post logic global $asfp_post_index; $asfp_post_index = 0; echo ''; $rendered_count = 0; while ( $query->have_posts() ) { $query->the_post(); $rendered_count++; echo ''; // Use template loader for consistent rendering if ( function_exists( 'asfp_render_post_item' ) ) { $output = asfp_render_post_item( get_the_ID(), $template_result_item, $blog_template, $excerpt_length, $display_settings ); } else { // Fallback to AJAX handler $ajax_handler = new ASFP_Ajax(); $display_settings['blog_template'] = $blog_template; $display_settings['excerpt_length'] = $excerpt_length; $output = $ajax_handler->render_item( get_post(), $template_result_item, $display_settings ); } echo ''; echo $output; } echo ''; $status_html = sprintf( /* translators: 1: displayed count, 2: total found */ esc_html__( 'Found %1$d of %2$d', 'Ajax-smart-filter' ), (int) $query->post_count, (int) $query->found_posts ); $meta = [ 'status' => $status_html, 'found_posts' => (int) $query->found_posts, 'max_num_pages' => (int) $query->max_num_pages, 'current_page' => max( 1, (int) $args['paged'] ), ]; wp_reset_postdata(); return $meta; } }