From 8f44d8385a8e47a295fbe962bb8a266c19ab627f Mon Sep 17 00:00:00 2001 From: Keith Solomon Date: Tue, 4 Feb 2025 16:22:55 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feature:=20Filters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/script.js | 41 ++++++++++++++++++++++------- assets/style.css | 54 ++++++++++++++++++++++++++++++++++----- resource-filter.php | 14 +++++----- templates/filter-form.php | 48 +++++++++++++++++++++++----------- 4 files changed, 120 insertions(+), 37 deletions(-) diff --git a/assets/script.js b/assets/script.js index 310788c..ea6538e 100644 --- a/assets/script.js +++ b/assets/script.js @@ -2,12 +2,34 @@ jQuery(document).ready(function ($) { $('#resource-filter').on('submit', function (e) { e.preventDefault(); + let searchTerm = $('#search').val(); + + let selectedTypes = $('input[name="resource_type[]"]:checked').map(function () { + return $(this).closest('label').text().trim(); + }).get(); + + let selectedSubjects = $('input[name="resource_subject[]"]:checked').map(function () { + return $(this).closest('label').text().trim(); + }).get(); + + let appliedFilters = []; + + if (searchTerm) appliedFilters.push(`Search: "${searchTerm}"`); + if (selectedTypes.length > 0) appliedFilters.push(`Type: ${selectedTypes.join(', ')}`); + if (selectedSubjects.length > 0) appliedFilters.push(`Subject: ${selectedSubjects.join(', ')}`); + + $('#applied-filters').html(appliedFilters.length ? appliedFilters.join(' | ') : 'None'); + let formData = { action: 'filter_resources', nonce: resourceFilterAjax.nonce, search: $('#search').val(), - resource_type: $('#resource_type').val(), - resource_subject: $('#resource_subject').val(), + resource_type: $('input[name="resource_type[]"]:checked').map(function () { + return this.value; + }).get(), + resource_subject: $('input[name="resource_subject[]"]:checked').map(function () { + return this.value; + }).get() }; $.post(resourceFilterAjax.ajaxurl, formData, function (response) { @@ -15,13 +37,14 @@ jQuery(document).ready(function ($) { $('#resource-results').html(response.html); $('#result-count').text(response.count); - - let filters = []; - if (response.filters.search) filters.push('Search: "' + response.filters.search + '"'); - if (response.filters.resource_type) filters.push('Type: ' + $('#resource_type option:selected').text()); - if (response.filters.resource_subject) filters.push('Subject: ' + $('#resource_subject option:selected').text()); - - $('#applied-filters').text(filters.length ? filters.join(', ') : 'None'); }); }); }); + +jQuery(document).on('click', function (event) { + jQuery('details[open]').each(function () { + if (!jQuery(this).is(event.target) && jQuery(this).has(event.target).length === 0) { + jQuery(this).removeAttr('open'); + } + }); +}); diff --git a/assets/style.css b/assets/style.css index fc2212f..cd4b71d 100644 --- a/assets/style.css +++ b/assets/style.css @@ -1,14 +1,56 @@ -#resource-filter { +.search-text { display: flex; - gap: 1rem; + gap: 0; margin-bottom: 1.25rem; - input, - select, + .full-width { + border: 1px solid #ccc; + border-right: none; + font-size: 1rem; + padding: .5rem; + width: 100%; + } + button { border: 1px solid #ccc; font-size: 1rem; - padding: .5rem; + padding: .5rem 1rem; + } +} + +.search-tax { + align-items: flex-start; /* Ensures each
aligns to the top */ + display: flex; + gap: 1rem; + margin-bottom: 1.25rem; + position: relative; + + details { + border: 1px solid #ccc; + border-radius: 5px; + flex: 1; /* Ensures equal width but allows independent height */ + margin-bottom: 10px; + max-height: 4rem; + overflow: hidden; + padding: 8px; + transition: max-height 0.8s ease; + width: 100%; + } + + details[open] { + max-height: 65rem; /* Adjust based on expected content */ + } + + summary { + cursor: pointer; + font-weight: bold; + } + + .filter-options { padding-top: .5rem; } + + .filter-options label { + display: block; + margin-bottom: 5px; } } @@ -22,8 +64,8 @@ #resource-results { display: grid; - grid-template-columns: repeat(1, minmax(0, 1fr)); gap: 1rem; + grid-template-columns: repeat(1, minmax(0, 1fr)); } diff --git a/resource-filter.php b/resource-filter.php index 0cb8a14..d4f20d1 100644 --- a/resource-filter.php +++ b/resource-filter.php @@ -47,7 +47,7 @@ class ResourceFilterPlugin {
Showing resources -

Filters applied: None

+

Filters applied: None

@@ -71,19 +71,19 @@ class ResourceFilterPlugin { $tax_query = []; if (!empty($_POST['resource_type'])) { - $tax_query[] = [ + $query_args['tax_query'][] = [ 'taxonomy' => 'resource_type', - 'field' => 'slug', - 'terms' => [sanitize_text_field($_POST['resource_type'])], // Ensure it's an array + 'field' => 'slug', + 'terms' => array_map('sanitize_text_field', $_POST['resource_type']), 'operator' => 'IN' ]; } if (!empty($_POST['resource_subject'])) { - $tax_query[] = [ + $query_args['tax_query'][] = [ 'taxonomy' => 'resource_subject', - 'field' => 'slug', - 'terms' => [sanitize_text_field($_POST['resource_subject'])], + 'field' => 'slug', + 'terms' => array_map('sanitize_text_field', $_POST['resource_subject']), 'operator' => 'IN' ]; } diff --git a/templates/filter-form.php b/templates/filter-form.php index 1e5efd5..d985a93 100644 --- a/templates/filter-form.php +++ b/templates/filter-form.php @@ -1,26 +1,44 @@ 'resource_type', 'hide_empty' => true]); +$resource_types = get_terms(['taxonomy' => 'resource_type', 'hide_empty' => true]); $resource_subjects = get_terms(['taxonomy' => 'resource_subject', 'hide_empty' => true]); ?>
- + +
+ + +
- +
+ +
+ Resource Type - +
+ + + +
+
- + +
+ Resource Subject + +
+ + + +
+
+