From 1ca09d9d0caf84b37ce7d7b7ee4aa8cba4aeee0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 6 Dec 2024 15:59:29 -0500 Subject: [PATCH] clean host fonts CSS when enabling/disabling CDN --- inc/Engine/Media/Fonts/Clean/Clean.php | 46 ++++++++++++++++++--- inc/Engine/Media/Fonts/Clean/Subscriber.php | 13 ++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/inc/Engine/Media/Fonts/Clean/Clean.php b/inc/Engine/Media/Fonts/Clean/Clean.php index 02cf9cd4ef..42019a4275 100644 --- a/inc/Engine/Media/Fonts/Clean/Clean.php +++ b/inc/Engine/Media/Fonts/Clean/Clean.php @@ -50,11 +50,7 @@ public function clean_css_fonts() { * @return void */ public function clean_on_option_change( $old_value, $value ) { - if ( ! isset( $old_value['host_fonts_locally'], $value['host_fonts_locally'] ) ) { - return; - } - - if ( $old_value['host_fonts_locally'] === $value['host_fonts_locally'] ) { + if ( ! $this->did_setting_change( 'host_fonts_locally', $old_value, $value ) ) { return; } @@ -67,4 +63,44 @@ public function clean_on_option_change( $old_value, $value ) { */ do_action( 'rocket_host_fonts_locally_changed' ); } + + /** + * Clean CSS & fonts files stored locally on CDN change + * + * @param mixed $old_value Old option value. + * @param mixed $value New option value. + * + * @return void + */ + public function clean_on_cdn_change( $old_value, $value ) { + if ( ! $this->did_setting_change( 'cdn', $old_value, $value ) ) { + return; + } + + if ( ! $this->did_setting_change( 'cdn_cnames', $old_value, $value ) ) { + return; + } + + $this->clean_css_fonts(); + } + + /** + * Checks if the given setting's value changed. + * + * @param string $setting The settings's value to check in the old and new values. + * @param mixed $old_value Old option value. + * @param mixed $value New option value. + * + * @return bool + */ + private function did_setting_change( $setting, array $old_value, array $value ) { + return ( + array_key_exists( $setting, $old_value ) + && + array_key_exists( $setting, $value ) + && + // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual + $old_value[ $setting ] != $value[ $setting ] + ); + } } diff --git a/inc/Engine/Media/Fonts/Clean/Subscriber.php b/inc/Engine/Media/Fonts/Clean/Subscriber.php index 128c6ed586..ca415b7606 100644 --- a/inc/Engine/Media/Fonts/Clean/Subscriber.php +++ b/inc/Engine/Media/Fonts/Clean/Subscriber.php @@ -31,6 +31,7 @@ public static function get_subscribed_events(): array { return [ 'rocket_after_clean_domain' => 'clean_css_fonts', 'update_option_wp_rocket_settings' => [ 'clean_on_option_change', 10, 2 ], + 'update_option_wp_rocket_settings' => [ 'clean_on_cdn_change', 11, 2 ], ]; } @@ -54,4 +55,16 @@ public function clean_css_fonts() { public function clean_on_option_change( $old_value, $value ) { $this->clean->clean_on_option_change( $old_value, $value ); } + + /** + * Clean CSS & fonts files stored locally on CDN change + * + * @param mixed $old_value Old option value. + * @param mixed $value New option value. + * + * @return void + */ + public function clean_on_cdn_change( $old_value, $value ) { + $this->clean->clean_on_cdn_change( $old_value, $value ); + } }