-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from 10up/feature/query-monitor-support
Add support to Query Monitor
- Loading branch information
Showing
7 changed files
with
286 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
/** | ||
* CommonPanel class file. | ||
* | ||
* @since 3.1.0 | ||
* @package DebugBarElasticPress | ||
*/ | ||
|
||
namespace DebugBarElasticPress; | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
/** | ||
* CommonPanel class. | ||
*/ | ||
class CommonPanel { | ||
/** | ||
* Panel menu title | ||
* | ||
* @var string | ||
*/ | ||
public $title = ''; | ||
|
||
/** | ||
* Class constructor | ||
*/ | ||
public function __construct() { | ||
$this->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() ) { | ||
?> | ||
<div class="ep-debug-bar-warning"> | ||
<?php esc_html_e( 'ElasticPress is currently indexing.', 'debug-bar-elasticpress' ); ?> | ||
</div> | ||
<?php | ||
} | ||
|
||
$debug_bar_output = new \DebugBarElasticPress\QueryOutput( $queries ); | ||
$debug_bar_output->render_buttons(); | ||
$debug_bar_output->render_additional_buttons(); | ||
$debug_bar_output->render_queries(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
/** | ||
* QueryMonitorCollector class file. | ||
* | ||
* @since 3.1.0 | ||
* @package DebugBarElasticPress | ||
*/ | ||
|
||
namespace DebugBarElasticPress; | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
/** | ||
* QueryMonitorCollector class. | ||
*/ | ||
class QueryMonitorCollector extends \QM_Collector { | ||
/** | ||
* Collector ID | ||
* | ||
* @var string | ||
*/ | ||
public $id = 'elasticpress'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
/** | ||
* QueryMonitorOutput class file. | ||
* | ||
* @since 3.1.0 | ||
* @package DebugBarElasticPress | ||
*/ | ||
|
||
namespace DebugBarElasticPress; | ||
|
||
defined( 'ABSPATH' ) || exit; | ||
|
||
/** | ||
* QueryMonitorOutput class. | ||
*/ | ||
class QueryMonitorOutput extends \QM_Output_Html { | ||
/** | ||
* Common panel instance | ||
* | ||
* @var CommonPanel | ||
*/ | ||
protected $common_panel; | ||
|
||
/** | ||
* Class constructor | ||
* | ||
* @param QueryMonitorCollector $collector Our QM Collector | ||
*/ | ||
public function __construct( QueryMonitorCollector $collector ) { | ||
parent::__construct( $collector ); | ||
|
||
$this->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() { | ||
?> | ||
<div class="qm qm-non-tabular qm-debug-bar qm-panel-show" id="<?php echo esc_attr( $this->collector->id() ); ?>"> | ||
<?php $this->render_summary(); ?> | ||
<?php $this->common_panel->render(); ?> | ||
</div> | ||
<?php | ||
} | ||
|
||
/** | ||
* Render the summary in Query Monitor format | ||
* | ||
* @return void | ||
*/ | ||
protected function render_summary() { | ||
$queries = \ElasticPress\Elasticsearch::factory()->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'] ); | ||
} | ||
} | ||
?> | ||
<div class="qm-boxed"> | ||
<section> | ||
<h3> | ||
<?php esc_html_e( 'Total ElasticPress Queries:', 'debug-bar-elasticpress' ); ?> | ||
</h3> | ||
<p><?php echo count( $queries ); ?></p> | ||
</section> | ||
<section> | ||
<h3> | ||
<?php esc_html_e( 'Total Blocking ElasticPress Query Time:', 'debug-bar-elasticpress' ); ?> | ||
</h3> | ||
<p> | ||
<?php | ||
echo esc_html( | ||
sprintf( | ||
/* translators: time spent */ | ||
__( '%d ms', 'debug-bar-elasticpress' ), | ||
(int) ( $total_query_time * 1000 ) | ||
) | ||
); | ||
?> | ||
</p> | ||
</section> | ||
</div> | ||
<?php | ||
} | ||
} |
Oops, something went wrong.