diff --git a/assets/css/main.css b/assets/css/main.css index 1d27389..ea39970 100755 --- a/assets/css/main.css +++ b/assets/css/main.css @@ -5,6 +5,10 @@ padding: 0.5em 10px; } +.ep-queries-debug pre { + padding: 10px; +} + .ep-queries-debug .dashicons { cursor: pointer; } @@ -69,17 +73,14 @@ clear: left; } -#query-monitor-main .qm-debug-bar { - margin: 20px; -} - /* stylelint-disable selector-id-pattern */ /* The giant list of selectors here is needed to * override QM's important rules */ #debug-menu-target-EP_Debug_Bar_ElasticPress .button, -#debug-menu-target-EP_Debug_Bar_ElasticPress a.button { +#debug-menu-target-EP_Debug_Bar_ElasticPress a.button, +#qm-elasticpress .button { background: #f6f7f7 !important; border: 1px solid #2271b1 !important; border-radius: 3px !important; @@ -97,7 +98,7 @@ } #debug-menu-target-EP_Debug_Bar_ElasticPress .button:hover, -#query-monitor-main #qm-debug_bar_ep_debug_bar_elasticpress .button:hover { +#query-monitor-main #qm-elasticpress .button:hover { background: #f0f0f1; border-color: #0a4b78; color: #0a4b78 !important; @@ -105,29 +106,47 @@ } #debug-menu-target-EP_Debug_Bar_ElasticPress .button:active, -#query-monitor-main #qm-debug_bar_ep_debug_bar_elasticpress .button:hover { +#query-monitor-main #qm-elasticpress .button:hover { background: #f6f7f7 !important; border-color: #8c8f94 !important; box-shadow: none; } -#debug-menu-target-EP_Debug_Bar_ElasticPress .button:focus { +#debug-menu-target-EP_Debug_Bar_ElasticPress .button:focus, +#query-monitor-main #qm-elasticpress .button:focus { outline: none; } /* Overriding some Debug Bar "important" rules */ #debug-menu-target-EP_Debug_Bar_ElasticPress .button-primary, #debug-menu-target-EP_Debug_Bar_ElasticPress a.button-primary, -#debug-menu-target-EP_Debug_Bar_ElasticPress button.button-primary { +#debug-menu-target-EP_Debug_Bar_ElasticPress button.button-primary, +#query-monitor-main #qm-elasticpress .button-primary { background: #007cba !important; border: 1px solid #007cba !important; color: #fff !important; } #debug-menu-target-EP_Debug_Bar_ElasticPress a.button-primary:hover, -#debug-menu-target-EP_Debug_Bar_ElasticPress a.button-primary:focus { +#debug-menu-target-EP_Debug_Bar_ElasticPress a.button-primary:focus, +#query-monitor-main #qm-elasticpress a.button-primary:hover, +#query-monitor-main #qm-elasticpress a.button-primary:focus { + background: #007cba !important; color: #fff !important; text-decoration: none !important; } +#query-monitor-main #qm-elasticpress h3, +#query-monitor-main #qm-elasticpress p { + margin: 0 !important; + padding: 0 !important; + text-align: center !important; +} + +#query-monitor-main #qm-elasticpress li { + border-top: 1px solid var(--qm-panel-separator) !important; + line-height: 20px !important; + padding: 20px 0 !important; +} + /* stylelint-enable selector-id-pattern */ diff --git a/classes/CommonPanel.php b/classes/CommonPanel.php new file mode 100644 index 0000000..bdfd655 --- /dev/null +++ b/classes/CommonPanel.php @@ -0,0 +1,70 @@ +title = esc_html__( 'ElasticPress', 'debug-bar-elasticpress' ); + } + + /** + * Return the panel title + * + * @return string + */ + public function get_title() : string { + return $this->title; + } + /** + * Enqueue scripts for front end and admin + */ + public function enqueue_scripts_styles() { + if ( ! is_user_logged_in() ) { + return; + } + + wp_enqueue_script( 'debug-bar-elasticpress', EP_DEBUG_URL . 'assets/js/main.js', array( 'wp-dom-ready', 'clipboard' ), EP_DEBUG_VERSION, true ); + wp_enqueue_style( 'debug-bar-elasticpress', EP_DEBUG_URL . 'assets/css/main.css', array(), EP_DEBUG_VERSION ); + } + + /** + * Show the contents of the panel + */ + public function render() { + $queries = \ElasticPress\Elasticsearch::factory()->get_query_log(); + + if ( function_exists( '\ElasticPress\Utils\is_indexing' ) && \ElasticPress\Utils\is_indexing() ) { + ?> +
+ render_buttons(); + $debug_bar_output->render_additional_buttons(); + $debug_bar_output->render_queries(); + } +} diff --git a/classes/EP_Debug_Bar_ElasticPress.php b/classes/EP_Debug_Bar_ElasticPress.php index 61d3e5c..ed0ab2b 100755 --- a/classes/EP_Debug_Bar_ElasticPress.php +++ b/classes/EP_Debug_Bar_ElasticPress.php @@ -24,26 +24,28 @@ class EP_Debug_Bar_ElasticPress extends \Debug_Bar_Panel { */ public $title; + /** + * Common panel instance + * + * @var \DebugBarElasticPress\CommonPanel + */ + protected $common_panel; + /** * Initial debug bar stuff */ public function init() { $this->title( esc_html__( 'ElasticPress', 'debug-bar-elasticpress' ) ); - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts_styles' ) ); - add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts_styles' ) ); + $this->common_panel = new \DebugBarElasticPress\CommonPanel(); + $this->common_panel->enqueue_scripts_styles(); } /** * Enqueue scripts for front end and admin */ public function enqueue_scripts_styles() { - if ( ! is_user_logged_in() ) { - return; - } - - wp_enqueue_script( 'debug-bar-elasticpress', EP_DEBUG_URL . 'assets/js/main.js', array( 'wp-dom-ready', 'clipboard' ), EP_DEBUG_VERSION, true ); - wp_enqueue_style( 'debug-bar-elasticpress', EP_DEBUG_URL . 'assets/css/main.css', array(), EP_DEBUG_VERSION ); + _deprecated_function( __METHOD__, '3.1.0', 'DebugBarElasticPress\EP_Panel::enqueue_scripts_styles()' ); } /** @@ -85,16 +87,7 @@ public function render() { ?> - - - - render_buttons(); - $debug_bar_output->render_additional_buttons(); - $debug_bar_output->render_queries(); + $this->common_panel->render(); } } diff --git a/classes/QueryLog.php b/classes/QueryLog.php index 4dd32c6..a1a4868 100644 --- a/classes/QueryLog.php +++ b/classes/QueryLog.php @@ -34,6 +34,7 @@ public function setup() { add_action( 'admin_init', array( $this, 'action_admin_init' ) ); add_action( 'admin_init', array( $this, 'maybe_clear_log' ) ); add_action( 'init', array( $this, 'maybe_disable' ) ); + add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); /** * Handle query storage as JSON strings. @@ -109,11 +110,18 @@ public function maybe_clear_log() { /** * Add options page * - * @since 1.3 + * @since 1.3 * @return void */ public function action_admin_menu() { - add_submenu_page( 'elasticpress', esc_html__( 'Query Log', 'debug-bar-elasticpress' ), esc_html__( 'Query Log', 'debug-bar-elasticpress' ), 'manage_options', 'ep-query-log', array( $this, 'screen_options' ) ); + add_submenu_page( + 'elasticpress', + esc_html__( 'Query Log', 'debug-bar-elasticpress' ), + esc_html__( 'Query Log', 'debug-bar-elasticpress' ), + 'manage_options', + 'ep-query-log', + array( $this, 'screen_options' ) + ); } /** @@ -452,6 +460,21 @@ public function maybe_add_request_query_type( array $request_args, string $path, return $request_args; } + /** + * Enqueue assets if we are in the correct admin screen + * + * @since 3.1.0 + */ + public function admin_enqueue_scripts() { + $current_screen = get_current_screen(); + + if ( ! isset( $current_screen->id ) || 'elasticpress_page_ep-query-log' !== $current_screen->id ) { + return; + } + + ( new CommonPanel() )->enqueue_scripts_styles(); + } + /** * Conditionally add the request type to the request args (for query requests) * diff --git a/classes/QueryMonitorCollector.php b/classes/QueryMonitorCollector.php new file mode 100644 index 0000000..0e177ce --- /dev/null +++ b/classes/QueryMonitorCollector.php @@ -0,0 +1,23 @@ +common_panel = new CommonPanel(); + + add_filter( 'qm/output/menus', [ $this, 'admin_menu' ] ); + } + + /** + * Panel title + * + * @return string + */ + public function name() : string { + return $this->common_panel->get_title(); + } + + /** + * Echoes the output + * + * @return void + */ + public function output() { + ?> + + get_query_log(); + $total_query_time = 0; + + foreach ( $queries as $query ) { + if ( ! empty( $query['time_start'] ) && ! empty( $query['time_finish'] ) ) { + $total_query_time += ( $query['time_finish'] - $query['time_start'] ); + } + } + ?> ++ +
+