Skip to content

Commit e845dd8

Browse files
authored
Merge pull request #1849 from SohamPatel46/add/iframe-for-raw-response
Use `IFRAME` to display HTML responses for REST API storage request failures in Site Health test
2 parents 5fb247b + c748645 commit e845dd8

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

plugins/optimization-detective/site-health.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ function od_compose_site_health_result( $response ): array {
127127
$message = wp_remote_retrieve_response_message( $response );
128128
$body = wp_remote_retrieve_body( $response );
129129
$data = json_decode( $body, true );
130+
$header = wp_remote_retrieve_header( $response, 'content-type' );
131+
if ( is_array( $header ) ) {
132+
$header = array_pop( $header );
133+
}
130134

131135
$is_expected = (
132136
400 === $code &&
@@ -156,7 +160,18 @@ function od_compose_site_health_result( $response ): array {
156160
$result['description'] .= '<blockquote>' . esc_html( $data['message'] ) . '</blockquote>';
157161
}
158162

159-
$result['description'] .= '<details><summary>' . esc_html__( 'Raw response:', 'optimization-detective' ) . '</summary><pre style="white-space: pre-wrap">' . esc_html( $body ) . '</pre></details>';
163+
if ( '' !== $body ) {
164+
$result['description'] .= '<details>';
165+
$result['description'] .= '<summary>' . esc_html__( 'Raw response:', 'optimization-detective' ) . '</summary>';
166+
167+
if ( is_string( $header ) && str_contains( $header, 'html' ) ) {
168+
$escaped_content = htmlspecialchars( $body, ENT_QUOTES, 'UTF-8' );
169+
$result['description'] .= '<iframe srcdoc="' . $escaped_content . '" sandbox width="100%" height="300"></iframe>';
170+
} else {
171+
$result['description'] .= '<pre style="white-space: pre-wrap">' . esc_html( $body ) . '</pre>';
172+
}
173+
$result['description'] .= '</details>';
174+
}
160175
}
161176
}
162177
return $result;
@@ -238,14 +253,24 @@ function od_maybe_render_rest_api_health_check_admin_notice( bool $in_plugin_row
238253
$message = "<details>$message</details>";
239254
}
240255

241-
wp_admin_notice(
256+
$notice = wp_get_admin_notice(
242257
$message,
243258
array(
244259
'type' => 'warning',
245260
'additional_classes' => $in_plugin_row ? array( 'inline', 'notice-alt' ) : array(),
246261
'paragraph_wrap' => false,
247262
)
248263
);
264+
265+
echo wp_kses(
266+
$notice,
267+
array_merge(
268+
wp_kses_allowed_html( 'post' ),
269+
array(
270+
'iframe' => array_fill_keys( array( 'srcdoc', 'sandbox', 'width', 'height' ), true ),
271+
)
272+
)
273+
);
249274
}
250275

251276
/**

plugins/optimization-detective/tests/test-site-health.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,33 @@ public function data_provider_test_rest_api_availability(): array {
120120
'code' => 403,
121121
'message' => 'Forbidden',
122122
),
123+
'headers' => array(
124+
'content-type' => 'text/html',
125+
),
123126
'body' => "<html>\n<head><title>403 Forbidden</title></head>\n<body>\n<center><h1>403 Forbidden</h1></center>\n<hr><center>nginx</center>\n</body>\n</html>",
124127
),
125128
'expected_option' => '1',
126129
'expected_status' => 'recommended',
127130
'expected_unavailable' => true,
128131
),
132+
'other_forbidden' => array(
133+
'mocked_response' => array(
134+
'response' => array(
135+
'code' => 403,
136+
'message' => 'Forbidden',
137+
),
138+
'headers' => array(
139+
'content-type' => array(
140+
'text/html; charset=utf-8',
141+
'application/xhtml+xml',
142+
),
143+
),
144+
'body' => '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don\'t have permission to access this resource.</p></body></html>',
145+
),
146+
'expected_option' => '1',
147+
'expected_status' => 'recommended',
148+
'expected_unavailable' => true,
149+
),
129150
'error' => array(
130151
'mocked_response' => new WP_Error( 'bad', 'Something terrible has happened' ),
131152
'expected_option' => '1',
@@ -142,6 +163,8 @@ public function data_provider_test_rest_api_availability(): array {
142163
* @covers ::od_compose_site_health_result
143164
* @covers ::od_get_rest_api_health_check_response
144165
* @covers ::od_is_rest_api_unavailable
166+
* @covers ::od_render_rest_api_health_check_admin_notice_in_plugin_row
167+
* @covers ::od_maybe_render_rest_api_health_check_admin_notice
145168
*
146169
* @dataProvider data_provider_test_rest_api_availability
147170
*
@@ -151,6 +174,24 @@ public function test_rest_api_availability( $mocked_response, string $expected_o
151174
$this->filter_rest_api_response( $mocked_response );
152175

153176
$result = od_test_rest_api_availability();
177+
$notice = get_echo( 'od_render_rest_api_health_check_admin_notice_in_plugin_row', array( 'optimization-detective/load.php' ) );
178+
if ( $expected_unavailable ) {
179+
$this->assertStringContainsString( '<code>', $notice );
180+
if ( is_array( $mocked_response ) ) {
181+
if ( isset( $mocked_response['headers']['content-type'] ) && str_contains( join( '', (array) $mocked_response['headers']['content-type'] ), 'html' ) ) {
182+
$this->assertStringContainsString( '</iframe>', $notice );
183+
$this->assertStringNotContainsString( '</pre>', $notice );
184+
} else {
185+
$this->assertStringContainsString( '</pre>', $notice );
186+
$this->assertStringNotContainsString( '</iframe>', $notice );
187+
}
188+
} else {
189+
$this->assertStringNotContainsString( '</iframe>', $notice );
190+
$this->assertStringNotContainsString( '</pre>', $notice );
191+
}
192+
} else {
193+
$this->assertSame( '', $notice );
194+
}
154195
$this->assertArrayHasKey( 'label', $result );
155196
$this->assertArrayHasKey( 'status', $result );
156197
$this->assertArrayHasKey( 'badge', $result );
@@ -392,7 +433,7 @@ static function ( $pre, array $args, string $url ) use ( $mocked_response, $obse
392433
}
393434

394435
/**
395-
* Build a mock response.
436+
* Build a mock JSON response.
396437
*
397438
* @param int $status_code HTTP status code.
398439
* @param string $message HTTP status message.
@@ -406,6 +447,9 @@ protected function build_mock_response( int $status_code, string $message, array
406447
'message' => $message,
407448
),
408449
'body' => wp_json_encode( $body ),
450+
'headers' => array(
451+
'content-type' => 'application/json',
452+
),
409453
);
410454
}
411455
}

0 commit comments

Comments
 (0)