🖥️ wip: Homepage search taxonomy configuration

This commit is contained in:
Keith Solomon
2025-02-13 21:33:00 -06:00
parent c79becb430
commit 4ec92a6014
4 changed files with 82 additions and 19 deletions

View File

@@ -73,7 +73,7 @@ jQuery(document).ready(function ($) {
/**
* Handle form submission for filtering resources.
*/
$('#resource-filter').on('submit', function (e) {
$('#resource-filter','#homepage-filter').on('submit', function (e) {
e.preventDefault();
triggerFiltering(1); // Start at page 1 on new form submission
});

View File

@@ -70,7 +70,9 @@ class ContentFilterPlugin {
$post_types = isset($_POST['post_types']) ? array_map('sanitize_text_field', $_POST['post_types']) : [];
$taxonomies = isset($_POST['taxonomies']) ? array_map('sanitize_text_field', $_POST['taxonomies']) : [];
$posts_per_page = isset($_POST['posts_per_page']) ? intval($_POST['posts_per_page']) : 12;
$homepage_taxonomy = isset($_POST['homepage_taxonomy']) ? sanitize_text_field($_POST['homepage_taxonomy']) : '';
update_option('content_filter_homepage_taxonomy', $homepage_taxonomy);
update_option('content_filter_post_types', $post_types);
update_option('content_filter_taxonomies', $taxonomies);
update_option('content_filter_posts_per_page', $posts_per_page);
@@ -82,6 +84,7 @@ class ContentFilterPlugin {
$post_types = get_option('content_filter_post_types', []);
$taxonomies = get_option('content_filter_taxonomies', []);
$posts_per_page = get_option('content_filter_posts_per_page', 12);
$homepage_taxonomy = get_option('content_filter_homepage_taxonomy', '');
// Get all available post types and taxonomies
$all_post_types = get_post_types(['public' => true], 'objects');
@@ -114,6 +117,18 @@ class ContentFilterPlugin {
<p>Set the number of posts to display per page in the filter results.</p>
<input type="number" name="posts_per_page" value="<?php echo esc_attr($posts_per_page); ?>" min="1" step="1">
<h2>Homepage Taxonomy</h2>
<p>Select one taxonomy to include in the homepage search form.</p>
<select name="homepage_taxonomy">
<option value="">-- Select Taxonomy --</option>
<?php
foreach ($all_taxonomies as $taxonomy) : ?>
<option value="<?php echo esc_attr($taxonomy->name); ?>" <?php selected($taxonomy->name, $homepage_taxonomy); ?>>
<?php echo esc_html($taxonomy->labels->singular_name); ?>
</option>
<?php endforeach; ?>
</select>
<p><input type="submit" class="button-primary" value="Save Settings"></p>
</form>
</div>
@@ -174,7 +189,20 @@ class ContentFilterPlugin {
$resTotal = $query->found_posts; // Default total resource count
// Determine which form template to load
$attTmpl = ($atts['type'] === 'homepage') ? 'filter-homepage.php' : 'filter-form.php';
if ($atts['type'] === 'homepage') {
$homepage_taxonomy = get_option('content_filter_homepage_taxonomy', '');
if (!empty($homepage_taxonomy) && taxonomy_exists($homepage_taxonomy)) {
$GLOBALS['homepage_taxonomy'] = $homepage_taxonomy; // Pass the taxonomy globally for the template
$attTmpl = 'filter-homepage.php';
} else {
echo '<p>Error: No valid taxonomy configured for the homepage filter.</p>';
return ob_get_clean();
}
} else {
$attTmpl = 'filter-form.php';
}
$resForm = rfGetTemplate($attTmpl);
$summary = rfGetTemplate('filter-summary.php');

View File

@@ -0,0 +1,20 @@
<?php if (!defined('ABSPATH')) { exit; } ?>
<form class="flex w-full" id="homepage-filter" action="<?php echo site_url('/browse-resources/'); ?>" method="POST">
<?php
$resource_types = get_terms(['taxonomy' => 'resource_type']);
if (!empty($resource_types)) :
?>
<select class="w-fit" name="resource_type">
<option value="">All Types</option>
<?php foreach ($resource_types as $type) : ?>
<option value="<?php echo esc_attr($type->slug); ?>"><?php echo esc_html($type->name); ?></option>
<?php endforeach; ?>
</select>
<?php endif; ?>
<input class="full-width" type="text" name="search" placeholder="Search resources...">
<button class="btn btn-primary" type="submit">Search</button>
</form>

View File

@@ -1,20 +1,35 @@
<?php if (!defined('ABSPATH')) { exit; } ?>
<?php
if (!defined('ABSPATH')) { exit; } // Prevent direct access
<form class="flex w-full" id="homepage-filter" action="<?php echo site_url('/browse-resources/'); ?>" method="POST">
<?php
$resource_types = get_terms(['taxonomy' => 'resource_type']);
$homepage_taxonomy = isset($GLOBALS['homepage_taxonomy']) ? $GLOBALS['homepage_taxonomy'] : '';
if (!empty($resource_types)) :
?>
<select class="w-fit" name="resource_type">
<option value="">All Types</option>
<?php foreach ($resource_types as $type) : ?>
<option value="<?php echo esc_attr($type->slug); ?>"><?php echo esc_html($type->name); ?></option>
<?php endforeach; ?>
</select>
if (!empty($homepage_taxonomy)) :
$taxonomy_obj = get_taxonomy($homepage_taxonomy);
$terms = get_terms(['taxonomy' => $homepage_taxonomy, 'hide_empty' => true]);
if (!empty($taxonomy_obj) && !empty($terms)) : ?>
<form id="homepage-filter">
<!-- Search Field -->
<div class="search-text">
<input class="full-width" type="text" id="search" name="search" placeholder="Search resources..." value="<?php echo isset($search) ? esc_attr($search) : ''; ?>">
<button type="reset" id="clear-search">&times;</button>
<button type="submit">Search</button>
</div>
<!-- Single Taxonomy Filter -->
<div class="search-tax">
<details class="taxonomy-filter" data-taxonomy="<?php echo esc_attr($homepage_taxonomy); ?>">
<summary><?php echo esc_html($taxonomy_obj->labels->singular_name); ?></summary>
<div class="filter-options">
<?php foreach ($terms as $term) : ?>
<label>
<input type="checkbox" name="<?php echo esc_attr($homepage_taxonomy); ?>[]" value="<?php echo esc_attr($term->slug); ?>">
<?php echo esc_html($term->name); ?>
</label>
<?php endforeach; ?>
</div>
</details>
</div>
</form>
<?php endif; ?>
<input class="full-width" type="text" name="search" placeholder="Search resources...">
<button class="btn btn-primary" type="submit">Search</button>
</form>
<?php endif; ?>