Skip to content

Commit

Permalink
Merge pull request #59 from alleyinteractive/feature/restrict-by-post…
Browse files Browse the repository at this point in the history
…-status

Add ability to configure/restrict indexing by post status
  • Loading branch information
renatonascalves committed Nov 13, 2023
2 parents 5085e09 + 83d83e0 commit 76ec651
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 34 deletions.
25 changes: 25 additions & 0 deletions lib/adapters/class-adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
88 changes: 55 additions & 33 deletions lib/adapters/class-searchpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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_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' ] );
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' ] );
}
}
24 changes: 23 additions & 1 deletion lib/adapters/class-vip-enterprise-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 );
Expand All @@ -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' ] );
Expand Down

0 comments on commit 76ec651

Please sign in to comment.