From bc3e7e14bdbc30c787fe191b7ec69b2ad8b801d3 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 19 Feb 2025 10:17:16 +0200 Subject: [PATCH] start fixing integration tests --- inc/Engine/Common/Head/Subscriber.php | 28 +++++++++++- .../CriticalPath/CriticalCSSSubscriber.php | 2 +- inc/Engine/Optimization/Buffer/Subscriber.php | 26 ----------- .../Optimization/GoogleFonts/Combine.php | 2 +- tests/Integration/IsolateHookTrait.php | 43 +++++++++++++++++++ .../GoogleFonts/Combine/optimize.php | 4 +- 6 files changed, 74 insertions(+), 31 deletions(-) diff --git a/inc/Engine/Common/Head/Subscriber.php b/inc/Engine/Common/Head/Subscriber.php index 48772d3cbc..6f8fc26b2e 100644 --- a/inc/Engine/Common/Head/Subscriber.php +++ b/inc/Engine/Common/Head/Subscriber.php @@ -35,7 +35,8 @@ class Subscriber implements Subscriber_Interface { */ public static function get_subscribed_events() { return [ - 'rocket_head' => 'print_head_elements', + 'rocket_buffer' => [ 'insert_rocket_head', 100000 ], + 'rocket_head' => 'print_head_elements', ]; } @@ -136,4 +137,29 @@ private function prepare_element( $element ) { return $open_tag . ' ' . $attributes_html . '>' . $inner_content . $close_tag; } + + /** + * Insert rocket_head into the buffer HTML + * + * @param string $html Buffer HTML. + * @return string + */ + public function insert_rocket_head( $html ) { + if ( empty( $html ) ) { + return $html; + } + + $filtered_buffer = preg_replace( + '##iU', + '' . wpm_apply_filters_typed( 'string', 'rocket_head', '' ), + $html, + 1 + ); + + if ( empty( $filtered_buffer ) ) { + return $html; + } + + return $filtered_buffer; + } } diff --git a/inc/Engine/CriticalPath/CriticalCSSSubscriber.php b/inc/Engine/CriticalPath/CriticalCSSSubscriber.php index 45250a06d9..38421de43b 100644 --- a/inc/Engine/CriticalPath/CriticalCSSSubscriber.php +++ b/inc/Engine/CriticalPath/CriticalCSSSubscriber.php @@ -639,7 +639,7 @@ public function insert_css_in_head( $items ) { * @return string */ public function get_critical_css_content() { - return $this->critical_css_content; + return $this->should_async_css() ? $this->critical_css_content : ''; } /** diff --git a/inc/Engine/Optimization/Buffer/Subscriber.php b/inc/Engine/Optimization/Buffer/Subscriber.php index 5ef62a2c30..ae32bb989a 100644 --- a/inc/Engine/Optimization/Buffer/Subscriber.php +++ b/inc/Engine/Optimization/Buffer/Subscriber.php @@ -33,7 +33,6 @@ public function __construct( Optimization $optimizer ) { public static function get_subscribed_events() { return [ 'template_redirect' => [ 'start_content_process', 2 ], - 'rocket_buffer' => [ 'insert_rocket_head', 100000 ], ]; } @@ -45,29 +44,4 @@ public static function get_subscribed_events() { public function start_content_process() { return $this->optimizer->maybe_init_process(); } - - /** - * Insert rocket_head into the buffer HTML - * - * @param string $html Buffer HTML. - * @return string - */ - public function insert_rocket_head( $html ) { - if ( empty( $html ) ) { - return $html; - } - - $filtered_buffer = preg_replace( - '##iU', - '' . wpm_apply_filters_typed( 'string', 'rocket_head', '' ), - $html, - 1 - ); - - if ( empty( $filtered_buffer ) ) { - return $html; - } - - return $filtered_buffer; - } } diff --git a/inc/Engine/Optimization/GoogleFonts/Combine.php b/inc/Engine/Optimization/GoogleFonts/Combine.php index 0e9a39152c..552eb71547 100644 --- a/inc/Engine/Optimization/GoogleFonts/Combine.php +++ b/inc/Engine/Optimization/GoogleFonts/Combine.php @@ -64,7 +64,7 @@ public function optimize( $html ): string { function ( $font ) use ( $exclusions ) { return ! $this->is_excluded( $font[0], $exclusions ); } - ); + ); $num_fonts = count( $filtered_fonts ); diff --git a/tests/Integration/IsolateHookTrait.php b/tests/Integration/IsolateHookTrait.php index 9f3c0daa5e..415e9e2256 100644 --- a/tests/Integration/IsolateHookTrait.php +++ b/tests/Integration/IsolateHookTrait.php @@ -11,6 +11,49 @@ protected $original_wp_priorities; + protected function unregisterAllCallbacksExceptMulti( $event_name, $methods = [] ) { + global $wp_filter; + $this->original_wp_filter = $wp_filter[ $event_name ]->callbacks; + + foreach ( $methods as $priority => $method_name ) { + foreach ( $this->original_wp_filter[ $priority ] as $key => $config ) { + + // Skip if not this tests callback. + if ( substr( $key, - strlen( $method_name ) ) !== $method_name ) { + continue; + } + + $wp_filter[ $event_name ]->callbacks = [ + $priority => [ $key => $config ], + ]; + } + } + + try { + $wp_hooks = $wp_filter[ $event_name ]; + $reflection = new ReflectionClass($wp_hooks); + $priorities_property = $reflection->getProperty('priorities'); + $priorities_property->setAccessible(true); + $this->original_wp_priorities = $priorities_property->getValue($wp_hooks); + $priorities = $priorities_property->getValue($wp_hooks); + } catch (ReflectionException $e) { + return; + } + + foreach ($this->original_wp_priorities as $priority) { + if ( key_exists($priority, $wp_filter[ $event_name ]->callbacks)) { + continue; + } + + $priorities = array_values(array_filter($priorities, function ($current) use ($priority) { + return $current !== $priority; + })); + + } + + $priorities_property->setValue($wp_hooks, $priorities); + } + protected function unregisterAllCallbacksExcept( $event_name, $method_name, $priority = 10 ) { global $wp_filter; $this->original_wp_filter = $wp_filter[ $event_name ]->callbacks; diff --git a/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php b/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php index 5b2e4a013d..5318e8c88e 100644 --- a/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php +++ b/tests/Integration/inc/Engine/Optimization/GoogleFonts/Combine/optimize.php @@ -29,7 +29,7 @@ public function set_up() { ], ]; - $this->unregisterAllCallbacksExcept('rocket_buffer', 'process', 17 ); + $this->unregisterAllCallbacksExceptMulti('rocket_buffer', [ 17 => 'process', 100000 => 'insert_rocket_head' ] ); add_filter('rocket_exclude_locally_host_fonts', [ $this, 'exclude_locally_host_fonts' ] ); // @phpstan-ignore-line } @@ -84,7 +84,7 @@ private function doTest( $config, $original, $expected ) { $actual = apply_filters( 'rocket_buffer', $original ); $this->assertSame( - $this->format_the_html( $expected ), + $this->format_the_html( $expected['html'] ), $this->format_the_html( $actual ) ); }