File tree Expand file tree Collapse file tree 2 files changed +43
-9
lines changed
plugins/optimization-detective Expand file tree Collapse file tree 2 files changed +43
-9
lines changed Original file line number Diff line number Diff line change @@ -105,20 +105,13 @@ function od_maybe_add_template_output_buffer_filter(): void {
105
105
add_action (
106
106
'wp_print_footer_scripts ' ,
107
107
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 );
117
109
}
118
110
);
119
111
}
120
112
return ;
121
113
}
114
+
122
115
$ callback = 'od_optimize_template_output_buffer ' ;
123
116
if (
124
117
function_exists ( 'perflab_wrap_server_timing ' )
@@ -132,6 +125,28 @@ function_exists( 'perflab_server_timing_use_output_buffer' )
132
125
add_filter ( 'od_template_output_buffer ' , $ callback );
133
126
}
134
127
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
+
135
150
/**
136
151
* Determines whether the current response can be optimized.
137
152
*
Original file line number Diff line number Diff line change @@ -215,6 +215,25 @@ public function test_od_maybe_add_template_output_buffer_filter( Closure $set_up
215
215
$ this ->assertSame ( $ expected_has_filter , has_filter ( 'od_template_output_buffer ' ) );
216
216
}
217
217
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
+
218
237
/**
219
238
* Data provider.
220
239
*
You can’t perform that action at this time.
0 commit comments