From 46964266c04c77cc73b2a3cab4bb43e339c11996 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20FOUCRET?= Date: Tue, 14 Sep 2021 17:16:07 +0200 Subject: [PATCH] Do not index attachment by default anymore. (#66) --- Config/Config.php | 2 +- Document/Indexer.php | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Config/Config.php b/Config/Config.php index 7d57010..e8e3430 100644 --- a/Config/Config.php +++ b/Config/Config.php @@ -144,6 +144,6 @@ public function allowedPostTypes() ); } - return $allowedPostTypes; + return \apply_filters('swiftype_allowed_post_types', $allowedPostTypes); } } diff --git a/Document/Indexer.php b/Document/Indexer.php index a2f3789..64026f8 100644 --- a/Document/Indexer.php +++ b/Document/Indexer.php @@ -16,6 +16,11 @@ class Indexer extends AbstractSwiftypeComponent */ private $documentMapper; + /** + * @var string[] + */ + private static $FORBIDDEN_POST_TYPES = ['attachment']; + /** * Constructor. */ @@ -23,9 +28,22 @@ public function __construct() { parent::__construct(); $this->documentMapper = new Mapper(); + \add_filter('swiftype_allowed_post_types', [$this, 'filterAllowedPostType']); \add_action('swiftype_engine_loaded', [$this, 'installHooks']); } + /** + * Filter out forbiden post types fron indexing. + * + * @param string[] $allowedPostTypes + * + * @return string[] + */ + public function filterAllowedPostType($allowedPostTypes) + { + return \array_diff($allowedPostTypes, self::$FORBIDDEN_POST_TYPES); + } + /** * Install hooks only when the engine is created (swiftype_engine_loaded). */ @@ -44,8 +62,8 @@ public function installHooks() \add_action('trashed_post', [$this, 'handleTrashedPost']); - add_action('swiftype_batch_post_index', [$this, 'handlePostBatchIndex']); - add_action('swiftype_batch_post_delete', [$this, 'handlePostBatchDelete']); + \add_action('swiftype_batch_post_index', [$this, 'handlePostBatchIndex']); + \add_action('swiftype_batch_post_delete', [$this, 'handlePostBatchDelete']); } }