Skip to content

Commit

Permalink
Tweak: handling of failure during saving options
Browse files Browse the repository at this point in the history
  • Loading branch information
AleTorrisi committed Nov 18, 2024
1 parent 319e8c9 commit 23ed28b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 26 deletions.
20 changes: 11 additions & 9 deletions components/advancedSettings/JetpackBoost/SingleOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ const SingleOption = ( {params, isChild, methods, constants } ) => {

setOptionDetails({ ...optionDetails, value: value });

// Cancella il timeout precedente se l'utente digita di nuovo
// Clear the previous timeout if user types again.
if (debounceTimeout.current) {
clearTimeout(debounceTimeout.current);
}

// Imposta un nuovo timeout di 2 secondi
// Set a new timeout of 2 seconds.
debounceTimeout.current = setTimeout(() => {
apiFetch({
path: 'newfold-performance/v1/jetpack/settings',
Expand All @@ -52,24 +52,26 @@ const SingleOption = ( {params, isChild, methods, constants } ) => {
}).then((response) => {
methods.makeNotice(
"cache-level-change-notice",
constants.text.jetpackOptionSaved,
constants.text.optionSet,
'',
"success",
5000
);
}).catch((error) => {

console.error(error);
methods.makeNotice(
"cache-level-change-notice",
constants.text.optionNotSet,
'',
"error",
5000
);
});
}, 1000);


}

const handleTextInputChange = ( value, id ) => {

};


const displayOption = ( params ) => {
switch( params.type ){
case 'toggle':
Expand Down
3 changes: 2 additions & 1 deletion components/performance/defaultText.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ const defaultText = {
jetpackBoostShowMore: __( 'Show more', 'wp-module-performance' ),
jetpackBoostShowLess: __( 'Show less', 'wp-module-performance' ),
jetpackBoostDicoverMore: __( 'Discover More', 'wp-module-performance' ),
jetpackOptionSaved: __( 'Option saved', 'wp-module-performance' ),
jetpackBoostCtaText: __( 'Install Jetpack Boost to unlock', 'wp-module-performance' ),
jetpackBoostInstalling: __( "Installing Jetpack Boost...", 'wp-module-performance' ),
jetpackBoostActivated: __( "Jetpack Boost is now active", 'wp-module-performance' ),
jetpackBoostActivationFailed: __( "Activation failed", 'wp-module-performance' ),
optionSet: __( 'Option saved correctly', 'wp-module-performance' ),
optionNotSet: __( 'Error saving option', 'wp-module-performance' ),
};

export default defaultText;
81 changes: 65 additions & 16 deletions includes/RestApi/JetpackController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class JetpackController {
protected $rest_base = '/jetpack';

/**
* Plugin slug
* Plugin slug.
*
* @var string
*/
Expand Down Expand Up @@ -58,7 +58,7 @@ public function register_routes() {
}

/**
* Get options
* Get Jetpack options.
*
* @return WP_REST_Response
*/
Expand All @@ -79,26 +79,75 @@ public function get_options() {


/**
* Set options
* Set Jetpack options.
*
* @param WP_REST_Request $request The request object.
* @return WP_REST_Response
*/
public function set_options( $request ) {
$data = $request->get_params();
if ( isset( $data['field'] ) ) {
$field = $data['field'];
if ( in_array( $field['id'], array( 'minify-js-excludes', 'minify-css-excludes' ) ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
$id = str_replace( '-', '_', $field['id'] );
$value = explode( ',', $field['value'] );
$data = update_option( 'jetpack_boost_ds_' . $id, $value );
} else {
$data = update_option( 'jetpack_boost_status_' . $field['id'], $field['value'] );
try {
$params = $request->get_params();

if ( ! isset( $params['field'] ) || ! is_array( $params['field'] ) ) {
return new \WP_REST_Response(
array(
'success' => false,
'error' => 'Il parametro "field" è mancante o non è valido.',
),
400
);
}

$field = $params['field'];

if ( ! isset( $field['id'], $field['value'] ) ) {
return new \WP_REST_Response(
array(
'success' => false,
'error' => 'I campi "id" e "value" sono richiesti.',
),
400
);
}

$option_key = 'jetpack_boost_status_' . $field['id'];
$option_value = $field['value'];

if ( in_array( $field['id'], array( 'minify-js-excludes', 'minify-css-excludes' ), true ) ) {
$option_key = 'jetpack_boost_ds_' . str_replace( '-', '_', $field['id'] );
$option_value = explode( ',', $field['value'] );
}

$result = update_option( $option_key, $option_value );

if ( $result === false ) {
return new \WP_REST_Response(
array(
'success' => false,
'error' => 'Errore durante l\'aggiornamento delle opzioni.',
),
500
);
}

// Restituisci una risposta di successo.
return new \WP_REST_Response(
array(
'success' => true,
'updated_option' => $option_key,
'updated_value' => $option_value,
),
200
);
} catch ( \Exception $e ) {
// Gestione delle eccezioni.
return new \WP_REST_Response(
array(
'success' => false,
'error' => 'Si è verificato un errore: ' . $e->getMessage(),
),
500
);
}
return new \WP_REST_Response(
array( $data ),
200
);
}
}

0 comments on commit 23ed28b

Please sign in to comment.