collector->get_data();
if ( ! empty( $data->http ) ) {
$statuses = array_keys( $data->types );
$components = array_column( $data->component_times, 'component' );
usort( $statuses, 'strcasecmp' );
usort( $components, 'strcasecmp' );
$status_output = array();
$hosts = array_unique( array_column( $data->http, 'host' ) );
sort( $hosts );
foreach ( $statuses as $status ) {
if ( 'error' === $status ) {
$status_output['error'] = __( 'Error', 'query-monitor' );
} elseif ( 'non-blocking' === $status ) {
/* translators: A non-blocking HTTP API request */
$status_output['non-blocking'] = __( 'Non-blocking', 'query-monitor' );
} else {
$status_output[] = $status;
}
}
$this->before_tabular_output();
echo '';
echo '';
echo '' . esc_html__( 'Method', 'query-monitor' ) . ' | ';
echo '';
echo $this->build_filter( 'host', $hosts, __( 'URL', 'query-monitor' ) ); // WPCS: XSS ok.
echo ' | ';
echo '';
echo $this->build_filter( 'type', $status_output, __( 'Status', 'query-monitor' ) ); // WPCS: XSS ok.
echo ' | ';
echo '' . esc_html__( 'Caller', 'query-monitor' ) . ' | ';
echo '';
echo $this->build_filter( 'component', $components, __( 'Component', 'query-monitor' ) ); // WPCS: XSS ok.
echo ' | ';
echo '' . esc_html__( 'Size', 'query-monitor' ) . ' | ';
echo '' . esc_html__( 'Timeout', 'query-monitor' ) . ' | ';
echo '' . esc_html__( 'Time', 'query-monitor' ) . ' | ';
echo '
';
echo '';
echo '
';
$i = 0;
foreach ( $data->http as $row ) {
$ltime = $row['ltime'];
$i++;
$is_error = false;
$row_attr = array();
$css = '';
if ( $row['response'] instanceof WP_Error ) {
$response = $row['response']->get_error_message();
$is_error = true;
} elseif ( ! $row['args']['blocking'] ) {
/* translators: A non-blocking HTTP API request */
$response = __( 'Non-blocking', 'query-monitor' );
} else {
$code = wp_remote_retrieve_response_code( $row['response'] );
$msg = wp_remote_retrieve_response_message( $row['response'] );
if ( intval( $code ) >= 400 ) {
$is_error = true;
}
$response = $code . ' ' . $msg;
}
if ( $is_error ) {
$css = 'qm-warn';
}
$url = self::format_url( $row['url'] );
$info = '';
$url = preg_replace( '|^http:|', 'http:', $url );
if ( 'https' === parse_url( $row['url'], PHP_URL_SCHEME ) ) {
if ( empty( $row['args']['sslverify'] ) && ! $row['local'] ) {
$info .= '' . QueryMonitor::icon( 'warning' ) . esc_html( sprintf(
/* translators: An HTTP API request has disabled certificate verification. 1: Relevant argument name */
__( 'Certificate verification disabled (%s)', 'query-monitor' ),
'sslverify=false'
) ) . '
';
$url = preg_replace( '|^https:|', 'https:', $url );
} elseif ( ! $is_error && $row['args']['blocking'] ) {
$url = preg_replace( '|^https:|', 'https:', $url );
}
}
$component = $row['component'];
$stack = array();
$filtered_trace = $row['filtered_trace'];
foreach ( $filtered_trace as $frame ) {
$stack[] = self::output_filename( $frame['display'], $frame['calling_file'], $frame['calling_line'] );
}
$row_attr['data-qm-component'] = $component->name;
$row_attr['data-qm-type'] = $row['type'];
$row_attr['data-qm-time'] = $row['ltime'];
$row_attr['data-qm-host'] = $row['host'];
if ( 'core' !== $component->context ) {
$row_attr['data-qm-component'] .= ' non-core';
}
$attr = '';
foreach ( $row_attr as $a => $v ) {
$attr .= ' ' . $a . '="' . esc_attr( (string) $v ) . '"';
}
printf( // WPCS: XSS ok.
'',
$attr,
esc_attr( $css )
);
printf(
'%s | ',
esc_html( $row['args']['method'] )
);
if ( ! empty( $row['redirected_to'] ) ) {
$url .= sprintf(
'
%1$s%2$s
%3$s',
QueryMonitor::icon( 'warning' ),
/* translators: An HTTP API request redirected to another URL */
__( 'Redirected to:', 'query-monitor' ),
self::format_url( $row['redirected_to'] )
);
}
printf( // WPCS: XSS ok.
'%s%s | ',
$info,
$url
);
$show_toggle = ! empty( $row['info'] );
echo '';
if ( $is_error ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo QueryMonitor::icon( 'warning' );
}
echo esc_html( $response );
if ( $show_toggle ) {
echo self::build_toggler(); // WPCS: XSS ok;
echo '';
}
if ( ! empty( $row['info'] ) ) {
$time_fields = array(
'namelookup_time' => __( 'DNS Resolution Time', 'query-monitor' ),
'connect_time' => __( 'Connection Time', 'query-monitor' ),
'starttransfer_time' => __( 'Transfer Start Time (TTFB)', 'query-monitor' ),
);
foreach ( $time_fields as $key => $value ) {
if ( ! isset( $row['info'][ $key ] ) ) {
continue;
}
printf(
'- %1$s: %2$s
',
esc_html( $value ),
esc_html( number_format_i18n( $row['info'][ $key ], 4 ) )
);
}
$other_fields = array(
'content_type' => __( 'Response Content Type', 'query-monitor' ),
'primary_ip' => __( 'IP Address', 'query-monitor' ),
);
foreach ( $other_fields as $key => $value ) {
if ( ! isset( $row['info'][ $key ] ) ) {
continue;
}
printf(
'- %1$s: %2$s
',
esc_html( $value ),
esc_html( $row['info'][ $key ] )
);
}
}
if ( $show_toggle ) {
echo ' ';
}
echo ' | ';
$caller = array_shift( $stack );
echo '';
if ( ! empty( $stack ) ) {
echo self::build_toggler(); // WPCS: XSS ok;
}
echo '';
echo "- {$caller}
"; // WPCS: XSS ok.
if ( ! empty( $stack ) ) {
echo '- ' . implode( '
- ', $stack ) . '
'; // WPCS: XSS ok.
}
echo ' | ';
printf(
'%s | ',
esc_html( $component->name )
);
$size = '';
if ( isset( $row['info']['size_download'] ) ) {
$size = sprintf(
/* translators: %s: Memory used in kilobytes */
__( '%s kB', 'query-monitor' ),
number_format_i18n( $row['info']['size_download'] / 1024, 1 )
);
}
printf(
'%s | ',
esc_html( $size )
);
printf(
'%s | ',
esc_html( $row['args']['timeout'] )
);
if ( empty( $ltime ) ) {
$stime = '';
} else {
$stime = number_format_i18n( $ltime, 4 );
}
printf(
'%s | ',
esc_html( $stime )
);
echo '
';
}
echo '';
echo '';
$total_stime = number_format_i18n( $data->ltime, 4 );
$count = count( $data->http );
echo '';
printf(
'%s | ',
sprintf(
/* translators: %s: Number of HTTP API requests */
esc_html( _nx( 'Total: %s', 'Total: %s', $count, 'HTTP API calls', 'query-monitor' ) ),
'' . esc_html( number_format_i18n( $count ) ) . ''
)
);
echo '' . esc_html( $total_stime ) . ' | ';
echo '
';
echo '';
$this->after_tabular_output();
} else {
$this->before_non_tabular_output();
$notice = __( 'No HTTP API calls.', 'query-monitor' );
echo $this->build_notice( $notice ); // WPCS: XSS ok.
$this->after_non_tabular_output();
}
}
/**
* @param array $class
* @return array
*/
public function admin_class( array $class ) {
/** @var QM_Data_HTTP $data */
$data = $this->collector->get_data();
if ( isset( $data->errors['alert'] ) ) {
$class[] = 'qm-alert';
}
if ( isset( $data->errors['warning'] ) ) {
$class[] = 'qm-warning';
}
return $class;
}
/**
* @param array $menu
* @return array
*/
public function admin_menu( array $menu ) {
/** @var QM_Data_HTTP $data */
$data = $this->collector->get_data();
$count = ! empty( $data->http ) ? count( $data->http ) : 0;
$title = ( empty( $count ) )
? __( 'HTTP API Calls', 'query-monitor' )
/* translators: %s: Number of calls to the HTTP API */
: __( 'HTTP API Calls (%s)', 'query-monitor' );
$args = array(
'title' => esc_html( sprintf(
$title,
number_format_i18n( $count )
) ),
);
if ( isset( $data->errors['alert'] ) ) {
$args['meta']['classname'] = 'qm-alert';
}
if ( isset( $data->errors['warning'] ) ) {
$args['meta']['classname'] = 'qm-warning';
}
$menu[ $this->collector->id() ] = $this->menu( $args );
return $menu;
}
}
/**
* @param array $output
* @param QM_Collectors $collectors
* @return array
*/
function register_qm_output_html_http( array $output, QM_Collectors $collectors ) {
$collector = QM_Collectors::get( 'http' );
if ( $collector ) {
$output['http'] = new QM_Output_Html_HTTP( $collector );
}
return $output;
}
add_filter( 'qm/outputter/html', 'register_qm_output_html_http', 90, 2 );