From c83007f2dc21ba9cd1e1a6c71907cc2ab2ee6c04 Mon Sep 17 00:00:00 2001 From: Hug0-Drelon Date: Thu, 4 Jun 2026 14:55:06 +0200 Subject: [PATCH 1/2] Use wp_template_enhancement_output_buffer hook when available. --- plugins/optimization-detective/hooks.php | 7 ++++++- plugins/optimization-detective/optimization.php | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/plugins/optimization-detective/hooks.php b/plugins/optimization-detective/hooks.php index 18b0ad1f05..6cc4784e25 100644 --- a/plugins/optimization-detective/hooks.php +++ b/plugins/optimization-detective/hooks.php @@ -18,7 +18,12 @@ // @codeCoverageIgnoreStart add_action( 'init', 'od_initialize_extensions', PHP_INT_MAX ); -add_filter( 'template_include', 'od_buffer_output', PHP_INT_MAX ); + +// Backward compatibility with WP<6.9. +if ( ! function_exists( 'wp_start_template_enhancement_output_buffer' ) ) { + add_filter( 'template_include', 'od_buffer_output', PHP_INT_MAX ); +} + OD_URL_Metrics_Post_Type::add_hooks(); OD_Storage_Lock::add_hooks(); add_action( 'wp', 'od_maybe_add_template_output_buffer_filter' ); diff --git a/plugins/optimization-detective/optimization.php b/plugins/optimization-detective/optimization.php index a604d4b476..3ff0236241 100644 --- a/plugins/optimization-detective/optimization.php +++ b/plugins/optimization-detective/optimization.php @@ -106,7 +106,13 @@ function_exists( 'perflab_server_timing_use_output_buffer' ) ) { $callback = perflab_wrap_server_timing( $callback, 'optimization-detective', 'exist' ); } - add_filter( 'od_template_output_buffer', $callback ); + + // Backward compatibility with WP<6.9. + if ( function_exists( 'wp_start_template_enhancement_output_buffer' ) ) { + add_filter( 'wp_template_enhancement_output_buffer', $callback ); + } else { + add_filter( 'od_template_output_buffer', $callback ); + } } /** From 09e818473c3d17d92f09ce9f3ad52464cb0e1692 Mon Sep 17 00:00:00 2001 From: Hug0-Drelon Date: Thu, 4 Jun 2026 15:26:57 +0200 Subject: [PATCH 2/2] Adapt tests to WP6.9+. --- plugins/optimization-detective/tests/test-hooks.php | 8 +++++++- .../optimization-detective/tests/test-optimization.php | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/plugins/optimization-detective/tests/test-hooks.php b/plugins/optimization-detective/tests/test-hooks.php index 6e1fa19efe..9c7eab3e3b 100644 --- a/plugins/optimization-detective/tests/test-hooks.php +++ b/plugins/optimization-detective/tests/test-hooks.php @@ -14,7 +14,13 @@ class Test_OD_Hooks extends WP_UnitTestCase { */ public function test_hooks_added(): void { $this->assertEquals( PHP_INT_MAX, has_action( 'init', 'od_initialize_extensions' ) ); - $this->assertEquals( PHP_INT_MAX, has_filter( 'template_include', 'od_buffer_output' ) ); + + // Backward compatibility with WP<6.9. + if ( version_compare( $GLOBALS['wp_version'], '6.9', '<' ) ) { + $this->assertEquals( PHP_INT_MAX, has_filter( 'template_include', 'od_buffer_output' ) ); + } else { + $this->assertFalse( has_filter( 'template_include', 'od_buffer_output' ) ); + } $this->assertEquals( 10, has_filter( 'wp', 'od_maybe_add_template_output_buffer_filter' ) ); $this->assertEquals( 10, has_action( 'wp_head', 'od_render_generator_meta_tag' ) ); diff --git a/plugins/optimization-detective/tests/test-optimization.php b/plugins/optimization-detective/tests/test-optimization.php index b27f43efdc..32ba5fa9b7 100644 --- a/plugins/optimization-detective/tests/test-optimization.php +++ b/plugins/optimization-detective/tests/test-optimization.php @@ -240,7 +240,13 @@ public function test_od_maybe_add_template_output_buffer_filter( Closure $set_up remove_all_filters( 'od_template_output_buffer' ); // In case go_to() caused them to be added. od_maybe_add_template_output_buffer_filter(); - $this->assertSame( $expected_has_filter, has_filter( 'od_template_output_buffer' ) ); + + // Backward compatibility with WP<6.9. + if ( version_compare( $GLOBALS['wp_version'], '6.9', '<' ) ) { + $this->assertSame( $expected_has_filter, has_filter( 'od_template_output_buffer' ) ); + } else { + $this->assertSame( $expected_has_filter, has_filter( 'wp_template_enhancement_output_buffer' ) ); + } } /**