Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDPT-2079 AJAX search fix & refactor #738

Merged
merged 9 commits into from
Oct 16, 2024
Merged
6 changes: 4 additions & 2 deletions public/app/themes/clarity/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
require_once 'inc/amazon-s3-and-cloudfront-signing.php';
require_once 'inc/amazon-s3-and-cloudfront.php';

require_once 'inc/api/get-posts-rest-api.php';
require_once 'inc/api/campaign-api.php';
require_once 'inc/api/get-campaign-posts-api.php';
require_once 'inc/api/get-news-rest-api.php';
Expand All @@ -76,6 +75,7 @@
require_once 'inc/cookies.php';
require_once 'inc/comments.php';
require_once 'inc/constants.php';
require_once 'inc/content-filter/search-query.php';
require_once 'inc/content-filter/search.php';
require_once 'inc/enqueue.php';
require_once 'inc/form-builder.php';
Expand All @@ -94,7 +94,6 @@
require_once 'inc/menu.php';
require_once 'inc/utilities.php';
require_once 'inc/pagination.php';
require_once 'inc/pagination-newscategory.php';

require_once 'inc/post-types/post.php';
require_once 'inc/post-types/event.php';
Expand All @@ -120,6 +119,9 @@
new MOJ\Intranet\WPOffloadMedia();
new MOJ\Intranet\WPElasticPress();

$search = new MOJ\Intranet\Search();
$search->hooks();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to notate it like this? Can we follow the pattern of include and or do we need other $search variables in the functions.php script?


/// Prevent the Agency Switcher page from being overwritten
add_action('save_post', function ($post_id, $post) {
if ($post->post_name === 'agency-switcher') {
Expand Down
2 changes: 1 addition & 1 deletion public/app/themes/clarity/inc/api/get-news-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function get_news_api($set_cpt = '')
$oAgency = new Agency();
$activeAgency = $oAgency->getCurrentAgency();
$post_per_page = 10;
$post_type = 'news';
$post_type = get_post_type();
$post_id = get_the_ID();
$region_id = get_the_terms($post_id, 'region');
$regional_template = get_post_meta(get_the_ID(), 'dw_regional_template', true);
Expand Down
41 changes: 0 additions & 41 deletions public/app/themes/clarity/inc/api/get-posts-rest-api.php

This file was deleted.

94 changes: 94 additions & 0 deletions public/app/themes/clarity/inc/content-filter/search-query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace MOJ\Intranet;

defined('ABSPATH') || exit;

/**
* QueryProps
*
* This class is responsible for handling the query properties.
* It is used for backend and AJAX requests, to get consistent results.
*
* @package Clarity
*
* @property string $agency - The active agency.
* @property string $post_type - The post type.
* @property int $page - The page number.
* @property int $posts_per_page - The number of posts per page.
* @property ?bool $exclude_current - Exclude the current post.
* @property ?string $keywords_filter - The keywords filter.
* @property ?string $date_filter - The date filter.
* @property ?string $news_category_id - The news category ID.
* @property ?string $region_id - The region ID.
*
* @return void
*/

class SearchQueryArgs
{
public function __construct(
public string $agency_term_id,
public string $post_type,
public int $page,
public int $posts_per_page = 10,
public ?bool $exclude_current = false,
public ?string $keywords_filter = null,
public ?string $date_filter = null,
public ?string $news_category_id = null,
public ?string $region_id = null,
) {}

function get()
{
// Pagination.
$offset = $this->page ? (($this->page - 1) * $this->posts_per_page) : 0;

$args = [
'posts_per_page' => $this->posts_per_page,
'post_type' => $this->post_type,
'post_status' => 'publish',
'offset' => $offset,
...($this->exclude_current ? ['post__not_in' => [get_the_ID()]] : []),
'tax_query' => [
'relation' => 'AND',
[
'taxonomy' => 'agency',
'field' => 'term_id',
'terms' => $this->agency_term_id
],
// If the region is set add its ID to the taxonomy query
...(!empty($this->region_id) ? [
'taxonomy' => 'region',
'field' => 'region_id',
'terms' => $this->region_id,
] : []),
// If the news category is set add its ID unless the query is regional,
// as it will have already been added to the tax query.
...(!empty($this->news_category_id) && empty($this->region_id) ? [
'taxonomy' => 'news_category',
'field' => 'category_id',
'terms' => $this->news_category_id,
] : []),
]
];

// Parse dates from the date filter.
if (!empty($this->date_filter)) {
preg_match('/&after=([^&]*)&before=([^&]*)/', $this->date_filter, $matches);
$args['date_query'] = [
'after' => date('Y-m-d', strtotime($matches[1])),
'before' => date('Y-m-d', strtotime($matches[2])),
'inclusive' => false,
];
}

// If there is a search query, set the orderby to relevance.
if (!empty($this->keywords_filter)) {
$args['orderby'] = 'relevance';
$args['s'] = $this->keywords_filter;
}

return $args;
}
}
Loading
Loading