-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6386363
Initial commit WIP.
EarthlingDavey a9580bb
AJAX News & Blogs filtering complete
EarthlingDavey a2889bb
Merge branch 'develop' into ajax-search-refactor
EarthlingDavey 03b5b18
AJAX Events filtering complete
EarthlingDavey fcee827
Comment the js.
EarthlingDavey 59245e6
Comment the php
EarthlingDavey 12d2e06
Merge branch 'develop' into ajax-search-refactor
EarthlingDavey c04ddba
Move SearchQueryArgs to own file.
EarthlingDavey a6aa692
Merge branch 'ajax-search-refactor' of https://github.com/ministryofj…
EarthlingDavey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
public/app/themes/clarity/inc/content-filter/search-query.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?