Skip to content

Commit

Permalink
Merge pull request #30 from newfold-labs/enhance/PRESS7-77-skip-404
Browse files Browse the repository at this point in the history
Enhance/press7 77 skip 404
  • Loading branch information
arunshenoy99 authored Jan 8, 2025
2 parents a0922f1 + 3a2b61d commit bd73107
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 62 deletions.
128 changes: 71 additions & 57 deletions components/cacheSettings/index.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,79 @@
import { Container, RadioGroup } from "@newfold/ui-component-library";
// Newfold
import { Container, RadioGroup } from '@newfold/ui-component-library';

const CacheSettings = ({ methods, constants, Components }) => {
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,
() => {
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 (
<>
Expand Down Expand Up @@ -97,4 +110,5 @@ const CacheSettings = ({ methods, constants, Components }) => {
);
}

export default CacheSettings;

export default CacheSettings;
10 changes: 10 additions & 0 deletions components/performance/defaultText.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ const defaultText = {
),
clearCacheNoticeTitle: __( 'Cache cleared', 'wp-module-performance' ),
clearCacheTitle: __( 'Clear Cache', 'wp-module-performance' ),

// Skip 404
skip404Title: __( 'Skip 404', 'wp-module-performance' ),
skip404Description: __(
'When enabled, static resources like images and fonts will use a default server 404 page and not WordPress 404 pages. Pages and posts will continue using WordPress for 404 pages. This can considerably speed up your website if a static resource like an image or font is missing.',
'wp-module-performance'
),
skip404NoticeTitle: __( 'Skip 404 saved', 'wp-module-performance' ),
skip404Notice: __( 'Skip 404 saved', 'wp-module-performance' ),

// Image Optimization
imageOptimizationSettingsTitle: __(
Expand Down Expand Up @@ -164,6 +173,7 @@ const defaultText = {
'Something went wrong while updating the settings. Please try again.',
'wp-module-performance'
),

linkPrefetchDescription: __(
'Asks the browser to download and cache links on the page ahead of them being clicked on, so that when they are clicked they load almost instantly. ',
'wp-module-performance'
Expand Down
8 changes: 8 additions & 0 deletions components/performance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Container } from '@newfold/ui-component-library';
// Components.
import { default as CacheSettings } from '../cacheSettings/';
import { default as ClearCache } from '../clearCache/';
import { default as Skip404 } from '../skip404/';
import { default as AdvancedSettings } from '../advancedSettings';
import { default as defaultText } from './defaultText';
import ImageOptimizationSettings from '../imageOptimizationSettings';
Expand Down Expand Up @@ -62,6 +63,13 @@ const Performance = ( { methods, constants, Components, ...props } ) => {
>
<ClearCache methods={ methods } constants={ constants } />
</Container.Block>
<Container.Block separator={ true } className={ 'newfold-skip404' }>
<Skip404
className={ 'newfold-skip404' }
methods={ methods }
constants={ constants }
/>
</Container.Block>
<Container.Block
separator={ true }
className={ 'newfold-performance-advanced-settings' }
Expand Down
71 changes: 71 additions & 0 deletions components/skip404/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// WordPress
import apiFetch from '@wordpress/api-fetch';

// Newfold
import { Checkbox, Container } from '@newfold/ui-component-library';
import { NewfoldRuntime } from '@newfold-labs/wp-module-runtime';

const Skip404 = ( { methods, constants } ) => {
const [ skip404, setSkip404 ] = methods.useState(
NewfoldRuntime.sdk.performance.skip404
);

const getSkip404NoticeTitle = () => {
return constants.text.skip404NoticeTitle;
};

const handleSkip404Change = () => {
const value = ! skip404;
apiFetch( {
path: 'newfold-performance/v1/settings',
method: 'POST',
data: {
field: {
id: 'skip404',
value,
},
},
} )
.then( () => {
methods.makeNotice(
'skip404-change-notice',
getSkip404NoticeTitle(),
'',
'success',
5000
);
} )
.catch( () => {
methods.makeNotice(
'skip404-change-notice',
constants.text.optionNotSet,
'',
'error',
5000
);
} );

setSkip404( value );
};

return (
<Container.SettingsField
title={ constants.text.skip404Title }
description={ constants.text.skip404Description }
>
<Checkbox
id="skip-404"
name="skip-404"
onChange={ handleSkip404Change }
value={ skip404 }
checked={ skip404 }
label={ __(
'Skip 404 Handling For Static Files',
'wp-module-performance'
) }
/>
</Container.SettingsField>
);
};

export default Skip404;
12 changes: 10 additions & 2 deletions includes/Performance.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
namespace NewfoldLabs\WP\Module\Performance;

use NewfoldLabs\WP\ModuleLoader\Container;
use NewfoldLabs\WP\Module\Performance\Permissions;

use NewfoldLabs\WP\Module\Installer\Services\PluginInstaller;

use NewfoldLabs\WP\Module\Performance\Permissions;
use NewfoldLabs\WP\Module\Performance\Images\ImageManager;
use NewfoldLabs\WP\Module\Performance\RestApi\RestApi;
use NewfoldLabs\WP\Module\Performance\Data\Constants;
use NewfoldLabs\WP\Module\Performance\CacheTypes\Browser;
use NewfoldLabs\WP\Module\Performance\CacheTypes\File;
use NewfoldLabs\WP\Module\Performance\CacheTypes\Skip404;

use Automattic\Jetpack\Current_Plan;
use NewfoldLabs\WP\Module\Performance\Data\Constants;


/**
* Performance Class
Expand Down Expand Up @@ -311,6 +317,7 @@ public function enqueue_scripts() {
}

/*
* Add to Newfold SDK runtime.
*
* @param array $sdk SDK data.
Expand All @@ -327,6 +334,7 @@ public function add_to_runtime( $sdk ) {
'jetpack_boost_minify_css' => get_option( 'jetpack_boost_status_minify-css', array() ),
'jetpack_boost_minify_css_excludes' => implode( ',', get_option( 'jetpack_boost_ds_minify_css_excludes', array( 'admin-bar', 'dashicons', 'elementor-app' ) ) ),
'install_token' => PluginInstaller::rest_get_plugin_install_hash(),
'skip404' => (bool) get_option( 'newfold_skip_404_handling', false ),
);

return array_merge( $sdk, array( 'performance' => $values ) );
Expand Down
2 changes: 1 addition & 1 deletion includes/RestApi/RestApi.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace NewfoldLabs\WP\Module\Performance\RestApi;

/**
Expand All @@ -13,6 +12,7 @@ final class RestApi {
* @var array
*/
protected $controllers = array(
'NewfoldLabs\\WP\\Module\\Performance\\RestApi\\SettingsController',
'NewfoldLabs\\WP\\Module\\Performance\\RestApi\\LinkPrefetchController',
'NewfoldLabs\\WP\\Module\\Performance\\RestApi\\JetpackController',
);
Expand Down
Loading

0 comments on commit bd73107

Please sign in to comment.