From eee87a6eb8997745a69059829405325e7cdf82a6 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Wed, 6 Sep 2023 14:31:04 -0300 Subject: [PATCH 1/2] Display the query type --- classes/QueryLog.php | 114 +++++++++++++++++++++++++++++++++++++++- classes/QueryOutput.php | 23 ++++++-- 2 files changed, 130 insertions(+), 7 deletions(-) diff --git a/classes/QueryLog.php b/classes/QueryLog.php index 6cdeff4..7b78b0d 100644 --- a/classes/QueryLog.php +++ b/classes/QueryLog.php @@ -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' ] ); } /** @@ -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' ); @@ -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 ] ); ?> 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; + } } diff --git a/classes/QueryOutput.php b/classes/QueryOutput.php index fc5888d..103f242 100644 --- a/classes/QueryOutput.php +++ b/classes/QueryOutput.php @@ -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 = [] ) { ?>
    @@ -79,7 +80,11 @@ public function render_queries() { 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 ); } } ?> @@ -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'] ); @@ -162,6 +168,13 @@ public function render_query( $query, $type = '' ) {
+ +
+ + +
+ +
From 3e97580b3aed4f9be7e5779d3aafc5b2d0f069ef Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Tue, 12 Sep 2023 16:46:25 -0300 Subject: [PATCH 2/2] Change class Co-authored-by: Burhan Nasir --- classes/QueryOutput.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/QueryOutput.php b/classes/QueryOutput.php index 103f242..20e7d92 100644 --- a/classes/QueryOutput.php +++ b/classes/QueryOutput.php @@ -169,7 +169,7 @@ public function render_query( $query, $type = '', $context = '' ) { -
+