Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP_Error tracker #559

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions collectors/wp_errors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* WP error collector.
*
* @package query-monitor
*/

class QM_Collector_WP_Errors extends QM_Collector {

public $id = 'wp_errors';

public function __construct() {
parent::__construct();

add_action( 'wp_error_added', array( $this, 'action_wp_error_added' ), 10, 4 );
add_action( 'is_wp_error_instance', array( $this, 'action_is_wp_error_instance' ) );
}

public function action_wp_error_added( $code, $message, $data, WP_Error $wp_error ) {
$id = spl_object_hash( $wp_error );

$this->data['errors'][ $id ] = array(
'error' => $wp_error,
'trace' => new QM_Backtrace( array(
'ignore_current_filter' => false,
'ignore_frames' => 6,
) ),
);
}

public function action_is_wp_error_instance( WP_Error $wp_error ) {
$id = spl_object_hash( $wp_error );

if ( ! isset( $this->data['checked'][ $id ] ) ) {
$this->data['checked'][ $id ] = array();
}

$this->data['checked'][ $id ][] = array(
'error' => $wp_error,
'trace' => new QM_Backtrace( array(
'ignore_current_filter' => false,
'ignore_frames' => 5,
) ),
);
}
}

# Load early to catch early errors
QM_Collectors::add( new QM_Collector_WP_Errors() );
138 changes: 138 additions & 0 deletions output/html/wp_errors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* WP error output for HTML pages.
*
* @package query-monitor
*/

class QM_Output_Html_WP_Errors extends QM_Output_Html {

/**
* Collector instance.
*
* @var QM_Collector_WP_Errors Collector.
*/
protected $collector;

public function __construct( QM_Collector $collector ) {
parent::__construct( $collector );
add_filter( 'qm/output/menus', array( $this, 'admin_menu' ), 10 );
}

public function name() {
return __( 'WP Errors', 'query-monitor' );
}

public function output() {

$data = $this->collector->get_data();

if ( empty( $data['errors'] ) && empty( $data['checked'] ) ) {
return;
}

$components = array();

foreach ( $data['errors'] as $error ) {
$components[] = $error['trace']->get_component()->name;
}

$components = array_unique( $components );

$this->before_tabular_output();

echo '<thead>';
echo '<tr>';
echo '<th scope="col">' . esc_html__( 'Error Code', 'query-monitor' ) . '</th>';
echo '<th scope="col">' . esc_html__( 'Error Message', 'query-monitor' ) . '</th>';
echo '<th scope="col">' . esc_html__( 'Error Source', 'query-monitor' ) . '</th>';
echo '<th scope="col">' . esc_html__( 'Trace', 'query-monitor' ) . '</th>';
echo '<th scope="col" class="qm-filterable-column">';
echo $this->build_filter( 'component', $components, __( 'Component', 'query-monitor' ) ); // WPCS: XSS ok.
echo '</th>';
echo '</tr>';
echo '</thead>';

echo '<tbody>';

foreach ( $data['errors'] as $id => $error ) {
/** @var WP_Error */
$wp_error = $error['error'];

/** @var QM_Backtrace */
$trace = $error['trace'];

$caller = $trace->get_caller();

$component = $trace->get_component();

$row_attr = array();
$row_attr['data-qm-component'] = $component->name;

if ( 'core' !== $component->context ) {
$row_attr['data-qm-component'] .= ' non-core';
}

$attr = '';

foreach ( $row_attr as $a => $v ) {
$attr .= ' ' . $a . '="' . esc_attr( $v ) . '"';
}

printf( // WPCS: XSS ok.
'<tr %s>',
$attr
);

echo '<td>' . esc_html( $wp_error->get_error_code() ) . '</td>';
echo '<td>' . esc_html( $wp_error->get_error_message() ) . '</td>';
echo '<td>';
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo self::output_filename( $caller['display'], $caller['calling_file'], $caller['calling_line'] );
echo '</td>';
echo '<td>';

if ( isset( $data['checked'][ $id ] ) ) {
echo '<ol>';
foreach ( $data['checked'][ $id ] as $check ) {
$check_caller = $check['trace']->get_caller();
echo '<li>';
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo self::output_filename( $check_caller['display'], $check_caller['calling_file'], $check_caller['calling_line'] );
echo '</li>';
}
echo '</ol>';
} else {
echo 'No';
}

echo '</td>';
echo '<td>' . esc_html( $trace->get_component()->name ) . '</td>';
echo '</tr>';
}
echo '</tbody>';

$this->after_tabular_output();
}

public function admin_menu( array $menu ) {
$title = __( 'WP Errors', 'query-monitor' );

$menu[ $this->collector->id() ] = $this->menu( array(
'title' => $title,
) );
return $menu;

}

}

function register_qm_output_html_wp_errors( array $output, QM_Collectors $collectors ) {
$collector = QM_Collectors::get( 'wp_errors' );
if ( $collector ) {
$output['wp_errors'] = new QM_Output_Html_WP_Errors( $collector );
}
return $output;
}

add_filter( 'qm/outputter/html', 'register_qm_output_html_wp_errors', 110, 2 );
3 changes: 3 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<!-- @TODO remove this exclusion: -->
<exclude name="Squiz.Commenting" />

<!-- Exclude short description sniff so short `@var` notation can be used -->
<exclude name="Generic.Commenting.DocComment.MissingShort" />

<!-- Not interested in whitespace issues at the moment -->
<exclude name="PEAR.Functions.FunctionCallSignature" />
<exclude name="Generic.Functions.FunctionCallArgumentSpacing.TooMuchSpaceAfterComma" />
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/test-dispatcher-html.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function testAdminToolbarContainsMenuItems() {
'rewrites' => true,
'timing' => false,
'transients' => true,
'wp_errors' => true,
);

$collectors = QM_Collectors::init();
Expand Down