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

Feature/post type enhancements #1689

Merged
merged 11 commits into from
Jun 28, 2021
37 changes: 36 additions & 1 deletion assets/js/autosuggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,26 @@ function init() {
}
};

/**
* Get the searched post types from the search form.
*
* @param {HTMLFormElement} form - form containing the search input field
* @returns {Array} - post types
*/
function getPostTypesFromForm(form) {
const data = new FormData(form);

if (data.has('post_type')) {
return data.getAll('post_type').slice(-1);
}

if (data.has('post_type[]')) {
return data.getAll('post_type[]');
}

return [];
}

/**
* Calls the ajax request, and outputs the results.
* Called by the handleKeyup callback, debounced.
Expand All @@ -554,6 +574,7 @@ function init() {
const fetchResults = async (input) => {
const searchText = input.value;
const placeholder = 'ep_autosuggest_placeholder';
const postTypes = getPostTypesFromForm(input.form);

// retrieves the PHP-genereated query to pass to ElasticSearch
const queryJSON = getJsonQuery();
Expand All @@ -565,7 +586,21 @@ function init() {
if (searchText.length >= 2) {
setFormIsLoading(true, input);

const query = buildSearchQuery(searchText, placeholder, queryJSON);
let query = buildSearchQuery(searchText, placeholder, queryJSON);

if (postTypes.length > 0) {
query = JSON.parse(query);

if (typeof query.post_filter.bool.must !== 'undefined') {
JakePT marked this conversation as resolved.
Show resolved Hide resolved
query.post_filter.bool.must.push({
terms: {
'post_type.raw': postTypes,
},
});
}

query = JSON.stringify(query);
}

// fetch the results
const response = await esSearch(query, searchText);
Expand Down
2 changes: 1 addition & 1 deletion dist/js/autosuggest-script.min.js

Large diffs are not rendered by default.

54 changes: 42 additions & 12 deletions includes/classes/Feature/Facets/Facets.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ public function get_selected() {
'taxonomies' => [],
);

$allowed_args = $this->get_allowed_query_args();

foreach ( $_GET as $key => $value ) { // phpcs:ignore WordPress.Security.NonceVerification
if ( 0 === strpos( $key, 'filter_' ) ) {
$taxonomy = str_replace( 'filter_', '', $key );
Expand All @@ -379,6 +381,10 @@ public function get_selected() {
'terms' => array_fill_keys( array_map( 'trim', explode( ',', trim( $value, ',' ) ) ), true ),
);
}

if ( in_array( $key, $allowed_args, true ) ) {
$filters[ $key ] = $value;
}
}

return $filters;
Expand All @@ -392,36 +398,39 @@ public function get_selected() {
* @return string
*/
public function build_query_url( $filters ) {
$query_string = '';

$s = get_search_query();

if ( ! empty( $s ) ) {
$query_string .= 's=' . $s;
}
$query_param = array();

if ( ! empty( $filters['taxonomies'] ) ) {
$tax_filters = $filters['taxonomies'];

foreach ( $tax_filters as $taxonomy => $filter ) {
if ( ! empty( $filter['terms'] ) ) {
if ( ! empty( $query_string ) ) {
$query_string .= '&';
}
$query_param[ 'filter_' . $taxonomy ] = implode( ',', array_keys( $filter['terms'] ) );
}
}
}

$allowed_args = $this->get_allowed_query_args();

$query_string .= 'filter_' . $taxonomy . '=' . implode( ',', array_keys( $filter['terms'] ) );
if ( ! empty( $filters ) ) {
foreach ( $filters as $filter => $value ) {
if ( ! empty( $value ) && in_array( $filter, $allowed_args, true ) ) {
$query_param[ $filter ] = $value;
}
}
}

$query_string = http_build_query( $query_param );

/**
* Filter facet query string
*
* @hook ep_facet_query_string
* @param {string} $query_string Current query string
* @param {array} $query_param Query parameters
* @return {string} New query string
*/
$query_string = apply_filters( 'ep_facet_query_string', $query_string );
$query_string = apply_filters( 'ep_facet_query_string', $query_string, $query_param );

if ( is_post_type_archive() ) {
$pagination = strpos( $_SERVER['REQUEST_URI'], '/page' );
Expand Down Expand Up @@ -470,4 +479,25 @@ public function output_feature_box_long() {
</p>
<?php
}

/**
* Returns allowed query args for facets
*
* @return mixed|void
* @since 3.4.1
*/
public function get_allowed_query_args() {
$args = array( 's', 'post_type' );

/**
* Filter allowed query args
*
* @hook ep_facet_allowed_query_args
*
* @param {array} $args Post types
*
* @return {array} New post types
*/
return apply_filters( 'ep_facet_allowed_query_args', $args );
}
}