From 0fe5055a0b1303d7d7ed94306966ce08bad7b821 Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Thu, 9 Nov 2023 23:13:00 -0300 Subject: [PATCH 1/2] Add ability to configure/restrict indexing by post status --- lib/adapters/class-adapter.php | 25 ++++++ lib/adapters/class-searchpress.php | 88 ++++++++++++-------- lib/adapters/class-vip-enterprise-search.php | 24 +++++- 3 files changed, 103 insertions(+), 34 deletions(-) diff --git a/lib/adapters/class-adapter.php b/lib/adapters/class-adapter.php index a6a1a6d..188cbd5 100644 --- a/lib/adapters/class-adapter.php +++ b/lib/adapters/class-adapter.php @@ -77,6 +77,13 @@ abstract class Adapter implements Hookable { */ private array $restricted_post_types = []; + /** + * An optional array of indexable post statuses to restrict to. + * + * @var string[] + */ + private array $restricted_post_statuses = []; + /** * An optional array of taxonomies to restrict search to. * @@ -386,6 +393,15 @@ protected function get_restricted_post_types(): array { return $this->restricted_post_types; } + /** + * Gets the list of restricted post statuses. + * + * @return string[] The list of restricted post statuses. + */ + protected function get_restricted_post_statuses(): array { + return $this->restricted_post_statuses; + } + /** * Gets the list of restricted taxonomies. * @@ -488,6 +504,15 @@ public function restrict_post_types( array $post_types ): void { $this->restricted_post_types = $post_types; } + /** + * Restricts indexable post statuses to the provided list. + * + * @param string[] $post_statuses The array of indexabled post statuses to restrict to. + */ + public function restrict_post_statuses( array $post_statuses ): void { + $this->restricted_post_statuses = $post_statuses; + } + /** * Restricts searchable taxonomies to the provided list. * diff --git a/lib/adapters/class-searchpress.php b/lib/adapters/class-searchpress.php index 1c95576..815727a 100644 --- a/lib/adapters/class-searchpress.php +++ b/lib/adapters/class-searchpress.php @@ -14,39 +14,6 @@ */ class SearchPress extends Adapter { - /** - * Registers action and/or filter hooks with WordPress. - */ - public function hook(): void { - // Register filter hooks. - add_filter( 'sp_pre_search_results', [ $this, 'extract_aggs_from_results' ], 10, 2 ); - add_filter( 'sp_config_sync_post_types', [ $this, 'apply_sync_post_types' ] ); - add_filter( 'sp_config_mapping', [ $this, 'add_search_suggest_to_mapping' ] ); - add_filter( 'sp_post_pre_index', [ $this, 'add_search_suggest_to_indexed_post_data' ], 10, 2 ); - add_filter( 'sp_search_query_args', [ $this, 'add_aggs_to_es_query' ] ); - add_filter( 'sp_searchable_post_types', [ $this, 'apply_searchable_post_types' ] ); - add_filter( 'sp_post_allowed_meta', [ $this, 'apply_allowed_meta' ] ); - add_filter( 'sp_post_pre_index', [ $this, 'apply_allowed_taxonomies' ] ); - add_filter( 'wp_rest_search_handlers', [ $this, 'filter__wp_rest_search_handlers' ] ); - } - - /** - * Unregisters action and/or filter hooks that were registered in the hook - * method. - */ - public function unhook(): void { - // Unregister filter hooks. - remove_filter( 'sp_pre_search_results', [ $this, 'extract_aggs_from_results' ] ); - remove_filter( 'sp_config_sync_post_types', [ $this, 'apply_sync_post_types' ] ); - remove_filter( 'sp_config_mapping', [ $this, 'add_search_suggest_to_mapping' ] ); - remove_filter( 'sp_post_pre_index', [ $this, 'add_search_suggest_to_indexed_post_data' ] ); - remove_filter( 'sp_search_query_args', [ $this, 'add_aggs_to_es_query' ] ); - remove_filter( 'sp_searchable_post_types', [ $this, 'apply_searchable_post_types' ] ); - remove_filter( 'sp_post_allowed_meta', [ $this, 'apply_allowed_meta' ] ); - remove_filter( 'sp_post_pre_index', [ $this, 'apply_allowed_taxonomies' ] ); - remove_filter( 'wp_rest_search_handlers', [ $this, 'filter__wp_rest_search_handlers' ] ); - } - /** * A callback for the sp_pre_search_results action hook. Parses aggregations * from the raw Elasticsearch response and adds the buckets to the @@ -82,6 +49,26 @@ public function apply_sync_post_types( $post_types ) { return empty( $restricted_post_types ) ? $post_types : $restricted_post_types; } + /** + * A callback for the sp_config_sync_statuses filter hook. Filters the list + * of post statuses that should be indexed in SearchPress based on what was + * configured. If no restrictions were specified, uses the default list. + * + * @param array $post_statuses Indexabled post statuses. + * @return string[] The modified list of post status to index. + */ + public function apply_sync_post_statuses( $post_statuses ): array { + + // Determine whether we should filter the list or not. + $restricted_post_statuses = $this->get_restricted_post_statuses(); + + if ( empty( $restricted_post_statuses ) ) { + return $post_statuses; + } + + return array_unique( array_merge( $post_statuses, $restricted_post_statuses ) ); + } + /** * A callback for the sp_config_mapping filter. Adds the 'search_suggest' * field to the mapping if search suggestions are enabled. @@ -487,4 +474,39 @@ public function get_field_map(): array { 'term_tt_id' => 'terms.%s.term_taxonomy_id', ]; } + + /** + * Registers action and/or filter hooks with WordPress. + */ + public function hook(): void { + // Register filter hooks. + add_filter( 'sp_pre_search_results', [ $this, 'extract_aggs_from_results' ], 10, 2 ); + add_filter( 'sp_config_sync_post_types', [ $this, 'apply_sync_post_types' ] ); + add_filter( 'sp_config_sync_statuses', [ $this, 'apply_sync_post_statuses' ] ); + add_filter( 'sp_config_mapping', [ $this, 'add_search_suggest_to_mapping' ] ); + add_filter( 'sp_post_pre_index', [ $this, 'add_search_suggest_to_indexed_post_data' ], 10, 2 ); + add_filter( 'sp_search_query_args', [ $this, 'add_aggs_to_es_query' ] ); + add_filter( 'sp_searchable_post_types', [ $this, 'apply_searchable_post_types' ] ); + add_filter( 'sp_post_allowed_meta', [ $this, 'apply_allowed_meta' ] ); + add_filter( 'sp_post_pre_index', [ $this, 'apply_allowed_taxonomies' ] ); + add_filter( 'wp_rest_search_handlers', [ $this, 'filter__wp_rest_search_handlers' ] ); + } + + /** + * Unregisters action and/or filter hooks that were registered in the hook + * method. + */ + public function unhook(): void { + // Unregister filter hooks. + remove_filter( 'sp_pre_search_results', [ $this, 'extract_aggs_from_results' ] ); + remove_filter( 'sp_config_sync_post_types', [ $this, 'apply_sync_post_statuses' ] ); + remove_filter( 'sp_config_sync_statuses', [ $this, 'apply_sync_post_types' ] ); + remove_filter( 'sp_config_mapping', [ $this, 'add_search_suggest_to_mapping' ] ); + remove_filter( 'sp_post_pre_index', [ $this, 'add_search_suggest_to_indexed_post_data' ] ); + remove_filter( 'sp_search_query_args', [ $this, 'add_aggs_to_es_query' ] ); + remove_filter( 'sp_searchable_post_types', [ $this, 'apply_searchable_post_types' ] ); + remove_filter( 'sp_post_allowed_meta', [ $this, 'apply_allowed_meta' ] ); + remove_filter( 'sp_post_pre_index', [ $this, 'apply_allowed_taxonomies' ] ); + remove_filter( 'wp_rest_search_handlers', [ $this, 'filter__wp_rest_search_handlers' ] ); + } } diff --git a/lib/adapters/class-vip-enterprise-search.php b/lib/adapters/class-vip-enterprise-search.php index 71b4a83..aac2d68 100644 --- a/lib/adapters/class-vip-enterprise-search.php +++ b/lib/adapters/class-vip-enterprise-search.php @@ -80,6 +80,26 @@ public function filter__ep_indexable_post_types( $post_types ) { return $filtered_post_types; } + /** + * A callback for the ep_indexable_post_status filter hook. Filters the list + * of post statuses that should be indexed in ElasticPress based on what was + * configured. If no restrictions were specified, uses the default list. + * + * @param array $post_statuses Indexabled post statuses. + * @return string[] The modified list of post statuses to index. + */ + public function filter__ep_indexable_post_statuses( $post_statuses ): array { + + // Determine whether we should filter the list or not. + $restricted_post_statuses = $this->get_restricted_post_statuses(); + + if ( empty( $restricted_post_statuses ) ) { + return $post_statuses; + } + + return array_unique( array_merge( $post_statuses, $restricted_post_statuses ) ); + } + /** * A callback for the ep_post_mapping filter. Adds the 'search_suggest' * field to the mapping if search suggestions are enabled. @@ -251,7 +271,7 @@ public function filter__ep_searchable_post_types( $post_types ) { /** * A callback for the vip_search_post_meta_allow_list filter hook. - * Filters the list of post meta fields that should be indexed in + * Filters the list of post meta fields that should be indexed in * ElasticPress based on what was configured. * * @param array $post_meta A list of meta keys. @@ -547,6 +567,7 @@ public function hook(): void { // Register filter hooks. add_filter( 'ep_elasticpress_enabled', [ $this, 'filter__ep_elasticpress_enabled' ], 10, 2 ); + add_filter( 'ep_indexable_post_status', [ $this, 'filter__ep_indexable_post_statuses' ] ); add_filter( 'ep_indexable_post_types', [ $this, 'filter__ep_indexable_post_types' ] ); add_filter( 'ep_post_mapping', [ $this, 'filter__ep_post_mapping' ] ); add_filter( 'ep_post_sync_args_post_prepare_meta', [ $this, 'filter__ep_post_sync_args_post_prepare_meta' ], 10, 2 ); @@ -568,6 +589,7 @@ public function unhook(): void { // Unregister filter hooks. remove_filter( 'ep_elasticpress_enabled', [ $this, 'filter__ep_elasticpress_enabled' ] ); + remove_filter( 'ep_indexable_post_status', [ $this, 'filter__ep_indexable_post_statuses' ] ); remove_filter( 'ep_indexable_post_types', [ $this, 'filter__ep_indexable_post_types' ] ); remove_filter( 'ep_post_mapping', [ $this, 'filter__ep_post_mapping' ] ); remove_filter( 'ep_post_sync_args_post_prepare_meta', [ $this, 'filter__ep_post_sync_args_post_prepare_meta' ] ); From 83d83e0826d2ee653b4822976d3a6e57b1eb8d68 Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Thu, 9 Nov 2023 23:20:27 -0300 Subject: [PATCH 2/2] Fix typo --- lib/adapters/class-searchpress.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/adapters/class-searchpress.php b/lib/adapters/class-searchpress.php index 815727a..ec92084 100644 --- a/lib/adapters/class-searchpress.php +++ b/lib/adapters/class-searchpress.php @@ -499,8 +499,8 @@ public function hook(): void { public function unhook(): void { // Unregister filter hooks. remove_filter( 'sp_pre_search_results', [ $this, 'extract_aggs_from_results' ] ); - remove_filter( 'sp_config_sync_post_types', [ $this, 'apply_sync_post_statuses' ] ); - remove_filter( 'sp_config_sync_statuses', [ $this, 'apply_sync_post_types' ] ); + remove_filter( 'sp_config_sync_post_types', [ $this, 'apply_sync_post_types' ] ); + remove_filter( 'sp_config_sync_statuses', [ $this, 'apply_sync_post_statuses' ] ); remove_filter( 'sp_config_mapping', [ $this, 'add_search_suggest_to_mapping' ] ); remove_filter( 'sp_post_pre_index', [ $this, 'add_search_suggest_to_indexed_post_data' ] ); remove_filter( 'sp_search_query_args', [ $this, 'add_aggs_to_es_query' ] );