Skip to content

Commit b19e57b

Browse files
committed
Expose the reason(s) for why a page is not optimized when WP_DEBUG is enabled
1 parent a7cedce commit b19e57b

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

plugins/optimization-detective/optimization.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,43 @@ 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() && ( 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+
}
88120
return;
89121
}
90122
$callback = 'od_optimize_template_output_buffer';

0 commit comments

Comments
 (0)