From 517e4ff86b8da19f25ce72902e9223bd5d41f30c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Thu, 10 Oct 2024 14:58:00 -0400 Subject: [PATCH 01/14] update mobile detect version --- .../mobiledetectlib/Mobile_Detect.php | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/inc/classes/dependencies/mobiledetect/mobiledetectlib/Mobile_Detect.php b/inc/classes/dependencies/mobiledetect/mobiledetectlib/Mobile_Detect.php index cfb51b9244..47d2e9c5a3 100644 --- a/inc/classes/dependencies/mobiledetect/mobiledetectlib/Mobile_Detect.php +++ b/inc/classes/dependencies/mobiledetect/mobiledetectlib/Mobile_Detect.php @@ -18,7 +18,7 @@ * @author Nick Ilyin * @author: Victor Stanciu (original author) * - * @version 2.8.41 + * @version 2.8.45 * * Auto-generated isXXXX() magic methods. * php -a examples/dump_magic_methods.php @@ -255,7 +255,7 @@ class WP_Rocket_Mobile_Detect /** * Stores the version number of the current release. */ - const VERSION = '2.8.41'; + const VERSION = '2.8.45'; /** * A type WP_Rocket_for the version() method indicating a string return value. @@ -416,7 +416,7 @@ class WP_Rocket_Mobile_Detect 'NexusTablet' => 'Android.*Nexus[\s]+(7|9|10)', // https://en.wikipedia.org/wiki/Pixel_C 'GoogleTablet' => 'Android.*Pixel C', - 'SamsungTablet' => 'SAMSUNG.*Tablet|Galaxy.*Tab|SC-01C|GT-P1000|GT-P1003|GT-P1010|GT-P3105|GT-P6210|GT-P6800|GT-P6810|GT-P7100|GT-P7300|GT-P7310|GT-P7500|GT-P7510|SCH-I800|SCH-I815|SCH-I905|SGH-I957|SGH-I987|SGH-T849|SGH-T859|SGH-T869|SPH-P100|GT-P3100|GT-P3108|GT-P3110|GT-P5100|GT-P5110|GT-P6200|GT-P7320|GT-P7511|GT-N8000|GT-P8510|SGH-I497|SPH-P500|SGH-T779|SCH-I705|SCH-I915|GT-N8013|GT-P3113|GT-P5113|GT-P8110|GT-N8010|GT-N8005|GT-N8020|GT-P1013|GT-P6201|GT-P7501|GT-N5100|GT-N5105|GT-N5110|SHV-E140K|SHV-E140L|SHV-E140S|SHV-E150S|SHV-E230K|SHV-E230L|SHV-E230S|SHW-M180K|SHW-M180L|SHW-M180S|SHW-M180W|SHW-M300W|SHW-M305W|SHW-M380K|SHW-M380S|SHW-M380W|SHW-M430W|SHW-M480K|SHW-M480S|SHW-M480W|SHW-M485W|SHW-M486W|SHW-M500W|GT-I9228|SCH-P739|SCH-I925|GT-I9200|GT-P5200|GT-P5210|GT-P5210X|SM-T311|SM-T310|SM-T310X|SM-T210|SM-T210R|SM-T211|SM-P600|SM-P601|SM-P605|SM-P900|SM-P901|SM-T217|SM-T217A|SM-T217S|SM-P6000|SM-T3100|SGH-I467|XE500|SM-T110|GT-P5220|GT-I9200X|GT-N5110X|GT-N5120|SM-P905|SM-T111|SM-T2105|SM-T315|SM-T320|SM-T320X|SM-T321|SM-T520|SM-T525|SM-T530NU|SM-T230NU|SM-T330NU|SM-T900|XE500T1C|SM-P605V|SM-P905V|SM-T337V|SM-T537V|SM-T707V|SM-T807V|SM-P600X|SM-P900X|SM-T210X|SM-T230|SM-T230X|SM-T325|GT-P7503|SM-T531|SM-T330|SM-T530|SM-T705|SM-T705C|SM-T535|SM-T331|SM-T800|SM-T700|SM-T537|SM-T807|SM-P907A|SM-T337A|SM-T537A|SM-T707A|SM-T807A|SM-T237|SM-T807P|SM-P607T|SM-T217T|SM-T337T|SM-T807T|SM-T116NQ|SM-T116BU|SM-P550|SM-T350|SM-T550|SM-T9000|SM-P9000|SM-T705Y|SM-T805|GT-P3113|SM-T710|SM-T810|SM-T815|SM-T360|SM-T533|SM-T113|SM-T335|SM-T715|SM-T560|SM-T670|SM-T677|SM-T377|SM-T567|SM-T357T|SM-T555|SM-T561|SM-T713|SM-T719|SM-T813|SM-T819|SM-T580|SM-T355Y?|SM-T280|SM-T817A|SM-T820|SM-W700|SM-P580|SM-T587|SM-P350|SM-P555M|SM-P355M|SM-T113NU|SM-T815Y|SM-T585|SM-T285|SM-T825|SM-W708|SM-T835|SM-T830|SM-T837V|SM-T720|SM-T510|SM-T387V|SM-P610|SM-T290|SM-T515|SM-T590|SM-T595|SM-T725|SM-T817P|SM-P585N0|SM-T395|SM-T295|SM-T865|SM-P610N|SM-P615|SM-T970|SM-T380|SM-T5950|SM-T905|SM-T231|SM-T500|SM-T860|SM-T536|SM-T837A|SM-X200|SM-T220|SM-T870|SM-X906C', // SCH-P709|SCH-P729|SM-T2558|GT-I9205 - Samsung Mega - treat them like a regular phone. + 'SamsungTablet' => 'SAMSUNG.*Tablet|Galaxy.*Tab|SC-01C|GT-P1000|GT-P1003|GT-P1010|GT-P3105|GT-P6210|GT-P6800|GT-P6810|GT-P7100|GT-P7300|GT-P7310|GT-P7500|GT-P7510|SCH-I800|SCH-I815|SCH-I905|SGH-I957|SGH-I987|SGH-T849|SGH-T859|SGH-T869|SPH-P100|GT-P3100|GT-P3108|GT-P3110|GT-P5100|GT-P5110|GT-P6200|GT-P7320|GT-P7511|GT-N8000|GT-P8510|SGH-I497|SPH-P500|SGH-T779|SCH-I705|SCH-I915|GT-N8013|GT-P3113|GT-P5113|GT-P8110|GT-N8010|GT-N8005|GT-N8020|GT-P1013|GT-P6201|GT-P7501|GT-N5100|GT-N5105|GT-N5110|SHV-E140K|SHV-E140L|SHV-E140S|SHV-E150S|SHV-E230K|SHV-E230L|SHV-E230S|SHW-M180K|SHW-M180L|SHW-M180S|SHW-M180W|SHW-M300W|SHW-M305W|SHW-M380K|SHW-M380S|SHW-M380W|SHW-M430W|SHW-M480K|SHW-M480S|SHW-M480W|SHW-M485W|SHW-M486W|SHW-M500W|GT-I9228|SCH-P739|SCH-I925|GT-I9200|GT-P5200|GT-P5210|GT-P5210X|SM-T311|SM-T310|SM-T310X|SM-T210|SM-T210R|SM-T211|SM-P600|SM-P601|SM-P605|SM-P900|SM-P901|SM-T217|SM-T217A|SM-T217S|SM-P6000|SM-T3100|SGH-I467|XE500|SM-T110|GT-P5220|GT-I9200X|GT-N5110X|GT-N5120|SM-P905|SM-T111|SM-T2105|SM-T315|SM-T320|SM-T320X|SM-T321|SM-T520|SM-T525|SM-T530NU|SM-T230NU|SM-T330NU|SM-T900|XE500T1C|SM-P605V|SM-P905V|SM-T337V|SM-T537V|SM-T707V|SM-T807V|SM-P600X|SM-P900X|SM-T210X|SM-T230|SM-T230X|SM-T325|GT-P7503|SM-T531|SM-T330|SM-T530|SM-T705|SM-T705C|SM-T535|SM-T331|SM-T800|SM-T700|SM-T537|SM-T807|SM-P907A|SM-T337A|SM-T537A|SM-T707A|SM-T807A|SM-T237|SM-T807P|SM-P607T|SM-T217T|SM-T337T|SM-T807T|SM-T116NQ|SM-T116BU|SM-P550|SM-T350|SM-T550|SM-T9000|SM-P9000|SM-T705Y|SM-T805|GT-P3113|SM-T710|SM-T810|SM-T815|SM-T360|SM-T533|SM-T113|SM-T335|SM-T715|SM-T560|SM-T670|SM-T677|SM-T377|SM-T567|SM-T357T|SM-T555|SM-T561|SM-T713|SM-T719|SM-T813|SM-T819|SM-T580|SM-T355Y?|SM-T280|SM-T817A|SM-T820|SM-W700|SM-P580|SM-T587|SM-P350|SM-P555M|SM-P355M|SM-T113NU|SM-T815Y|SM-T585|SM-T285|SM-T825|SM-W708|SM-T835|SM-T830|SM-T837V|SM-T720|SM-T510|SM-T387V|SM-P610|SM-T290|SM-T515|SM-T590|SM-T595|SM-T725|SM-T817P|SM-P585N0|SM-T395|SM-T295|SM-T865|SM-P610N|SM-P615|SM-T970|SM-T380|SM-T5950|SM-T905|SM-T231|SM-T500|SM-T860|SM-T536|SM-T837A|SM-X200|SM-T220|SM-T870|SM-X906C|SM-X700|SM-X706|SM-X706B|SM-X706U|SM-X706N|SM-X800|SM-X806|SM-X806B|SM-X806U|SM-X806N|SM-X900|SM-X906|SM-X906B|SM-X906U|SM-X906N|SM-P613', // SCH-P709|SCH-P729|SM-T2558|GT-I9205 - Samsung Mega - treat them like a regular phone. // http://docs.aws.amazon.com/silk/latest/developerguide/user-agent.html 'Kindle' => 'Kindle|Silk.*Accelerated|Android.*\b(KFOT|KFTT|KFJWI|KFJWA|KFOTE|KFSOWI|KFTHWI|KFTHWA|KFAPWI|KFAPWA|WFJWAE|KFSAWA|KFSAWI|KFASWI|KFARWI|KFFOWI|KFGIWI|KFMEWI)\b|Android.*Silk/[0-9.]+ like Chrome/[0-9.]+ (?!Mobile)', // Only the Surface tablets with Windows RT are considered mobile. @@ -769,7 +769,7 @@ class WP_Rocket_Mobile_Detect // http://scottcate.com/technology/windows-phone-8-ie10-desktop-or-mobile/ // https://github.com/serbanghita/Mobile-Detect/issues/57#issuecomment-15024011 // https://developers.facebook.com/docs/sharing/webmasters/crawler/ - 'Bot' => 'Googlebot|facebookexternalhit|Google-AMPHTML|s~amp-validator|AdsBot-Google|Google Keyword Suggestion|Facebot|YandexBot|YandexMobileBot|bingbot|ia_archiver|AhrefsBot|Ezooms|GSLFbot|WBSearchBot|Twitterbot|TweetmemeBot|Twikle|PaperLiBot|Wotbox|UnwindFetchor|Exabot|MJ12bot|YandexImages|TurnitinBot|Pingdom|contentkingapp|AspiegelBot', + 'Bot' => 'Googlebot|facebookexternalhit|Google-AMPHTML|s~amp-validator|AdsBot-Google|Google Keyword Suggestion|Facebot|YandexBot|YandexMobileBot|bingbot|ia_archiver|AhrefsBot|Ezooms|GSLFbot|WBSearchBot|Twitterbot|TweetmemeBot|Twikle|PaperLiBot|Wotbox|UnwindFetchor|Exabot|MJ12bot|YandexImages|TurnitinBot|Pingdom|contentkingapp|AspiegelBot|Semrush|DotBot|PetalBot|MetadataScraper', 'MobileBot' => 'Googlebot-Mobile|AdsBot-Google-Mobile|YahooSeeker/M1A1-R2D2', 'DesktopMode' => 'WPDesktop', 'TV' => 'SonyDTV|HbbTV', // experimental @@ -1180,10 +1180,10 @@ public static function getMobileDetectionRules() if (!$rules) { $rules = array_merge( - self::$phoneDevices, - self::$tabletDevices, - self::$operatingSystems, - self::$browsers + self::getPhoneDevices(), + self::getTabletDevices(), + self::getOperatingSystems(), + self::getBrowsers() ); } @@ -1208,11 +1208,11 @@ public function getMobileDetectionRulesExtended() if (!$rules) { // Merge all rules together. $rules = array_merge( - self::$phoneDevices, - self::$tabletDevices, - self::$operatingSystems, - self::$browsers, - self::$utilities + static::getPhoneDevices(), + static::getTabletDevices(), + static::getOperatingSystems(), + static::getBrowsers(), + static::getUtilities() ); } @@ -1406,7 +1406,7 @@ public function isTablet($userAgent = null, $httpHeaders = null) $this->setDetectionType(self::DETECTION_TYPE_MOBILE); - foreach (self::$tabletDevices as $_regex) { + foreach (static::getTabletDevices() as $_regex) { if ($this->match($_regex, $userAgent)) { return true; } @@ -1462,7 +1462,7 @@ public function match($regex, $userAgent = null) return false; } - $match = (bool) preg_match(sprintf('#%s#is', $regex), (false === empty($userAgent) ? $userAgent : $this->userAgent), $matches); + $match = (bool) preg_match(sprintf('#%s#is', $regex), (false === empty($userAgent) ? $userAgent : (is_string($this->userAgent) ? $this->userAgent : '')), $matches); // If positive match is found, store the results WP_Rocket_for debug. if ($match) { $this->matchingRegex = $regex; @@ -1531,7 +1531,7 @@ public function version($propertyName, $type = self::VERSION_TYPE_STRING) $type = self::VERSION_TYPE_STRING; } - $properties = self::getProperties(); + $properties = static::getProperties(); // Check if the property exists in the properties array. if (true === isset($properties[$propertyName])) { @@ -1545,7 +1545,7 @@ public function version($propertyName, $type = self::VERSION_TYPE_STRING) $propertyPattern = str_replace('[VER]', self::VER, $propertyMatchString); // Identify and extract the version. - preg_match(sprintf('#%s#is', $propertyPattern), $this->userAgent, $match); + preg_match(sprintf('#%s#is', $propertyPattern), (is_string($this->userAgent) ? $this->userAgent : ''), $match); if (false === empty($match[1])) { $version = ($type == self::VERSION_TYPE_FLOAT ? $this->prepareVersionNo($match[1]) : $match[1]); From c3f30a4b413b171b34fa27cb8f0df80ecfef4870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Thu, 10 Oct 2024 14:58:23 -0400 Subject: [PATCH 02/14] add OCI & ALR features to support data --- inc/Engine/Support/Data.php | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/inc/Engine/Support/Data.php b/inc/Engine/Support/Data.php index 59b624c112..bebd84fdad 100644 --- a/inc/Engine/Support/Data.php +++ b/inc/Engine/Support/Data.php @@ -1,4 +1,5 @@ to_send, array_filter( $this->options->get_options() ) ); - return [ 'Website' => home_url(), 'WordPress Version' => get_bloginfo( 'version' ), - 'WP Rocket Version' => rocket_get_constant( 'WP_ROCKET_VERSION' ), + 'WP Rocket Version' => rocket_get_constant( 'WP_ROCKET_VERSION', '' ), 'Theme' => wp_get_theme()->get( 'Name' ), 'Plugins Enabled' => implode( ' - ', rocket_get_active_plugins() ), - 'WP Rocket Active Options' => implode( ' - ', $active_options ), + 'WP Rocket Active Options' => implode( ' - ', $this->get_active_options() ), ]; } + + /** + * Returns the active options + * + * @return array + */ + public function get_active_options(): array { + $active_options = array_intersect_key( $this->to_send, array_filter( $this->options->get_options() ) ); + + // This filter is documented in inc/Engine/Media/AboveTheFold/Context/Context.php. + if ( wpm_apply_filters_typed( 'boolean', 'rocket_above_the_fold_optimization', true ) ) { + $active_options['optimize_critical_images'] = 'Optimize Critical Images'; + } + + // This filter is documented in inc/Engine/Optimization/LazyRenderContent/Context/Context.php. + if ( wpm_apply_filters_typed( 'boolean', 'rocket_lrc_optimization', true ) ) { + $active_options['automatic_lazy_rendering'] = 'Automatic Lazy Rendering'; + } + + return $active_options; + } } From 68bf3616fcc2763292092cb50bc9b0c899aa89f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 15 Oct 2024 14:52:06 -0400 Subject: [PATCH 03/14] add Meta class --- inc/Engine/Support/Meta.php | 113 ++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 inc/Engine/Support/Meta.php diff --git a/inc/Engine/Support/Meta.php b/inc/Engine/Support/Meta.php new file mode 100644 index 0000000000..dd420b36a3 --- /dev/null +++ b/inc/Engine/Support/Meta.php @@ -0,0 +1,113 @@ +mobile_detect = $mobile_detect; + $this->options = $options; + } + + /** + * Add the WP Rocket meta generator tag to the HTML + * + * @param string $html The HTML content. + * @return string + */ + public function add_meta_generator( $html ): string { + if ( rocket_bypass() ) { + return $html; + } + + if ( rocket_get_constant( 'DONOTROCKETOPTIMIZE' ) ) { + return $html; + } + + if ( rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_FOOTPRINT', false ) ) { + return $html; + } + + if ( wpm_apply_filters_typed( 'boolean', 'rocket_disable_meta_generator', false ) ) { + return $html; + } + + if ( false === preg_match_all( '//i', $html, $comments, PREG_PATTERN_ORDER ) ) { + return $html; + } + + $meta = $this->get_meta_tag( $comments['feature'] ); + + $result = preg_replace( '/<\/head>/i', $meta . '', $html, 1 ); + + if ( null === $result ) { + return $html; + } + + $result = preg_replace( '//i', '', $result ); + + if ( null === $result ) { + return $html; + } + + return $result; + } + + /** + * Get the WP Rocket meta generator tag + * + * @param array $features Features to add to the meta tag. + * + * @return string + */ + private function get_meta_tag( array $features = [] ): string { + if ( wpm_apply_filters_typed( 'boolean', 'do_rocket_generate_caching_files', true ) ) { + $features[] = 'wpr_cached'; + + if ( $this->options->get( 'do_caching_mobile_files', 0 ) ) { + $features[] = $this->mobile_detect->isMobile() ? 'wpr_cached_mobile' : 'wpr_cached_desktop'; + } + } + + if ( ! empty( rocket_get_dns_prefetch_domains() ) ) { + $features[] = 'wpr_dns_prefetch'; + } + + if ( (bool) $this->options->get( 'preload_links', 0 ) ) { + $features[] = 'wpr_preload_links'; + } + + $content = ''; + + if ( wpm_apply_filters_typed( 'boolean', 'rocket_display_meta_generator_content', true ) ) { + $content = ' content="WP Rocket ' . rocket_get_constant( 'WP_ROCKET_VERSION', '' ) . '"'; + } + + $meta = ''; + + return $meta; + } +} From 19a618975d494f571943e255d58c6a0a267b1e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 15 Oct 2024 14:52:12 -0400 Subject: [PATCH 04/14] add CommentTrait --- inc/Engine/Support/CommentTrait.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 inc/Engine/Support/CommentTrait.php diff --git a/inc/Engine/Support/CommentTrait.php b/inc/Engine/Support/CommentTrait.php new file mode 100644 index 0000000000..7d4b62e6d3 --- /dev/null +++ b/inc/Engine/Support/CommentTrait.php @@ -0,0 +1,21 @@ +#', '', $html, 1 ); + + if ( null === $result ) { + return $html; + } + + return $result; + } +} From 29a14c46accbc33778d586a839c7bc2ce6601d9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 15 Oct 2024 14:52:28 -0400 Subject: [PATCH 05/14] update Subscriber to add meta generator --- inc/Engine/Support/Subscriber.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/inc/Engine/Support/Subscriber.php b/inc/Engine/Support/Subscriber.php index 9f0068ce82..88f58ddd74 100644 --- a/inc/Engine/Support/Subscriber.php +++ b/inc/Engine/Support/Subscriber.php @@ -1,4 +1,5 @@ rest = $rest; + $this->meta = $meta; } /** @@ -29,6 +39,7 @@ public function __construct( Rest $rest ) { public static function get_subscribed_events() { return [ 'rest_api_init' => 'register_support_route', + 'rocket_buffer' => [ 'add_meta_generator', PHP_INT_MAX ], ]; } @@ -42,4 +53,14 @@ public static function get_subscribed_events() { public function register_support_route() { $this->rest->register_route(); } + + /** + * Add the WP Rocket meta generator tag to the HTML + * + * @param string $html The HTML content. + * @return string + */ + public function add_meta_generator( $html ): string { + return $this->meta->add_meta_generator( $html ); + } } From 9d6d0c83c6e00a145e741c37fbff2b565ec3f075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 15 Oct 2024 14:52:34 -0400 Subject: [PATCH 06/14] update instantiation --- inc/Engine/Support/ServiceProvider.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/inc/Engine/Support/ServiceProvider.php b/inc/Engine/Support/ServiceProvider.php index 3dea74891b..e16d26a386 100644 --- a/inc/Engine/Support/ServiceProvider.php +++ b/inc/Engine/Support/ServiceProvider.php @@ -1,11 +1,10 @@ getContainer()->get( 'options' ); + $this->getContainer()->add( 'mobile_detect', WP_Rocket_Mobile_Detect::class ); + $this->getContainer()->add( 'support_data', Data::class ) ->addArgument( $options ); - $this->getContainer()->add( 'rest_support', Rest::class ) + $this->getContainer()->add( 'support_rest', Rest::class ) ->addArgument( $this->getContainer()->get( 'support_data' ) ) ->addArgument( $options ); + $this->getContainer()->add( 'support_meta', Meta::class ) + ->addArgument( $this->getContainer()->get( 'mobile_detect' ) ) + ->addArgument( $options ); $this->getContainer()->addShared( 'support_subscriber', Subscriber::class ) - ->addArgument( $this->getContainer()->get( 'rest_support' ) ); + ->addArgument( $this->getContainer()->get( 'support_rest' ) ) + ->addArgument( $this->getContainer()->get( 'support_meta' ) ); } } From 921c7d79e61f6134d870ba434cd19cb53aac8757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 15 Oct 2024 14:52:46 -0400 Subject: [PATCH 07/14] use CommentTrait --- inc/Addon/WebP/Subscriber.php | 5 +++++ inc/Engine/CDN/CDN.php | 5 ++++- inc/Engine/CriticalPath/CriticalCSSSubscriber.php | 6 +++++- .../Media/AboveTheFold/Frontend/Controller.php | 6 +++++- inc/Engine/Media/ImageDimensions/ImageDimensions.php | 6 +++++- inc/Engine/Media/Lazyload/CSS/Subscriber.php | 6 +++++- inc/Engine/Media/Lazyload/Subscriber.php | 6 ++++++ inc/Engine/Optimization/DeferJS/DeferJS.php | 5 ++++- inc/Engine/Optimization/DelayJS/HTML.php | 6 +++++- .../LazyRenderContent/Frontend/Controller.php | 7 ++++++- inc/Engine/Optimization/Minify/CSS/Minify.php | 4 +++- inc/Engine/Optimization/Minify/JS/Combine.php | 5 ++++- inc/Engine/Optimization/Minify/JS/Minify.php | 5 ++++- inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php | 4 +++- inc/Engine/Preload/Fonts.php | 12 +++++++++++- 15 files changed, 75 insertions(+), 13 deletions(-) diff --git a/inc/Addon/WebP/Subscriber.php b/inc/Addon/WebP/Subscriber.php index 8951d6fa7c..a245e6467e 100644 --- a/inc/Addon/WebP/Subscriber.php +++ b/inc/Addon/WebP/Subscriber.php @@ -7,6 +7,7 @@ use WP_Rocket\Admin\Options_Data; use WP_Rocket\Event_Management\Subscriber_Interface; use WP_Rocket\Engine\CDN\Subscriber as CDNSubscriber; +use WP_Rocket\Engine\Support\CommentTrait; /** * Subscriber for the WebP support. @@ -14,6 +15,8 @@ * @since 3.4 */ class Subscriber extends AbstractWebp implements Subscriber_Interface { + use CommentTrait; + /** * Options_Data instance. * @@ -158,6 +161,8 @@ public function convert_to_webp( $html ) { $has_webp = apply_filters( 'rocket_page_has_webp_files', $has_webp, $html ); if ( $has_webp ) { + $html = $this->add_meta_comment( 'cache_webp', $html ); + return $html . ''; } diff --git a/inc/Engine/CDN/CDN.php b/inc/Engine/CDN/CDN.php index 985e037328..ba864553d1 100644 --- a/inc/Engine/CDN/CDN.php +++ b/inc/Engine/CDN/CDN.php @@ -4,6 +4,7 @@ namespace WP_Rocket\Engine\CDN; use WP_Rocket\Admin\Options_Data; +use WP_Rocket\Engine\Support\CommentTrait; /** * CDN class @@ -11,6 +12,8 @@ * @since 3.4 */ class CDN { + use CommentTrait; + /** * WP Rocket Options instance * @@ -80,7 +83,7 @@ public function rewrite( $html ) { $html = str_replace( $match[0], $cdn_url, $html ); } - return $html; + return $this->add_meta_comment( 'cdn', $html ); } /** diff --git a/inc/Engine/CriticalPath/CriticalCSSSubscriber.php b/inc/Engine/CriticalPath/CriticalCSSSubscriber.php index ffba529501..4137c14485 100644 --- a/inc/Engine/CriticalPath/CriticalCSSSubscriber.php +++ b/inc/Engine/CriticalPath/CriticalCSSSubscriber.php @@ -8,6 +8,7 @@ use WP_Rocket\Engine\Optimization\RegexTrait; use WP_Rocket\Event_Management\Subscriber_Interface; use WP_Filesystem_Direct; +use WP_Rocket\Engine\Support\CommentTrait; /** * Critical CSS Subscriber. @@ -16,6 +17,7 @@ */ class CriticalCSSSubscriber implements Subscriber_Interface { use RegexTrait; + use CommentTrait; /** * Instance of Critical CSS. @@ -602,7 +604,9 @@ public function insert_critical_css_buffer( $buffer ) { 1 ); - return preg_replace( '##iU', $this->return_remove_cpcss_script() . '', $buffer, 1 ); + $buffer = preg_replace( '##iU', $this->return_remove_cpcss_script() . '', $buffer, 1 ); + + return $this->add_meta_comment( 'async_css', $buffer ); } /** diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index e34481d969..530a525e48 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -9,10 +9,12 @@ use WP_Rocket\Engine\Optimization\RegexTrait; use WP_Rocket\Engine\Optimization\UrlTrait; use WP_Rocket\Engine\Common\PerformanceHints\Frontend\ControllerInterface; +use WP_Rocket\Engine\Support\CommentTrait; class Controller implements ControllerInterface { use RegexTrait; use UrlTrait; + use CommentTrait; /** * Options instance @@ -61,7 +63,9 @@ public function optimize( string $html, $row ): string { return $html; } - return $this->preload_lcp( $html, $row ); + $html = $this->preload_lcp( $html, $row ); + + return $this->add_meta_comment( 'oci', $html ); } /** diff --git a/inc/Engine/Media/ImageDimensions/ImageDimensions.php b/inc/Engine/Media/ImageDimensions/ImageDimensions.php index ca2f8fc866..4f4934ad27 100644 --- a/inc/Engine/Media/ImageDimensions/ImageDimensions.php +++ b/inc/Engine/Media/ImageDimensions/ImageDimensions.php @@ -8,10 +8,12 @@ use WP_Rocket\Admin\Options_Data; use WP_Rocket\Engine\Admin\Settings\Settings; use WP_Rocket\Engine\Optimization\RegexTrait; +use WP_Rocket\Engine\Support\CommentTrait; use WP_Rocket\Logger\Logger; class ImageDimensions { use RegexTrait; + use CommentTrait; /** * Options_Data instance @@ -155,7 +157,9 @@ public function specify_image_dimensions( $html ) { return $html; } - return str_replace( array_keys( $replaces ), $replaces, $html ); + $html = str_replace( array_keys( $replaces ), $replaces, $html ); + + return $this->add_meta_comment( 'image_dimensions', $html ); } /** diff --git a/inc/Engine/Media/Lazyload/CSS/Subscriber.php b/inc/Engine/Media/Lazyload/CSS/Subscriber.php index 0a33ec5350..bd3051d6a7 100644 --- a/inc/Engine/Media/Lazyload/CSS/Subscriber.php +++ b/inc/Engine/Media/Lazyload/CSS/Subscriber.php @@ -16,6 +16,7 @@ TagGenerator}; use WP_Rocket\Engine\Common\Cache\CacheInterface; use WP_Rocket\Engine\Optimization\RegexTrait; +use WP_Rocket\Engine\Support\CommentTrait; use WP_Rocket\Event_Management\Subscriber_Interface; use WP_Rocket\Logger\LoggerAware; use WP_Rocket\Logger\LoggerAwareInterface; @@ -23,6 +24,7 @@ class Subscriber implements Subscriber_Interface, LoggerAwareInterface { use LoggerAware; use RegexTrait; + use CommentTrait; /** * Extract background images from CSS. @@ -205,7 +207,9 @@ public function maybe_replace_css_images( string $html ): string { ) ); - return $output['html']; + $html = $this->add_meta_comment( 'lazyload_css_bg_img', $output['html'] ); + + return $html; } /** diff --git a/inc/Engine/Media/Lazyload/Subscriber.php b/inc/Engine/Media/Lazyload/Subscriber.php index 2b35cfe5bc..23c0a5eff4 100644 --- a/inc/Engine/Media/Lazyload/Subscriber.php +++ b/inc/Engine/Media/Lazyload/Subscriber.php @@ -8,6 +8,7 @@ use WP_Rocket\Dependencies\RocketLazyload\Iframe; use WP_Rocket\Admin\Options_Data; use WP_Rocket\Engine\Optimization\RegexTrait; +use WP_Rocket\Engine\Support\CommentTrait; use WP_Rocket\Event_Management\Subscriber_Interface; /** @@ -18,6 +19,7 @@ class Subscriber implements Subscriber_Interface { use RegexTrait; use CanLazyloadTrait; + use CommentTrait; const SCRIPT_VERSION = '17.8.3'; @@ -330,6 +332,8 @@ public function lazyload( $html ) { ]; $html = $this->iframe->lazyloadIframes( $html, $buffer, $args ); + + $html = $this->add_meta_comment( 'lazyload_iframes', $html ); } if ( $this->can_lazyload_images() ) { @@ -352,6 +356,8 @@ public function lazyload( $html ) { if ( apply_filters( 'rocket_lazyload_background_images', true ) ) { $html = $this->image->lazyloadBackgroundImages( $html, $buffer ); } + + $html = $this->add_meta_comment( 'lazyload_images', $html ); } return $html; diff --git a/inc/Engine/Optimization/DeferJS/DeferJS.php b/inc/Engine/Optimization/DeferJS/DeferJS.php index 70f27fe0cc..6d73858135 100644 --- a/inc/Engine/Optimization/DeferJS/DeferJS.php +++ b/inc/Engine/Optimization/DeferJS/DeferJS.php @@ -5,8 +5,11 @@ use WP_Rocket\Admin\Options_Data; use WP_Rocket\Engine\Optimization\DynamicLists\DefaultLists\DataManager; +use WP_Rocket\Engine\Support\CommentTrait; class DeferJS { + use CommentTrait; + /** * Options instance * @@ -86,7 +89,7 @@ public function defer_js( string $html ): string { $html = str_replace( $tag[0], $deferred_tag, $html ); } - return $html; + return $this->add_meta_comment( 'defer_js', $html ); } /** diff --git a/inc/Engine/Optimization/DelayJS/HTML.php b/inc/Engine/Optimization/DelayJS/HTML.php index 7acd06bc09..05cfe4971d 100644 --- a/inc/Engine/Optimization/DelayJS/HTML.php +++ b/inc/Engine/Optimization/DelayJS/HTML.php @@ -6,10 +6,12 @@ use WP_Rocket\Admin\Options_Data; use WP_Rocket\Engine\Optimization\DynamicLists\DefaultLists\DataManager; use WP_Rocket\Engine\Optimization\RegexTrait; +use WP_Rocket\Engine\Support\CommentTrait; use WP_Rocket\Logger\Logger; class HTML { use RegexTrait; + use CommentTrait; /** * Plugin options instance. @@ -122,7 +124,9 @@ function ( $value ) { $this->excluded ); - return $this->parse( $html ); + $html = $this->parse( $html ); + + return $this->add_meta_comment( 'delay_js', $html ); } /** diff --git a/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php b/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php index df9a478ada..e7533f587d 100644 --- a/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php +++ b/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller.php @@ -6,8 +6,11 @@ use WP_Rocket\Engine\Common\Context\ContextInterface; use WP_Rocket\Engine\Common\PerformanceHints\Frontend\ControllerInterface; use WP_Rocket\Engine\Optimization\LazyRenderContent\Frontend\Processor\Processor; +use WP_Rocket\Engine\Support\CommentTrait; class Controller implements ControllerInterface { + use CommentTrait; + /** * Processor instance * @@ -69,7 +72,9 @@ public function optimize( string $html, $row ): string { $html = $result; $html = $this->remove_hashes( $html ); - return $this->add_css( $html ); + $html = $this->add_css( $html ); + + return $this->add_meta_comment( 'automatic_lazy_rendering', $html ); } /** diff --git a/inc/Engine/Optimization/Minify/CSS/Minify.php b/inc/Engine/Optimization/Minify/CSS/Minify.php index 69431919e1..6e94222aa0 100644 --- a/inc/Engine/Optimization/Minify/CSS/Minify.php +++ b/inc/Engine/Optimization/Minify/CSS/Minify.php @@ -4,6 +4,7 @@ use WP_Rocket\Dependencies\Minify as Minifier; use WP_Rocket\Engine\Optimization\CSSTrait; use WP_Rocket\Engine\Optimization\Minify\ProcessorInterface; +use WP_Rocket\Engine\Support\CommentTrait; use WP_Rocket\Logger\Logger; /** @@ -13,6 +14,7 @@ */ class Minify extends AbstractCSSOptimization implements ProcessorInterface { use CSSTrait; + use CommentTrait; /** * Minifies CSS files @@ -77,7 +79,7 @@ public function optimize( $html ) { $html = $this->replace_style( $style, $minify_url, $html ); } - return $html; + return $this->add_meta_comment( 'minify_css', $html ); } /** diff --git a/inc/Engine/Optimization/Minify/JS/Combine.php b/inc/Engine/Optimization/Minify/JS/Combine.php index cf6e75a941..2658ee4c32 100644 --- a/inc/Engine/Optimization/Minify/JS/Combine.php +++ b/inc/Engine/Optimization/Minify/JS/Combine.php @@ -7,6 +7,7 @@ use WP_Rocket\Engine\Optimization\DeferJS\DeferJS; use WP_Rocket\Engine\Optimization\DynamicLists\DynamicLists; use WP_Rocket\Engine\Optimization\Minify\ProcessorInterface; +use WP_Rocket\Engine\Support\CommentTrait; use WP_Rocket\Logger\Logger; /** @@ -15,6 +16,8 @@ * @since 3.1 */ class Combine extends AbstractJSOptimization implements ProcessorInterface { + use CommentTrait; + /** * Minifier instance * @@ -155,7 +158,7 @@ public function optimize( $html ) { ] ); - return $html; + return $this->add_meta_comment( 'minify_concatenate_js', $html ); } /** diff --git a/inc/Engine/Optimization/Minify/JS/Minify.php b/inc/Engine/Optimization/Minify/JS/Minify.php index 91961cf9d6..56bc094087 100644 --- a/inc/Engine/Optimization/Minify/JS/Minify.php +++ b/inc/Engine/Optimization/Minify/JS/Minify.php @@ -3,6 +3,7 @@ use WP_Rocket\Dependencies\Minify as Minifier; use WP_Rocket\Engine\Optimization\Minify\ProcessorInterface; +use WP_Rocket\Engine\Support\CommentTrait; use WP_Rocket\Logger\Logger; /** @@ -11,6 +12,8 @@ * @since 3.1 */ class Minify extends AbstractJSOptimization implements ProcessorInterface { + use CommentTrait; + /** * Minifies JS files * @@ -111,7 +114,7 @@ public function optimize( $html ) { $html = $this->replace_script( $script, $minify_url, $html ); } - return $html; + return $this->add_meta_comment( 'minify_js', $html ); } /** diff --git a/inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php b/inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php index 8cbb154ffd..6c19692474 100644 --- a/inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php +++ b/inc/Engine/Optimization/RUCSS/Controller/UsedCSS.php @@ -10,10 +10,12 @@ use WP_Rocket\Engine\Optimization\RegexTrait; use WP_Rocket\Engine\Optimization\RUCSS\Database\Queries\UsedCSS as UsedCSS_Query; use WP_Rocket\Engine\Optimization\RUCSS\Jobs\Manager; +use WP_Rocket\Engine\Support\CommentTrait; class UsedCSS { use RegexTrait; use CSSTrait; + use CommentTrait; /** * UsedCss Query instance. @@ -165,7 +167,7 @@ public function treeshake( string $html ): string { $this->used_css_query->update_last_accessed( (int) $used_css->id ); } - return $html; + return $this->add_meta_comment( 'remove_unused_css', $html ); } /** diff --git a/inc/Engine/Preload/Fonts.php b/inc/Engine/Preload/Fonts.php index 891d9a958e..ae190ab932 100644 --- a/inc/Engine/Preload/Fonts.php +++ b/inc/Engine/Preload/Fonts.php @@ -4,6 +4,7 @@ use WP_Rocket\Admin\Options_Data; use WP_Rocket\Engine\CDN\CDN; +use WP_Rocket\Engine\Support\CommentTrait; use WP_Rocket\Event_Management\Subscriber_Interface; /** @@ -12,6 +13,7 @@ * @since 3.6 */ class Fonts implements Subscriber_Interface { + use CommentTrait; /** * WP Rocket Options instance. @@ -114,7 +116,15 @@ public function preload_fonts( $html ): string { ); } - return preg_replace( '##', $preloads, $html, 1 ); + $result = preg_replace( '##', $preloads, $html, 1 ); + + if ( null === $result ) { + return $html; + } + + $result = $this->add_meta_comment( 'preload_fonts', $result ); + + return $result; } /** From 28a54b19e49bfcdfca5fd8f6e17e7fcd52833440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 15 Oct 2024 15:38:20 -0400 Subject: [PATCH 08/14] fix PHPCS --- inc/Engine/CriticalPath/CriticalCSSSubscriber.php | 2 +- inc/Engine/Support/CommentTrait.php | 8 ++++++++ inc/Engine/Support/Data.php | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/inc/Engine/CriticalPath/CriticalCSSSubscriber.php b/inc/Engine/CriticalPath/CriticalCSSSubscriber.php index 4137c14485..8d663f8e99 100644 --- a/inc/Engine/CriticalPath/CriticalCSSSubscriber.php +++ b/inc/Engine/CriticalPath/CriticalCSSSubscriber.php @@ -604,7 +604,7 @@ public function insert_critical_css_buffer( $buffer ) { 1 ); - $buffer = preg_replace( '##iU', $this->return_remove_cpcss_script() . '', $buffer, 1 ); + $buffer = preg_replace( '##iU', $this->return_remove_cpcss_script() . '', $buffer, 1 ); return $this->add_meta_comment( 'async_css', $buffer ); } diff --git a/inc/Engine/Support/CommentTrait.php b/inc/Engine/Support/CommentTrait.php index 7d4b62e6d3..86a75ece08 100644 --- a/inc/Engine/Support/CommentTrait.php +++ b/inc/Engine/Support/CommentTrait.php @@ -4,6 +4,14 @@ namespace WP_Rocket\Engine\Support; trait CommentTrait { + /** + * Add a comment to the HTML + * + * @param string $feature The feature name. + * @param string $html The HTML content. + * + * @return string + */ public function add_meta_comment( $feature, $html ) { // This filter is documented in inc/Engine/Support/Meta.php. if ( wpm_apply_filters_typed( 'boolean', 'rocket_disable_meta_generator', false ) ) { diff --git a/inc/Engine/Support/Data.php b/inc/Engine/Support/Data.php index bebd84fdad..25129429a4 100644 --- a/inc/Engine/Support/Data.php +++ b/inc/Engine/Support/Data.php @@ -81,7 +81,7 @@ public function get_active_options(): array { $active_options = array_intersect_key( $this->to_send, array_filter( $this->options->get_options() ) ); // This filter is documented in inc/Engine/Media/AboveTheFold/Context/Context.php. - if ( wpm_apply_filters_typed( 'boolean', 'rocket_above_the_fold_optimization', true ) ) { + if ( wpm_apply_filters_typed( 'boolean', 'rocket_above_the_fold_optimization', true ) ) { $active_options['optimize_critical_images'] = 'Optimize Critical Images'; } From 42b4a688ed540612de444c8dbf59e638fd0a85d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 16 Oct 2024 16:26:10 -0400 Subject: [PATCH 09/14] update fixture for OCI and ALR --- tests/Fixtures/inc/Engine/Support/Data/getSupportData.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fixtures/inc/Engine/Support/Data/getSupportData.php b/tests/Fixtures/inc/Engine/Support/Data/getSupportData.php index ba803de2e7..504a41d44c 100644 --- a/tests/Fixtures/inc/Engine/Support/Data/getSupportData.php +++ b/tests/Fixtures/inc/Engine/Support/Data/getSupportData.php @@ -79,7 +79,7 @@ 'WP Rocket Version' => '3.7.5', 'Theme' => 'WordPress Default', 'Plugins Enabled' => 'Hello Dolly', - 'WP Rocket Active Options' => 'Mobile Cache - Disable Emojis - Combine Google Fonts - Preload - Heartbeat Control', + 'WP Rocket Active Options' => 'Mobile Cache - Disable Emojis - Combine Google Fonts - Preload - Heartbeat Control - Optimize Critical Images - Automatic Lazy Rendering', ], ], ]; From facca0b8c0f084ad16364d4aee1668c1b27c8d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Wed, 16 Oct 2024 16:49:55 -0400 Subject: [PATCH 10/14] add filter to remove HTML comment --- tests/Unit/inc/Addon/WebP/Subscriber/convertToWebp.php | 5 +++++ tests/Unit/inc/Engine/CDN/CDN/rewrite.php | 7 ++++++- .../CriticalCSSSubscriber/insertCriticalCssBuffer.php | 7 ++++++- .../ImageDimensions/specifyImageDimensions.php | 5 +++++ .../Lazyload/CSS/Subscriber/maybeReplaceCssImages.php | 5 +++++ .../inc/Engine/Optimization/DelayJS/HTML/delayJs.php | 6 ++++++ .../LazyRenderContent/Frontend/Controller/optimize.php | 6 ++++++ .../Engine/Optimization/Minify/CSS/Minify/optimize.php | 5 +++++ .../Engine/Optimization/Minify/JS/Combine/optimize.php | 5 +++++ .../Engine/Optimization/Minify/JS/Minify/optimize.php | 5 +++++ .../Optimization/RUCSS/Controller/UsedCSS/treeshake.php | 9 +++++++-- tests/Unit/inc/Engine/Preload/Fonts/preloadFonts.php | 4 ++++ 12 files changed, 65 insertions(+), 4 deletions(-) diff --git a/tests/Unit/inc/Addon/WebP/Subscriber/convertToWebp.php b/tests/Unit/inc/Addon/WebP/Subscriber/convertToWebp.php index 323a3ffc62..6adb3c7948 100644 --- a/tests/Unit/inc/Addon/WebP/Subscriber/convertToWebp.php +++ b/tests/Unit/inc/Addon/WebP/Subscriber/convertToWebp.php @@ -40,6 +40,11 @@ public function setUp(): void { * @dataProvider providerTestData */ public function testShouldReturnExpected( $config, $original, $expected ) { + Filters\expectApplied( 'rocket_disable_meta_generator' ) + ->atMost() + ->once() + ->andReturn( true ); + $this->options->shouldReceive( 'get' ) ->with( 'cache_webp', 0 ) ->andReturn( $config['webp'] ); diff --git a/tests/Unit/inc/Engine/CDN/CDN/rewrite.php b/tests/Unit/inc/Engine/CDN/CDN/rewrite.php index 0f6d13c994..79d0e4e8b1 100644 --- a/tests/Unit/inc/Engine/CDN/CDN/rewrite.php +++ b/tests/Unit/inc/Engine/CDN/CDN/rewrite.php @@ -3,7 +3,7 @@ namespace WP_Rocket\Tests\Unit\inc\Engine\CDN\CDN; use Mockery; -use Brain\Monkey\Functions; +use Brain\Monkey\{Filters, Functions}; use WP_Rocket\Admin\Options_Data; use WP_Rocket\Engine\CDN\CDN; @@ -26,6 +26,11 @@ public function setUp() : void { * @dataProvider providerTestData */ public function testShouldRewriteURLToCDN( $home_url, $original, $expected ) { + Filters\expectApplied( 'rocket_disable_meta_generator' ) + ->atMost() + ->once() + ->andReturn( true ); + Functions\when( 'content_url' )->justReturn( "{$home_url}/wp-content/" ); Functions\when( 'includes_url' )->justReturn( "{$home_url}/wp-includes/" ); Functions\when( 'wp_upload_dir' )->justReturn( "{$home_url}/wp-content/uploads/" ); diff --git a/tests/Unit/inc/Engine/CriticalPath/CriticalCSSSubscriber/insertCriticalCssBuffer.php b/tests/Unit/inc/Engine/CriticalPath/CriticalCSSSubscriber/insertCriticalCssBuffer.php index 648919b6b4..f653214891 100644 --- a/tests/Unit/inc/Engine/CriticalPath/CriticalCSSSubscriber/insertCriticalCssBuffer.php +++ b/tests/Unit/inc/Engine/CriticalPath/CriticalCSSSubscriber/insertCriticalCssBuffer.php @@ -2,7 +2,7 @@ namespace WP_Rocket\Tests\Unit\inc\Engine\CriticalPath\CriticalCSSSubscriber; -use Brain\Monkey\Functions; +use Brain\Monkey\{Filters, Functions}; use WP_Rocket\Tests\Unit\FilesystemTestCase; /** @@ -33,6 +33,11 @@ public function setUp() : void { * @dataProvider providerTestData */ public function testShouldInsertCriticalCSS( $config, $expected, $expected_html = null ) { + Filters\expectApplied( 'rocket_disable_meta_generator' ) + ->atMost() + ->once() + ->andReturn( true ); + $critical_css_path = $this->config['vfs_dir'] . '1/'; $this->assertTrue( $this->filesystem->is_dir( $critical_css_path ) ); diff --git a/tests/Unit/inc/Engine/Media/ImageDimensions/ImageDimensions/specifyImageDimensions.php b/tests/Unit/inc/Engine/Media/ImageDimensions/ImageDimensions/specifyImageDimensions.php index 9741c10a22..8636d63684 100644 --- a/tests/Unit/inc/Engine/Media/ImageDimensions/ImageDimensions/specifyImageDimensions.php +++ b/tests/Unit/inc/Engine/Media/ImageDimensions/ImageDimensions/specifyImageDimensions.php @@ -52,6 +52,11 @@ public function testShouldAddMissedDimensions( $input, $config, $expected ) { return $site_url . ltrim( $path, '/' ); } ); + Filters\expectApplied( 'rocket_disable_meta_generator' ) + ->atMost() + ->once() + ->andReturn( true ); + if ( isset( $config['rocket_specify_image_dimensions_filter'] ) ){ Filters\expectApplied( 'rocket_specify_image_dimensions' ) ->once() diff --git a/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/maybeReplaceCssImages.php b/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/maybeReplaceCssImages.php index 1062346ae4..a2b5a8e2ae 100644 --- a/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/maybeReplaceCssImages.php +++ b/tests/Unit/inc/Engine/Media/Lazyload/CSS/Subscriber/maybeReplaceCssImages.php @@ -23,6 +23,11 @@ public function testShouldReturnAsExpected( $config, $expected ) { Filters\expectApplied('rocket_generate_lazyloaded_css')->with($expected['data'])->andReturn($config['data']); } + Filters\expectApplied( 'rocket_disable_meta_generator' ) + ->atMost() + ->once() + ->andReturn( true ); + $this->context->shouldReceive('is_allowed')->andReturn($config['is_allowed']); $this->assertSame($expected['output'], $this->subscriber->maybe_replace_css_images($config['html'])); } diff --git a/tests/Unit/inc/Engine/Optimization/DelayJS/HTML/delayJs.php b/tests/Unit/inc/Engine/Optimization/DelayJS/HTML/delayJs.php index c94000547c..0ccd195c8c 100644 --- a/tests/Unit/inc/Engine/Optimization/DelayJS/HTML/delayJs.php +++ b/tests/Unit/inc/Engine/Optimization/DelayJS/HTML/delayJs.php @@ -3,6 +3,7 @@ namespace WP_Rocket\Tests\Unit\inc\Engine\Optimization\DelayJS\HTML; use Mockery; +use Brain\Monkey\Filters; use Brain\Monkey\Functions; use WP_Rocket\Admin\Options_Data; use WP_Rocket\Engine\Optimization\DelayJS\HTML; @@ -38,6 +39,11 @@ public function testShouldProcessScriptHTML( $config, $html, $expected ) { $this->logger->allows()->debug(Mockery::any()); + Filters\expectApplied( 'rocket_disable_meta_generator' ) + ->atMost() + ->once() + ->andReturn( true ); + Functions\expect( 'rocket_bypass' ) ->atMost() ->once() diff --git a/tests/Unit/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller/optimize.php b/tests/Unit/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller/optimize.php index 30575b979d..917d5d9ab4 100644 --- a/tests/Unit/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller/optimize.php +++ b/tests/Unit/inc/Engine/Optimization/LazyRenderContent/Frontend/Controller/optimize.php @@ -3,6 +3,7 @@ namespace WP_Rocket\Tests\Unit\inc\Engine\Optimization\LazyRenderContent\Frontend\Controller; +use Brain\Monkey\Filters; use Mockery; use WP_Rocket\Engine\Optimization\LazyRenderContent\Context\Context; use WP_Rocket\Engine\Optimization\LazyRenderContent\Database\Rows\LazyRenderContent as LRCRow; @@ -26,6 +27,11 @@ public function set_up() { * @dataProvider configTestData */ public function testShouldReturnExpected( $config, $html, $expected ) { + Filters\expectApplied( 'rocket_disable_meta_generator' ) + ->atMost() + ->once() + ->andReturn( true ); + $row = $this->getMockBuilder( LRCRow::class ) ->disableOriginalConstructor() ->getMock(); diff --git a/tests/Unit/inc/Engine/Optimization/Minify/CSS/Minify/optimize.php b/tests/Unit/inc/Engine/Optimization/Minify/CSS/Minify/optimize.php index f5baf92726..b6c1006dd7 100644 --- a/tests/Unit/inc/Engine/Optimization/Minify/CSS/Minify/optimize.php +++ b/tests/Unit/inc/Engine/Optimization/Minify/CSS/Minify/optimize.php @@ -34,6 +34,11 @@ public function setUp() : void { * @dataProvider providerTestData */ public function testShouldMinifyCSS( $original, $expected, $cdn_host, $cdn_url, $site_url, $external_url, $excluded_css = [], $has_integrity = false, $valid_integrity = true ) { + Filters\expectApplied( 'rocket_disable_meta_generator' ) + ->atMost() + ->once() + ->andReturn( true ); + Filters\expectApplied( 'rocket_cdn_hosts' ) ->zeroOrMoreTimes() ->with( [], [ 'all', 'css_and_js', 'css' ] ) diff --git a/tests/Unit/inc/Engine/Optimization/Minify/JS/Combine/optimize.php b/tests/Unit/inc/Engine/Optimization/Minify/JS/Combine/optimize.php index 6a0f4e4a5a..e69f392bef 100644 --- a/tests/Unit/inc/Engine/Optimization/Minify/JS/Combine/optimize.php +++ b/tests/Unit/inc/Engine/Optimization/Minify/JS/Combine/optimize.php @@ -62,6 +62,11 @@ public function setUp(): void { * @dataProvider providerTestData */ public function testShouldCombineJS( $original, $expected, $config ) { + Filters\expectApplied( 'rocket_disable_meta_generator' ) + ->atMost() + ->once() + ->andReturn( true ); + $this->options->shouldReceive( 'get' ) ->with( 'minify_js_key', 'rocket_uniqid' ) ->andReturn( 'rocket_uniqid' ); diff --git a/tests/Unit/inc/Engine/Optimization/Minify/JS/Minify/optimize.php b/tests/Unit/inc/Engine/Optimization/Minify/JS/Minify/optimize.php index b8e17bedff..352ddb5f8a 100644 --- a/tests/Unit/inc/Engine/Optimization/Minify/JS/Minify/optimize.php +++ b/tests/Unit/inc/Engine/Optimization/Minify/JS/Minify/optimize.php @@ -47,6 +47,11 @@ public function setUp() : void { * @dataProvider providerTestData */ public function testShouldMinifyJS( $original, $expected, $cdn_hosts, $cdn_url, $site_url, $external_url, $has_integrity = false, $valid_integrity = true ) { + Filters\expectApplied( 'rocket_disable_meta_generator' ) + ->atMost() + ->once() + ->andReturn( true ); + Filters\expectApplied( 'rocket_minify_excluded_external_js' ) ->andReturn( ['cse.google.com/cse.js'] ); diff --git a/tests/Unit/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/treeshake.php b/tests/Unit/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/treeshake.php index c52583b2e7..5b7aea1781 100644 --- a/tests/Unit/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/treeshake.php +++ b/tests/Unit/inc/Engine/Optimization/RUCSS/Controller/UsedCSS/treeshake.php @@ -1,6 +1,6 @@ atMost() + ->once() + ->andReturn( true ); + $wp = new WP(); $GLOBALS['wp'] = $wp; diff --git a/tests/Unit/inc/Engine/Preload/Fonts/preloadFonts.php b/tests/Unit/inc/Engine/Preload/Fonts/preloadFonts.php index 9da31b847d..2b81779026 100644 --- a/tests/Unit/inc/Engine/Preload/Fonts/preloadFonts.php +++ b/tests/Unit/inc/Engine/Preload/Fonts/preloadFonts.php @@ -37,6 +37,10 @@ public function setUp() : void { public function testShouldAddPreloadTagsWhenValidFonts( $buffer, $bypass, $filter, $rocket_options, $expected ) { Functions\when( 'rocket_bypass' )->justReturn( $bypass ); Functions\when( 'home_url' )->justReturn( 'http://example.org' ); + Filters\expectApplied( 'rocket_disable_meta_generator' ) + ->atMost() + ->once() + ->andReturn( true ); if ( $filter ) { Filters\expectApplied( 'rocket_disable_preload_fonts' ) From c8b3d2b21c298264436095a33b390d1b56ad555d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Thu, 17 Oct 2024 15:27:35 -0400 Subject: [PATCH 11/14] prevent meta generator on tests --- .../Integration/inc/Addon/WebP/Subscriber/convertToWebp.php | 3 +++ tests/Integration/inc/Engine/CDN/Subscriber/rewrite.php | 3 +++ .../Frontend/Subscriber/maybe_apply_optimizations.php | 3 +++ .../ImageDimensions/Subscriber/specifyImageDimensions.php | 3 +++ .../Media/Lazyload/CSS/Subscriber/maybeReplaceCssImages.php | 3 +++ .../inc/Engine/Optimization/DelayJS/Subscriber/delayJs.php | 2 ++ .../Engine/Optimization/Minify/CSS/Subscriber/process.php | 2 ++ .../Engine/Optimization/Minify/JS/Subscriber/process.php | 4 ++++ .../Optimization/RUCSS/Frontend/Subscriber/treeshake.php | 2 ++ .../Integration/inc/Engine/Support/Data/getSupportData.php | 6 ++++++ .../Themes/Divi/disableImageDimensionsHeightPercentage.php | 2 ++ 11 files changed, 33 insertions(+) diff --git a/tests/Integration/inc/Addon/WebP/Subscriber/convertToWebp.php b/tests/Integration/inc/Addon/WebP/Subscriber/convertToWebp.php index 67c5f6e4e0..c2cc04b50e 100644 --- a/tests/Integration/inc/Addon/WebP/Subscriber/convertToWebp.php +++ b/tests/Integration/inc/Addon/WebP/Subscriber/convertToWebp.php @@ -21,6 +21,8 @@ public function set_up() { parent::set_up(); $this->unregisterAllCallbacksExcept( 'rocket_buffer', 'convert_to_webp', 16 ); + + add_filter( 'rocket_disable_meta_generator', '__return_true' ); } public function tear_down() { @@ -29,6 +31,7 @@ public function tear_down() { remove_filter( 'rocket_file_extensions_for_webp', [ $this, 'set_extensions_webp'] ); remove_filter( 'rocket_attributes_for_webp', [ $this, 'set_attributes_webp' ] ); remove_filter( 'rocket_cdn_cnames', [ $this, 'set_cdn_cnames'] ); + remove_filter( 'rocket_disable_meta_generator', '__return_true' ); $this->restoreWpHook( 'rocket_buffer' ); diff --git a/tests/Integration/inc/Engine/CDN/Subscriber/rewrite.php b/tests/Integration/inc/Engine/CDN/Subscriber/rewrite.php index 053fe89328..ed74242187 100644 --- a/tests/Integration/inc/Engine/CDN/Subscriber/rewrite.php +++ b/tests/Integration/inc/Engine/CDN/Subscriber/rewrite.php @@ -12,12 +12,15 @@ class Test_Rewrite extends TestCase { public function set_up() { parent::set_up(); + add_filter( 'rocket_disable_meta_generator', '__return_true' ); + $this->unregisterAllCallbacksExcept( 'rocket_buffer', 'rewrite', 20 ); } public function tear_down() { remove_filter( 'content_url', [ $this, 'setContentURL' ] ); remove_filter( 'includes_url', [ $this, 'setIncludesURL' ] ); + add_filter( 'rocket_disable_meta_generator', '__return_true' ); $this->restoreWpHook( 'rocket_buffer' ); diff --git a/tests/Integration/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php b/tests/Integration/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php index dda3411799..d0b8429a25 100644 --- a/tests/Integration/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php +++ b/tests/Integration/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/maybe_apply_optimizations.php @@ -34,6 +34,8 @@ public static function tear_down_after_class() { public function set_up() { parent::set_up(); + add_filter( 'rocket_disable_meta_generator', '__return_true' ); + $this->unregisterAllCallbacksExcept( 'rocket_buffer', 'maybe_apply_optimizations', 17 ); } @@ -41,6 +43,7 @@ public function tear_down() { unset( $_GET ); remove_filter( 'rocket_performance_hints_optimization_delay', [ $this, 'add_delay' ] ); remove_filter( 'pre_get_rocket_option_cache_logged_user', [ $this, 'get_cache_user' ] ); + remove_filter( 'rocket_disable_meta_generator', '__return_true' ); $this->restoreWpHook( 'rocket_buffer' ); if ( $this->user_id > 0 ) { diff --git a/tests/Integration/inc/Engine/Media/ImageDimensions/Subscriber/specifyImageDimensions.php b/tests/Integration/inc/Engine/Media/ImageDimensions/Subscriber/specifyImageDimensions.php index aec2370c49..170de944a6 100644 --- a/tests/Integration/inc/Engine/Media/ImageDimensions/Subscriber/specifyImageDimensions.php +++ b/tests/Integration/inc/Engine/Media/ImageDimensions/Subscriber/specifyImageDimensions.php @@ -17,6 +17,8 @@ class Test_SpecifyImageDimensions extends FilesystemTestCase { public function set_up() { parent::set_up(); + add_filter( 'rocket_disable_meta_generator', '__return_true' ); + $this->unregisterAllCallbacksExcept('rocket_buffer', 'specify_image_dimensions', 17); } public function tear_down() { @@ -40,6 +42,7 @@ public function tear_down() { remove_filter( 'site_url', [ $this, 'setSiteUrl' ] ); remove_filter( 'home_url', [ $this, 'setHomeUrl' ] ); + remove_filter( 'rocket_disable_meta_generator', '__return_true' ); unset( $GLOBALS['wp'] ); diff --git a/tests/Integration/inc/Engine/Media/Lazyload/CSS/Subscriber/maybeReplaceCssImages.php b/tests/Integration/inc/Engine/Media/Lazyload/CSS/Subscriber/maybeReplaceCssImages.php index dec107d61c..58eca8d8dd 100644 --- a/tests/Integration/inc/Engine/Media/Lazyload/CSS/Subscriber/maybeReplaceCssImages.php +++ b/tests/Integration/inc/Engine/Media/Lazyload/CSS/Subscriber/maybeReplaceCssImages.php @@ -24,6 +24,7 @@ public function set_up() { add_filter('rocket_lazyload_excluded_src', [$this, 'exclude_lazyload']); add_filter('pre_http_request', [$this, 'mock_http'], 10, 3); add_filter('rocket_lazyload_css_hash', [$this, 'rocket_lazyload_css_hash'], 10, 2); + add_filter( 'rocket_disable_meta_generator', '__return_true' ); } public function tear_down() { @@ -31,6 +32,8 @@ public function tear_down() { remove_filter('rocket_lazyload_excluded_src', [$this, 'exclude_lazyload']); remove_filter('pre_get_rocket_option_lazyload_css_bg_img', [$this, 'lazyload_css_bg_img']); remove_filter('rocket_lazyload_css_hash', [$this, 'rocket_lazyload_css_hash']); + remove_filter( 'rocket_disable_meta_generator', '__return_true' ); + $this->restoreWpHook('rocket_buffer'); parent::tear_down(); } diff --git a/tests/Integration/inc/Engine/Optimization/DelayJS/Subscriber/delayJs.php b/tests/Integration/inc/Engine/Optimization/DelayJS/Subscriber/delayJs.php index 52ef757140..b57db416fa 100644 --- a/tests/Integration/inc/Engine/Optimization/DelayJS/Subscriber/delayJs.php +++ b/tests/Integration/inc/Engine/Optimization/DelayJS/Subscriber/delayJs.php @@ -25,6 +25,7 @@ public function set_up() { // Disable ATF optimization to prevent DB request (unrelated to the test). add_filter( 'rocket_above_the_fold_optimization', '__return_false' ); + add_filter( 'rocket_disable_meta_generator', '__return_true' ); $this->unregisterAllCallbacksExcept( 'rocket_buffer', 'delay_js', 26 ); } @@ -37,6 +38,7 @@ public function tear_down() { remove_filter( 'pre_get_rocket_option_delay_js', [ $this, 'set_delay_js' ] ); remove_filter( 'pre_get_rocket_option_delay_js_exclusions', [ $this, 'set_delay_js_exclusions' ] ); + remove_filter( 'rocket_disable_meta_generator', '__return_true' ); delete_transient( 'wpr_dynamic_lists' ); if ( isset( $this->post->ID ) ) { diff --git a/tests/Integration/inc/Engine/Optimization/Minify/CSS/Subscriber/process.php b/tests/Integration/inc/Engine/Optimization/Minify/CSS/Subscriber/process.php index 2d459e4f8c..ae219ecb7a 100644 --- a/tests/Integration/inc/Engine/Optimization/Minify/CSS/Subscriber/process.php +++ b/tests/Integration/inc/Engine/Optimization/Minify/CSS/Subscriber/process.php @@ -29,6 +29,7 @@ public function set_up() { add_filter( 'pre_get_rocket_option_minify_css', [ $this, 'return_true' ] ); add_filter( 'pre_get_rocket_option_minify_css_key', [ $this, 'return_key' ] ); + add_filter( 'rocket_disable_meta_generator', '__return_true' ); } public function tear_down() { @@ -38,6 +39,7 @@ public function tear_down() { remove_filter( 'pre_get_rocket_option_minify_css', [ $this, 'return_true' ] ); remove_filter( 'pre_get_rocket_option_minify_css_key', [ $this, 'return_key' ] ); + remove_filter( 'rocket_disable_meta_generator', '__return_true' ); $this->unsetSettings(); } diff --git a/tests/Integration/inc/Engine/Optimization/Minify/JS/Subscriber/process.php b/tests/Integration/inc/Engine/Optimization/Minify/JS/Subscriber/process.php index 807e036025..1519d7e33a 100644 --- a/tests/Integration/inc/Engine/Optimization/Minify/JS/Subscriber/process.php +++ b/tests/Integration/inc/Engine/Optimization/Minify/JS/Subscriber/process.php @@ -27,6 +27,8 @@ class Test_Process extends TestCase { public function set_up() { parent::set_up(); + add_filter( 'rocket_disable_meta_generator', '__return_true' ); + $this->unregisterAllCallbacksExcept( 'rocket_buffer', 'process', 22 ); } @@ -40,6 +42,8 @@ public function tear_down() { remove_filter( 'pre_get_rocket_option_defer_all_js', [ $this, 'return_defer_all_js' ] ); remove_filter( 'rocket_excluded_inline_js_content', [ $this, 'set_excluded_inline'] ); remove_filter( 'rocket_minify_excluded_external_js', [ $this, 'set_excluded_external'] ); + remove_filter( 'rocket_disable_meta_generator', '__return_true' ); + delete_transient( 'wpr_dynamic_lists' ); $this->unsetSettings(); diff --git a/tests/Integration/inc/Engine/Optimization/RUCSS/Frontend/Subscriber/treeshake.php b/tests/Integration/inc/Engine/Optimization/RUCSS/Frontend/Subscriber/treeshake.php index 12062e5dfe..d4d8ce747d 100644 --- a/tests/Integration/inc/Engine/Optimization/RUCSS/Frontend/Subscriber/treeshake.php +++ b/tests/Integration/inc/Engine/Optimization/RUCSS/Frontend/Subscriber/treeshake.php @@ -23,6 +23,7 @@ public function set_up() { add_filter('pre_get_rocket_option_remove_unused_css', [$this, 'rucss']); add_filter('rocket_exclude_rucss_fonts_preload', [$this, 'exclude_fonts_preload']); add_filter('rocket_used_css_dir_level', [$this, 'used_css_dir_level']); + add_filter( 'rocket_disable_meta_generator', '__return_true' ); } public function tear_down() { @@ -31,6 +32,7 @@ public function tear_down() { remove_filter('pre_get_rocket_option_remove_unused_css', [$this, 'rucss']); remove_filter('rocket_exclude_rucss_fonts_preload', [$this, 'exclude_fonts_preload']); remove_filter('rocket_used_css_dir_level', [$this, 'used_css_dir_level']); + remove_filter( 'rocket_disable_meta_generator', '__return_true' ); $this->restoreWpHook('rocket_buffer'); diff --git a/tests/Integration/inc/Engine/Support/Data/getSupportData.php b/tests/Integration/inc/Engine/Support/Data/getSupportData.php index f9e45ad4d5..c97d505485 100644 --- a/tests/Integration/inc/Engine/Support/Data/getSupportData.php +++ b/tests/Integration/inc/Engine/Support/Data/getSupportData.php @@ -19,12 +19,18 @@ public function set_up() { global $wp_version; + add_filter( 'rocket_above_the_fold_optimization', '__return_true' ); + add_filter( 'rocket_lrc_optimization', '__return_true' ); + $this->wp_version = $wp_version; } public function tear_down() { global $wp_version; + remove_filter( 'rocket_above_the_fold_optimization', '__return_true' ); + remove_filter( 'rocket_lrc_optimization', '__return_true' ); + $wp_version = $this->wp_version; parent::tear_down(); diff --git a/tests/Integration/inc/ThirdParty/Themes/Divi/disableImageDimensionsHeightPercentage.php b/tests/Integration/inc/ThirdParty/Themes/Divi/disableImageDimensionsHeightPercentage.php index 2b9b5391f0..d042089161 100644 --- a/tests/Integration/inc/ThirdParty/Themes/Divi/disableImageDimensionsHeightPercentage.php +++ b/tests/Integration/inc/ThirdParty/Themes/Divi/disableImageDimensionsHeightPercentage.php @@ -25,6 +25,7 @@ public function set_up() { $this->unregisterAllCallbacksExcept( 'rocket_buffer', 'specify_image_dimensions', 17 ); add_filter( 'rocket_specify_image_dimensions', '__return_true' ); + add_filter( 'rocket_disable_meta_generator', '__return_true' ); } public function tear_down() { @@ -34,6 +35,7 @@ public function tear_down() { remove_filter( 'rocket_specify_image_dimensions', '__return_true' ); unset( $GLOBALS['wp'] ); + remove_filter( 'rocket_disable_meta_generator', '__return_true' ); parent::tear_down(); } From 5d517a8f974d10a720b2a24715dcdc707a9b35d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Thu, 17 Oct 2024 16:42:27 -0400 Subject: [PATCH 12/14] code improvement --- inc/Engine/Support/Meta.php | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/inc/Engine/Support/Meta.php b/inc/Engine/Support/Meta.php index dd420b36a3..5a56bb74f8 100644 --- a/inc/Engine/Support/Meta.php +++ b/inc/Engine/Support/Meta.php @@ -48,7 +48,7 @@ public function add_meta_generator( $html ): string { } if ( rocket_get_constant( 'WP_ROCKET_WHITE_LABEL_FOOTPRINT', false ) ) { - return $html; + return $this->remove_features_comments( $html ); } if ( wpm_apply_filters_typed( 'boolean', 'rocket_disable_meta_generator', false ) ) { @@ -61,19 +61,17 @@ public function add_meta_generator( $html ): string { $meta = $this->get_meta_tag( $comments['feature'] ); - $result = preg_replace( '/<\/head>/i', $meta . '', $html, 1 ); - - if ( null === $result ) { + if ( empty( $meta ) ) { return $html; } - $result = preg_replace( '//i', '', $result ); + $result = preg_replace( '/<\/head>/i', $meta . '', $html, 1 ); if ( null === $result ) { return $html; } - return $result; + return $this->remove_features_comments( $result ); } /** @@ -100,6 +98,10 @@ private function get_meta_tag( array $features = [] ): string { $features[] = 'wpr_preload_links'; } + if ( empty( $features ) ) { + return ''; + } + $content = ''; if ( wpm_apply_filters_typed( 'boolean', 'rocket_display_meta_generator_content', true ) ) { @@ -110,4 +112,21 @@ private function get_meta_tag( array $features = [] ): string { return $meta; } + + /** + * Remove WP Rocket features comments from the HTML + * + * @param string $html The HTML content. + * + * @return string + */ + private function remove_features_comments( $html ): string { + $result = preg_replace( '//i', '', $html ); + + if ( null === $result ) { + return $html; + } + + return $result; + } } From 7af6e7a7d22537d0ec6275a314d5aafb9cbcc67c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Thu, 17 Oct 2024 16:42:37 -0400 Subject: [PATCH 13/14] add test for add_meta_generator() --- .../Engine/Support/Meta/addMetaGenerator.php | 53 +++++++++++++ .../Engine/Support/Meta/addMetaGenerator.php | 76 +++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 tests/Fixtures/inc/Engine/Support/Meta/addMetaGenerator.php create mode 100644 tests/Unit/inc/Engine/Support/Meta/addMetaGenerator.php diff --git a/tests/Fixtures/inc/Engine/Support/Meta/addMetaGenerator.php b/tests/Fixtures/inc/Engine/Support/Meta/addMetaGenerator.php new file mode 100644 index 0000000000..db926e9b39 --- /dev/null +++ b/tests/Fixtures/inc/Engine/Support/Meta/addMetaGenerator.php @@ -0,0 +1,53 @@ + [ + 'config' => [ + 'nowprocket' => 1, + ], + 'html' => '', + 'expected' => '', + ], + 'testShouldReturnDefaultWhenDNRO' => [ + 'config' => [ + 'donotrocketoptimize' => 1, + ], + 'html' => '', + 'expected' => '', + ], + 'testShouldReturnDefaultWhenWL' => [ + 'config' => [ + 'white_label_footprint' => 1, + ], + 'html' => '', + 'expected' => '', + ], + 'testShouldReturnDefaultWhenFilterDisabled' => [ + 'config' => [ + 'disable_meta' => true, + ], + 'html' => '', + 'expected' => '', + ], + 'testShouldReturnDefaultWhenNoFeatures' => [ + 'config' => [ + 'disable_meta' => false, + 'cache' => false, + 'do_caching_mobile_files' => 0, + 'preload_links' => 0, + ], + 'html' => '', + 'expected' => '', + ], + 'testShouldReturnAddMeta' => [ + 'config' => [ + 'disable_meta' => false, + 'cache' => true, + 'do_caching_mobile_files' => 1, + 'preload_links' => 1, + 'is_mobile' => true, + ], + 'html' => '', + 'expected' => '', + ], +]; diff --git a/tests/Unit/inc/Engine/Support/Meta/addMetaGenerator.php b/tests/Unit/inc/Engine/Support/Meta/addMetaGenerator.php new file mode 100644 index 0000000000..a360c39088 --- /dev/null +++ b/tests/Unit/inc/Engine/Support/Meta/addMetaGenerator.php @@ -0,0 +1,76 @@ +options = Mockery::mock( Options_Data::class ); + $this->mobile_detect = Mockery::mock( WP_Rocket_Mobile_Detect::class ); + $this->meta = new Meta( $this->mobile_detect, $this->options ); + } + + /** + * @dataProvider configTestData + */ + public function testShouldReturnExpected( $config, $html, $expected ) { + Functions\when( 'rocket_bypass' )->alias( function() use ( $config ) { + return isset( $config['nowprocket'] ) ? (bool) $config['nowprocket'] : false; + } ); + + $this->white_label_footprint = $config['white_label_footprint'] ?? null; + $this->donotrocketoptimize = $config['donotrocketoptimize'] ?? null; + $this->rocket_version = '3.17'; + + if ( isset( $config['disable_meta'] ) ) { + Filters\expectApplied( 'rocket_disable_meta_generator' ) + ->once() + ->andReturn( $config['disable_meta'] ); + } + + if ( isset( $config['cache'] ) ) { + Filters\expectApplied( 'do_rocket_generate_caching_files' ) + ->andReturn( $config['cache'] ); + } + + if ( isset( $config['do_caching_mobile_files'] ) ) { + $this->options->shouldReceive( 'get' ) + ->with( 'do_caching_mobile_files', 0 ) + ->andReturn( $config['do_caching_mobile_files'] ); + } + + Functions\when( 'rocket_get_dns_prefetch_domains' )->justReturn( [] ); + + if ( isset( $config['preload_links'] ) ) { + $this->options->shouldReceive( 'get' ) + ->with( 'preload_links', 0 ) + ->andReturn( $config['preload_links'] ); + } + + $this->mobile_detect->shouldReceive( 'isMobile' ) + ->andReturn( $config['is_mobile'] ?? false ); + + $this->assertSame( + $expected, + $this->meta->add_meta_generator( $html ) + ); + } +} From a923dddff03fa25acd7ff40b89531003bad5daa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Fri, 18 Oct 2024 09:34:34 -0400 Subject: [PATCH 14/14] add missing docblocks --- inc/Engine/Support/Meta.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/inc/Engine/Support/Meta.php b/inc/Engine/Support/Meta.php index 5a56bb74f8..59a042cb18 100644 --- a/inc/Engine/Support/Meta.php +++ b/inc/Engine/Support/Meta.php @@ -51,6 +51,13 @@ public function add_meta_generator( $html ): string { return $this->remove_features_comments( $html ); } + /** + * Filters whether to disable the WP Rocket meta generator tag. + * + * @since 3.17.2 + * + * @param bool $disable True to disable, false otherwise. + */ if ( wpm_apply_filters_typed( 'boolean', 'rocket_disable_meta_generator', false ) ) { return $html; } @@ -82,6 +89,7 @@ public function add_meta_generator( $html ): string { * @return string */ private function get_meta_tag( array $features = [] ): string { + // This filter is documented in inc/classes/Buffer/class-cache.php. if ( wpm_apply_filters_typed( 'boolean', 'do_rocket_generate_caching_files', true ) ) { $features[] = 'wpr_cached'; @@ -104,6 +112,13 @@ private function get_meta_tag( array $features = [] ): string { $content = ''; + /** + * Filters the display of the content attribute in the meta generator tag. + * + * @since 3.17.2 + * + * @param bool $display True to display, false otherwise. + */ if ( wpm_apply_filters_typed( 'boolean', 'rocket_display_meta_generator_content', true ) ) { $content = ' content="WP Rocket ' . rocket_get_constant( 'WP_ROCKET_VERSION', '' ) . '"'; }