Skip to content

Commit

Permalink
Merge pull request #82 from 10up/feature/issue-81
Browse files Browse the repository at this point in the history
Display the query type
  • Loading branch information
felipeelia authored Sep 13, 2023
2 parents 4f44d0c + 3e97580 commit a1b1f5f
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 7 deletions.
114 changes: 112 additions & 2 deletions classes/QueryLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function setup() {
add_filter( 'pre_update_option_ep_query_log', array( $this, 'json_encode_query_log' ) );
add_filter( 'option_ep_query_log', array( $this, 'json_decode_query_log' ) );
add_filter( 'site_option_ep_query_log', array( $this, 'json_decode_query_log' ) );

add_filter( 'ep_query_request_args', [ $this, 'maybe_add_request_query_type' ], 10, 7 );
add_filter( 'ep_pre_request_args', [ $this, 'maybe_add_request_type' ], 10, 4 );
add_filter( 'ep_pre_request_args', [ $this, 'maybe_add_request_context' ] );
}

/**
Expand All @@ -49,7 +53,6 @@ public function setup() {
* @since 1.3
*/
public function action_admin_init() {

// Save options for multisite
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK && isset( $_POST['ep_enable_logging'] ) ) {
check_admin_referer( 'ep-debug-options' );
Expand Down Expand Up @@ -274,7 +277,7 @@ function( $query ) {

$debug_bar_output = new QueryOutput( $queries );
$debug_bar_output->render_buttons();
$debug_bar_output->render_queries();
$debug_bar_output->render_queries( [ 'display_context' => true ] );
?>
</div>
<?php
Expand Down Expand Up @@ -318,4 +321,111 @@ public function json_encode_query_log( $value ) {
public function json_decode_query_log( $value ) {
return ( is_string( $value ) ) ? json_decode( $value, true ) : $value;
}

/**
* Conditionally add the request type to the request args
*
* @since 3.1.0
* @param array $args Request args
* @param string $path Site URL to retrieve
* @param array $query_args The query args originally passed to WP_Query.
* @param string|null $type Type of request, used for debugging.
* @return array New request args
*/
public function maybe_add_request_type( array $args, string $path, array $query_args, $type ) : array {
if ( ! empty( $args['ep_query_type'] ) ) {
return $args;
}

if ( ! empty( $type ) ) {
$args['ep_query_type'] = $type;

if ( 'get' === $type ) {
$args['ep_query_type'] = esc_html__( 'Raw ES document', 'debug-bar-elasticpress' );
}
}

if ( '_nodes/plugins' === $path ) {
$args['ep_query_type'] = esc_html__( 'Elasticsearch check', 'debug-bar-elasticpress' );
}

return $args;
}

/**
* Conditionally add the context of the query
*
* @param array $args Request args
* @return array
*/
public function maybe_add_request_context( array $args ) : array {
$args['ep_context'] = 'public';

if ( is_admin() ) {
$args['ep_context'] = 'admin';
}

if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
$args['ep_context'] = 'ajax';
}

if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
$args['ep_context'] = 'rest';
}

return $args;
}

/**
* Conditionally add the request type to the request args (for query requests)
*
* @since 3.1.0
* @param array $request_args Request arguments
* @param string $path Request path
* @param string $index Index name
* @param string $type Index type
* @param array $query Prepared Elasticsearch query
* @param array $query_args Query arguments
* @param mixed $query_object Could be WP_Query, WP_User_Query, etc.
* @return array New request arguments
*/
public function maybe_add_request_query_type( array $request_args, string $path, string $index, string $type, array $query, array $query_args, $query_object ) : array {
$request_args['ep_query_type'] = $this->determine_request_query_type( $request_args, $path, $index, $type, $query, $query_args, $query_object );
return $request_args;
}

/**
* Conditionally add the request type to the request args (for query requests)
*
* @since 3.1.0
* @param array $request_args Request arguments
* @param string $path Request path
* @param string $index Index name
* @param string $type Index type
* @param array $query Prepared Elasticsearch query
* @param array $query_args Query arguments
* @param mixed $query_object Could be WP_Query, WP_User_Query, etc.
* @return string Request type
*/
protected function determine_request_query_type( array $request_args, string $path, string $index, string $type, array $query, array $query_args, $query_object ) : string {
if ( $query_object instanceof \WP_Query && $query_object->is_main_query() ) {
return esc_html__( 'Main query', 'debug-bar-elasticpress' );
}

if ( empty( $query['query'] ) && ! empty( $query['aggs'] ) ) {
return esc_html__( 'Possible values for EP filter', 'debug-bar-elasticpress' );
}

$search_term = $query_args['s'] ?? '';
if ( '' !== $search_term ) {
$type = 'Search';
if ( apply_filters( 'ep_autosuggest_query_placeholder', 'ep_autosuggest_placeholder' ) === $search_term ) {
return esc_html__( 'Autosuggest template', 'debug-bar-elasticpress' );
}

return esc_html__( 'Search', 'debug-bar-elasticpress' );
}

return $type;
}
}
23 changes: 18 additions & 5 deletions classes/QueryOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ public function render_buttons() {
* Render the queries
*
* @since 3.0.0
* @param array $args Arguments to adjust the queries list
* @return void
*/
public function render_queries() {
public function render_queries( $args = [] ) {
?>
<div class="ep-queries-debug-container">
<ol class="wpd-queries ep-queries-debug">
Expand All @@ -79,7 +80,11 @@ public function render_queries() {
<?php
} else {
foreach ( $this->queries as $query ) {
$this->render_query( $query );
$type = $query['args']['ep_query_type'] ?? '';
$context = ( ! empty( $args['display_context'] ) && ! empty( $query['args']['ep_context'] ) ) ?
$query['args']['ep_context'] :
'';
$this->render_query( $query, $type, $context );
}
}
?>
Expand All @@ -91,11 +96,12 @@ public function render_queries() {
/**
* Render a query in a list.
*
* @param array $query The query info.
* @param string $type The type of the query.
* @param array $query The query info.
* @param string $type The type of the query.
* @param string $context Context of the query (public, admin, ajax, or rest).
* @return void
*/
public function render_query( $query, $type = '' ) {
public function render_query( $query, $type = '', $context = '' ) {
$error = '';
$query_time = ( ! empty( $query['time_start'] ) && ! empty( $query['time_finish'] ) ) ? $query['time_finish'] - $query['time_start'] : false;
$result = wp_remote_retrieve_body( $query['request'] );
Expand Down Expand Up @@ -162,6 +168,13 @@ public function render_query( $query, $type = '' ) {
</div>
<?php endif; ?>

<?php if ( $context ) : ?>
<div class="ep-query-context">
<strong><?php esc_html_e( 'Context:', 'debug-bar-elasticpress' ); ?></strong>
<?php echo esc_html( $context ); ?>
</div>
<?php endif; ?>

<div class="ep-query-host">
<strong><?php esc_html_e( 'Host:', 'debug-bar-elasticpress' ); ?></strong>
<?php echo esc_html( $query['host'] ); ?>
Expand Down

0 comments on commit a1b1f5f

Please sign in to comment.