__( '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 '
';
}
if ( $show_tax_cat ) {
// Check if category taxonomy is registered for this post type
if ( is_object_in_taxonomy( $post_type, 'category' ) ) {
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 '