Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ElasticPress debugging code for mu-plugins
<?php
/**
* @param string $type
*
* @return bool
*/
function debug_ep_is_debug( $type = 'normal' ) {
if ( ! empty( $_GET['epdebug'] ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
if ( 'normal' === $type ) {
return true;
} elseif ( 'error' === $type ) {
if ( ! empty( $_GET['epdebug_error'] ) ) {
return true;
}
}
}
return false;
}
if ( ! debug_ep_is_debug() ) {
return;
}
/**
* Debugging EP errors
*
* @param array $response
* @param array $args
* @param array|string|int $scope
*/
function debug_ep_retrieve_raw_error( $response, $args, $scope = null ) {
if ( debug_ep_is_debug( 'error' ) ) {
echo '<pre>[' . __FUNCTION__ . '] EP ERROR:' . "\n";
var_dump( compact( 'response', 'args', 'scope' ) );
echo '</pre>';
}
}
add_action( 'ep_retrieve_raw_error', 'debug_ep_retrieve_raw_error', 10, 3 );
add_action( 'ep_index_post_retrieve_raw_error', 'debug_ep_retrieve_raw_error', 10, 3 );
/**
* Debugging EP responses
*
* @param array $response
* @param array $args
* @param array|string|int $scope
*/
function debug_ep_retrieve_raw_response( $response, $args, $scope = null ) {
if ( debug_ep_is_debug() ) {
echo '<pre>[' . __FUNCTION__ . '] EP RAW RESPONSE:' . "\n";
var_dump( compact( 'response', 'args', 'scope' ) );
echo '</pre>';
}
}
add_action( 'ep_retrieve_raw_response', 'debug_ep_retrieve_raw_response', 10, 3 );
//add_action( 'ep_index_post_retrieve_raw_response', 'debug_ep_retrieve_raw_response', 10, 3 );
/**
* Debugging EP args
*
* @param array $formatted_args
* @param array $args
*
* @return array
*/
function debug_ep_formatted_args( $formatted_args, $args ) {
if ( ! empty( $args['s'] ) ) {
if ( isset( $formatted_args['query']['bool']['should'][1] ) ) {
unset( $formatted_args['query']['bool']['should'][1] );
// Fix array keys
$formatted_args['query']['bool']['should'] = array_values( $formatted_args['query']['bool']['should'] );
}
$formatted_args['query']['bool']['should'][0]['multi_match']['type'] = 'cross_fields';
$formatted_args['query']['bool']['should'][0]['multi_match']['operator'] = 'and';
}
// Fix offset usage
if ( ! empty( $args['offset'] ) && empty( $args['paged'] ) ) {
$formatted_args['from'] = absint( $args['offset'] );
}
if ( debug_ep_is_debug() ) {
$args = array_filter( $args );
echo '<pre>[' . __FUNCTION__ . '] EP Original Args:' . "\n";
//var_dump( debug_backtrace() );
var_dump( compact( 'args', 'formatted_args' ) );
echo '</pre>';
}
return $formatted_args;
}
add_filter( 'ep_formatted_args', 'debug_ep_formatted_args', 999, 2 );
/**
* Debugging EP request args
*
* @param array $request_args
* @param array $args
* @param string $scope
*
* @return array
*/
function debug_ep_search_request_args( $request_args, $args, $scope ) {
if ( debug_ep_is_debug() ) {
echo '<pre>[' . __FUNCTION__ . '] EP Search Request Args:' . "\n";
var_dump( compact( 'request_args', 'args', 'scope' ) );
echo '</pre>';
}
return $request_args;
}
add_filter( 'ep_search_request_args', 'debug_ep_search_request_args', 10, 3 );
/**
* Debugging EP request results
*
* @param array $results
* @param array $response
* @param string $scope
*
* @return array
*/
function debug_ep_search_results_array( $results, $response ) {
if ( debug_ep_is_debug() ) {
echo '<pre>[' . __FUNCTION__ . '] EP Search Request Array:' . "\n";
var_dump( array( 'found_posts' => $results['found_posts'], 'posts_count' => count( $results['posts'] ) ) );
echo '</pre>';
}
return $results;
}
add_filter( 'ep_search_results_array', 'debug_ep_search_results_array', 10, 2 );
/**
* Debugging WP posts
*
* @param array $posts
*
* @return array
*/
function debug_the_posts( $posts ) {
if ( debug_ep_is_debug() ) {
echo '<pre>[' . __FUNCTION__ . '] WP Query Posts:' . "\n";
var_dump( array( 'posts_count' => count( $posts ) ) );
echo '</pre>';
}
return $posts;
}
add_filter( 'the_posts', 'debug_the_posts', 999 );
/**
* Debugging WP post request
*
* @param string $request
*
* @return string
*/
function debug_posts_request( $request ) {
if ( debug_ep_is_debug() ) {
echo '<pre>[' . __FUNCTION__ . '] WP Query Request:' . "\n";
var_dump( $request );
echo '</pre>';
}
return $request;
}
add_filter( 'posts_request', 'debug_posts_request', 999 );
/**
* @param \WP_Query $query
*/
function debug_pre_get_posts( $query ) {
if ( debug_ep_is_debug() ) {
echo '<pre>[' . __FUNCTION__ . '] Pre Get Posts:' . "\n";
var_dump( $query->query_vars, debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 6 ) );
echo '</pre>';
}
}
add_action( 'pre_get_posts', 'debug_pre_get_posts', 1 );
/**
* @param array $headers
*
* @return array
*/
function debug_ep_format_request_headers( $headers ) {
if ( debug_ep_is_debug() ) {
$headers['Content-Type'] = 'application/json';
}
return $headers;
}
add_filter( 'ep_format_request_headers', 'debug_ep_format_request_headers' );
/**
* @param string $url
*
* @return string
*/
function debug_ep_pre_request_url( $url ) {
if ( debug_ep_is_debug() ) {
echo '<pre>[' . __FUNCTION__ . ']:' . "\n";
var_dump( $url );
echo '</pre>';
}
return $url;
}
add_filter( 'ep_pre_request_url', 'debug_ep_pre_request_url', 999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.