@@ -80,14 +80,38 @@ static function ( string $output, ?int $phase ): string {
80
80
* @access private
81
81
*/
82
82
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
+ }
89
112
return ;
90
113
}
114
+
91
115
$ callback = 'od_optimize_template_output_buffer ' ;
92
116
if (
93
117
function_exists ( 'perflab_wrap_server_timing ' )
@@ -101,6 +125,28 @@ function_exists( 'perflab_server_timing_use_output_buffer' )
101
125
add_filter ( 'od_template_output_buffer ' , $ callback );
102
126
}
103
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
+
104
150
/**
105
151
* Determines whether the current response can be optimized.
106
152
*
0 commit comments