post_type ) {
// Enqueue WordPress color picker
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'wp-color-picker' );
// Enqueue ACE Editor from CDN
wp_enqueue_script(
'ace-editor',
'https://cdnjs.cloudflare.com/ajax/libs/ace/1.23.4/ace.js',
[],
'1.23.4',
true
);
wp_enqueue_script(
'ace-mode-html',
'https://cdnjs.cloudflare.com/ajax/libs/ace/1.23.4/mode-html.js',
[ 'ace-editor' ],
'1.23.4',
true
);
wp_enqueue_script(
'ace-theme-monokai',
'https://cdnjs.cloudflare.com/ajax/libs/ace/1.23.4/theme-monokai.js',
[ 'ace-editor' ],
'1.23.4',
true
);
// Enqueue admin script with dependencies
wp_enqueue_script(
'asfp-admin',
plugin_dir_url( dirname( __FILE__ ) ) . 'assets/js/admin.js',
[ 'jquery', 'wp-color-picker', 'ace-editor', 'ace-mode-html', 'ace-theme-monokai' ],
'1.0.0',
true
);
// Add inline styles for ACE editor containers
$ace_editor_css = '
.asfp-ace-editor-container {
width: 100%;
height: 300px;
border: 1px solid #ddd;
border-radius: 4px;
margin-top: 5px;
}
.asfp-ace-editor-container.result-item {
height: 400px;
}
';
wp_add_inline_style( 'wp-color-picker', $ace_editor_css );
}
}
}
public function add_meta_boxes() {
add_meta_box(
'apf_settings',
__( 'Filter Set Settings', 'Ajax-smart-filter' ),
[ $this, 'render_metabox' ],
'asfp_filter_set',
'normal',
'high'
);
}
public function render_metabox( $post ) {
wp_nonce_field( 'apf_save_meta', 'apf_meta_nonce' );
$post_type = get_post_meta( $post->ID, '_asfp_post_type', true );
if ( ! $post_type ) { $post_type = 'post'; }
$show_search = (bool) get_post_meta( $post->ID, '_asfp_show_search', true );
$show_tax_category = (bool) get_post_meta( $post->ID, '_asfp_show_tax_category', true );
$show_tax_post_tag = (bool) get_post_meta( $post->ID, '_asfp_show_tax_post_tag', true );
$show_sort = (bool) get_post_meta( $post->ID, '_asfp_show_sort', true );
$template_filter_bar = get_post_meta( $post->ID, '_asfp_template_filter_bar', true );
$template_result_item = get_post_meta( $post->ID, '_asfp_template_result_item', true );
$search_scope = get_post_meta( $post->ID, '_asfp_search_scope', true );
if ( ! is_array( $search_scope ) ) { $search_scope = [ 'title' => 1, 'content' => 0, 'meta' => 0 ]; }
$match_logic = get_post_meta( $post->ID, '_asfp_match_logic', true );
if ( ! in_array( $match_logic, [ 'AND', 'OR' ], true ) ) { $match_logic = 'AND'; }
$apply_mode = get_post_meta( $post->ID, '_asfp_apply_mode', true );
if ( ! in_array( $apply_mode, [ 'button', 'auto' ], true ) ) { $apply_mode = 'button'; }
$tax_display = get_post_meta( $post->ID, '_asfp_tax_display', true );
if ( ! in_array( $tax_display, [ 'dropdown', 'tabs' ], true ) ) { $tax_display = 'dropdown'; }
$per_page = (int) get_post_meta( $post->ID, '_asfp_per_page', true );
if ( $per_page <= 0 ) { $per_page = 6; }
$pagination_display = get_post_meta( $post->ID, '_asfp_pagination_display', true );
if ( ! in_array( $pagination_display, [ 'none', 'next_prev', 'numeric', 'both' ], true ) ) { $pagination_display = 'numeric'; }
$filter_design = get_post_meta( $post->ID, '_asfp_filter_design', true );
if ( ! in_array( $filter_design, [ 'default', 'minimalist', 'sidebar', 'compact' ], true ) ) { $filter_design = 'compact'; }
$global_label_show = (bool) get_post_meta( $post->ID, '_asfp_global_label_show', true );
$global_label_text = (string) get_post_meta( $post->ID, '_asfp_global_label_text', true );
$label_search_show = (bool) get_post_meta( $post->ID, '_asfp_label_search_show', true );
$label_search_text = (string) get_post_meta( $post->ID, '_asfp_label_search_text', true );
$label_category_show = (bool) get_post_meta( $post->ID, '_asfp_label_category_show', true );
$label_category_text = (string) get_post_meta( $post->ID, '_asfp_label_category_text', true );
$label_tag_show = (bool) get_post_meta( $post->ID, '_asfp_label_tag_show', true );
$label_tag_text = (string) get_post_meta( $post->ID, '_asfp_label_tag_text', true );
$label_sort_show = (bool) get_post_meta( $post->ID, '_asfp_label_sort_show', true );
$label_sort_text = (string) get_post_meta( $post->ID, '_asfp_label_sort_text', true );
// Display settings
$show_title = (bool) get_post_meta( $post->ID, '_asfp_show_title', true );
$show_excerpt = (bool) get_post_meta( $post->ID, '_asfp_show_excerpt', true );
$show_date = (bool) get_post_meta( $post->ID, '_asfp_show_date', true );
$show_author = (bool) get_post_meta( $post->ID, '_asfp_show_author', true );
$show_image = (bool) get_post_meta( $post->ID, '_asfp_show_image', true );
$excerpt_length = (int) get_post_meta( $post->ID, '_asfp_excerpt_length', true );
if ( $excerpt_length <= 0 ) { $excerpt_length = 25; } // Default 25 words
// Query string settings
$enable_querystring = get_post_meta( $post->ID, '_asfp_enable_querystring', true );
$enable_querystring = $enable_querystring === '' ? false : (bool) $enable_querystring;
$querystring_format = get_post_meta( $post->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( $post->ID, '_asfp_qs_search', true );
if ( empty( $qs_search ) ) { $qs_search = 'asfp_search'; }
$qs_category = get_post_meta( $post->ID, '_asfp_qs_category', true );
if ( empty( $qs_category ) ) { $qs_category = 'asfp_cat'; }
$qs_tag = get_post_meta( $post->ID, '_asfp_qs_tag', true );
if ( empty( $qs_tag ) ) { $qs_tag = 'asfp_tag'; }
$qs_sort = get_post_meta( $post->ID, '_asfp_qs_sort', true );
if ( empty( $qs_sort ) ) { $qs_sort = 'asfp_sort'; }
$qs_page = get_post_meta( $post->ID, '_asfp_qs_page', true );
if ( empty( $qs_page ) ) { $qs_page = 'asfp_page'; }
// Template settings
$blog_template = get_post_meta( $post->ID, '_asfp_blog_template', true );
if ( ! in_array( $blog_template, [ 'template1', 'template2', 'template3', 'template4', 'template5', 'template6', 'template7' ], true ) ) { $blog_template = 'template1'; }
// Enable/Disable filter setting - default to enabled for new filter sets
$enable_filter_meta = get_post_meta( $post->ID, '_asfp_enable_filter', true );
$enable_filter = $enable_filter_meta === '' ? true : (bool) $enable_filter_meta;
// Get all public post types
$all_post_types = get_post_types( [ 'public' => true ], 'objects' );
// Exclude plugin-generated post types
$excluded_post_types = [
'attachment', // WordPress attachments
'elementor_library', // Elementor templates
'e-landing-page', // Elementor landing pages
'product', // WooCommerce products
'product_variation', // WooCommerce variations
'shop_order', // WooCommerce orders
'shop_coupon', // WooCommerce coupons
'acf-field-group', // ACF field groups
'acf-field', // ACF fields
'oembed_cache', // WordPress oEmbed cache
'user_request', // WordPress privacy requests
'wp_block', // WordPress reusable blocks
'wp_template', // WordPress templates
'wp_template_part', // WordPress template parts
'wp_navigation', // WordPress navigation
'wp_global_styles', // WordPress global styles
];
// Filter out excluded post types
$post_types = array_filter( $all_post_types, function( $pt ) use ( $excluded_post_types ) {
return ! in_array( $pt->name, $excluded_post_types, true );
} );
echo '
';
echo '' . esc_html__( 'Post Type', 'Ajax-smart-filter' ) . ' ';
echo '';
foreach ( $post_types as $pt ) {
echo 'name, false ) . '>' . esc_html( $pt->labels->singular_name ) . ' ';
}
echo ' ';
echo '' . esc_html__( 'Select the post type to filter. Only user-created post types and default WordPress types are shown.', 'Ajax-smart-filter' ) . '
';
echo ' ';
echo '' . esc_html__( 'Enable Filter', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Enable this filter set', 'Ajax-smart-filter' ) . ' ';
echo '' . esc_html__( 'When disabled, the filter will not be rendered on the frontend.', 'Ajax-smart-filter' ) . '
';
echo ' ';
echo '' . esc_html__( 'Show Filters', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Search', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Category', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Tag', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Sort', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo '' . esc_html__( 'Apply Mode', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Apply on button click', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Auto apply on change', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo '' . esc_html__( 'Taxonomy Display', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Dropdowns', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Tabs', 'Ajax-smart-filter' ) . ' ';
echo '' . esc_html__( 'Applies to Category and Tag controls.', 'Ajax-smart-filter' ) . '
';
echo ' ';
$hide_empty_terms = (bool) get_post_meta( $post->ID, '_asfp_hide_empty_terms', true );
echo '' . esc_html__( 'Hide Empty Terms', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Hide categories and tags that have no posts', 'Ajax-smart-filter' ) . ' ';
echo '' . esc_html__( 'When enabled, only terms with at least one post will be displayed in the filter.', 'Ajax-smart-filter' ) . '
';
echo ' ';
echo '' . esc_html__( 'Filter Design', 'Ajax-smart-filter' ) . ' ';
echo '';
echo '' . esc_html__( 'Choose the visual style for your filter interface.', 'Ajax-smart-filter' ) . '
';
echo ' ';
$primary_color = get_post_meta( $post->ID, '_asfp_primary_color', true );
if ( ! $primary_color ) { $primary_color = '#3b82f6'; }
$secondary_color = get_post_meta( $post->ID, '_asfp_secondary_color', true );
if ( ! $secondary_color ) { $secondary_color = '#2563eb'; }
$accent_color = get_post_meta( $post->ID, '_asfp_accent_color', true );
if ( ! $accent_color ) { $accent_color = '#1d4ed8'; }
echo '' . esc_html__( 'Color Theme', 'Ajax-smart-filter' ) . ' ';
echo '';
echo '' . esc_html__( 'Choose custom colors for your filter interface. Primary is used for active states, Secondary for links, and Accent for hover effects.', 'Ajax-smart-filter' ) . '
';
echo ' ';
echo '' . esc_html__( 'Pagination', 'Ajax-smart-filter' ) . ' ';
echo '' . esc_html__( 'Posts Per Page', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo '' . esc_html__( 'How many posts to load per page.', 'Ajax-smart-filter' ) . '
';
echo '';
echo '' . esc_html__( 'Pagination Display', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'None', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Next/Prev buttons', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Numeric page numbers', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Both (Next/Prev + Numeric)', 'Ajax-smart-filter' ) . ' ';
echo '
';
echo ' ';
echo '' . esc_html__( 'Search Scope', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Title', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Content', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Custom Fields (meta)', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo '' . esc_html__( 'Match Logic', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'AND (all conditions)', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'OR (any condition)', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo '' . esc_html__( 'Custom Filter Bar HTML', 'ajax-smart-filter' ) . ' ';
// Hidden textarea for form submission (name attribute must match save function expectation)
echo '';
// ACE Editor container
echo '
';
echo '' . esc_html__( 'Available Filter Placeholders:', 'ajax-smart-filter' ) . '
';
echo '';
echo '' . esc_html__( 'Search & Sort:', 'ajax-smart-filter' ) . ' ';
echo '';
echo '[filter attribute="search"] - ' . esc_html__( 'Search input with autocomplete', 'ajax-smart-filter' ) . ' ';
echo '[filter attribute="sort"] - ' . esc_html__( 'Sort dropdown (Newest, Oldest, Most Viewed, A-Z, Z-A)', 'ajax-smart-filter' ) . ' ';
echo '[filter attribute="apply-button"] - ' . esc_html__( 'Apply button (for manual filter application)', 'ajax-smart-filter' ) . ' ';
echo ' ';
echo '' . esc_html__( 'Category Filters:', 'ajax-smart-filter' ) . ' ';
echo '';
echo '[filter attribute="category"] - ' . esc_html__( 'Category dropdown select', 'ajax-smart-filter' ) . ' ';
echo '[filter attribute="category-dropdown"] - ' . esc_html__( 'Category dropdown select (alternative)', 'ajax-smart-filter' ) . ' ';
echo '[filter attribute="category-tabs"] - ' . esc_html__( 'Category tabs (clickable buttons)', 'ajax-smart-filter' ) . ' ';
echo ' ';
echo '' . esc_html__( 'Tag Filters:', 'ajax-smart-filter' ) . ' ';
echo '';
echo '[filter attribute="tag"] - ' . esc_html__( 'Tag dropdown select', 'ajax-smart-filter' ) . ' ';
echo '[filter attribute="tag-dropdown"] - ' . esc_html__( 'Tag dropdown select (alternative)', 'ajax-smart-filter' ) . ' ';
echo '[filter attribute="tag-tabs"] - ' . esc_html__( 'Tag tabs (clickable buttons)', 'ajax-smart-filter' ) . ' ';
echo ' ';
echo ' ';
echo ' ';
echo '' . esc_html__( 'Custom Result Item HTML', 'ajax-smart-filter' ) . ' ';
// Hidden textarea for form submission (name attribute must match save function expectation)
echo '';
// ACE Editor container
echo '
';
echo '';
echo '' . esc_html__( 'Available Placeholders:', 'ajax-smart-filter' ) . ' ';
echo esc_html__( 'Basic Post Information:', 'ajax-smart-filter' ) . ' ';
echo '• {{post_id}} - ' . esc_html__( 'Post ID number', 'ajax-smart-filter' ) . ' ';
echo '• {{post_title}} - ' . esc_html__( 'Post title', 'ajax-smart-filter' ) . ' ';
echo '• {{post_link}} - ' . esc_html__( 'Post permalink URL', 'ajax-smart-filter' ) . ' ';
echo '• {{post_slug}} - ' . esc_html__( 'Post slug/permalink slug', 'ajax-smart-filter' ) . ' ';
echo '• {{post_excerpt}} - ' . esc_html__( 'Post excerpt (first 30 words)', 'ajax-smart-filter' ) . ' ';
echo '• {{post_content}} - ' . esc_html__( 'Full post content', 'ajax-smart-filter' ) . ' ';
echo '• {{post_date}} - ' . esc_html__( 'Formatted post date (e.g., "Jan 15, 2024")', 'ajax-smart-filter' ) . ' ';
echo ' ';
echo esc_html__( 'Author Information:', 'ajax-smart-filter' ) . ' ';
echo '• {{post_author}} - ' . esc_html__( 'Author display name', 'ajax-smart-filter' ) . ' ';
echo '• {{post_author_avatar}} - ' . esc_html__( 'Author avatar image HTML', 'ajax-smart-filter' ) . ' ';
echo ' ';
echo esc_html__( 'Taxonomy Information:', 'ajax-smart-filter' ) . ' ';
echo '• {{post_category}} - ' . esc_html__( 'First category name (singular)', 'ajax-smart-filter' ) . ' ';
echo '• {{post_categories}} - ' . esc_html__( 'Comma-separated category names', 'ajax-smart-filter' ) . ' ';
echo '• {{post_tag}} - ' . esc_html__( 'First tag name (singular)', 'ajax-smart-filter' ) . ' ';
echo '• {{post_tags}} - ' . esc_html__( 'Comma-separated tag names', 'ajax-smart-filter' ) . ' ';
echo ' ';
echo esc_html__( 'Featured Image:', 'ajax-smart-filter' ) . ' ';
echo '• {{post_image}} - ' . esc_html__( 'Full featured image HTML tag', 'ajax-smart-filter' ) . ' (<img src="..." alt="...">) ';
echo '• {{post_image_url}} - ' . esc_html__( 'Featured image URL only (without <img> tag)', 'ajax-smart-filter' ) . ' ';
echo '
';
echo ' ';
// Content Display Settings Section
echo '' . esc_html__( 'Content Display Settings', 'Ajax-smart-filter' ) . ' ';
echo '' . esc_html__( 'Show Post Elements', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Show Post Title', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Show Post Excerpt', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Show Post Date', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Show Post Author', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Show Featured Image', 'Ajax-smart-filter' ) . ' ';
echo '' . esc_html__( 'Choose which elements to display in the filtered results.', 'Ajax-smart-filter' ) . '
';
echo ' ';
echo '' . esc_html__( 'Excerpt Length', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'words', 'Ajax-smart-filter' );
echo '' . esc_html__( 'Number of words to display in the post excerpt (default: 25 words).', 'Ajax-smart-filter' ) . '
';
echo ' ';
// Blog Template Design Section
echo '' . esc_html__( 'Blog Template Design', 'Ajax-smart-filter' ) . ' ';
echo '' . esc_html__( 'Template Style', 'Ajax-smart-filter' ) . ' ';
echo '';
echo '' . esc_html__( 'Choose the template design for your blog posts. All templates include zoom-in hover effects on images.', 'Ajax-smart-filter' ) . '
';
echo ' ';
// Query String Settings Section
echo '' . esc_html__( 'URL & State Management', 'Ajax-smart-filter' ) . ' ';
echo '' . esc_html__( 'URL Query String', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Enable URL query string parameters', 'Ajax-smart-filter' ) . ' ';
echo '' . esc_html__( 'When enabled, filter selections will be saved in the URL. This allows users to bookmark filtered results and maintain filter state on page refresh.', 'Ajax-smart-filter' ) . '
';
echo '';
echo '
' . esc_html__( 'Query String Format', 'Ajax-smart-filter' ) . ' ';
echo '
' . esc_html__( 'Use IDs (e.g., ?' . esc_html( $qs_category ) . '=5)', 'Ajax-smart-filter' ) . '';
echo '
' . esc_html__( 'Use Names (e.g., ?' . esc_html( $qs_category ) . '=technology)', 'Ajax-smart-filter' ) . '';
echo '
' . esc_html__( 'Choose whether to use term IDs or names in the URL parameters.', 'Ajax-smart-filter' ) . '
';
echo '
';
echo ' ';
echo '' . esc_html__( 'Custom Parameter Names', 'Ajax-smart-filter' ) . ' ';
echo '';
echo '' . esc_html__( 'Search:', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo '' . esc_html__( 'Category:', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo '' . esc_html__( 'Tag:', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo '' . esc_html__( 'Sort:', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo '' . esc_html__( 'Page:', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo '
';
echo '' . esc_html__( 'Customize the URL parameter names. Default uses "asfp_" prefix (e.g., ?asfp_cat=5&asfp_tag=news).', 'Ajax-smart-filter' ) . '
';
echo ' ';
// Label Settings Section
echo '' . esc_html__( 'Filter Labels & Text', 'Ajax-smart-filter' ) . ' ';
echo '' . esc_html__( 'Global Label', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Show Global Label', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo '' . esc_html__( 'A label that appears before all filter controls.', 'Ajax-smart-filter' ) . '
';
echo ' ';
echo '' . esc_html__( 'Search Label', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Show Label', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo ' ';
echo '' . esc_html__( 'Category Label', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Show Label', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo ' ';
echo '' . esc_html__( 'Tag Label', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Show Label', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo ' ';
echo '' . esc_html__( 'Sort Label', 'Ajax-smart-filter' ) . ' ';
echo ' ' . esc_html__( 'Show Label', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo ' ';
$no_results_message = get_post_meta( $post->ID, '_asfp_no_results_message', true );
if ( empty( $no_results_message ) ) {
$no_results_message = __( 'No posts found. Please try different filters.', 'Ajax-smart-filter' );
}
echo '' . esc_html__( 'No Results Message', 'Ajax-smart-filter' ) . ' ';
echo ' ';
echo '' . esc_html__( 'Message to display when no posts match the selected filters.', 'Ajax-smart-filter' ) . '
';
echo ' ';
echo '
';
}
public function save_meta( $post_id ) {
if ( ! isset( $_POST['apf_meta_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['apf_meta_nonce'] ) ), 'apf_save_meta' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
$post_type = isset( $_POST['apf_post_type'] ) ? sanitize_key( wp_unslash( $_POST['apf_post_type'] ) ) : 'post';
update_post_meta( $post_id, '_asfp_post_type', $post_type );
// Save enable/disable filter setting
update_post_meta( $post_id, '_asfp_enable_filter', isset( $_POST['apf_enable_filter'] ) ? 1 : 0 );
update_post_meta( $post_id, '_asfp_show_search', isset( $_POST['apf_show_search'] ) ? 1 : 0 );
update_post_meta( $post_id, '_asfp_show_tax_category', isset( $_POST['apf_show_tax_category'] ) ? 1 : 0 );
update_post_meta( $post_id, '_asfp_show_tax_post_tag', isset( $_POST['apf_show_tax_post_tag'] ) ? 1 : 0 );
update_post_meta( $post_id, '_asfp_show_sort', isset( $_POST['apf_show_sort'] ) ? 1 : 0 );
$search_scope = [
'title' => ! empty( $_POST['apf_search_scope']['title'] ) ? 1 : 0,
'content' => ! empty( $_POST['apf_search_scope']['content'] ) ? 1 : 0,
'meta' => ! empty( $_POST['apf_search_scope']['meta'] ) ? 1 : 0,
];
update_post_meta( $post_id, '_asfp_search_scope', $search_scope );
$match_logic = isset( $_POST['apf_match_logic'] ) ? sanitize_text_field( wp_unslash( $_POST['apf_match_logic'] ) ) : '';
$match_logic = in_array( $match_logic, [ 'AND', 'OR' ], true ) ? $match_logic : 'AND';
update_post_meta( $post_id, '_asfp_match_logic', $match_logic );
$apply_mode = isset( $_POST['apf_apply_mode'] ) ? sanitize_text_field( wp_unslash( $_POST['apf_apply_mode'] ) ) : '';
$apply_mode = in_array( $apply_mode, [ 'button', 'auto' ], true ) ? $apply_mode : 'button';
update_post_meta( $post_id, '_asfp_apply_mode', $apply_mode );
$tax_display = isset( $_POST['apf_tax_display'] ) ? sanitize_text_field( wp_unslash( $_POST['apf_tax_display'] ) ) : '';
$tax_display = in_array( $tax_display, [ 'dropdown', 'tabs' ], true ) ? $tax_display : 'dropdown';
update_post_meta( $post_id, '_asfp_tax_display', $tax_display );
// Save hide empty terms setting
update_post_meta( $post_id, '_asfp_hide_empty_terms', isset( $_POST['apf_hide_empty_terms'] ) ? 1 : 0 );
$filter_design = isset( $_POST['apf_filter_design'] ) ? sanitize_text_field( wp_unslash( $_POST['apf_filter_design'] ) ) : '';
$filter_design = in_array( $filter_design, [ 'default', 'minimalist', 'sidebar', 'compact' ], true ) ? $filter_design : 'default';
update_post_meta( $post_id, '_asfp_filter_design', $filter_design );
// Save custom colors
$primary_color = isset( $_POST['apf_primary_color'] ) ? sanitize_hex_color( wp_unslash( $_POST['apf_primary_color'] ) ) : '#3b82f6';
$secondary_color = isset( $_POST['apf_secondary_color'] ) ? sanitize_hex_color( wp_unslash( $_POST['apf_secondary_color'] ) ) : '#2563eb';
$accent_color = isset( $_POST['apf_accent_color'] ) ? sanitize_hex_color( wp_unslash( $_POST['apf_accent_color'] ) ) : '#1d4ed8';
update_post_meta( $post_id, '_asfp_primary_color', $primary_color );
update_post_meta( $post_id, '_asfp_secondary_color', $secondary_color );
update_post_meta( $post_id, '_asfp_accent_color', $accent_color );
// Custom sanitization that allows style tags and script tags for template HTML
$allowed_html = wp_kses_allowed_html( 'post' );
// Add style tag support
$allowed_html['style'] = [
'type' => true,
];
// Add script tag support (for custom JavaScript if needed)
$allowed_html['script'] = [
'type' => true,
];
// Allow more attributes on common HTML elements
$allowed_html['div']['class'] = true;
$allowed_html['div']['id'] = true;
$allowed_html['div']['style'] = true;
$allowed_html['span']['class'] = true;
$allowed_html['span']['id'] = true;
$allowed_html['span']['style'] = true;
$allowed_html['a']['class'] = true;
$allowed_html['a']['id'] = true;
$allowed_html['a']['style'] = true;
$allowed_html['img']['class'] = true;
$allowed_html['img']['id'] = true;
$allowed_html['img']['style'] = true;
$template_filter_bar = isset( $_POST['apf_template_filter_bar'] ) ? wp_kses( wp_unslash( $_POST['apf_template_filter_bar'] ), $allowed_html ) : '';
update_post_meta( $post_id, '_asfp_template_filter_bar', $template_filter_bar );
$template_result_item = isset( $_POST['apf_template_result_item'] ) ? wp_kses( wp_unslash( $_POST['apf_template_result_item'] ), $allowed_html ) : '';
update_post_meta( $post_id, '_asfp_template_result_item', $template_result_item );
$per_page = isset( $_POST['apf_per_page'] ) ? max( 1, intval( wp_unslash( $_POST['apf_per_page'] ) ) ) : 6;
update_post_meta( $post_id, '_asfp_per_page', $per_page );
$pagination_display = isset( $_POST['apf_pagination_display'] ) ? sanitize_text_field( wp_unslash( $_POST['apf_pagination_display'] ) ) : '';
$pagination_display = in_array( $pagination_display, [ 'none', 'next_prev', 'numeric', 'both' ], true ) ? $pagination_display : 'numeric';
update_post_meta( $post_id, '_asfp_pagination_display', $pagination_display );
update_post_meta( $post_id, '_asfp_global_label_show', isset( $_POST['apf_global_label_show'] ) ? 1 : 0 );
$global_label_text = isset( $_POST['apf_global_label_text'] ) ? sanitize_text_field( wp_unslash( $_POST['apf_global_label_text'] ) ) : '';
update_post_meta( $post_id, '_asfp_global_label_text', $global_label_text );
update_post_meta( $post_id, '_asfp_label_search_show', isset( $_POST['apf_label_search_show'] ) ? 1 : 0 );
update_post_meta( $post_id, '_asfp_label_category_show', isset( $_POST['apf_label_category_show'] ) ? 1 : 0 );
update_post_meta( $post_id, '_asfp_label_tag_show', isset( $_POST['apf_label_tag_show'] ) ? 1 : 0 );
update_post_meta( $post_id, '_asfp_label_sort_show', isset( $_POST['apf_label_sort_show'] ) ? 1 : 0 );
update_post_meta( $post_id, '_asfp_label_search_text', isset( $_POST['apf_label_search_text'] ) ? sanitize_text_field( wp_unslash( $_POST['apf_label_search_text'] ) ) : '' );
update_post_meta( $post_id, '_asfp_label_category_text', isset( $_POST['apf_label_category_text'] ) ? sanitize_text_field( wp_unslash( $_POST['apf_label_category_text'] ) ) : '' );
update_post_meta( $post_id, '_asfp_label_tag_text', isset( $_POST['apf_label_tag_text'] ) ? sanitize_text_field( wp_unslash( $_POST['apf_label_tag_text'] ) ) : '' );
update_post_meta( $post_id, '_asfp_label_sort_text', isset( $_POST['apf_label_sort_text'] ) ? sanitize_text_field( wp_unslash( $_POST['apf_label_sort_text'] ) ) : '' );
// Save no results message
$no_results_message = isset( $_POST['apf_no_results_message'] ) ? sanitize_text_field( wp_unslash( $_POST['apf_no_results_message'] ) ) : '';
if ( empty( $no_results_message ) ) {
$no_results_message = __( 'No posts found. Please try different filters.', 'Ajax-smart-filter' );
}
update_post_meta( $post_id, '_asfp_no_results_message', $no_results_message );
// Save display settings
update_post_meta( $post_id, '_asfp_show_title', isset( $_POST['apf_show_title'] ) ? 1 : 0 );
update_post_meta( $post_id, '_asfp_show_excerpt', isset( $_POST['apf_show_excerpt'] ) ? 1 : 0 );
update_post_meta( $post_id, '_asfp_show_date', isset( $_POST['apf_show_date'] ) ? 1 : 0 );
update_post_meta( $post_id, '_asfp_show_author', isset( $_POST['apf_show_author'] ) ? 1 : 0 );
update_post_meta( $post_id, '_asfp_show_image', isset( $_POST['apf_show_image'] ) ? 1 : 0 );
// Save excerpt length
$excerpt_length = isset( $_POST['apf_excerpt_length'] ) ? absint( $_POST['apf_excerpt_length'] ) : 25;
if ( $excerpt_length < 5 ) { $excerpt_length = 5; }
if ( $excerpt_length > 100 ) { $excerpt_length = 100; }
update_post_meta( $post_id, '_asfp_excerpt_length', $excerpt_length );
// Save query string settings
update_post_meta( $post_id, '_asfp_enable_querystring', isset( $_POST['apf_enable_querystring'] ) ? 1 : 0 );
$querystring_format = isset( $_POST['apf_querystring_format'] ) ? sanitize_text_field( wp_unslash( $_POST['apf_querystring_format'] ) ) : '';
$querystring_format = in_array( $querystring_format, [ 'id', 'name' ], true ) ? $querystring_format : 'id';
update_post_meta( $post_id, '_asfp_querystring_format', $querystring_format );
// Save custom query string parameter names
$qs_search = isset( $_POST['apf_qs_search'] ) ? sanitize_key( wp_unslash( $_POST['apf_qs_search'] ) ) : 'asfp_search';
update_post_meta( $post_id, '_asfp_qs_search', $qs_search );
$qs_category = isset( $_POST['apf_qs_category'] ) ? sanitize_key( wp_unslash( $_POST['apf_qs_category'] ) ) : 'asfp_cat';
update_post_meta( $post_id, '_asfp_qs_category', $qs_category );
$qs_tag = isset( $_POST['apf_qs_tag'] ) ? sanitize_key( wp_unslash( $_POST['apf_qs_tag'] ) ) : 'asfp_tag';
update_post_meta( $post_id, '_asfp_qs_tag', $qs_tag );
$qs_sort = isset( $_POST['apf_qs_sort'] ) ? sanitize_key( wp_unslash( $_POST['apf_qs_sort'] ) ) : 'asfp_sort';
update_post_meta( $post_id, '_asfp_qs_sort', $qs_sort );
$qs_page = isset( $_POST['apf_qs_page'] ) ? sanitize_key( wp_unslash( $_POST['apf_qs_page'] ) ) : 'asfp_page';
update_post_meta( $post_id, '_asfp_qs_page', $qs_page );
// Save template setting
$blog_template = isset( $_POST['apf_blog_template'] ) ? sanitize_text_field( wp_unslash( $_POST['apf_blog_template'] ) ) : '';
$blog_template = in_array( $blog_template, [ 'template1', 'template2', 'template3', 'template4', 'template5', 'template6', 'template7' ], true ) ? $blog_template : 'template1';
update_post_meta( $post_id, '_asfp_blog_template', $blog_template );
// Track admin settings changes (configuration only, not template selection)
$analytics = new ASFP_Analytics();
$settings_changed = [
'filter_design' => $filter_design,
'apply_mode' => $apply_mode,
'tax_display' => $tax_display,
'pagination_display' => $pagination_display,
'enable_querystring' => isset( $_POST['apf_enable_querystring'] ) ? 1 : 0,
'show_search' => isset( $_POST['apf_show_search'] ) ? 1 : 0,
'show_category' => isset( $_POST['apf_show_tax_category'] ) ? 1 : 0,
'show_tag' => isset( $_POST['apf_show_tax_post_tag'] ) ? 1 : 0,
'show_sort' => isset( $_POST['apf_show_sort'] ) ? 1 : 0,
'show_title' => isset( $_POST['apf_show_title'] ) ? 1 : 0,
'show_excerpt' => isset( $_POST['apf_show_excerpt'] ) ? 1 : 0,
'show_date' => isset( $_POST['apf_show_date'] ) ? 1 : 0,
'show_author' => isset( $_POST['apf_show_author'] ) ? 1 : 0,
'show_image' => isset( $_POST['apf_show_image'] ) ? 1 : 0,
];
$analytics->track_admin_settings_change( $post_id, $settings_changed );
// Track template selection separately (goes to Template Popularity)
$has_custom = ! empty( $template_result_item );
$template_to_track = $has_custom ? 'custom_template' : $blog_template;
$analytics->track_custom_template_usage( $post_id, $template_to_track, $has_custom );
// Track filter design selection (goes to Filter Template Analytics)
// If custom filter bar HTML is used, track as 'custom_filter_design'
$has_custom_filter_bar = ! empty( $template_filter_bar );
$filter_design_to_track = $has_custom_filter_bar ? 'custom_filter_design' : $filter_design;
$analytics->track_filter_design_usage( $post_id, $filter_design_to_track );
}
}