From e959afb347a4d10c6e7ae68399e27fcf3f62d114 Mon Sep 17 00:00:00 2001
From: mustafauysal
Date: Thu, 5 Mar 2020 15:46:17 +0300
Subject: [PATCH 1/8] Add facets query args support
---
includes/classes/Feature/Facets/Facets.php | 54 +++++++++++++++++-----
1 file changed, 42 insertions(+), 12 deletions(-)
diff --git a/includes/classes/Feature/Facets/Facets.php b/includes/classes/Feature/Facets/Facets.php
index 14aa599547..eaf0403566 100644
--- a/includes/classes/Feature/Facets/Facets.php
+++ b/includes/classes/Feature/Facets/Facets.php
@@ -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 );
@@ -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;
@@ -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' );
@@ -470,4 +479,25 @@ public function output_feature_box_long() {
Date: Thu, 15 Apr 2021 20:13:02 +1000
Subject: [PATCH 2/8] Add post types from search form to autosuggest query.
---
assets/js/autosuggest.js | 37 ++++++++++++++++++++++++++++++-
dist/js/autosuggest-script.min.js | 2 +-
2 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/assets/js/autosuggest.js b/assets/js/autosuggest.js
index ef319f6dbe..47b12c90ee 100644
--- a/assets/js/autosuggest.js
+++ b/assets/js/autosuggest.js
@@ -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.
@@ -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();
@@ -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') {
+ query.post_filter.bool.must.push({
+ terms: {
+ 'post_type.raw': postTypes,
+ },
+ });
+ }
+
+ query = JSON.stringify(query);
+ }
// fetch the results
const response = await esSearch(query, searchText);
diff --git a/dist/js/autosuggest-script.min.js b/dist/js/autosuggest-script.min.js
index c17f8634d8..52a78e8dbb 100644
--- a/dist/js/autosuggest-script.min.js
+++ b/dist/js/autosuggest-script.min.js
@@ -1 +1 @@
-!function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!==typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"===typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=434)}([,,function(t,e,n){(function(e){var n=function(t){return t&&t.Math==Math&&t};t.exports=n("object"==typeof globalThis&&globalThis)||n("object"==typeof window&&window)||n("object"==typeof self&&self)||n("object"==typeof e&&e)||function(){return this}()||Function("return this")()}).call(this,n(52))},function(t,e){t.exports=function(t){try{return!!t()}catch(t){return!0}}},function(t,e,n){var r=n(2),o=n(35),i=n(7),a=n(36),c=n(39),u=n(66),s=o("wks"),f=r.Symbol,l=u?f:f&&f.withoutSetter||a;t.exports=function(t){return i(s,t)&&(c||"string"==typeof s[t])||(c&&i(f,t)?s[t]=f[t]:s[t]=l("Symbol."+t)),s[t]}},function(t,e){t.exports=function(t){return"object"===typeof t?null!==t:"function"===typeof t}},function(t,e,n){var r=n(5);t.exports=function(t){if(!r(t))throw TypeError(String(t)+" is not an object");return t}},function(t,e){var n={}.hasOwnProperty;t.exports=function(t,e){return n.call(t,e)}},function(t,e,n){var r=n(2),o=n(30).f,i=n(10),a=n(21),c=n(23),u=n(70),s=n(53);t.exports=function(t,e){var n,f,l,p,h,d=t.target,v=t.global,y=t.stat;if(n=v?r:y?r[d]||c(d,{}):(r[d]||{}).prototype)for(f in e){if(p=e[f],l=t.noTargetGet?(h=o(n,f))&&h.value:n[f],!s(v?f:d+(y?".":"#")+f,t.forced)&&void 0!==l){if(typeof p===typeof l)continue;u(p,l)}(t.sham||l&&l.sham)&&i(p,"sham",!0),a(n,f,p,t)}}},function(t,e,n){var r=n(3);t.exports=!r((function(){return 7!=Object.defineProperty({},1,{get:function(){return 7}})[1]}))},function(t,e,n){var r=n(9),o=n(11),i=n(20);t.exports=r?function(t,e,n){return o.f(t,e,i(1,n))}:function(t,e,n){return t[e]=n,t}},function(t,e,n){var r=n(9),o=n(40),i=n(6),a=n(26),c=Object.defineProperty;e.f=r?c:function(t,e,n){if(i(t),e=a(e,!0),i(n),o)try{return c(t,e,n)}catch(t){}if("get"in n||"set"in n)throw TypeError("Accessors not supported");return"value"in n&&(t[e]=n.value),t}},function(t,e,n){var r=n(19),o=Math.min;t.exports=function(t){return t>0?o(r(t),9007199254740991):0}},function(t,e,n){var r=n(28),o=n(15);t.exports=function(t){return r(o(t))}},function(t,e){var n={}.toString;t.exports=function(t){return n.call(t).slice(8,-1)}},function(t,e){t.exports=function(t){if(void 0==t)throw TypeError("Can't call method on "+t);return t}},,function(t,e,n){var r=n(15);t.exports=function(t){return Object(r(t))}},function(t,e,n){var r=n(64),o=n(2),i=function(t){return"function"==typeof t?t:void 0};t.exports=function(t,e){return arguments.length<2?i(r[t])||i(o[t]):r[t]&&r[t][e]||o[t]&&o[t][e]}},function(t,e){var n=Math.ceil,r=Math.floor;t.exports=function(t){return isNaN(t=+t)?0:(t>0?r:n)(t)}},function(t,e){t.exports=function(t,e){return{enumerable:!(1&t),configurable:!(2&t),writable:!(4&t),value:e}}},function(t,e,n){var r=n(2),o=n(10),i=n(7),a=n(23),c=n(34),u=n(42),s=u.get,f=u.enforce,l=String(String).split("String");(t.exports=function(t,e,n,c){var u,s=!!c&&!!c.unsafe,p=!!c&&!!c.enumerable,h=!!c&&!!c.noTargetGet;"function"==typeof n&&("string"!=typeof e||i(n,"name")||o(n,"name",e),(u=f(n)).source||(u.source=l.join("string"==typeof e?e:""))),t!==r?(s?!h&&t[e]&&(p=!0):delete t[e],p?t[e]=n:o(t,e,n)):p?t[e]=n:a(e,n)})(Function.prototype,"toString",(function(){return"function"==typeof this&&s(this).source||c(this)}))},function(t,e,n){var r=n(115),o=n(116),i=n(100),a=n(117);t.exports=function(t){return r(t)||o(t)||i(t)||a()},t.exports.default=t.exports,t.exports.__esModule=!0},function(t,e,n){var r=n(2),o=n(10);t.exports=function(t,e){try{o(r,t,e)}catch(n){r[t]=e}return e}},function(t,e,n){var r=n(2),o=n(23),i=r["__core-js_shared__"]||o("__core-js_shared__",{});t.exports=i},function(t,e){t.exports={}},function(t,e,n){var r=n(5);t.exports=function(t,e){if(!r(t))return t;var n,o;if(e&&"function"==typeof(n=t.toString)&&!r(o=n.call(t)))return o;if("function"==typeof(n=t.valueOf)&&!r(o=n.call(t)))return o;if(!e&&"function"==typeof(n=t.toString)&&!r(o=n.call(t)))return o;throw TypeError("Can't convert object to primitive value")}},function(t,e){t.exports=["constructor","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","toLocaleString","toString","valueOf"]},function(t,e,n){var r=n(3),o=n(14),i="".split;t.exports=r((function(){return!Object("z").propertyIsEnumerable(0)}))?function(t){return"String"==o(t)?i.call(t,""):Object(t)}:Object},function(t,e,n){var r,o,i=n(2),a=n(60),c=i.process,u=c&&c.versions,s=u&&u.v8;s?o=(r=s.split("."))[0]+r[1]:a&&(!(r=a.match(/Edge\/(\d+)/))||r[1]>=74)&&(r=a.match(/Chrome\/(\d+)/))&&(o=r[1]),t.exports=o&&+o},function(t,e,n){var r=n(9),o=n(58),i=n(20),a=n(13),c=n(26),u=n(7),s=n(40),f=Object.getOwnPropertyDescriptor;e.f=r?f:function(t,e){if(t=a(t),e=c(e,!0),s)try{return f(t,e)}catch(t){}if(u(t,e))return i(!o.f.call(t,e),t[e])}},function(t,e,n){var r=n(35),o=n(36),i=r("keys");t.exports=function(t){return i[t]||(i[t]=o(t))}},function(t,e,n){var r=n(50),o=n(28),i=n(17),a=n(12),c=n(46),u=[].push,s=function(t){var e=1==t,n=2==t,s=3==t,f=4==t,l=6==t,p=7==t,h=5==t||l;return function(d,v,y,g){for(var m,x,b=i(d),w=o(b),E=r(v,y,3),_=a(w.length),T=0,S=g||c,j=e?S(d,_):n||p?S(d,0):void 0;_>T;T++)if((h||T in w)&&(x=E(m=w[T],T,b),t))if(e)j[T]=x;else if(x)switch(t){case 3:return!0;case 5:return m;case 6:return T;case 2:u.call(j,m)}else switch(t){case 4:return!1;case 7:u.call(j,m)}return l?-1:s||f?f:j}};t.exports={forEach:s(0),map:s(1),filter:s(2),some:s(3),every:s(4),find:s(5),findIndex:s(6),filterOut:s(7)}},,function(t,e,n){var r=n(24),o=Function.toString;"function"!=typeof r.inspectSource&&(r.inspectSource=function(t){return o.call(t)}),t.exports=r.inspectSource},function(t,e,n){var r=n(43),o=n(24);(t.exports=function(t,e){return o[t]||(o[t]=void 0!==e?e:{})})("versions",[]).push({version:"3.9.1",mode:r?"pure":"global",copyright:"© 2021 Denis Pushkarev (zloirock.ru)"})},function(t,e){var n=0,r=Math.random();t.exports=function(t){return"Symbol("+String(void 0===t?"":t)+")_"+(++n+r).toString(36)}},function(t,e){t.exports=function(t){if("function"!=typeof t)throw TypeError(String(t)+" is not a function");return t}},function(t,e,n){var r=n(14);t.exports=Array.isArray||function(t){return"Array"==r(t)}},function(t,e,n){var r=n(54),o=n(29),i=n(3);t.exports=!!Object.getOwnPropertySymbols&&!i((function(){return!Symbol.sham&&(r?38===o:o>37&&o<41)}))},function(t,e,n){var r=n(9),o=n(3),i=n(41);t.exports=!r&&!o((function(){return 7!=Object.defineProperty(i("div"),"a",{get:function(){return 7}}).a}))},function(t,e,n){var r=n(2),o=n(5),i=r.document,a=o(i)&&o(i.createElement);t.exports=function(t){return a?i.createElement(t):{}}},function(t,e,n){var r,o,i,a=n(69),c=n(2),u=n(5),s=n(10),f=n(7),l=n(24),p=n(31),h=n(25),d=c.WeakMap;if(a){var v=l.state||(l.state=new d),y=v.get,g=v.has,m=v.set;r=function(t,e){return e.facade=t,m.call(v,t,e),e},o=function(t){return y.call(v,t)||{}},i=function(t){return g.call(v,t)}}else{var x=p("state");h[x]=!0,r=function(t,e){return e.facade=t,s(t,x,e),e},o=function(t){return f(t,x)?t[x]:{}},i=function(t){return f(t,x)}}t.exports={set:r,get:o,has:i,enforce:function(t){return i(t)?o(t):r(t,{})},getterFor:function(t){return function(e){var n;if(!u(e)||(n=o(e)).type!==t)throw TypeError("Incompatible receiver, "+t+" required");return n}}}},function(t,e){t.exports=!1},function(t,e,n){var r=n(7),o=n(13),i=n(59).indexOf,a=n(25);t.exports=function(t,e){var n,c=o(t),u=0,s=[];for(n in c)!r(a,n)&&r(c,n)&&s.push(n);for(;e.length>u;)r(c,n=e[u++])&&(~i(s,n)||s.push(n));return s}},function(t,e,n){var r=n(3),o=n(4),i=n(29),a=o("species");t.exports=function(t){return i>=51||!r((function(){var e=[];return(e.constructor={})[a]=function(){return{foo:1}},1!==e[t](Boolean).foo}))}},function(t,e,n){var r=n(5),o=n(38),i=n(4)("species");t.exports=function(t,e){var n;return o(t)&&("function"!=typeof(n=t.constructor)||n!==Array&&!o(n.prototype)?r(n)&&null===(n=n[i])&&(n=void 0):n=void 0),new(void 0===n?Array:n)(0===e?0:e)}},function(t,e,n){"use strict";var r=n(71),o=n(83),i=RegExp.prototype.exec,a=String.prototype.replace,c=i,u=function(){var t=/a/,e=/b*/g;return i.call(t,"a"),i.call(e,"a"),0!==t.lastIndex||0!==e.lastIndex}(),s=o.UNSUPPORTED_Y||o.BROKEN_CARET,f=void 0!==/()??/.exec("")[1];(u||f||s)&&(c=function(t){var e,n,o,c,l=this,p=s&&l.sticky,h=r.call(l),d=l.source,v=0,y=t;return p&&(-1===(h=h.replace("y","")).indexOf("g")&&(h+="g"),y=String(t).slice(l.lastIndex),l.lastIndex>0&&(!l.multiline||l.multiline&&"\n"!==t[l.lastIndex-1])&&(d="(?: "+d+")",y=" "+y,v++),n=new RegExp("^(?:"+d+")",h)),f&&(n=new RegExp("^"+d+"$(?!\\s)",h)),u&&(e=l.lastIndex),o=i.call(p?n:l,y),p?o?(o.input=o.input.slice(v),o[0]=o[0].slice(v),o.index=l.lastIndex,l.lastIndex+=o[0].length):l.lastIndex=0:u&&o&&(l.lastIndex=l.global?o.index+o[0].length:e),f&&o&&o.length>1&&a.call(o[0],n,(function(){for(c=1;cf;)if((c=u[f++])!=c)return!0}else for(;s>f;f++)if((t||f in u)&&u[f]===n)return t||f||0;return!t&&-1}};t.exports={includes:a(!0),indexOf:a(!1)}},function(t,e,n){var r=n(18);t.exports=r("navigator","userAgent")||""},function(t,e,n){var r,o=n(6),i=n(80),a=n(27),c=n(25),u=n(75),s=n(41),f=n(31),l=f("IE_PROTO"),p=function(){},h=function(t){return"