From 7366b50802f7bf12257f51682b7db2492c2eedf0 Mon Sep 17 00:00:00 2001 From: Alessio Torrisi Date: Tue, 17 Dec 2024 14:20:47 +0100 Subject: [PATCH 1/6] Fix: string not localized --- components/advancedSettings/JetpackBoost/SingleOption.js | 2 +- components/performance/defaultText.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/components/advancedSettings/JetpackBoost/SingleOption.js b/components/advancedSettings/JetpackBoost/SingleOption.js index 70c96e8..9da7949 100644 --- a/components/advancedSettings/JetpackBoost/SingleOption.js +++ b/components/advancedSettings/JetpackBoost/SingleOption.js @@ -83,7 +83,7 @@ const SingleOption = ( { params, isChild, methods, constants } ) => { ! NewfoldRuntime.sdk.performance .jetpack_boost_premium_is_active && ( Date: Wed, 18 Dec 2024 15:39:33 +0100 Subject: [PATCH 2/6] Handle htaccess file according to burst safety mode --- bootstrap.php | 2 + includes/BurstSafetyMode/Browser.php | 59 +++++++++ .../BurstSafetyMode/ResponseHeaderManager.php | 116 ++++++++++++++++++ includes/BurstSafetyMode/Skip404.php | 45 +++++++ includes/BurstSafetyMode/init.php | 61 +++++++++ includes/burstSafetyModeFunctions.php | 44 +++++++ tests/cypress/integration/performance.cy.js | 2 +- 7 files changed, 328 insertions(+), 1 deletion(-) create mode 100644 includes/BurstSafetyMode/Browser.php create mode 100644 includes/BurstSafetyMode/ResponseHeaderManager.php create mode 100644 includes/BurstSafetyMode/Skip404.php create mode 100644 includes/BurstSafetyMode/init.php create mode 100644 includes/burstSafetyModeFunctions.php diff --git a/bootstrap.php b/bootstrap.php index c4d16d8..f5bfb9c 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -12,3 +12,5 @@ function ( $features ) { } new PerformanceFeatureHooks(); + +require_once __DIR__ . '/includes/BurstSafetyMode/init.php'; diff --git a/includes/BurstSafetyMode/Browser.php b/includes/BurstSafetyMode/Browser.php new file mode 100644 index 0000000..9bbba02 --- /dev/null +++ b/includes/BurstSafetyMode/Browser.php @@ -0,0 +1,59 @@ +addHeader( 'X-Newfold-Cache-Level', BURST_SAFETY_CACHE_LEVEL ); + $this->addRules(); + } + + /** + * Add htaccess rules. + * + * @return void + */ + public static function addRules() { + + $file_typ_expirations = array( + 'default' => '1 week', + 'text/html' => '8 hours', + 'image/jpg' => '1 week', + 'image/jpeg' => '1 week', + 'image/gif' => '1 week', + 'image/png' => '1 week', + 'text/css' => '1 week', + 'text/javascript' => '1 week', + 'application/pdf' => '1 month', + 'image/x-icon' => '1 year', + ); + + $tab = "\t"; + + $rules[] = ''; + $rules[] = "{$tab}ExpiresActive On"; + + foreach ( $file_typ_expirations as $file_type => $expiration ) { + if ( 'default' === $file_type ) { + $rules[] = "{$tab}ExpiresDefault \"access plus {$expiration}\""; + } else { + $rules[] = "{$tab}ExpiresByType {$file_type} \"access plus {$expiration}\""; + } + } + + $rules [] = ''; + + $htaccess = new htaccess( self::MARKER ); + + return $htaccess->addContent( $rules ); + } +} diff --git a/includes/BurstSafetyMode/ResponseHeaderManager.php b/includes/BurstSafetyMode/ResponseHeaderManager.php new file mode 100644 index 0000000..3a3365c --- /dev/null +++ b/includes/BurstSafetyMode/ResponseHeaderManager.php @@ -0,0 +1,116 @@ +htaccess = new htaccess( self::MARKER ); + } + + /** + * Parse existing headers. + * + * @return array + */ + public function parseHeaders() { + + $headers = array(); + + $content = $this->htaccess->readContent(); + $lines = array_map( 'trim', convertContentToLines( $content ) ); + + array_shift( $lines ); // Remove opening IfModule + array_pop( $lines ); // Remove closing IfModule + + $pattern = '/^Header set (.*) "(.*)"$/'; + + foreach ( $lines as $line ) { + if ( preg_match( $pattern, trim( $line ), $matches ) && isset( $matches[1], $matches[2] ) ) { + $headers[ $matches[1] ] = $matches[2]; + } + } + + return $headers; + } + + /** + * Add a header. + * + * @param string $name Header name + * @param string $value Header value + */ + public function addHeader( string $name, string $value ) { + $this->setHeaders( + array_merge( + $this->parseHeaders(), + array( $name => $value ) + ) + ); + } + + /** + * Add multiple headers at once. + * + * @param string[] $headers + */ + public function addHeaders( array $headers ) { + $headers = array_merge( $this->parseHeaders(), $headers ); + $this->setHeaders( $headers ); + } + + /** + * Remove a header. + * + * @param string $name Header name + */ + public function removeHeader( $name ) { + $headers = $this->parseHeaders(); + unset( $headers[ $name ] ); + $this->setHeaders( $headers ); + } + + /** + * Remove all headers. + */ + public function removeAllHeaders() { + $this->setHeaders( array() ); + } + + /** + * Set headers. + * + * @param array $headers + */ + public function setHeaders( array $headers ) { + + if ( empty( $headers ) ) { + $this->htaccess->removeContent(); + + return; + } + + $content = '' . PHP_EOL; + foreach ( $headers as $key => $value ) { + $content .= "\t" . "Header set {$key} \"{$value}\"" . PHP_EOL; + } + $content .= ''; + + $this->htaccess->addContent( $content ); + } +} diff --git a/includes/BurstSafetyMode/Skip404.php b/includes/BurstSafetyMode/Skip404.php new file mode 100644 index 0000000..a7e061e --- /dev/null +++ b/includes/BurstSafetyMode/Skip404.php @@ -0,0 +1,45 @@ +addRules(); + } + + + /** + * Add our rules to the .htacces file. + */ + public static function addRules() { + $content = << + RewriteEngine On + RewriteCond %{REQUEST_FILENAME} !-f + RewriteCond %{REQUEST_FILENAME} !-d + RewriteCond %{REQUEST_URI} !(robots\.txt|ads\.txt|[a-z0-9_\-]*sitemap[a-z0-9_\.\-]*\.(xml|xsl|html)(\.gz)?) + RewriteCond %{REQUEST_URI} \.(css|htc|less|js|js2|js3|js4|html|htm|rtf|rtx|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|avif|avifs|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|webp|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|webm|mpp|otf|_otf|odb|odc|odf|odg|odp|ods|odt|ogg|ogv|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|_ttf|wav|wma|wri|woff|woff2|xla|xls|xlsx|xlt|xlw|zip)$ [NC] + RewriteRule .* - [L] + +HTACCESS; + + addContent( self::MARKER, $content ); + } + + /** + * Remove our rules from the .htaccess file. + */ + public static function removeRules() { + removeMarkers( self::MARKER ); + } +} diff --git a/includes/BurstSafetyMode/init.php b/includes/BurstSafetyMode/init.php new file mode 100644 index 0000000..66a3c8e --- /dev/null +++ b/includes/BurstSafetyMode/init.php @@ -0,0 +1,61 @@ +addHeader( 'X-Newfold-Cache-Level', $newfold_cache_level ); + + delete_option( 'newfold_burst_safety_mode' ); + } +} else { + if ( ! $newfold_burst_safety_mode ) { + $files_to_include = array( + 'htaccess' => BLUEHOST_PLUGIN_DIR . 'vendor/wp-forge/wp-htaccess-manager/includes/htaccess.php', + 'htaccess_functions' => BLUEHOST_PLUGIN_DIR . 'vendor/wp-forge/wp-htaccess-manager/includes/functions.php', + 'skip404' => BLUEHOST_PLUGIN_DIR . 'vendor/newfold-labs/wp-module-performance/includes/BurstSafetyMode/Skip404.php', + 'browser' => BLUEHOST_PLUGIN_DIR . 'vendor/newfold-labs/wp-module-performance/includes/BurstSafetyMode/browser.php', + 'response_header_manager' => BLUEHOST_PLUGIN_DIR . 'vendor/newfold-labs/wp-module-performance/includes/BurstSafetyMode/ResponseHeaderManager.php', + ); + + foreach ( $files_to_include as $path ) { + if ( file_exists( $path ) ) { + require_once $path; + } + } + + define( 'BURST_SAFETY_CACHE_LEVEL', 3 ); + + $skip_404_handling = (bool) get_option( 'newfold_skip_404_handling', true ); + + if ( ! $skip_404_handling && class_exists( BurstSkip404::class ) ) { + $skip404 = new BurstSkip404(); + } + + if ( BURST_SAFETY_CACHE_LEVEL !== $newfold_cache_level && class_exists( BurstBrowser::class ) ) { + $browser = new BurstBrowser(); + } + + update_option( 'newfold_burst_safety_mode', true ); + } +} diff --git a/includes/burstSafetyModeFunctions.php b/includes/burstSafetyModeFunctions.php new file mode 100644 index 0000000..8de058d --- /dev/null +++ b/includes/burstSafetyModeFunctions.php @@ -0,0 +1,44 @@ +addHeader( 'X-Newfold-Cache-Level', 3 ); + } +} elseif ( $newfold_burst_safety_mode ) { + $cache_level = get_option( 'newfold_burst_safety_mode_site_cache_level' ); + $browser = new Browser(); + $browser::maybeAddRules( $cache_level ); + if( function_exists( 'getSkip404Option' ) && ! getSkip404Option() ) { + $skip404 = new Skip404(); + $skip404::maybeAddRules( false ); + } + $responseHeaderManager = new ResponseHeaderManager(); + $responseHeaderManager->addHeader( 'X-Newfold-Cache-Level', $cache_level ); + delete_option( 'newfold_burst_safety_mode' ); + delete_option( 'newfold_burst_safety_mode_site_cache_level' ); +} diff --git a/tests/cypress/integration/performance.cy.js b/tests/cypress/integration/performance.cy.js index e12fc15..111f9f2 100644 --- a/tests/cypress/integration/performance.cy.js +++ b/tests/cypress/integration/performance.cy.js @@ -13,7 +13,7 @@ describe( 'Performance Page', function () { } ); it( 'Is Accessible', () => { - cy.wait( 1000 ); + cy.wait( 2000 ); cy.checkA11y( appClass + '-app-body' ); } ); From 1d32f507fece6d04e3d56d348a5476dc7d25ad46 Mon Sep 17 00:00:00 2001 From: Alessio Torrisi Date: Fri, 20 Dec 2024 13:05:22 +0100 Subject: [PATCH 3/6] Fix: restore Skip 404 htaccess code once BURST SATEFY MODE is disabled --- includes/BurstSafetyMode/init.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/includes/BurstSafetyMode/init.php b/includes/BurstSafetyMode/init.php index 66a3c8e..7118599 100644 --- a/includes/BurstSafetyMode/init.php +++ b/includes/BurstSafetyMode/init.php @@ -46,11 +46,7 @@ define( 'BURST_SAFETY_CACHE_LEVEL', 3 ); - $skip_404_handling = (bool) get_option( 'newfold_skip_404_handling', true ); - - if ( ! $skip_404_handling && class_exists( BurstSkip404::class ) ) { - $skip404 = new BurstSkip404(); - } + $skip404 = new BurstSkip404(); if ( BURST_SAFETY_CACHE_LEVEL !== $newfold_cache_level && class_exists( BurstBrowser::class ) ) { $browser = new BurstBrowser(); From a010ca4c219fd96a1ba95025256f836459619bf7 Mon Sep 17 00:00:00 2001 From: Alessio Torrisi Date: Mon, 30 Dec 2024 14:31:01 +0100 Subject: [PATCH 4/6] Fix: lint JS files --- .../JetpackBoost/SingleOption.js | 2 +- components/cacheSettings/index.js | 196 ++++++++++-------- components/clearCache/index.js | 70 +++---- includes/burstSafetyModeFunctions.php | 32 ++- 4 files changed, 153 insertions(+), 147 deletions(-) diff --git a/components/advancedSettings/JetpackBoost/SingleOption.js b/components/advancedSettings/JetpackBoost/SingleOption.js index 9da7949..1aa09e8 100644 --- a/components/advancedSettings/JetpackBoost/SingleOption.js +++ b/components/advancedSettings/JetpackBoost/SingleOption.js @@ -83,7 +83,7 @@ const SingleOption = ( { params, isChild, methods, constants } ) => { ! NewfoldRuntime.sdk.performance .jetpack_boost_premium_is_active && ( { - const [ cacheLevel, setCacheLevel ] = methods.useState(constants.store.cacheLevel); +const CacheSettings = ( { methods, constants, Components } ) => { + const [ cacheLevel, setCacheLevel ] = methods.useState( + constants.store.cacheLevel + ); - const cacheOptions = [ - { - label: constants.text.cacheLevel0Label, - description: constants.text.cacheLevel0Description + constants.text.cacheLevel0Recommendation, - value: 0, - notice: constants.text.cacheLevel0NoticeText, - }, - { - label: constants.text.cacheLevel1Label, - description: constants.text.cacheLevel1Description + constants.text.cacheLevel1Recommendation, - value: 1, - notice: constants.text.cacheLevel1NoticeText, - }, - { - label: constants.text.cacheLevel2Label, - description: constants.text.cacheLevel2Description + constants.text.cacheLevel2Recommendation, - value: 2, - notice: constants.text.cacheLevel2NoticeText, - }, - { - label: constants.text.cacheLevel3Label, - description: constants.text.cacheLevel3Description + constants.text.cacheLevel3Recommendation, - value: 3, - notice: constants.text.cacheLevel3NoticeText, - }, - ]; + const cacheOptions = [ + { + label: constants.text.cacheLevel0Label, + description: + constants.text.cacheLevel0Description + + constants.text.cacheLevel0Recommendation, + value: 0, + notice: constants.text.cacheLevel0NoticeText, + }, + { + label: constants.text.cacheLevel1Label, + description: + constants.text.cacheLevel1Description + + constants.text.cacheLevel1Recommendation, + value: 1, + notice: constants.text.cacheLevel1NoticeText, + }, + { + label: constants.text.cacheLevel2Label, + description: + constants.text.cacheLevel2Description + + constants.text.cacheLevel2Recommendation, + value: 2, + notice: constants.text.cacheLevel2NoticeText, + }, + { + label: constants.text.cacheLevel3Label, + description: + constants.text.cacheLevel3Description + + constants.text.cacheLevel3Recommendation, + value: 3, + notice: constants.text.cacheLevel3NoticeText, + }, + ]; - const getCacheLevelNoticeTitle = () => { - return constants.text.cacheLevelNoticeTitle; - }; + const getCacheLevelNoticeTitle = () => { + return constants.text.cacheLevelNoticeTitle; + }; - const getCacheLevelNoticeText = () => { - return cacheOptions[cacheLevel].notice; - }; + const getCacheLevelNoticeText = () => { + return cacheOptions[ cacheLevel ].notice; + }; - const handleCacheLevelChange = (e) => { - methods.newfoldSettingsApiFetch( - { cacheLevel: parseInt(e.target.value) }, - methods.setError, (response) => { - setCacheLevel(parseInt(e.target.value)); - } - ); - }; + const handleCacheLevelChange = ( e ) => { + methods.newfoldSettingsApiFetch( + { cacheLevel: parseInt( e.target.value ) }, + methods.setError, + ( response ) => { + setCacheLevel( parseInt( e.target.value ) ); + } + ); + }; - methods.useUpdateEffect(() => { - methods.setStore({ - ...constants.store, - cacheLevel, - }); + methods.useUpdateEffect( () => { + methods.setStore( { + ...constants.store, + cacheLevel, + } ); - methods.makeNotice( - "cache-level-change-notice", - getCacheLevelNoticeTitle(), - getCacheLevelNoticeText(), - "success", - 5000 - ); - }, [cacheLevel]); + methods.makeNotice( + 'cache-level-change-notice', + getCacheLevelNoticeTitle(), + getCacheLevelNoticeText(), + 'success', + 5000 + ); + }, [ cacheLevel ] ); - return ( - <> - - - {cacheOptions.map((option) => { - return ( - - -
- {option.description} -
-
- ); - })} -
-
- - ); -} + return ( + <> + + + { cacheOptions.map( ( option ) => { + return ( + + +
+ { option.description } +
+
+ ); + } ) } +
+
+ + ); +}; -export default CacheSettings; \ No newline at end of file +export default CacheSettings; diff --git a/components/clearCache/index.js b/components/clearCache/index.js index 793ddab..494a3fc 100644 --- a/components/clearCache/index.js +++ b/components/clearCache/index.js @@ -1,39 +1,37 @@ -import { Button, Container } from "@newfold/ui-component-library"; +import { Button, Container } from '@newfold/ui-component-library'; -const ClearCache = ({ methods, constants }) => { +const ClearCache = ( { methods, constants } ) => { + const clearCache = () => { + methods.newfoldPurgeCacheApiFetch( + {}, + methods.setError, + ( response ) => { + methods.makeNotice( + 'disable-old-posts-comments-notice', + constants.text.clearCacheNoticeTitle, + null, + 'success', + 5000 + ); + } + ); + }; - const clearCache = () => { - methods.newfoldPurgeCacheApiFetch( - {}, - methods.setError, - (response) => { - methods.makeNotice( - "disable-old-posts-comments-notice", - constants.text.clearCacheNoticeTitle, - null, - "success", - 5000 - ); - } - ); - }; + return ( + + + + ); +}; - return ( - - - - - ); -;} - -export default ClearCache; \ No newline at end of file +export default ClearCache; diff --git a/includes/burstSafetyModeFunctions.php b/includes/burstSafetyModeFunctions.php index 8de058d..6cc03cc 100644 --- a/includes/burstSafetyModeFunctions.php +++ b/includes/burstSafetyModeFunctions.php @@ -1,4 +1,4 @@ -addHeader( 'X-Newfold-Cache-Level', 3 ); } } elseif ( $newfold_burst_safety_mode ) { $cache_level = get_option( 'newfold_burst_safety_mode_site_cache_level' ); $browser = new Browser(); $browser::maybeAddRules( $cache_level ); - if( function_exists( 'getSkip404Option' ) && ! getSkip404Option() ) { - $skip404 = new Skip404(); - $skip404::maybeAddRules( false ); - } - $responseHeaderManager = new ResponseHeaderManager(); - $responseHeaderManager->addHeader( 'X-Newfold-Cache-Level', $cache_level ); + if ( function_exists( 'getSkip404Option' ) && ! getSkip404Option() ) { + $skip404 = new Skip404(); + $skip404::maybeAddRules( false ); + } + $responseHeaderManager = new ResponseHeaderManager(); + $responseHeaderManager->addHeader( 'X-Newfold-Cache-Level', $cache_level ); delete_option( 'newfold_burst_safety_mode' ); delete_option( 'newfold_burst_safety_mode_site_cache_level' ); } From 4348cf66759bc39115837cc6e95f79ac0c41fb54 Mon Sep 17 00:00:00 2001 From: Alessio Torrisi Date: Tue, 7 Jan 2025 09:31:50 +0100 Subject: [PATCH 5/6] Fix: phpcs issue on undefined WordPress function --- includes/BurstSafetyMode/init.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/BurstSafetyMode/init.php b/includes/BurstSafetyMode/init.php index 7118599..e78bded 100644 --- a/includes/BurstSafetyMode/init.php +++ b/includes/BurstSafetyMode/init.php @@ -7,8 +7,8 @@ use NewfoldLabs\WP\Module\Performance\ResponseHeaderManager; -$newfold_burst_safety_mode = (bool) get_option( 'newfold_burst_safety_mode', false ); -$newfold_cache_level = (int) get_option( 'newfold_cache_level', 0 ); +$newfold_burst_safety_mode = function_exists( 'get_option' ) ? (bool) get_option( 'newfold_burst_safety_mode', false ) : false; +$newfold_cache_level = function_exists( 'newfold_cache_level' ) ? (int) get_option( 'newfold_cache_level', 0 ) : 0; // Check if Performance feature is enabled and it's necessary reset the cache options if ( class_exists( 'NewfoldLabs\WP\Module\Performance\PerformanceFeatureHooks' ) ) { From 64a02550a581112eedaef75414b8f51740153f58 Mon Sep 17 00:00:00 2001 From: Alessio Torrisi Date: Tue, 7 Jan 2025 09:34:07 +0100 Subject: [PATCH 6/6] Fix: phpcs issue yoda condition --- includes/burstSafetyModeFunctions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/burstSafetyModeFunctions.php b/includes/burstSafetyModeFunctions.php index 6cc03cc..53cb68c 100644 --- a/includes/burstSafetyModeFunctions.php +++ b/includes/burstSafetyModeFunctions.php @@ -10,7 +10,7 @@ $site_skip404 = (bool) get_option( 'newfold_skip_404_handling', true ); if ( defined( 'BURST_SAFETY_MODE' ) && BURST_SAFETY_MODE ) { - if ( $newfold_burst_safety_mode === false ) { + if ( false === $newfold_burst_safety_mode ) { $current_level = get_option( Performance::OPTION_CACHE_LEVEL ); update_option( 'newfold_burst_safety_mode', true ); update_option( 'newfold_burst_safety_mode_site_cache_level', $current_level );