From 32eaaef81aaece3e7c6b653bc7c71f064a6c6a96 Mon Sep 17 00:00:00 2001 From: Keith Solomon Date: Wed, 5 Feb 2025 13:02:04 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feature:=20Implement=20sorting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/script.js | 24 ++++++++++++--- resource-filter.php | 71 +++++++++++++++++++++++++++++++-------------- 2 files changed, 70 insertions(+), 25 deletions(-) diff --git a/assets/script.js b/assets/script.js index de783cc..e127ae3 100644 --- a/assets/script.js +++ b/assets/script.js @@ -1,7 +1,5 @@ jQuery(document).ready(function ($) { - $('#resource-filter').on('submit', function (e) { - e.preventDefault(); - + function triggerFiltering() { let searchTerm = $('#search').val(); let selectedTypes = $('input[name="resource_type[]"]:checked').map(function () { @@ -29,22 +27,40 @@ jQuery(document).ready(function ($) { }).get(), resource_subject: $('input[name="resource_subject[]"]:checked').map(function () { return this.value; - }).get() + }).get(), + sort_order: $('#sort-order').val() // Pass sorting option }; $.post(resourceFilterAjax.ajaxurl, formData, function (response) { response = JSON.parse(response); $('#resource-results').html(response.html); + if (response.count !== undefined) { $('#result-count').text(response.count); } else { $('#result-count').text($('#result-count').text()); // Keep initial count } + + // Update sort order label + let sortText = $('#sort-order option:selected').text(); + $('#sort-label').text(sortText); }); + } + + // Handle form submission + $('#resource-filter').on('submit', function (e) { + e.preventDefault(); + triggerFiltering(); + }); + + // Handle sorting change + $('#sort-order').on('change', function () { + triggerFiltering(); }); }); +// Close
elements when clicking outside jQuery(document).on('click', function (event) { jQuery('details[open]').each(function () { if (!jQuery(this).is(event.target) && jQuery(this).has(event.target).length === 0) { diff --git a/resource-filter.php b/resource-filter.php index 101f786..1b15ee6 100644 --- a/resource-filter.php +++ b/resource-filter.php @@ -27,15 +27,15 @@ class ResourceFilterPlugin { add_action('wp_ajax_nopriv_filter_resources', [$this, 'filterResources']); } -/** - * Enqueues the necessary scripts and styles for the resource filter. - * - * Checks if the 'resource_filter' shortcode is present on the page before loading. - * Includes a CSS style and a JavaScript file specific to the plugin. - * Localizes the script with AJAX URL and nonce for secure AJAX requests. - * - * @since 1.0.0 - */ + /** + * Enqueues the necessary scripts and styles for the resource filter. + * + * Checks if the 'resource_filter' shortcode is present on the page before loading. + * Includes a CSS style and a JavaScript file specific to the plugin. + * Localizes the script with AJAX URL and nonce for secure AJAX requests. + * + * @since 1.0.0 + */ public function enqueueScripts() { // Load script only if the shortcode is present on the page if (!is_admin() && has_shortcode(get_post_field('post_content', get_the_ID()), 'resource_filter')) { @@ -49,17 +49,17 @@ class ResourceFilterPlugin { } } -/** - * 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 - */ + /** + * 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() { ob_start(); @@ -134,13 +134,42 @@ class ResourceFilterPlugin { public function filterResources() { 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']) : '', + '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'])) {