Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Press7 93 burst safety mode #41

Merged
merged 7 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ function ( $features ) {
}

new PerformanceFeatureHooks();

require_once __DIR__ . '/includes/BurstSafetyMode/init.php';
arunshenoy99 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion components/advancedSettings/JetpackBoost/SingleOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const SingleOption = ( { params, isChild, methods, constants } ) => {
! NewfoldRuntime.sdk.performance
.jetpack_boost_premium_is_active && (
<FeatureUpsell
cardText="Upgrade to Unlock"
cardText={ constants.text.upgradeModule }
cardLink={ option.premiumUrl }
>
<ToggleField
Expand Down
73 changes: 37 additions & 36 deletions components/cacheSettings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,41 +74,42 @@ const CacheSettings = ( { methods, constants, Components } ) => {
);
}, [ cacheLevel ] );


return (
<>
<Container.SettingsField
title={constants.text.cacheLevelTitle}
description={constants.text.cacheLevelDescription}
>
<RadioGroup
className="cache-options"
id="cache-type"
name="cache-level"
value=""
>
{cacheOptions.map((option) => {
return (
<Components.Fragment key={option.value}>
<RadioGroup.Radio
defaultChecked={option.value === constants.store.cacheLevel}
id={'cache-level-' + option.value}
label={option.label}
value={option.value}
name="cache-level"
onChange={handleCacheLevelChange}
/>
<div className="nfd-radio__description">
{option.description}
</div>
</Components.Fragment>
);
})}
</RadioGroup>
</Container.SettingsField>
</>
);
}

return (
<>
<Container.SettingsField
title={ constants.text.cacheLevelTitle }
description={ constants.text.cacheLevelDescription }
>
<RadioGroup
className="cache-options"
id="cache-type"
name="cache-level"
value=""
>
{ cacheOptions.map( ( option ) => {
return (
<Components.Fragment key={ option.value }>
<RadioGroup.Radio
defaultChecked={
option.value ===
constants.store.cacheLevel
}
id={ 'cache-level-' + option.value }
label={ option.label }
value={ option.value }
name="cache-level"
onChange={ handleCacheLevelChange }
/>
<div className="nfd-radio__description">
{ option.description }
</div>
</Components.Fragment>
);
} ) }
</RadioGroup>
</Container.SettingsField>
</>
);
};

export default CacheSettings;
70 changes: 34 additions & 36 deletions components/clearCache/index.js
Original file line number Diff line number Diff line change
@@ -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 (
<Container.SettingsField
title={ constants.text.clearCacheTitle }
description={ constants.text.clearCacheDescription }
>
<Button
variant="secondary"
className="clear-cache-button"
disabled={ constants.store.cacheLevel > 0 ? false : true }
onClick={ clearCache }
>
{ constants.text.clearCacheButton }
</Button>
</Container.SettingsField>
);
};

return (
<Container.SettingsField
title={constants.text.clearCacheTitle}
description={constants.text.clearCacheDescription}
>
<Button
variant="secondary"
className="clear-cache-button"
disabled={constants.store.cacheLevel > 0 ? false : true}
onClick={clearCache}
>
{constants.text.clearCacheButton}
</Button>
</Container.SettingsField>

);
;}

export default ClearCache;
export default ClearCache;
1 change: 1 addition & 0 deletions components/performance/defaultText.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ const defaultText = {
),
optionSet: __( 'Option saved correctly', 'wp-module-performance' ),
optionNotSet: __( 'Error saving option', 'wp-module-performance' ),
upgradeModule: __( 'Upgrade to unlock', 'wp-module-performance' ),
};

export default defaultText;
59 changes: 59 additions & 0 deletions includes/BurstSafetyMode/Browser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
namespace NewfoldLabs\WP\Module\Performance\BurstSafetyMode;
use NewfoldLabs\WP\Module\Performance\BurstSafetyMode\ResponseHeaderManager;
use WP_Forge\WP_Htaccess_Manager\htaccess;

class Browser {
/**
* The file marker name.
*
* @var string
*/
const MARKER = 'Newfold Browser Cache';

public function __construct() {
$responseHeaderManager = new ResponseHeaderManager();
$responseHeaderManager->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[] = '<IfModule mod_expires.c>';
$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 [] = '</IfModule>';

$htaccess = new htaccess( self::MARKER );

return $htaccess->addContent( $rules );
}
}
116 changes: 116 additions & 0 deletions includes/BurstSafetyMode/ResponseHeaderManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace NewfoldLabs\WP\Module\Performance\BurstSafetyMode;

use WP_Forge\WP_Htaccess_Manager\htaccess;
use function WP_Forge\WP_Htaccess_Manager\convertContentToLines;

class ResponseHeaderManager {

/**
* The file marker name.
*
* @var string
*/
const MARKER = 'Newfold Headers';

public $htaccess;

/**
* Constructor.
*/
public function __construct() {
$this->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 = '<IfModule mod_headers.c>' . PHP_EOL;
foreach ( $headers as $key => $value ) {
$content .= "\t" . "Header set {$key} \"{$value}\"" . PHP_EOL;
}
$content .= '</IfModule>';

$this->htaccess->addContent( $content );
}
}
45 changes: 45 additions & 0 deletions includes/BurstSafetyMode/Skip404.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
namespace NewfoldLabs\WP\Module\Performance\BurstSafetyMode;
use function WP_Forge\WP_Htaccess_Manager\addContent;
use function WP_Forge\WP_Htaccess_Manager\removeMarkers;

class Skip404 {
/**
* The file marker name.
*/
const MARKER = 'Newfold Skip 404 Handling for Static Files';

/**
* Constructor.
*/
public function __construct() {

$this->addRules();
}


/**
* Add our rules to the .htacces file.
*/
public static function addRules() {
$content = <<<HTACCESS
<IfModule mod_rewrite.c>
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]
</IfModule>
HTACCESS;

addContent( self::MARKER, $content );
}

/**
* Remove our rules from the .htaccess file.
*/
public static function removeRules() {
removeMarkers( self::MARKER );
}
}
Loading
Loading