From 5f762870ed70ee344378666511b83ba033117a6d Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Thu, 28 Nov 2024 12:33:29 +0000 Subject: [PATCH 1/2] Fix memory leak on indexing ES on local / dev / staging Add a new function `reset_elasticsearch_queries` to handle the logic for resetting the Elasticsearch queries. * Add `use ReflectionProperty` at the top of the file and ensure all `use` statements are in alphabetical order * Hook the new function to the `ep_stop_the_insanity` action using `add_action` --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/humanmade/altis-enhanced-search?shareId=XXXX-XXXX-XXXX-XXXX). --- inc/namespace.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/inc/namespace.php b/inc/namespace.php index 6d0b5b1..cb8a5fa 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -18,6 +18,7 @@ use ElasticPress\Utils; use GuzzleHttp\Psr7\Request; use Psr\Http\Message\RequestInterface; +use ReflectionProperty; use WP_CLI; use WP_Error; use WP_Post; @@ -211,6 +212,9 @@ function load_elasticpress() { // Set up packages feature. Packages\bootstrap(); + + // Hook the reset_elasticsearch_queries function to the ep_stop_the_insanity action + add_action( 'ep_stop_the_insanity', __NAMESPACE__ . '\\reset_elasticsearch_queries' ); } /** @@ -2194,3 +2198,17 @@ function sanitize_query_args( WP_Query $query ) : void { $query->set( $key, array_values( array_filter( (array) $query->get( $key ) ) ) ); } } + +/** + * Reset Elasticsearch queries. + * + * This function resets the Elasticsearch queries by using reflection to access + * and modify the private 'queries' property of the \ElasticPress\Elasticsearch class. + * + * @return void + */ +function reset_elasticsearch_queries() { + $reflection = new ReflectionProperty(Elasticsearch::class, 'queries'); + $reflection->setAccessible(true); + $reflection->setValue(Elasticsearch::factory(), []); +} From 5accc4ab5397fcca97fa76eab464e758974c4bf1 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Wed, 15 Jan 2025 22:07:20 +0000 Subject: [PATCH 2/2] PHPCS --- inc/namespace.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inc/namespace.php b/inc/namespace.php index cb8a5fa..7cb6337 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -213,7 +213,7 @@ function load_elasticpress() { // Set up packages feature. Packages\bootstrap(); - // Hook the reset_elasticsearch_queries function to the ep_stop_the_insanity action + // Hook the reset_elasticsearch_queries function to the ep_stop_the_insanity action. add_action( 'ep_stop_the_insanity', __NAMESPACE__ . '\\reset_elasticsearch_queries' ); } @@ -2208,7 +2208,7 @@ function sanitize_query_args( WP_Query $query ) : void { * @return void */ function reset_elasticsearch_queries() { - $reflection = new ReflectionProperty(Elasticsearch::class, 'queries'); - $reflection->setAccessible(true); - $reflection->setValue(Elasticsearch::factory(), []); + $reflection = new ReflectionProperty( Elasticsearch::class, 'queries' ); + $reflection->setAccessible( true ); + $reflection->setValue( Elasticsearch::factory(), [] ); }