Skip to content

Commit e9c0618

Browse files
committed
Add private od_print_disabled_reasons() and test coverage
1 parent a6c7cf1 commit e9c0618

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

plugins/optimization-detective/optimization.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,13 @@ function od_maybe_add_template_output_buffer_filter(): void {
105105
add_action(
106106
'wp_print_footer_scripts',
107107
static function () use ( $reasons ): void {
108-
foreach ( $reasons as $reason ) {
109-
wp_print_inline_script_tag(
110-
sprintf(
111-
'console.info( %s );',
112-
(string) wp_json_encode( '[Optimization Detective] ' . $reason )
113-
),
114-
array( 'type' => 'module' )
115-
);
116-
}
108+
od_print_disabled_reasons( $reasons );
117109
}
118110
);
119111
}
120112
return;
121113
}
114+
122115
$callback = 'od_optimize_template_output_buffer';
123116
if (
124117
function_exists( 'perflab_wrap_server_timing' )
@@ -132,6 +125,28 @@ function_exists( 'perflab_server_timing_use_output_buffer' )
132125
add_filter( 'od_template_output_buffer', $callback );
133126
}
134127

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+
135150
/**
136151
* Determines whether the current response can be optimized.
137152
*

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)