Skip to content

Commit

Permalink
Settings Saved Notice (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
namithj authored Nov 13, 2024
1 parent 79b36a3 commit 13d84ee
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions includes/class-admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct() {
add_action( is_multisite() ? 'network_admin_menu' : 'admin_menu', [ $this, 'register_admin_menu' ] );
add_action( 'admin_init', [ $this, 'reset_settings' ] );
add_action( 'admin_init', [ $this, 'register_settings' ] );
add_action( 'admin_notices', [ $this, 'reset_admin_notice' ] );
add_action( is_multisite() ? 'network_admin_notices' : 'admin_notices', [ $this, 'admin_notices' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );

add_action( 'admin_init', [ $this, 'update_settings' ] );
Expand Down Expand Up @@ -118,21 +118,46 @@ public function delete_all_settings() {
}

/**
* The Admin Notice to convey a Reset Operation has happened.
* Show Admin notices.
*
* @return void
*/
public function reset_admin_notice() {
public function admin_notices() {
/**
* The Admin Notice to convey a Reset Operation has happened.
*/
if (
( 'true' === get_site_option( 'aspireupdate-reset' ) ) &&
isset( $_GET['reset-success'] ) &&
( 'success' === $_GET['reset-success'] ) &&
isset( $_GET['reset-success-nonce'] ) &&
wp_verify_nonce( sanitize_key( $_GET['reset-success-nonce'] ), 'aspireupdate-reset-success-nonce' )
) {
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Settings have been reset to default.', 'AspireUpdate' ) . '.</p></div>';
add_settings_error(
$this->option_name,
'aspireupdate_settings_reset',
esc_html__( 'Settings have been reset to default.', 'AspireUpdate' ),
'success'
);
settings_errors( $this->option_name );
delete_site_option( 'aspireupdate-reset' );
}

/**
* The Admin Notice to convey settings have been successsfully saved.
*/
if (
isset( $_GET['settings-updated-wpnonce'] ) &&
wp_verify_nonce( sanitize_key( wp_unslash( $_GET['settings-updated-wpnonce'] ) ), 'aspireupdate-settings-updated-nonce' )
) {
add_settings_error(
$this->option_name,
'aspireupdate_settings_saved',
esc_html__( 'Settings Saved', 'AspireUpdate' ),
'success'
);
settings_errors( $this->option_name );
}
}

/**
Expand Down Expand Up @@ -247,7 +272,12 @@ public function update_settings() {
);

wp_safe_redirect(
add_query_arg( [ network_admin_url( 'index.php?page=aspireupdate-settings' ) ] )
add_query_arg(
[
'settings-updated-wpnonce' => wp_create_nonce( 'aspireupdate-settings-updated-nonce' ),
],
network_admin_url( 'index.php?page=aspireupdate-settings' )
)
);
exit;
}
Expand Down

0 comments on commit 13d84ee

Please sign in to comment.