From 313d97b034d8dd98dc7bba31e5fb611b65a3f92c Mon Sep 17 00:00:00 2001 From: Juan de Paco Date: Fri, 20 Dec 2024 13:59:38 +0100 Subject: [PATCH] wip --- src/Feature.php | 6 +++++ src/FeatureSupport/Autosuggest.php | 38 ++++++++++++++++++++++++++++++ src/Plugin.php | 5 ++++ 3 files changed, 49 insertions(+) create mode 100644 src/FeatureSupport/Autosuggest.php diff --git a/src/Feature.php b/src/Feature.php index c67756d..4e4d2d8 100644 --- a/src/Feature.php +++ b/src/Feature.php @@ -25,6 +25,9 @@ class Feature extends \ElasticPress\Feature { /** @var FeatureSupport\RelatedPosts */ private $frontendRelatedPosts; + /** @var FeatureSupport\Autosuggest */ + private $frontendAutosuggest; + /** @var Stats\Health */ private $statsHealth; @@ -59,6 +62,7 @@ public function __construct( Sync\CLI $syncCli, FeatureSupport\Search $frontendSearch, FeatureSupport\RelatedPosts $frontendRelatedPosts, + FeatureSupport\Autosuggest $frontendAutosuggest, Stats\Health $statsHealth, Stats\Report $statsReport ) { @@ -69,6 +73,7 @@ public function __construct( $this->syncCli = $syncCli; $this->frontendSearch = $frontendSearch; $this->frontendRelatedPosts = $frontendRelatedPosts; + $this->frontendAutosuggest = $frontendAutosuggest; $this->statsHealth = $statsHealth; $this->statsReport = $statsReport; @@ -87,6 +92,7 @@ public function setup() { $this->syncCli->addHooks(); $this->frontendSearch->addHooks(); $this->frontendRelatedPosts->addHooks(); + $this->frontendAutosuggest->addHooks(); $this->statsHealth->addHooks(); $this->statsReport->addHooks(); } diff --git a/src/FeatureSupport/Autosuggest.php b/src/FeatureSupport/Autosuggest.php new file mode 100644 index 0000000..f6c9c72 --- /dev/null +++ b/src/FeatureSupport/Autosuggest.php @@ -0,0 +1,38 @@ +isFeatureActive() ) { + return; + } + + add_filter( 'ep_autosuggest_options', [ $this, 'useIndexByLanguage' ], Constants::LATE_HOOK_PRIORITY ); + } + + /** + * @param array $options + * + * @return array + */ + public function useIndexByLanguage( $options ) { + $endpointUrl = $options['endpointUrl']; + $defaultIndexName = Indexables::factory()->get( 'post' )->get_index_name(); + $this->indicesManager->setCurrentIndexLanguage( $this->currentLanguage ); + $currentLanguageIndexName = Indexables::factory()->get( 'post' )->get_index_name(); + $this->indicesManager->clearCurrentIndexLanguage(); + $options['endpointUrl'] = str_replace( $defaultIndexName, $currentLanguageIndexName, $endpointUrl ); + return $options; + } + +} diff --git a/src/Plugin.php b/src/Plugin.php index 65136da..efee307 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -100,6 +100,11 @@ public static function init() { $indicesManager, $currentLanguage ), + new FeatureSupport\Autosuggest( + $features, + $indicesManager, + $currentLanguage + ), new Stats\Health( $indexables, $networkActivated,