From 1321adf31facebb056bb8e6f6671e7136f3902fa Mon Sep 17 00:00:00 2001 From: Alessio Torrisi Date: Fri, 15 Nov 2024 15:26:42 +0100 Subject: [PATCH 1/6] New: option Skip 404 in Performance section --- components/cacheSettings/index.js | 106 ++++++++++++++++++-------- components/performance/defaultText.js | 4 + 2 files changed, 80 insertions(+), 30 deletions(-) diff --git a/components/cacheSettings/index.js b/components/cacheSettings/index.js index 05a0553..2be0778 100644 --- a/components/cacheSettings/index.js +++ b/components/cacheSettings/index.js @@ -1,7 +1,8 @@ -import { Container, RadioGroup } from "@newfold/ui-component-library"; +import { Checkbox, Container, RadioGroup } from "@newfold/ui-component-library"; const CacheSettings = ({ methods, constants, Components }) => { const [ cacheLevel, setCacheLevel ] = methods.useState(constants.store.cacheLevel); + const [ skip404, setSkip404 ] = methods.useState(constants.store.skip404); const cacheOptions = [ { @@ -38,6 +39,11 @@ const CacheSettings = ({ methods, constants, Components }) => { return cacheOptions[cacheLevel].notice; }; + + const getSkip404NoticeTitle = () => { + return constants.text.skip404NoticeTitle; + }; + const handleCacheLevelChange = (e) => { methods.newfoldSettingsApiFetch( { cacheLevel: parseInt(e.target.value) }, @@ -47,6 +53,15 @@ const CacheSettings = ({ methods, constants, Components }) => { ); }; + const handleSkip404Change = (e) => { + methods.newfoldSettingsApiFetch( + { skip404: ! skip404 }, + methods.setError, (response) => { + setSkip404( ! skip404 ); + } + ); + }; + methods.useUpdateEffect(() => { methods.setStore({ ...constants.store, @@ -62,36 +77,67 @@ const CacheSettings = ({ methods, constants, Components }) => { ); }, [cacheLevel]); + + methods.useUpdateEffect(() => { + methods.setStore({ + ...constants.store, + skip404, + }); + + methods.makeNotice( + "skip-404-change-notice", + getSkip404NoticeTitle(), + '', + "success", + 5000 + ); + }, [skip404]); + return ( - - - {cacheOptions.map((option) => { - return ( - - -
- {option.description} -
-
- ); - })} -
-
+ <> + + + {cacheOptions.map((option) => { + return ( + + +
+ {option.description} +
+
+ ); + })} +
+
+ + + + + ); } diff --git a/components/performance/defaultText.js b/components/performance/defaultText.js index 2603b96..443b0a2 100644 --- a/components/performance/defaultText.js +++ b/components/performance/defaultText.js @@ -22,6 +22,10 @@ const defaultText = { clearCacheDescription: __('We automatically clear your cache as you work (creating content, changing settings, installing plugins and more). But you can manually clear it here to be confident it is fresh.', 'wp-module-performance'), clearCacheNoticeTitle: __('Cache cleared', 'wp-module-performance'), clearCacheTitle: __('Clear Cache', 'wp-module-performance'), + skip404Title: __( 'Skip 404', 'wp-module-performance' ), + skip404Description: __( 'Skip WordPress 404 Handling For Static Files', 'wp-module-performance' ), + skip404NoticeTitle: __('Skip 404 saved', 'wp-module-performance'), + skip404Notice: __('Skip 404 saved', 'wp-module-performance'), }; export default defaultText; \ No newline at end of file From 9baa46beaf1cccbec986b5434b453c14dddd624a Mon Sep 17 00:00:00 2001 From: Alessio Torrisi Date: Wed, 20 Nov 2024 13:10:28 +0100 Subject: [PATCH 2/6] Added API to handle change option --- components/cacheSettings/index.js | 290 +++++++++++++----------- components/performance/index.js | 83 +++---- includes/Performance.php | 24 +- includes/Permissions.php | 52 +++++ includes/RestApi/RestApi.php | 39 ++++ includes/RestApi/SettingsController.php | 117 ++++++++++ package.json | 3 +- 7 files changed, 435 insertions(+), 173 deletions(-) create mode 100644 includes/Permissions.php create mode 100644 includes/RestApi/RestApi.php create mode 100644 includes/RestApi/SettingsController.php diff --git a/components/cacheSettings/index.js b/components/cacheSettings/index.js index 2be0778..f77812d 100644 --- a/components/cacheSettings/index.js +++ b/components/cacheSettings/index.js @@ -1,144 +1,174 @@ -import { Checkbox, Container, RadioGroup } from "@newfold/ui-component-library"; +// WordPress +import apiFetch from '@wordpress/api-fetch'; -const CacheSettings = ({ methods, constants, Components }) => { - const [ cacheLevel, setCacheLevel ] = methods.useState(constants.store.cacheLevel); - const [ skip404, setSkip404 ] = methods.useState(constants.store.skip404); +// Newfold +import { Checkbox, Container, RadioGroup } from '@newfold/ui-component-library'; +import { NewfoldRuntime } from '@newfold-labs/wp-module-runtime'; - 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 CacheSettings = ( { methods, constants, Components } ) => { + const [ cacheLevel, setCacheLevel ] = methods.useState( + constants.store.cacheLevel + ); + const [ skip404, setSkip404 ] = methods.useState( + NewfoldRuntime.sdk.skip404 + ); - const getCacheLevelNoticeTitle = () => { - return constants.text.cacheLevelNoticeTitle; - }; + 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 getCacheLevelNoticeText = () => { - return cacheOptions[cacheLevel].notice; - }; + const getCacheLevelNoticeTitle = () => { + return constants.text.cacheLevelNoticeTitle; + }; + const getCacheLevelNoticeText = () => { + return cacheOptions[ cacheLevel ].notice; + }; - const getSkip404NoticeTitle = () => { - return constants.text.skip404NoticeTitle; - }; + const getSkip404NoticeTitle = () => { + return constants.text.skip404NoticeTitle; + }; - 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 ) ); + } + ); + }; - const handleSkip404Change = (e) => { - methods.newfoldSettingsApiFetch( - { skip404: ! skip404 }, - methods.setError, (response) => { - setSkip404( ! skip404 ); - } - ); - }; + const handleSkip404Change = () => { + const value = ! skip404; - methods.useUpdateEffect(() => { - methods.setStore({ - ...constants.store, - cacheLevel, - }); + apiFetch( { + path: 'newfold-performance/v1/settings', + method: 'POST', + data: { + field: { + id: 'skip404', + value, + }, + }, + } ) + .then( () => { + setSkip404( value ); - methods.makeNotice( - "cache-level-change-notice", - getCacheLevelNoticeTitle(), - getCacheLevelNoticeText(), - "success", - 5000 - ); - }, [cacheLevel]); + methods.makeNotice( + 'skip-404-change-notice', + getSkip404NoticeTitle(), + '', + 'success', + 5000 + ); + } ) + .catch( () => { + methods.makeNotice( + 'skip-404-change-notice', + constants.text.optionNotSet, + '', + 'error', + 5000 + ); + } ); + }; + methods.useUpdateEffect( () => { + methods.setStore( { + ...constants.store, + cacheLevel, + } ); - methods.useUpdateEffect(() => { - methods.setStore({ - ...constants.store, - skip404, - }); + methods.makeNotice( + 'cache-level-change-notice', + getCacheLevelNoticeTitle(), + getCacheLevelNoticeText(), + 'success', + 5000 + ); + }, [ cacheLevel ] ); - methods.makeNotice( - "skip-404-change-notice", - getSkip404NoticeTitle(), - '', - "success", - 5000 - ); - }, [skip404]); + 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/performance/index.js b/components/performance/index.js index e6d7483..0070001 100644 --- a/components/performance/index.js +++ b/components/performance/index.js @@ -6,54 +6,55 @@ import { default as defaultText } from './defaultText'; /** * Performance Module * For use in brand plugin apps to display performance page and settings - * - * @param {*} props - * @returns + * + * @param {*} props + * @return */ -const Performance = ({methods, constants, Components, ...props}) => { - const { store, setStore } = methods.useContext(methods.AppStore); - const [ isError, setError ] = methods.useState(false); +const Performance = ( { methods, constants, Components, ...props } ) => { + const { store, setStore } = methods.useContext( methods.AppStore ); + const [ isError, setError ] = methods.useState( false ); - let notify = methods.useNotification(); + const notify = methods.useNotification(); // set default text if not provided - constants.text = Object.assign(defaultText, constants.text); + constants.text = Object.assign( defaultText, constants.text ); - const makeNotice = (id, title, description, variant="success", duration=false) => { - notify.push(`performance-notice-${id}`, { - title, - description: ( - - {description} - - ), - variant, - autoDismiss: duration, - }); - }; - constants.store = store; - methods.makeNotice = makeNotice; - methods.setStore = setStore; - methods.setError = setError; + const makeNotice = ( + id, + title, + description, + variant = 'success', + duration = false + ) => { + notify.push( `performance-notice-${ id }`, { + title, + description: { description }, + variant, + autoDismiss: duration, + } ); + }; + constants.store = store; + methods.makeNotice = makeNotice; + methods.setStore = setStore; + methods.setError = setError; return ( - <> - - - - - - - + <> + + + + + + + ); - }; -export default Performance; \ No newline at end of file +export default Performance; diff --git a/includes/Performance.php b/includes/Performance.php index ac50c2f..5492c1f 100644 --- a/includes/Performance.php +++ b/includes/Performance.php @@ -2,9 +2,11 @@ namespace NewfoldLabs\WP\Module\Performance; +use NewfoldLabs\WP\Module\Performance\RestApi\RestApi; use NewfoldLabs\WP\Module\Performance\CacheTypes\Browser; use NewfoldLabs\WP\Module\Performance\CacheTypes\File; use NewfoldLabs\WP\Module\Performance\CacheTypes\Skip404; +use NewfoldLabs\WP\Module\Performance\Permissions; use NewfoldLabs\WP\ModuleLoader\Container; /** @@ -80,6 +82,12 @@ public function __construct( Container $container ) { $container->set( 'cachePurger', $cachePurger ); $container->set( 'hasMustUsePlugin', file_exists( WPMU_PLUGIN_DIR . '/endurance-page-cache.php' ) ); + + if ( Permissions::is_authorized_admin() || Permissions::rest_is_authorized_admin() ) { + new RestAPI(); + } + + add_filter( 'newfold-runtime', array( $this, 'add_to_runtime' ), 10 ); } /** @@ -287,9 +295,23 @@ public function adminBarMenu( \WP_Admin_Bar $wp_admin_bar ) { 'id' => 'nfd_purge_menu-cache_settings', 'title' => __( 'Cache Settings', 'newfold-module-performance' ), 'parent' => 'nfd_purge_menu', - 'href' => admin_url( 'options-general.php#' . Performance::SETTINGS_ID ), + 'href' => admin_url( 'options-general.php#' . self::SETTINGS_ID ), ) ); } } + + + /** + * Add to Newfold SDK runtime. + * + * @param array $sdk SDK data. + * @return array SDK data. + */ + public function add_to_runtime( $sdk ) { + $values = array( + 'skip404' => (bool) get_option( 'newfold_skip_404_handling', false ), + ); + return array_merge( $sdk, array( 'performance' => $values ) ); + } } diff --git a/includes/Permissions.php b/includes/Permissions.php new file mode 100644 index 0000000..1b5bf86 --- /dev/null +++ b/includes/Permissions.php @@ -0,0 +1,52 @@ +controllers as $controller ) { + /** + * Get an instance of the WP_REST_Controller. + * + * @var $instance WP_REST_Controller + */ + $instance = new $controller(); + $instance->register_routes(); + } + } +} diff --git a/includes/RestApi/SettingsController.php b/includes/RestApi/SettingsController.php new file mode 100644 index 0000000..8d25082 --- /dev/null +++ b/includes/RestApi/SettingsController.php @@ -0,0 +1,117 @@ +namespace, + $this->rest_base, + array( + array( + 'methods' => \WP_REST_Server::CREATABLE, + 'callback' => array( $this, 'set_options' ), + 'permission_callback' => function () { + return current_user_can( 'manage_options' ); + }, + ), + ) + ); + } + + /** + * Set Jetpack options. + * + * @param WP_REST_Request $request The request object. + * @return WP_REST_Response + */ + public function set_options( $request ) { + try { + $params = $request->get_params(); + + if ( ! isset( $params['field'] ) || ! is_array( $params['field'] ) ) { + return new \WP_REST_Response( + array( + 'success' => false, + 'error' => __( "The parameter 'field' is missing or invalid.", 'newfold-performance-module' ), + ), + 400 + ); + } + + $field = $params['field']; + + if ( ! isset( $field['id'], $field['value'] ) ) { + return new \WP_REST_Response( + array( + 'success' => false, + 'error' => __( "The fields 'id' and 'value' are required.", 'newfold-performance-module' ), + ), + 400 + ); + } + + switch ( $field['id'] ) { + case 'skip404': + $result = update_option( 'newfold_skip_404_handling', $field['value'] ); + break; + + default: + break; + } + + if ( false === $result ) { + return new \WP_REST_Response( + array( + 'success' => false, + 'error' => __( 'An error occurred while updating the option.', 'newfold-performance-module' ), + ), + 500 + ); + } + + // Success response. + return new \WP_REST_Response( + array( + 'success' => true, + 'updated_option' => $field['id'], + 'updated_value' => $field['value'], + ), + 200 + ); + } catch ( \Exception $e ) { + // Exceptions handling. + return new \WP_REST_Response( + array( + 'success' => false, + 'error' => __( 'An error occurred while updating the option.', 'newfold-performance-module' ) . $e->getMessage(), + ), + 500 + ); + } + } +} diff --git a/package.json b/package.json index 6388cb6..e57c793 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ ], "dependencies": { "@heroicons/react": "^2.0.18", - "@newfold/ui-component-library": "^1.0.1" + "@newfold/ui-component-library": "^1.0.1", + "@newfold-labs/wp-module-runtime": "^1.0.12" }, "devDependencies": { "@testing-library/cypress": "^9.0.0", From 1ccc6e9923d703b7e2840f5f8564b5f80ef3a8aa Mon Sep 17 00:00:00 2001 From: Alessio Torrisi Date: Wed, 20 Nov 2024 15:29:45 +0100 Subject: [PATCH 3/6] Tweak: show "skip404" option in a separated section --- components/cacheSettings/index.js | 61 +------------- components/performance/defaultText.js | 101 +++++++++++++++++------- components/performance/index.js | 8 ++ components/skip404/index.js | 71 +++++++++++++++++ includes/RestApi/SettingsController.php | 2 +- 5 files changed, 154 insertions(+), 89 deletions(-) create mode 100644 components/skip404/index.js diff --git a/components/cacheSettings/index.js b/components/cacheSettings/index.js index f77812d..fdc35a0 100644 --- a/components/cacheSettings/index.js +++ b/components/cacheSettings/index.js @@ -1,17 +1,10 @@ -// WordPress -import apiFetch from '@wordpress/api-fetch'; - // Newfold -import { Checkbox, Container, RadioGroup } from '@newfold/ui-component-library'; -import { NewfoldRuntime } from '@newfold-labs/wp-module-runtime'; +import { Container, RadioGroup } from '@newfold/ui-component-library'; const CacheSettings = ( { methods, constants, Components } ) => { const [ cacheLevel, setCacheLevel ] = methods.useState( constants.store.cacheLevel ); - const [ skip404, setSkip404 ] = methods.useState( - NewfoldRuntime.sdk.skip404 - ); const cacheOptions = [ { @@ -56,10 +49,6 @@ const CacheSettings = ( { methods, constants, Components } ) => { return cacheOptions[ cacheLevel ].notice; }; - const getSkip404NoticeTitle = () => { - return constants.text.skip404NoticeTitle; - }; - const handleCacheLevelChange = ( e ) => { methods.newfoldSettingsApiFetch( { cacheLevel: parseInt( e.target.value ) }, @@ -70,41 +59,6 @@ const CacheSettings = ( { methods, constants, Components } ) => { ); }; - const handleSkip404Change = () => { - const value = ! skip404; - - apiFetch( { - path: 'newfold-performance/v1/settings', - method: 'POST', - data: { - field: { - id: 'skip404', - value, - }, - }, - } ) - .then( () => { - setSkip404( value ); - - methods.makeNotice( - 'skip-404-change-notice', - getSkip404NoticeTitle(), - '', - 'success', - 5000 - ); - } ) - .catch( () => { - methods.makeNotice( - 'skip-404-change-notice', - constants.text.optionNotSet, - '', - 'error', - 5000 - ); - } ); - }; - methods.useUpdateEffect( () => { methods.setStore( { ...constants.store, @@ -154,19 +108,6 @@ const CacheSettings = ( { methods, constants, Components } ) => { } ) } - - - ); }; diff --git a/components/performance/defaultText.js b/components/performance/defaultText.js index 443b0a2..6bf5f3d 100644 --- a/components/performance/defaultText.js +++ b/components/performance/defaultText.js @@ -1,31 +1,76 @@ const defaultText = { - cacheLevel0Description: __('No cache enabled. Every page load is fresh. ', 'wp-module-performance'), - cacheLevel0Label: __('Disabled', 'wp-module-performance'), - cacheLevel0NoticeText: __('Caching disabled.', 'wp-module-performance'), - cacheLevel0Recommendation: __('Not recommended.', 'wp-module-performance'), - cacheLevel1Description: __('Cache static assets like images and the appearance of your site for 1 hour. ', 'wp-module-performance'), - cacheLevel1Label: __('Assets Only', 'wp-module-performance'), - cacheLevel1NoticeText: __('Cache enabled for assets only.', 'wp-module-performance'), - cacheLevel1Recommendation: __('Tuned for online stores and member sites that need to be fresh.', 'wp-module-performance'), - cacheLevel2Description: __('Cache static assets for 24 hours and web pages for 2 hours. ', 'wp-module-performance'), - cacheLevel2Label: __('Assets & Web Pages', 'wp-module-performance'), - cacheLevel2NoticeText: __('Cache enabled for assets and pages.', 'wp-module-performance'), - cacheLevel2Recommendation: __('Tuned for sites that change at least weekly.', 'wp-module-performance'), - cacheLevel3Description: __('Cache static assets for 1 week and web pages for 8 hours. ', 'wp-module-performance'), - cacheLevel3Label: __('Assets & Web Pages - Extended', 'wp-module-performance'), - cacheLevel3NoticeText: __('Cache enabled for assets and pages (extended).', 'wp-module-performance'), - cacheLevel3Recommendation: __('Tuned for sites that update a few times a month or less.', 'wp-module-performance'), - cacheLevelDescription: __('Boost speed and performance by storing a copy of your website content, files, and images online so the pages of your website load faster for your visitors.', 'wp-module-performance'), - cacheLevelNoticeTitle: __('Cache setting saved', 'wp-module-performance'), - cacheLevelTitle: __('Cache Level', 'wp-module-performance'), - clearCacheButton: __('Clear All Cache Now', 'wp-module-performance'), - clearCacheDescription: __('We automatically clear your cache as you work (creating content, changing settings, installing plugins and more). But you can manually clear it here to be confident it is fresh.', 'wp-module-performance'), - clearCacheNoticeTitle: __('Cache cleared', 'wp-module-performance'), - clearCacheTitle: __('Clear Cache', 'wp-module-performance'), - skip404Title: __( 'Skip 404', 'wp-module-performance' ), - skip404Description: __( 'Skip WordPress 404 Handling For Static Files', 'wp-module-performance' ), - skip404NoticeTitle: __('Skip 404 saved', 'wp-module-performance'), - skip404Notice: __('Skip 404 saved', 'wp-module-performance'), + cacheLevel0Description: __( + 'No cache enabled. Every page load is fresh.', + 'wp-module-performance' + ), + cacheLevel0Label: __( 'Disabled', 'wp-module-performance' ), + cacheLevel0NoticeText: __( 'Caching disabled.', 'wp-module-performance' ), + cacheLevel0Recommendation: __( + 'Not recommended.', + 'wp-module-performance' + ), + cacheLevel1Description: __( + 'Cache static assets like images and the appearance of your site for 1 hour.', + 'wp-module-performance' + ), + cacheLevel1Label: __( 'Assets Only', 'wp-module-performance' ), + cacheLevel1NoticeText: __( + 'Cache enabled for assets only.', + 'wp-module-performance' + ), + cacheLevel1Recommendation: __( + 'Tuned for online stores and member sites that need to be fresh.', + 'wp-module-performance' + ), + cacheLevel2Description: __( + 'Cache static assets for 24 hours and web pages for 2 hours.', + 'wp-module-performance' + ), + cacheLevel2Label: __( 'Assets & Web Pages', 'wp-module-performance' ), + cacheLevel2NoticeText: __( + 'Cache enabled for assets and pages.', + 'wp-module-performance' + ), + cacheLevel2Recommendation: __( + 'Tuned for sites that change at least weekly.', + 'wp-module-performance' + ), + cacheLevel3Description: __( + 'Cache static assets for 1 week and web pages for 8 hours.', + 'wp-module-performance' + ), + cacheLevel3Label: __( + 'Assets & Web Pages - Extended', + 'wp-module-performance' + ), + cacheLevel3NoticeText: __( + 'Cache enabled for assets and pages (extended).', + 'wp-module-performance' + ), + cacheLevel3Recommendation: __( + 'Tuned for sites that update a few times a month or less.', + 'wp-module-performance' + ), + cacheLevelDescription: __( + 'Boost speed and performance by storing a copy of your website content, files, and images online so the pages of your website load faster for your visitors.', + 'wp-module-performance' + ), + cacheLevelNoticeTitle: __( 'Cache setting saved', 'wp-module-performance' ), + cacheLevelTitle: __( 'Cache Level', 'wp-module-performance' ), + clearCacheButton: __( 'Clear All Cache Now', 'wp-module-performance' ), + clearCacheDescription: __( + 'We automatically clear your cache as you work (creating content, changing settings, installing plugins and more). But you can manually clear it here to be confident it is fresh.', + 'wp-module-performance' + ), + clearCacheNoticeTitle: __( 'Cache cleared', 'wp-module-performance' ), + clearCacheTitle: __( 'Clear Cache', 'wp-module-performance' ), + 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' ), }; -export default defaultText; \ No newline at end of file +export default defaultText; diff --git a/components/performance/index.js b/components/performance/index.js index 0070001..8460dcf 100644 --- a/components/performance/index.js +++ b/components/performance/index.js @@ -1,6 +1,7 @@ import { Container } from '@newfold/ui-component-library'; import { default as CacheSettings } from '../cacheSettings/'; import { default as ClearCache } from '../clearCache/'; +import { default as Skip404 } from '../skip404/'; import { default as defaultText } from './defaultText'; /** @@ -50,6 +51,13 @@ const Performance = ( { methods, constants, Components, ...props } ) => { Components={ Components } /> + + + diff --git a/components/skip404/index.js b/components/skip404/index.js new file mode 100644 index 0000000..cefb179 --- /dev/null +++ b/components/skip404/index.js @@ -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 ( + + + + ); +}; + +export default Skip404; diff --git a/includes/RestApi/SettingsController.php b/includes/RestApi/SettingsController.php index 8d25082..0b043d6 100644 --- a/includes/RestApi/SettingsController.php +++ b/includes/RestApi/SettingsController.php @@ -74,7 +74,7 @@ public function set_options( $request ) { 400 ); } - +error_log($field['value']); switch ( $field['id'] ) { case 'skip404': $result = update_option( 'newfold_skip_404_handling', $field['value'] ); From 835ab32239c65635f2c692f4cda918978505da32 Mon Sep 17 00:00:00 2001 From: Alessio Torrisi Date: Wed, 20 Nov 2024 15:33:07 +0100 Subject: [PATCH 4/6] Removed error_log --- includes/RestApi/SettingsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/RestApi/SettingsController.php b/includes/RestApi/SettingsController.php index 0b043d6..8d25082 100644 --- a/includes/RestApi/SettingsController.php +++ b/includes/RestApi/SettingsController.php @@ -74,7 +74,7 @@ public function set_options( $request ) { 400 ); } -error_log($field['value']); + switch ( $field['id'] ) { case 'skip404': $result = update_option( 'newfold_skip_404_handling', $field['value'] ); From 8e3902db86ca30330ca1931e377088da3cf929d5 Mon Sep 17 00:00:00 2001 From: Alessio Torrisi Date: Tue, 7 Jan 2025 09:05:25 +0100 Subject: [PATCH 5/6] Fix: phpcs issue --- includes/Performance.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Performance.php b/includes/Performance.php index 2d168bc..d2debd7 100644 --- a/includes/Performance.php +++ b/includes/Performance.php @@ -93,7 +93,7 @@ public function __construct( Container $container ) { $container->set( 'hasMustUsePlugin', file_exists( WPMU_PLUGIN_DIR . '/endurance-page-cache.php' ) ); - add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); if ( Permissions::is_authorized_admin() || Permissions::rest_is_authorized_admin() ) { new RestAPI(); @@ -334,7 +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 ), + 'skip404' => (bool) get_option( 'newfold_skip_404_handling', false ), ); return array_merge( $sdk, array( 'performance' => $values ) ); From 3a2b61d7096b9b33b727d7ac7a15f8132b6f3de1 Mon Sep 17 00:00:00 2001 From: Alessio Torrisi Date: Tue, 7 Jan 2025 15:36:40 +0100 Subject: [PATCH 6/6] Tweak: removed unused packages --- package-lock.json | 2 +- package.json | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 13360f7..57c29d7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,7 @@ "name": "@newfold-labs/wp-module-performance", "license": "GPL-2.0-or-later", "dependencies": { - "@newfold-labs/wp-module-runtime": "^1.0.0", + "@newfold-labs/wp-module-runtime": "^1.0.12", "@newfold/ui-component-library": "^1.1.0", "html-react-parser": "^5.1.18" }, diff --git a/package.json b/package.json index f6dac48..e900646 100644 --- a/package.json +++ b/package.json @@ -11,10 +11,8 @@ "William Earnhardt (https://wearnhardt.com)" ], "dependencies": { - "@heroicons/react": "^2.0.18", "@newfold/ui-component-library": "^1.1.0", "@newfold-labs/wp-module-runtime": "^1.0.12", - "@tailwindcss/forms": "^0.5.8", "html-react-parser": "^5.1.18" }, "devDependencies": {