Skip to content

Commit

Permalink
Introduce the Settings Reset tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
shazahm1 committed Jan 25, 2024
1 parent 755a76c commit 890bf6b
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
35 changes: 35 additions & 0 deletions assets/js/cn-system-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,41 @@
$form.find('input[type="submit"]').removeAttr('disabled');
},
});

$('#cn-settings-reset').ajaxForm({
type: 'post',
dataType: 'json',
url: ajaxurl,
beforeSubmit(arr, $form, options) {
$form
.find('input[type="submit"]')
.attr('disabled', 'disabled');
},
success(response, status, jqXHR, $form) {
CN_System_Tools.ajaxSuccess(
'#cn-settings-reset-response',
response.data.message,
status,
jqXHR
);
$form
.find('input[name="settings-reset-confirmation"]')
.val('');
$form.find('input[name="submit"]').removeAttr('disabled');
},
error(XMLHttpRequest, status, error, $form) {
CN_System_Tools.ajaxError(
'#cn-settings-reset-response',
XMLHttpRequest.responseJSON.data.message,
status,
error
);
$form
.find('input[name="settings-reset-confirmation"]')
.val('');
$form.find('input[type="submit"]').removeAttr('disabled');
},
});
},

setupValidation: function () {
Expand Down
104 changes: 104 additions & 0 deletions includes/Hook/Action/Ajax/Settings_Reset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Reset the settings to their default values.
*
* @since 10.4.62
*
* @category WordPress\Plugin
* @package Connections_Directory
* @subpackage Connections_Directory\Hook\Action\Ajax
* @author Steven A. Zahm
* @license GPL-2.0+
* @copyright Copyright (c) 2024, Steven A. Zahm
* @link https://connections-pro.com/
*/

declare( strict_types=1 );

namespace Connections_Directory\Hook\Action\Ajax;

use cnSettingsAPI;
use Connections_Directory\Request;
use Connections_Directory\Utility\_array;

/**
* Class Settings_Export_Import
*
* @package Connections_Directory\Hook\Action\Ajax
*/
final class Settings_Reset {

use Response;

/**
* Callback for the `admin_init` action.
*
* Register the action hooks.
*
* @since 10.4.62
*/
public static function register() {

add_action( 'wp_ajax_settings-reset', array( __CLASS__, 'reset' ) );
}

/**
* Callback for the `wp_ajax_settings-reset` action.
*
* AJAX callback to download the settings in a JSON encoded text file.
*
* @internal
* @since 10.4.62
*/
public static function reset() {

$action = new self();

if ( $action->isValid( 'settings-reset' ) ) {

// phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce validation occurs in the isValid() method.
$confirmation = _array::get( $_POST, 'settings-reset-confirmation', false );

if ( 'reset' !== $confirmation ) {

$action->error(
__( 'Please enter "reset" into the confirmation field to reset the settings.', 'connections' ),
null,
403
);
}

$settings = cnSettingsAPI::getAll();
$slugs = array_keys( $settings );

foreach ( $slugs as $slug ) {
cnSettingsAPI::reset( $slug );
}

$action->success( __( 'Settings have been reset to their default values.', 'connections' ) );

} else {

$action->error(
__( 'You do not have sufficient permissions to reset the settings.', 'connections' ),
null,
403
);
}
}

/**
* Whether the request nonce is valid.
*
* @since 10.4.62
*
* @param string $action The nonce action name to validate.
*
* @return bool
*/
private function isValid( $action ) {

return current_user_can( 'manage_options' ) &&
Request\Nonce::from( INPUT_POST, $action )->isValid();
}
}
22 changes: 22 additions & 0 deletions includes/admin/pages/tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,28 @@ public static function reset() {
do_action( 'cn_tools_reset_before' );

?>
<div class="postbox">
<h3><span><?php esc_html_e( 'Reset Settings', 'connections' ); ?></span></h3>
<div class="inside">
<div id="cn-settings-reset-response"></div>
<p>
<?php
esc_html_e(
'Reset settings to the default values. This action can not be undone.',
'connections'
);
?>
</p>
<form id="cn-settings-reset" method="post" action="<?php echo esc_url( self_admin_url( 'admin-ajax.php' ) ); ?>">
<p>
<input type="text" name="settings-reset-confirmation"> <?php esc_html_e( 'Type "reset" to confirm that you wish to reset the settings the the default values.', 'connections' ); ?>
</p>
<input type="hidden" name="action" value="settings-reset" />
<?php _nonce::field( 'settings-reset' ); ?>
<?php submit_button( __( 'Reset Settings', 'connections' ), 'secondary' ); ?>
</form>
</div><!-- .inside -->
</div><!-- .postbox -->
<div class="postbox">
<h3><span><?php esc_html_e( 'Reset Database', 'connections' ); ?></span></h3>
<div class="inside">
Expand Down
1 change: 1 addition & 0 deletions includes/class.connections-directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ private static function hooks() {
add_action( 'admin_init', array( Action\Admin\Tools\Import_Categories::class, 'register' ) );
add_action( 'admin_init', array( Action\Ajax\Category_Metabox_Height::class, 'register' ), 9 );
add_action( 'admin_init', array( Action\Ajax\Database_Reset::class, 'register' ), 9 );
add_action( 'admin_init', array( Action\Ajax\Settings_Reset::class, 'register' ), 9 );
add_action( 'admin_init', array( Action\Ajax\System_Information::class, 'register' ), 9 );
add_action( 'admin_init', array( Action\Ajax\Settings_Export_Import::class, 'register' ), 9 );

Expand Down
1 change: 1 addition & 0 deletions includes/class.dependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ private static function classMap() {
'Connections_Directory\Hook\Action\Ajax\Database_Reset' => 'includes/Hook/Action/Ajax/Database_Reset.php',
'Connections_Directory\Hook\Action\Ajax\Response' => 'includes/Hook/Action/Ajax/Response.php',
'Connections_Directory\Hook\Action\Ajax\Settings_Export_Import' => 'includes/Hook/Action/Ajax/Settings_Export_Import.php',
'Connections_Directory\Hook\Action\Ajax\Settings_Reset' => 'includes/Hook/Action/Ajax/Settings_Reset.php',
'Connections_Directory\Hook\Action\Ajax\System_Information' => 'includes/Hook/Action/Ajax/System_Information.php',
'Connections_Directory\Hook\Filter\Admin\Footer' => 'includes/Hook/Filter/Admin/Footer.php',
'Connections_Directory\Hook\Filter\Admin\Plugin_Row' => 'includes/Hook/Filter/Admin/Plugin_Row.php',
Expand Down

0 comments on commit 890bf6b

Please sign in to comment.