Skip to content

Commit 0615c18

Browse files
authored
Merge pull request #1822 from WordPress/fix/short-circuit-rest-api-unavailable
Ensure optimization is performed in the wp-env local environment and log debug messages to console when disabled
2 parents 230e1b8 + e6135b8 commit 0615c18

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

plugins/optimization-detective/optimization.php

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,38 @@ static function ( string $output, ?int $phase ): string {
8080
* @access private
8181
*/
8282
function od_maybe_add_template_output_buffer_filter(): void {
83-
if (
84-
! od_can_optimize_response() ||
85-
od_is_rest_api_unavailable() ||
86-
isset( $_GET['optimization_detective_disabled'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
87-
88-
) {
83+
$conditions = array(
84+
array(
85+
'test' => od_can_optimize_response(),
86+
'reason' => __( 'Page is not optimized because od_can_optimize_response() returned false. This can be overridden with the od_can_optimize_response filter.', 'optimization-detective' ),
87+
),
88+
array(
89+
'test' => ! od_is_rest_api_unavailable() || ( wp_get_environment_type() === 'local' && ! function_exists( 'tests_add_filter' ) ),
90+
'reason' => __( 'Page is not optimized because the REST API for storing URL Metrics is not available.', 'optimization-detective' ),
91+
),
92+
array(
93+
'test' => ! isset( $_GET['optimization_detective_disabled'] ), // phpcs:ignore WordPress.Security.NonceVerification.Recommended
94+
'reason' => __( 'Page is not optimized because the URL has the optimization_detective_disabled query parameter.', 'optimization-detective' ),
95+
),
96+
);
97+
$reasons = array();
98+
foreach ( $conditions as $condition ) {
99+
if ( ! $condition['test'] ) {
100+
$reasons[] = $condition['reason'];
101+
}
102+
}
103+
if ( count( $reasons ) > 0 ) {
104+
if ( WP_DEBUG ) {
105+
add_action(
106+
'wp_print_footer_scripts',
107+
static function () use ( $reasons ): void {
108+
od_print_disabled_reasons( $reasons );
109+
}
110+
);
111+
}
89112
return;
90113
}
114+
91115
$callback = 'od_optimize_template_output_buffer';
92116
if (
93117
function_exists( 'perflab_wrap_server_timing' )
@@ -101,6 +125,28 @@ function_exists( 'perflab_server_timing_use_output_buffer' )
101125
add_filter( 'od_template_output_buffer', $callback );
102126
}
103127

128+
/**
129+
* Prints the reasons why Optimization Detective is not optimizing the current page.
130+
*
131+
* This is only used when WP_DEBUG is enabled.
132+
*
133+
* @since n.e.x.t
134+
* @access private
135+
*
136+
* @param string[] $reasons Reason messages.
137+
*/
138+
function od_print_disabled_reasons( array $reasons ): void {
139+
foreach ( $reasons as $reason ) {
140+
wp_print_inline_script_tag(
141+
sprintf(
142+
'console.info( %s );',
143+
(string) wp_json_encode( '[Optimization Detective] ' . $reason )
144+
),
145+
array( 'type' => 'module' )
146+
);
147+
}
148+
}
149+
104150
/**
105151
* Determines whether the current response can be optimized.
106152
*

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,25 @@ public function test_od_maybe_add_template_output_buffer_filter( Closure $set_up
215215
$this->assertSame( $expected_has_filter, has_filter( 'od_template_output_buffer' ) );
216216
}
217217

218+
/**
219+
* Test od_print_disabled_reasons().
220+
*
221+
* @covers ::od_print_disabled_reasons
222+
*/
223+
public function test_od_print_disabled_reasons(): void {
224+
$this->assertSame( '', get_echo( 'od_print_disabled_reasons', array( array() ) ) );
225+
$foo_message = get_echo( 'od_print_disabled_reasons', array( array( 'Foo' ) ) );
226+
$this->assertStringContainsString( '<script', $foo_message );
227+
$this->assertStringContainsString( 'console.info(', $foo_message );
228+
$this->assertStringContainsString( '[Optimization Detective] Foo', $foo_message );
229+
230+
$foo_and_bar_messages = get_echo( 'od_print_disabled_reasons', array( array( 'Foo', 'Bar' ) ) );
231+
$this->assertStringContainsString( '<script', $foo_and_bar_messages );
232+
$this->assertStringContainsString( 'console.info(', $foo_and_bar_messages );
233+
$this->assertStringContainsString( '[Optimization Detective] Foo', $foo_and_bar_messages );
234+
$this->assertStringContainsString( '[Optimization Detective] Bar', $foo_and_bar_messages );
235+
}
236+
218237
/**
219238
* Data provider.
220239
*

0 commit comments

Comments
 (0)