Files
WP_Content_Filter/resource-filter.php
Keith Solomon 09222edeba 📄 docs: Add readme
2025-02-04 16:42:37 -06:00

148 lines
4.2 KiB
PHP

<?php
/**
* Plugin Name: Resource Filter
* Description: Adds filtering for the 'resource' post type by 'resource_type' and 'resource_subject'.
* Version: 1.0.0
* Author: Keith Solomon
*/
if (!defined('ABSPATH')) { exit; } // Prevent direct access
require_once plugin_dir_path(__FILE__) . 'includes/template-loader.php';
class ResourceFilterPlugin {
public function __construct() {
add_shortcode('resource_filter', [$this, 'renderFilterForm']);
add_action('wp_enqueue_scripts', [$this, 'enqueueScripts']);
add_action('wp_ajax_filter_resources', [$this, 'filterResources']);
add_action('wp_ajax_nopriv_filter_resources', [$this, 'filterResources']);
}
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')) {
wp_enqueue_style('resource-filter-style', plugins_url('assets/style.css', __FILE__));
wp_enqueue_script('resource-filter-script', plugins_url('assets/script.js', __FILE__), ['jquery'], null, true);
wp_localize_script('resource-filter-script', 'resourceFilterAjax', [
'ajaxurl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('resource_filter_nonce')
]);
}
}
public function renderFilterForm() {
ob_start();
$template = rfGetTemplate('filter-form.php');
if ($template) {
include_once $template;
} else {
echo '<p>Error: Template not found.</p>';
}
$total_resources = wp_count_posts('resource')->publish; // Get the count of published resources
?>
<div id="resource-filter-summary">
<strong>Showing <span id="result-count"><?php echo $total_resources; ?></span> resources</strong>
<p><strong>Filters applied:</strong> <span id="applied-filters">None</span></p>
</div>
<div id="resource-results">
<?php $this->loadResources(); ?>
</div>
<?php
return ob_get_clean();
}
public function filterResources() {
check_ajax_referer('resource_filter_nonce', 'nonce');
$query_args = [
'post_type' => 'resource',
'posts_per_page' => -1,
'tax_query' => [],
's' => isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '',
];
$tax_query = [];
if (!empty($_POST['resource_type'])) {
$query_args['tax_query'][] = [
'taxonomy' => 'resource_type',
'field' => 'slug',
'terms' => array_map('sanitize_text_field', $_POST['resource_type']),
'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;
$template = rfGetTemplate('resource-results.php');
if ($template) {
include_once $template;
} else {
echo '<p>Error: Results template not found.</p>';
}
// 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();
}
public function loadResources() {
$query_args = [
'post_type' => 'resource',
'posts_per_page' => -1
];
$query = new WP_Query($query_args);
$resources = $query->posts;
$template = rfGetTemplate('resource-results.php');
if ($template) {
include_once $template;
} else {
echo '<p>Error: Results template not found.</p>';
}
wp_reset_postdata();
}
}
new ResourceFilterPlugin();