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

Use IFRAME to display HTML responses for REST API storage request failures in Site Health test #1849

Merged
21 changes: 19 additions & 2 deletions plugins/optimization-detective/site-health.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
$message = wp_remote_retrieve_response_message( $response );
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true );
$header = wp_remote_retrieve_header( $response, 'content-type' );

$is_expected = (
400 === $code &&
Expand Down Expand Up @@ -156,7 +157,14 @@
$result['description'] .= '<blockquote>' . esc_html( $data['message'] ) . '</blockquote>';
}

$result['description'] .= '<details><summary>' . esc_html__( 'Raw response:', 'optimization-detective' ) . '</summary><pre style="white-space: pre-wrap">' . esc_html( $body ) . '</pre></details>';
$result['description'] .= '<details><summary>' . esc_html__( 'Raw response:', 'optimization-detective' ) . '</summary>';

if ( isset( $header ) && 'text/html' === $header ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will account for headers values like text/html; charset=utf-8 and application/xml+xhtml:

Suggested change
if ( isset( $header ) && 'text/html' === $header ) {
if ( is_string( $header ) && str_contains( $header, 'html' ) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: $header will always be set and never be null so the use of isset() probably was unnecessary, right?

$escaped_content = htmlspecialchars( $body, ENT_QUOTES, 'UTF-8' );
$result['description'] .= '<iframe srcdoc="' . $escaped_content . '" sandbox seamless></iframe></details>';

Check warning on line 164 in plugins/optimization-detective/site-health.php

View check run for this annotation

Codecov / codecov/patch

plugins/optimization-detective/site-health.php#L163-L164

Added lines #L163 - L164 were not covered by tests
} else {
$result['description'] .= '<pre style="white-space: pre-wrap">' . esc_html( $body ) . '</pre></details>';
}
}
}
return $result;
Expand Down Expand Up @@ -238,14 +246,23 @@
$message = "<details>$message</details>";
}

wp_admin_notice(
$notice = wp_get_admin_notice(
$message,
array(
'type' => 'warning',
'additional_classes' => $in_plugin_row ? array( 'inline', 'notice-alt' ) : array(),
'paragraph_wrap' => false,
)
);

$allowed_tags = wp_kses_allowed_html( 'post' );
$allowed_tags['iframe'] = array(
'srcdoc' => true,
'seamless' => true,
'sandbox' => true,
);

echo wp_kses( $notice, $allowed_tags );
}

/**
Expand Down
Loading