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

Autosuggest: Helper function to delete cached query #2105

Merged
merged 4 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
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
51 changes: 51 additions & 0 deletions includes/classes/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,17 @@ public function index( $args, $assoc_args ) {
update_option( 'ep_last_cli_index', $index_results, false );
}

/**
* Fires after executing a CLI index
*
* @hook ep_wp_cli_after_index
* @param {array} $args CLI command position args
* @param {array} $assoc_args CLI command associative args
*
* @since 3.5.5
*/
do_action( 'ep_wp_cli_after_index', $args, $assoc_args );

WP_CLI::log( WP_CLI::colorize( '%Y' . esc_html__( 'Total time elapsed: ', 'elasticpress' ) . '%N' . $index_time ) );

$this->delete_transient();
Expand Down Expand Up @@ -1269,8 +1280,26 @@ private function delete_transient() {
* @since 3.4
*/
public function clear_index() {
/**
* Fires before the CLI `clear-index` command is executed.
*
* @hook ep_cli_before_clear_index
*
* @since 3.5.5
*/
do_action( 'ep_cli_before_clear_index' );

$this->delete_transient();

/**
* Fires after the CLI `clear-index` command is executed.
*
* @hook ep_cli_after_clear_index
*
* @since 3.5.5
*/
do_action( 'ep_cli_after_clear_index' );

WP_CLI::success( esc_html__( 'Index cleared.', 'elasticpress' ) );
}

Expand Down Expand Up @@ -1448,6 +1477,17 @@ public function stop_indexing( $args, $assoc_args ) {
* @param array $assoc_args Associative CLI args.
*/
public function set_search_algorithm_version( $args, $assoc_args ) {
/**
* Fires before the algorithm version is changed via WP-CLI.
*
* @hook ep_cli_before_set_search_algorithm_version
* @param {array} $args CLI command position args
* @param {array} $assoc_args CLI command associative args
*
* @since 3.5.5
*/
do_action( 'ep_cli_before_set_search_algorithm_version', $args, $assoc_args );

if ( empty( $assoc_args['version'] ) && ! isset( $assoc_args['default'] ) ) {
WP_CLI::error( esc_html__( 'This command expects a version number or the --default flag.', 'elasticpress' ) );
}
Expand All @@ -1466,6 +1506,17 @@ public function set_search_algorithm_version( $args, $assoc_args ) {
}
}

/**
* Fires after the algorithm version is changed via WP-CLI.
*
* @hook ep_cli_after_set_search_algorithm_version
* @param {array} $args CLI command position args
* @param {array} $assoc_args CLI command associative args
*
* @since 3.5.5
*/
do_action( 'ep_cli_after_set_search_algorithm_version', $args, $assoc_args );

WP_CLI::success( esc_html__( 'Done.', 'elasticpress' ) );
}

Expand Down
23 changes: 22 additions & 1 deletion includes/classes/Feature/Autosuggest/Autosuggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ public function setup() {
add_filter( 'ep_pre_dashboard_index', [ $this, 'epio_send_autosuggest_public_request' ] );
add_filter( 'ep_wp_cli_pre_index', [ $this, 'epio_send_autosuggest_public_request' ] );
add_filter( 'debug_information', [ $this, 'epio_autosuggest_health_check_info' ] );

add_action( 'ep_cli_after_set_search_algorithm_version', [ $this, 'delete_cached_query' ] );
add_action( 'ep_wp_cli_after_index', [ $this, 'delete_cached_query' ] );
add_action( 'ep_after_dashboard_index', [ $this, 'delete_cached_query' ] );
add_action( 'ep_after_update_feature', [ $this, 'delete_cached_query' ] );
add_action( 'ep_cli_after_clear_index', [ $this, 'delete_cached_query' ] );
}

/**
Expand Down Expand Up @@ -562,13 +568,28 @@ public function intercept_search_request( $response, $query = [], $args = [], $f
if ( isset( $request->http_response ) && isset( $request->http_response->body ) ) {
$request->http_response->body = '';
}
set_transient( $cache_key, $request, 300 );
set_transient( $cache_key, $request, 5 * MINUTE_IN_SECONDS );
}
}

return $request;
}

/**
* Delete the cached query for autosuggest.
*
* @since 3.5.5
*/
public function delete_cached_query() {
global $wp_object_cache;
if ( wp_using_ext_object_cache() ) {
// Delete the entire group.
unset( $wp_object_cache->cache['ep_autosuggest'] );
} else {
delete_transient( 'ep_autosuggest_query_request_cache' );
}
}

/**
* Tell user whether requirements for feature are met or not.
*
Expand Down
17 changes: 17 additions & 0 deletions includes/classes/Features.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,23 @@ public function update_feature( $slug, $settings, $force = true ) {
$feature->post_activation();
}

/**
* Fires after activating, inactivating, or just updating a feature.
*
* @hook ep_after_update_feature
* @param {string} $feature Feature slug
* @param {array} $settings Feature settings
* @param {array} $data Feature activation data
*
* @since 3.5.5
*/
do_action(
'ep_after_update_feature',
$slug,
$settings,
$data
);

return $data;
}

Expand Down
11 changes: 11 additions & 0 deletions includes/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,13 +662,24 @@ function action_wp_ajax_ep_index() {

$indexable_object->create_network_alias( $indexes );
}

/**
* Fires after executing a reindex via Dashboard
*
* @since 3.5.5
* @hook ep_after_dashboard_index
*/
do_action( 'ep_after_dashboard_index' );
} else {
$index_meta['offset'] = (int) $query['total_objects'];
}
} else {
$index_meta['offset'] = (int) $query['total_objects'];

delete_option( 'ep_index_meta' );

/* This action is documented in this file */
do_action( 'ep_after_dashboard_index' );
}
}
} else {
Expand Down