From 851a40c1c0a560e27dd154e905391dce9aadd572 Mon Sep 17 00:00:00 2001 From: Keith Solomon Date: Thu, 30 Jan 2025 15:00:34 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feature:=20Initial=20commit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 8 +++ assets/script.js | 17 ++++++ assets/style.css | 13 +++++ resource-filter.php | 121 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 assets/script.js create mode 100644 assets/style.css create mode 100644 resource-filter.php diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..d921d50 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "workbench.colorCustomizations": { + "tree.indentGuidesStroke": "#3d92ec", + "activityBar.background": "#193414", + "titleBar.activeBackground": "#23481C", + "titleBar.activeForeground": "#F6FBF5" + } +} diff --git a/assets/script.js b/assets/script.js new file mode 100644 index 0000000..4f71fb0 --- /dev/null +++ b/assets/script.js @@ -0,0 +1,17 @@ +jQuery(document).ready(function ($) { + $('#resource-filter').on('submit', function (e) { + e.preventDefault(); + + let formData = { + action: 'filter_resources', + nonce: resourceFilterAjax.nonce, + search: $('#search').val(), + resource_type: $('#resource_type').val(), + resource_subject: $('#resource_subject').val(), + }; + + $.post(resourceFilterAjax.ajaxurl, formData, function (response) { + $('#resource-results').html(response); + }); + }); +}); diff --git a/assets/style.css b/assets/style.css new file mode 100644 index 0000000..d3446a4 --- /dev/null +++ b/assets/style.css @@ -0,0 +1,13 @@ +#resource-filter { + display: flex; + gap: 10px; + margin-bottom: 20px; +} + +#resource-filter input, +#resource-filter select, +#resource-filter button { + border: 1px solid #ccc; + font-size: 16px; + padding: 8px; +} diff --git a/resource-filter.php b/resource-filter.php new file mode 100644 index 0000000..015e5db --- /dev/null +++ b/resource-filter.php @@ -0,0 +1,121 @@ + admin_url('admin-ajax.php'), + 'nonce' => wp_create_nonce('resource_filter_nonce') + ]); + } + } + + public function renderFilterForm() { + ob_start(); + ?> +
+ + + 'resource_type', 'hide_empty' => true]); + $subjects = get_terms(['taxonomy' => 'resource_subject', 'hide_empty' => true]); + ?> + + + + + + +
+ +
+ loadResources(); ?> +
+ 'resource', + 'posts_per_page' => -1, + 'tax_query' => [], + 's' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '', + ]; + + if (!empty($_POST['resource_type']) || !empty($_POST['resource_subject'])) { + $query_args['tax_query']['relation'] = 'AND'; // Ensures both must match + + if (!empty($_POST['resource_type'])) { + $query_args['tax_query'][] = [ + 'taxonomy' => 'resource_type', + 'field' => 'slug', + 'terms' => sanitize_text_field($_POST['resource_type']) + ]; + } + + if (!empty($_POST['resource_subject'])) { + $query_args['tax_query'][] = [ + 'taxonomy' => 'resource_subject', + 'field' => 'slug', + 'terms' => sanitize_text_field($_POST['resource_subject']) + ]; + } + } + + error_log(print_r($query_args, true)); // Add this to debug query args + + $this->loadResources($query_args); + wp_die(); + } + + private function loadResources($query_args = ['post_type' => 'resource', 'posts_per_page' => -1]) { + $query = new WP_Query($query_args); + + if ($query->have_posts()) { + echo ''; + } else { + echo '

No resources found.

'; + } + + wp_reset_postdata(); + } +} + +new ResourceFilterPlugin();