@@ -80,11 +80,43 @@ 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 () && ( wp_get_environment_type () !== 'local ' || class_exists ( 'Test_OD_Optimization ' ) ) ) ||
86
- isset ( $ _GET ['optimization_detective_disabled ' ] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
87
- ) {
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 ' || class_exists ( 'Test_OD_Optimization ' ) ) ),
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
+ foreach ( $ reasons as $ reason ) {
109
+ wp_print_inline_script_tag (
110
+ sprintf (
111
+ 'console.log( %s ); ' ,
112
+ (string ) wp_json_encode ( '[Optimization Detective] ' . $ reason )
113
+ ),
114
+ array ( 'type ' => 'module ' )
115
+ );
116
+ }
117
+ }
118
+ );
119
+ }
88
120
return ;
89
121
}
90
122
$ callback = 'od_optimize_template_output_buffer ' ;
0 commit comments