admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('resource_filter_nonce') ]); } } /** * Renders the resource filter form. * * Loads the filter form template and displays a summary of the total resources. * Displays the number of resources and applied filters. * Calls the loadResources method to display the initial list of resources. * * @return string The HTML output of the filter form and resource list. * * @since 1.0.0 */ public function renderFilterForm($atts) { $atts = shortcode_atts([ 'type' => 'default' // Accepts 'default' or 'homepage' ], $atts, 'resource_filter'); ob_start(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { $sort_order = isset($_POST['sort_order']) ? sanitize_text_field($_POST['sort_order']) : 'date_desc'; $query_args = [ 'post_type' => 'resource', 'posts_per_page' => -1, 'tax_query' => [], 's' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '', ]; // Sorting logic switch ($sort_order) { case 'date_asc': $query_args['orderby'] = 'date'; $query_args['order'] = 'ASC'; break; case 'date_desc': $query_args['orderby'] = 'date'; $query_args['order'] = 'DESC'; break; case 'title_asc': $query_args['orderby'] = 'title'; $query_args['order'] = 'ASC'; break; case 'title_desc': $query_args['orderby'] = 'title'; $query_args['order'] = 'DESC'; break; default: $query_args['orderby'] = 'date'; $query_args['order'] = 'DESC'; } $tax_query = []; if (!empty($_POST['resource_type'])) { $resType = is_array($_POST['resource_type']) ? array_map('sanitize_text_field', $_POST['resource_type']) : sanitize_text_field($_POST['resource_type']); $query_args['tax_query'][] = [ 'taxonomy' => 'resource_type', 'field' => 'slug', 'terms' => $resType, 'operator' => 'IN' ]; } if (!empty($_POST['resource_subject'])) { $query_args['tax_query'][] = [ 'taxonomy' => 'resource_subject', 'field' => 'slug', 'terms' => array_map('sanitize_text_field', $_POST['resource_subject']), 'operator' => 'IN' ]; } if (!empty($tax_query)) { $query_args['tax_query'] = [ 'relation' => 'AND', // Both filters must match ...$tax_query ]; } $query = new WP_Query($query_args); } else { $query = new WP_Query([ 'post_type' => 'resource', 'posts_per_page' => -1, ]); } $resTotal = $query->found_posts; // Default total resource count // Determine which form template to load $attTmpl = ($atts['type'] === 'homepage') ? 'filter-homepage.php' : 'filter-form.php'; $resForm = rfGetTemplate($attTmpl); $summary = rfGetTemplate('filter-summary.php'); if ($resForm) { include_once $resForm; } else { echo '

Error: Form template not found.

'; } if ($atts['type'] === 'default') { if ($summary) { include_once $summary; } else { echo '

Error: Summary template not found.

'; } ?>
filterResources(); // Directly render filtered results } else { $this->loadResources(); // Load all resources initially } ?>
'resource', 'posts_per_page' => -1 ]; $query = new WP_Query($query_args); $resources = $query->posts; $resResults = rfGetTemplate('resource-results.php'); if ($resResults) { include_once $resResults; } else { echo '

Error: Results template not found.

'; } wp_reset_postdata(); } /** * AJAX handler for filtering resources. * * Searches for resources based on search term, resource type, and/or resource subject. * Returns a JSON response with the count of resources found and the HTML for the resource results. * * Verifies the nonce and sanitizes the input data. * * @since 1.0.0 */ public function filterResources() { $is_ajax = defined('DOING_AJAX') && DOING_AJAX; if ($is_ajax) { check_ajax_referer('resource_filter_nonce', 'nonce'); } $sort_order = isset($_POST['sort_order']) ? sanitize_text_field($_POST['sort_order']) : 'date_desc'; $query_args = [ 'post_type' => 'resource', 'posts_per_page' => -1, 'tax_query' => [], 's' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '', ]; // Sorting logic switch ($sort_order) { case 'date_asc': $query_args['orderby'] = 'date'; $query_args['order'] = 'ASC'; break; case 'date_desc': $query_args['orderby'] = 'date'; $query_args['order'] = 'DESC'; break; case 'title_asc': $query_args['orderby'] = 'title'; $query_args['order'] = 'ASC'; break; case 'title_desc': $query_args['orderby'] = 'title'; $query_args['order'] = 'DESC'; break; default: $query_args['orderby'] = 'date'; $query_args['order'] = 'DESC'; } $tax_query = []; if (!empty($_POST['resource_type'])) { $resType = is_array($_POST['resource_type']) ? array_map('sanitize_text_field', $_POST['resource_type']) : sanitize_text_field($_POST['resource_type']); $query_args['tax_query'][] = [ 'taxonomy' => 'resource_type', 'field' => 'slug', 'terms' => $resType, 'operator' => 'IN' ]; } if (!empty($_POST['resource_subject'])) { $query_args['tax_query'][] = [ 'taxonomy' => 'resource_subject', 'field' => 'slug', 'terms' => array_map('sanitize_text_field', $_POST['resource_subject']), 'operator' => 'IN' ]; } if (!empty($tax_query)) { $query_args['tax_query'] = [ 'relation' => 'AND', // Both filters must match ...$tax_query ]; } $query = new WP_Query($query_args); ob_start(); $resources = $query->posts; $resResults = rfGetTemplate('resource-results.php'); if ($resResults) { include_once $resResults; } else { echo '

Error: Results template not found.

'; } if ($is_ajax) { // Prepare response JSON $response = [ 'count' => $query->found_posts, 'filters' => [ 'search' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '', 'resource_type' => !empty($_POST['resource_type']) ? sanitize_text_field($_POST['resource_type']) : '', 'resource_subject' => !empty($_POST['resource_subject']) ? sanitize_text_field($_POST['resource_subject']) : '' ], 'html' => ob_get_clean() ]; echo json_encode($response); wp_die(); } else { echo ob_get_clean(); } } } new ResourceFilterPlugin();