Skip to content

Commit

Permalink
Merge pull request #4019 from rebeccahum/add/filter-ep_disable_query_…
Browse files Browse the repository at this point in the history
…logging

Add new filter to disable query logging: ep_disable_query_logging
  • Loading branch information
felipeelia authored Nov 27, 2024
2 parents 2fee5e2 + f0581f8 commit 023712b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
17 changes: 15 additions & 2 deletions includes/classes/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -1730,12 +1730,25 @@ public function add_elasticpress_version_to_user_agent( $user_agent ) {
* Query logging. Don't log anything to the queries property when
* WP_DEBUG is not enabled. Calls action 'ep_add_query_log' if you
* want to access the query outside of the ElasticPress plugin. This
* runs regardless of debufg settings.
* runs regardless of debug settings.
*
* @param array $query Query to log.
*/
protected function add_query_log( $query ) {
if ( ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ( defined( 'WP_EP_DEBUG' ) && WP_EP_DEBUG ) ) {
$wp_debug = defined( 'WP_DEBUG' ) && WP_DEBUG;
$wp_ep_debug = defined( 'WP_EP_DEBUG' ) && WP_EP_DEBUG;

/**
* Filter query logging. Don't log anything to the queries property when true.
*
* @hook ep_disable_query_logging
* @param {bool} Whether to log to the queries property. Defaults to false.
* @return {bool} New value
* @since 5.2.0
*/
$disable_query_logging = apply_filters( 'ep_disable_query_logging', false );

if ( ! $disable_query_logging && ( $wp_debug || $wp_ep_debug ) ) {
$this->queries[] = $query;
}

Expand Down
31 changes: 31 additions & 0 deletions tests/php/TestElasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,35 @@ public function testGetIndicesComparison() {
];
$this->assertEqualsCanonicalizing( $expected, \ElasticPress\Elasticsearch::factory()->get_indices_comparison() );
}

/**
* Test the ep_disable_query_logging filter
*
* @since 5.2.0
* @group elasticsearch
*/
public function testEpDisableQueryLoggingFilter() {
$elasticsearch = new \ElasticPress\Elasticsearch();

$reflection = new \ReflectionClass( $elasticsearch );
$property = $reflection->getProperty( 'queries' );
$property->setAccessible( true );
$method = $reflection->getMethod( 'add_query_log' );
$method->setAccessible( true );

$example_query = [ 'example_query' ];

add_filter( 'ep_disable_query_logging', '__return_true' );

$method->invokeArgs( $elasticsearch, [ $example_query ] );
$this->assertEmpty( $property->getValue( $elasticsearch ) );

remove_filter( 'ep_disable_query_logging', '__return_true' );

$method->invokeArgs( $elasticsearch, [ $example_query ] );

$queries = $property->getValue( $elasticsearch );
$this->assertCount( 1, $queries );
$this->assertSame( $example_query, $queries[0] );
}
}

0 comments on commit 023712b

Please sign in to comment.