Skip to content

Commit

Permalink
Merge pull request #79 from 10up/feature/add-query-document
Browse files Browse the repository at this point in the history
Add Addition Buttons (Explain queries & Query document from ES)
  • Loading branch information
felipeelia authored Sep 5, 2023
2 parents 802a608 + c09c28c commit 4f44d0c
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 12 deletions.
24 changes: 13 additions & 11 deletions assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
}

.ep-queries-buttons-wrapper {
align-items: center;
clear: both;
display: flex;
gap: 10px;
padding: 1em 0;
}

Expand Down Expand Up @@ -76,12 +79,10 @@
* 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 button.ep-copy-button.button.qm-button {
#debug-menu-target-EP_Debug_Bar_ElasticPress a.button {
background: #f6f7f7 !important;
border-color: #2271b1 !important;
border: 1px solid #2271b1 !important;
border-radius: 3px !important;
border-width: 1px;

/* Overriding some Debug Bar "important" rules */
color: #2271b1 !important;
Expand All @@ -95,16 +96,18 @@
text-shadow: none;
}

#debug-menu-target-EP_Debug_Bar_ElasticPress .button:hover {
#debug-menu-target-EP_Debug_Bar_ElasticPress .button:hover,
#query-monitor-main #qm-debug_bar_ep_debug_bar_elasticpress .button:hover {
background: #f0f0f1;
border-color: #0a4b78;
color: #0a4b78;
text-decoration: none;
color: #0a4b78 !important;
text-decoration: none !important;
}

#debug-menu-target-EP_Debug_Bar_ElasticPress .button:active {
background: #f6f7f7;
border-color: #8c8f94;
#debug-menu-target-EP_Debug_Bar_ElasticPress .button:active,
#query-monitor-main #qm-debug_bar_ep_debug_bar_elasticpress .button:hover {
background: #f6f7f7 !important;
border-color: #8c8f94 !important;
box-shadow: none;
}

Expand All @@ -119,7 +122,6 @@
background: #007cba !important;
border: 1px solid #007cba !important;
color: #fff !important;
margin-right: 5px !important;
}

#debug-menu-target-EP_Debug_Bar_ElasticPress a.button-primary:hover,
Expand Down
1 change: 1 addition & 0 deletions classes/EP_Debug_Bar_ElasticPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function render() {
<?php
$debug_bar_output = new \DebugBarElasticPress\QueryOutput( $queries );
$debug_bar_output->render_buttons();
$debug_bar_output->render_additional_buttons();
$debug_bar_output->render_queries();
}
}
96 changes: 95 additions & 1 deletion classes/QueryOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function render_buttons() {
<a download="debug-bar-elasticpress-report.txt" href="data:text/plain;charset=utf-8,<?php echo rawurlencode( $copy_paste_output ); ?>" class="button button-primary" id="ep-download-requests-info">
<?php esc_html_e( 'Download Requests Info', 'debug-bar-elasticpress' ); ?>
</a>
<button class="ep-copy-button button qm-button" data-clipboard-text="<?php echo esc_attr( $copy_paste_output ); ?>">
<button class="ep-copy-button button bordered-button" data-clipboard-text="<?php echo esc_attr( $copy_paste_output ); ?>">
<?php esc_html_e( 'Copy Requests Info to Clipboard', 'debug-bar-elasticpress' ); ?>
</button>
<span class="ep-copy-button-wrapper__success" style="display: none;">
Expand Down Expand Up @@ -298,4 +298,98 @@ protected function render_value( $value ) {

return (string) $value;
}

/**
* Render the additional buttons.
*
* @since 3.1.0
* @return void
*/
public function render_additional_buttons() {
$buttons = [
$this->get_explain_query_button(),
$this->get_retrieve_raw_document_button(),
];

/**
* Filter the additional buttons.
*
* @since 3.1.0
* @hook ep_debug_bar_additional_buttons
* @param array $buttons Buttons.
* @return array
*/
apply_filters( 'ep_debug_bar_additional_buttons', $buttons );

$buttons = array_filter( $buttons );

if ( empty( $buttons ) ) {
return;
}

?>
<div class="ep-queries-buttons-wrapper">
<?php
foreach ( $buttons as $button ) {
echo wp_kses_post( $button );
}
?>
</div>
<?php
}

/**
* Get the explain query button
*
* @since 3.1.0
* @return string
*/
public function get_explain_query_button() : string {
if ( empty( $this->queries ) ) {
return '';
}

$button_link = add_query_arg(
[
'explain' => '1',
],
isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''
);

return sprintf(
'<a href="%s" class="ep-explain-query button bordered-button">
%s
</a>',
esc_url( $button_link ),
esc_html__( 'Explain queries', 'debug-bar-elasticpress' )
);
}

/**
* Get the retrieve raw document button.
*
* @since 3.1.0
* @return string
*/
public function get_retrieve_raw_document_button() : string {
if ( ! is_indexable_singular() ) {
return '';
}

$button_link = add_query_arg(
[
'ep-retrieve-es-document' => '1',
'_wpnonce' => wp_create_nonce( 'ep-retrieve-es-document' ),
],
isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''
);

return sprintf(
'<a href="%s" class="ep-retrieve-es-document button bordered-button">
%s
</a>',
esc_url( $button_link ),
esc_html__( 'Reload and retrieve raw ES document', 'debug-bar-elasticpress' )
);
}
}
40 changes: 40 additions & 0 deletions debug-bar-elasticpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ function setup() {
add_filter( 'debug_bar_statuses', $n( 'add_debug_bar_stati' ) );
add_filter( 'ep_formatted_args', $n( 'add_explain_args' ), 10, 2 );

add_action( 'wp', $n( 'retrieve_raw_document_from_es' ) );

load_plugin_textdomain( 'debug-bar-elasticpress', false, basename( __DIR__ ) . '/lang' );

QueryLog::factory();
Expand Down Expand Up @@ -152,3 +154,41 @@ function admin_notice_min_ep_version() {
</div>
<?php
}

/**
* Check if the current page is an indexable singular of an ES document or not.
*
* @since 3.1.0
* @return boolean
*/
function is_indexable_singular() {
if ( ! is_singular() ) {
return false;
}

$id = get_the_ID();
$post_type = get_post_type( $id );

$post_indexable = \ElasticPress\Indexables::factory()->get( 'post' );
$indexable_post_types = $post_indexable->get_indexable_post_types();

return in_array( $post_type, $indexable_post_types, true );
}

/**
* Get document from Elasticsearch.
*
* @since 3.1.0
* @return void
*/
function retrieve_raw_document_from_es() {
if ( empty( $_GET['ep-retrieve-es-document'] ) || empty( $_GET['_wpnonce'] ) || ! wp_verify_nonce( $_GET['_wpnonce'], 'ep-retrieve-es-document' ) ) {
return;
}

if ( ! is_indexable_singular() ) {
return;
}

\ElasticPress\Indexables::factory()->get( 'post' )->get( get_the_ID() );
}

0 comments on commit 4f44d0c

Please sign in to comment.