Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete search template from ElasticPress.io when deactivating Instant Results #2821

Merged
merged 4 commits into from
Aug 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 76 additions & 18 deletions includes/classes/Feature/InstantResults/InstantResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,9 @@ public function requirements_status() {
* Installations using self-hosted Elasticsearch will need to implement an API for
* handling search requests before making the feature available.
*
* @hook ep_instant_results_available
* @param string $available Whether the feature is available.
*
* @since 4.0.0
* @hook ep_instant_results_available
* @param {string} $available Whether the feature is available.
*/
} elseif ( apply_filters( 'ep_instant_results_available', false ) ) {
$status->code = 1;
Expand All @@ -216,7 +215,7 @@ public function requirements_status() {
*/
public function setup() {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] );
add_filter( 'ep_after_update_feature', [ $this, 'epio_save_search_template' ] );
add_filter( 'ep_after_update_feature', [ $this, 'after_update_feature' ], 10, 3 );
add_filter( 'ep_formatted_args', [ $this, 'maybe_apply_aggs_args' ], 10, 3 );
add_filter( 'ep_post_mapping', [ $this, 'add_mapping_properties' ] );
add_filter( 'ep_post_sync_args', [ $this, 'add_post_sync_args' ], 10, 2 );
Expand Down Expand Up @@ -260,11 +259,10 @@ public function enqueue_frontend_assets() {
/**
* The search API endpoint.
*
* @hook ep_instant_results_search_endpoint
* @param string $endpoint Endpoint path.
* @param string $index Elasticsearch index.
*
* @since 4.0.0
* @hook ep_instant_results_search_endpoint
* @param {string} $endpoint Endpoint path.
* @param {string} $index Elasticsearch index.
*/
$api_endpoint = apply_filters( 'ep_instant_results_search_endpoint', "api/v1/search/posts/{$this->index}", $this->index );

Expand Down Expand Up @@ -316,20 +314,44 @@ public function enqueue_admin_assets( $hook_suffix ) {
);
}

/**
* Save or delete the search template on ElasticPress.io based on whether
* the Instant Results feature is being activated or deactivated.
*
* @param string $feature Feature slug
* @param array $settings Feature settings
* @param array $data Feature activation data
*
* @return void
*
* @since 4.3.0
*/
public function after_update_feature( $feature, $settings, $data ) {
if ( $feature !== $this->slug ) {
return;
}

if ( true === $data['active'] ) {
$this->epio_save_search_template();
} else {
$this->epio_delete_search_template();
}
}

/**
* Save the search template to ElasticPress.io.
*
* @return void
*/
public function epio_save_search_template() {
/**
* The search template API endpoint.
*
* @hook ep_instant_results_template_endpoint
* @param string $endpoint Endpoint path.
* @param string $index Elasticsearch index.
* Filters the search template API endpoint.
*
* @since 4.0.0
* @hook ep_instant_results_template_endpoint
* @param {string} $endpoint Endpoint path.
* @param {string} $index Elasticsearch index.
* @returns {string} Search template API endpoint.
*/
$endpoint = apply_filters( 'ep_instant_results_template_endpoint', "api/v1/search/posts/{$this->index}/template/", $this->index );

Expand All @@ -347,13 +369,49 @@ public function epio_save_search_template() {
/**
* Fires after the request is sent the search template API endpoint.
*
* @since 4.0.0
* @hook ep_instant_results_template_saved
* @param string $search_template The search template (JSON).
* @param string $index Index name.
* @param {string} $search_template The search template (JSON).
* @param {string} $index Index name.
*/
do_action( 'ep_instant_results_template_saved', $search_template, $this->index );
}

/**
* Delete the search template from ElasticPress.io.
*
* @return void
*
* @since 4.3.0
*/
public function epio_delete_search_template() {
/**
* Filters the search template API endpoint.
*
* @since 4.0.0
* @hook ep_instant_results_template_endpoint
* @param {string} $endpoint Endpoint path.
* @param {string} $index Elasticsearch index.
* @returns {string} Search template API endpoint.
*/
do_action( 'ep_instant_results_template_saved', $search_template, $this->index );
$endpoint = apply_filters( 'ep_instant_results_template_endpoint', "api/v1/search/posts/{$this->index}/template/", $this->index );

Elasticsearch::factory()->remote_request(
$endpoint,
[
'blocking' => false,
'method' => 'DELETE',
]
);

/**
* Fires after the request is sent the search template API endpoint.
*
* @since 4.3.0
* @hook ep_instant_results_template_deleted
* @param {string} $index Index name.
*/
do_action( 'ep_instant_results_template_deleted', $this->index );
}

/**
Expand Down Expand Up @@ -385,10 +443,10 @@ public function get_search_template() {
* template. This filter supports setting a specific user as the
* current user while the template is generated.
*
* @since 4.1.0
* @hook ep_search_template_user_id
* @param {int} $user_id User ID to use.
* @param {int} $user_id User ID to use.
* @return {int} New user ID to use.
* @since 4.1.0
*/
$template_user_id = apply_filters( 'ep_search_template_user_id', 0 );
$original_user_id = get_current_user_id();
Expand Down