track_tab_click( $filter_set_id, $taxonomy, $term_id, $term_name );
}
wp_send_json_success( [ 'tracked' => true ] );
}
public function handle_filter() {
check_ajax_referer( 'asfp_nonce', 'nonce' );
$filter_set_id = isset( $_POST['filter_set_id'] ) ? absint( wp_unslash( $_POST['filter_set_id'] ) ) : 0;
if ( ! $filter_set_id || get_post_type( $filter_set_id ) !== 'asfp_filter_set' ) {
wp_send_json_error( [ 'message' => __( 'Invalid filter set.', 'Ajax-smart-filter' ) ], 400 );
}
$post_type = get_post_meta( $filter_set_id, '_asfp_post_type', true );
if ( ! $post_type ) { $post_type = 'post'; }
$s = isset( $_POST['s'] ) ? sanitize_text_field( wp_unslash( $_POST['s'] ) ) : '';
$cat = isset( $_POST['cat'] ) ? absint( wp_unslash( $_POST['cat'] ) ) : 0;
$tag = isset( $_POST['tag'] ) ? absint( wp_unslash( $_POST['tag'] ) ) : 0;
$sort = isset( $_POST['sort'] ) ? sanitize_key( wp_unslash( $_POST['sort'] ) ) : 'newest';
$paged = isset( $_POST['page'] ) ? max( 1, absint( wp_unslash( $_POST['page'] ) ) ) : 1;
$per_page = (int) get_post_meta( $filter_set_id, '_asfp_per_page', true );
if ( $per_page <= 0 ) { $per_page = 6; }
// Track ACTUAL USER ACTIONS (what users clicked/typed)
$filters_used = [];
if ( $s ) { $filters_used[] = 'search'; } // User typed search
if ( $cat ) { $filters_used[] = 'category'; } // User selected category
if ( $tag ) { $filters_used[] = 'tag'; } // User selected tag
if ( $sort && $sort !== 'newest' ) { $filters_used[] = 'sort'; } // User changed sort
$blog_template = get_post_meta( $filter_set_id, '_asfp_blog_template', true );
if ( ! $blog_template ) { $blog_template = 'template1'; }
// Only track if user actually used filters (not just page load)
if ( ! empty( $filters_used ) ) {
$analytics = new ASFP_Analytics();
$analytics->track_filter_usage( $filter_set_id, $filters_used, $blog_template, $post_type, $s, $sort );
}
$search_scope = get_post_meta( $filter_set_id, '_asfp_search_scope', true );
$match_logic = get_post_meta( $filter_set_id, '_asfp_match_logic', true );
if ( ! in_array( $match_logic, [ 'AND', 'OR' ], true ) ) { $match_logic = 'AND'; }
$args = [
'post_type' => $post_type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => $per_page,
];
// Sorting
switch ( $sort ) {
case 'oldest':
$args['orderby'] = 'date';
$args['order'] = 'ASC';
break;
case 'popular':
// 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;
case 'newest':
default:
$args['orderby'] = 'date';
$args['order'] = 'DESC';
break;
}
// Taxonomy filters
$tax_query = [];
if ( $cat ) {
$tax_query[] = [
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => [ $cat ],
];
}
if ( $tag ) {
$tax_query[] = [
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => [ $tag ],
];
}
if ( ! empty( $tax_query ) ) {
if ( count( $tax_query ) > 1 ) {
$tax_query['relation'] = $match_logic;
}
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Taxonomy filtering is core functionality, queries are optimized with term_id field.
$args['tax_query'] = $tax_query;
}
// Search (core)
if ( $s ) {
$args['s'] = $s;
}
$q = new WP_Query( $args );
$template_result_item = get_post_meta( $filter_set_id, '_asfp_template_result_item', true );
// Don't track custom template here - it's admin configuration, not user action
// Custom HTML is tracked when admin saves settings
// Get display settings
$show_title = (bool) get_post_meta( $filter_set_id, '_asfp_show_title', true );
$show_excerpt = (bool) get_post_meta( $filter_set_id, '_asfp_show_excerpt', true );
$show_date = (bool) get_post_meta( $filter_set_id, '_asfp_show_date', true );
$show_author = (bool) get_post_meta( $filter_set_id, '_asfp_show_author', true );
$show_image = (bool) get_post_meta( $filter_set_id, '_asfp_show_image', true );
$excerpt_length = (int) get_post_meta( $filter_set_id, '_asfp_excerpt_length', true );
if ( $excerpt_length <= 0 ) { $excerpt_length = 25; }
// Get 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'; }
$display_settings = [
'show_title' => $show_title,
'show_excerpt' => $show_excerpt,
'show_date' => $show_date,
'show_author' => $show_author,
'show_image' => $show_image,
'blog_template' => $blog_template,
'excerpt_length' => $excerpt_length,
];
// Reset post index for template7 featured post logic
global $asfp_post_index;
$asfp_post_index = 0;
$items_html = '';
if ( $q->have_posts() ) {
while ( $q->have_posts() ) { $q->the_post();
$items_html .= $this->render_item( get_post(), $template_result_item, $display_settings );
}
wp_reset_postdata();
} else {
// No posts found - show custom message
$no_results_message = get_post_meta( $filter_set_id, '_asfp_no_results_message', true );
if ( empty( $no_results_message ) ) {
$no_results_message = __( 'No posts found. Please try different filters.', 'Ajax-smart-filter' );
}
$items_html = '
' . esc_html( $no_results_message ) . '
';
}
$status_html = sprintf(
/* translators: 1: found count, 2: total found */
esc_html__( 'Found %1$d of %2$d', 'Ajax-smart-filter' ),
$q->post_count,
$q->found_posts
);
wp_send_json_success( [
'html' => $items_html,
'status' => $status_html,
'max_num_pages' => (int) $q->max_num_pages,
'found_posts' => (int) $q->found_posts,
'current_page' => (int) $paged,
] );
}
public function render_item( $post, $template, $display_settings = [] ) {
$title = get_the_title( $post );
$permalink = get_permalink( $post );
// Get excerpt length from display settings
$excerpt_length = isset( $display_settings['excerpt_length'] ) ? $display_settings['excerpt_length'] : 25;
if ( $excerpt_length <= 0 ) { $excerpt_length = 25; }
// Use safe excerpt extraction
if ( function_exists( 'asfp_get_safe_excerpt' ) ) {
$excerpt = asfp_get_safe_excerpt( $post, $excerpt_length );
} else {
// Fallback to simple extraction
$content = wp_strip_all_tags( $post->post_content );
$excerpt = wp_trim_words( $content, $excerpt_length );
}
$img = get_the_post_thumbnail_url( $post, 'full' );
$date = get_the_date( 'M j, Y', $post ); // Short month format (e.g., Oct 12, 2025)
$author = get_the_author_meta( 'display_name', $post->post_author );
$author_avatar = get_avatar_url( $post->post_author, [ 'size' => 96 ] );
$categories = get_the_category( $post->ID );
$category_name = ! empty( $categories ) ? $categories[0]->name : '';
// Get additional data for new placeholders
$post_id = $post->ID;
$post_slug = $post->post_name;
$post_content = apply_filters( 'the_content', $post->post_content );
// Get all categories as comma-separated string
$category_names = [];
if ( ! empty( $categories ) ) {
foreach ( $categories as $cat ) {
$category_names[] = $cat->name;
}
}
$post_categories = implode( ', ', $category_names );
// Get all tags as comma-separated string
$tags = get_the_tags( $post->ID );
$tag_names = [];
if ( ! empty( $tags ) ) {
foreach ( $tags as $tag ) {
$tag_names[] = $tag->name;
}
}
$post_tags = implode( ', ', $tag_names );
// Keep tag_names array for single tag placeholder
// Default display settings if not provided
$show_title = isset( $display_settings['show_title'] ) ? $display_settings['show_title'] : true;
$show_excerpt = isset( $display_settings['show_excerpt'] ) ? $display_settings['show_excerpt'] : true;
$show_date = isset( $display_settings['show_date'] ) ? $display_settings['show_date'] : true;
$show_author = isset( $display_settings['show_author'] ) ? $display_settings['show_author'] : false;
$show_image = isset( $display_settings['show_image'] ) ? $display_settings['show_image'] : true;
$blog_template = isset( $display_settings['blog_template'] ) ? $display_settings['blog_template'] : 'template1';
if ( $template ) {
$out = $template;
// Prepare category HTML with card-categories class
$category_html = $category_name ? '' . esc_html( $category_name ) . '' : '';
$categories_html = $post_categories ? '' . esc_html( $post_categories ) . '' : '';
// Prepare tag HTML with card-tags class
$tag_html = ! empty( $tag_names ) ? '' . esc_html( $tag_names[0] ) . '' : '';
$tags_html = $post_tags ? '' . esc_html( $post_tags ) . '' : '';
$replace = [
'{{post_id}}' => esc_html( $post_id ),
'{{post_title}}' => $show_title ? esc_html( $title ) : '',
'{{post_link}}' => esc_url( $permalink ),
'{{post_slug}}' => esc_html( $post_slug ),
'{{post_excerpt}}' => $show_excerpt ? esc_html( $excerpt ) : '',
'{{post_content}}' => $post_content,
'{{post_image}}' => ( $show_image && $img ) ? '
' : '',
'{{post_image_url}}' => $img ? esc_url( $img ) : '',
'{{post_date}}' => $show_date ? esc_html( $date ) : '',
'{{post_author}}' => $show_author ? esc_html( $author ) : '',
'{{post_author_avatar}}' => $show_author && $author_avatar ? '
' : '',
'{{post_category}}' => $category_html,
'{{post_categories}}' => $categories_html,
'{{post_tag}}' => $tag_html,
'{{post_tags}}' => $tags_html,
];
$out = strtr( $out, $replace );
return $out;
}
// Use shared template loader for consistent rendering
if ( function_exists( 'asfp_render_post_item' ) ) {
return asfp_render_post_item( $post->ID, $template, $blog_template, $excerpt_length, $display_settings );
}
// Fallback: Render based on selected template
return $this->render_template( $post, $display_settings, $title, $permalink, $excerpt, $img, $date, $author, $author_avatar, $category_name );
}
private function render_template( $post, $display_settings, $title, $permalink, $excerpt, $img, $date, $author, $author_avatar, $category_name ) {
$show_title = $display_settings['show_title'];
$show_excerpt = $display_settings['show_excerpt'];
$show_date = $display_settings['show_date'];
$show_author = $display_settings['show_author'];
$show_image = $display_settings['show_image'];
$blog_template = $display_settings['blog_template'];
switch ( $blog_template ) {
case 'template1':
return $this->render_template1( $title, $permalink, $excerpt, $img, $date, $author, $author_avatar, $category_name, $show_title, $show_excerpt, $show_date, $show_author, $show_image );
case 'template5':
return $this->render_template5( $title, $permalink, $excerpt, $img, $date, $author, $author_avatar, $category_name, $show_title, $show_excerpt, $show_date, $show_author, $show_image );
case 'template6':
return $this->render_template6( $title, $permalink, $excerpt, $img, $date, $author, $author_avatar, $category_name, $show_title, $show_excerpt, $show_date, $show_author, $show_image );
case 'template7':
return $this->render_template7( $title, $permalink, $excerpt, $img, $date, $author, $author_avatar, $category_name, $show_title, $show_excerpt, $show_date, $show_author, $show_image );
default:
return $this->render_template1( $title, $permalink, $excerpt, $img, $date, $author, $author_avatar, $category_name, $show_title, $show_excerpt, $show_date, $show_author, $show_image );
}
}
public function handle_autocomplete() {
check_ajax_referer( 'asfp_nonce', 'nonce' );
$filter_set_id = isset( $_GET['filter_set_id'] ) ? absint( wp_unslash( $_GET['filter_set_id'] ) ) : 0;
if ( ! $filter_set_id || get_post_type( $filter_set_id ) !== 'asfp_filter_set' ) {
wp_send_json_error( [ 'message' => __( 'Invalid filter set.', 'Ajax-smart-filter' ) ], 400 );
}
$post_type = get_post_meta( $filter_set_id, '_asfp_post_type', true );
if ( ! $post_type ) { $post_type = 'post'; }
$term = isset( $_GET['term'] ) ? sanitize_text_field( wp_unslash( $_GET['term'] ) ) : '';
if ( strlen( $term ) < 2 ) {
wp_send_json_success( [ 'results' => [] ] );
}
// Track autocomplete usage
$analytics = new ASFP_Analytics();
$analytics->track_autocomplete_usage( $filter_set_id, $term, $post_type );
$q = new WP_Query( [
'post_type' => $post_type,
's' => $term,
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'relevance',
] );
$results = [];
if ( $q->have_posts() ) {
while ( $q->have_posts() ) { $q->the_post();
$thumbnail = get_the_post_thumbnail_url( get_the_ID(), 'thumbnail' );
if ( ! $thumbnail ) {
$thumbnail = ''; // Placeholder or default image
}
$results[] = [
'id' => get_the_ID(),
'title' => get_the_title(),
'link' => get_permalink(),
'image' => $thumbnail,
];
}
wp_reset_postdata();
}
wp_send_json_success( [ 'results' => $results ] );
}
// Template 1 - Modern Card with Image Overlay
private function render_template1( $title, $permalink, $excerpt, $img, $date, $author, $author_avatar, $category_name, $show_title, $show_excerpt, $show_date, $show_author, $show_image ) {
$html = '';
if ( $show_image && $img ) {
$html .= '';
}
$html .= '';
if ( $show_title ) {
$html .= '
';
}
if ( $show_excerpt ) {
$html .= '
' . esc_html( $excerpt ) . '
';
}
if ( $show_author ) {
$html .= '
';
if ( $author_avatar ) {
$html .= '
 . ')
';
}
$html .= '
';
$html .= '' . esc_html( $author ) . '';
if ( $show_date ) {
$html .= '' . esc_html( $date ) . '';
}
$html .= '
';
$html .= '
';
} elseif ( $show_date ) {
$html .= '
' . esc_html( $date ) . '
';
}
$html .= '
';
$html .= '';
return $html;
}
// Template 5 - Horizontal Card Layout (side-by-side)
private function render_template5( $title, $permalink, $excerpt, $img, $date, $author, $author_avatar, $category_name, $show_title, $show_excerpt, $show_date, $show_author, $show_image ) {
$html = '';
// Image Section (left side)
if ( $show_image && $img ) {
$html .= '';
}
// Content Section (right side)
$html .= '';
// Date and Category Header
$html .= '';
// Title
if ( $show_title ) {
$html .= '
';
}
// Excerpt
if ( $show_excerpt ) {
$html .= '
' . esc_html( $excerpt ) . '
';
}
// Author Info
if ( $show_author && $author_avatar ) {
$html .= '
';
$html .= '
 . ')
';
$html .= '
';
$html .= '
' . esc_html( $author ) . '
';
$html .= '
';
$html .= '
';
}
$html .= '
'; // Close content-section
$html .= '';
return $html;
}
// Template 6 - Masonry Grid Layout
private function render_template6( $title, $permalink, $excerpt, $img, $date, $author, $author_avatar, $category_name, $show_title, $show_excerpt, $show_date, $show_author, $show_image ) {
$html = '';
// Image
if ( $show_image && $img ) {
$html .= '';
}
// Content
$html .= '';
// Category
if ( $category_name ) {
$category_slug = sanitize_title( $category_name );
$html .= '
' . esc_html( $category_name ) . '
';
}
// Title
if ( $show_title ) {
$html .= '
';
}
// Meta (Date, Comments, Author)
$html .= '
';
$meta_items = [];
if ( $show_date ) {
$meta_items[] = '' . esc_html( $date ) . '';
}
// Add comments count (placeholder)
$comments_count = get_comments_number();
if ( $comments_count > 0 ) {
$meta_items[] = '' . sprintf( _n( '%s comment', '%s comments', $comments_count, 'Ajax-smart-filter' ), number_format_i18n( $comments_count ) ) . '';
}
if ( $show_author ) {
$meta_items[] = 'by ' . esc_html( $author ) . '';
}
$html .= implode( '|', $meta_items );
$html .= '
';
// Excerpt
if ( $show_excerpt ) {
$html .= '
' . esc_html( $excerpt ) . '
';
}
$html .= '
'; // Close content
$html .= '';
return $html;
}
// Template 7 - Featured Article + Grid Layout
// Note: This is a fallback method. The actual template uses template7.php file
private function render_template7( $title, $permalink, $excerpt, $img, $date, $author, $author_avatar, $category_name, $show_title, $show_excerpt, $show_date, $show_author, $show_image ) {
// This template is handled by the template loader (template7.php)
// This fallback renders a simple grid card
$html = '';
// Image
if ( $show_image && $img ) {
$html .= '';
$html .= '
';
$html .= '';
}
// Content
$html .= '';
// Meta
if ( $show_date || $show_author ) {
$html .= '
';
}
// Title with arrow icon
if ( $show_title ) {
$html .= '
';
}
// Excerpt
if ( $show_excerpt ) {
$html .= '
' . esc_html( $excerpt ) . '
';
}
// Category tags
if ( $category_name ) {
$html .= '
';
$html .= '' . esc_html( $category_name ) . '';
$html .= '
';
}
$html .= '
'; // Close content
$html .= '';
return $html;
}
}