Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/add data removing option #1251

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
185 changes: 185 additions & 0 deletions admin/class-admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class WPUF_Admin_Settings {
*/
private $menu_pages = [];

/**
* The settings upon plugin uninstallation
*
* @var array
*/
private $uninstall_settings = [];

public function __construct() {
if ( ! class_exists( 'WeDevs_Settings_API' ) ) {
require_once dirname( __DIR__ ) . '/lib/class.settings-api.php';
Expand All @@ -49,6 +56,7 @@ public function __construct() {
add_filter( 'submenu_file', [ $this, 'fix_submenu_file' ] );

add_action( 'admin_init', [ $this, 'handle_tools_action' ] );
add_action( 'admin_init', [ $this, 'uninstall_section' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_styles' ] );

add_filter( 'wp_handle_upload_prefilter', [ $this, 'enable_json_upload' ], 1 );
Expand Down Expand Up @@ -363,6 +371,97 @@ public function fix_submenu_file( $submenu_file ) {
return $submenu_file;
}

/**
* The uninstall settings section for User Frontend > Tools
*
* @since WPUF
*
* @return void
*/
public function uninstall_section() {
$section_id = 'wpuf_uninstall';

$uninstall_fields = apply_filters(
'wpuf_uninstall_settings', [
[
'id' => 'delete_settings',
'label' => __( 'Delete Plugin Settings', 'wp-user-frontend' ),
'callback' => 'delete_settings_markup',
],
[
'id' => 'delete_database',
'label' => __( 'Delete Database Tables', 'wp-user-frontend' ),
'callback' => 'delete_database_markup',
],
[
'id' => 'delete_forms',
'label' => __( 'Delete WPUF Forms', 'wp-user-frontend' ),
'callback' => 'delete_forms_markup',
],
[
'id' => 'delete_pages',
'label' => __( 'Delete WPUF Pages', 'wp-user-frontend' ),
'callback' => 'delete_pages_markup',
],
]
);

// registering the section
register_setting( $section_id, $section_id, [ $this, 'sanitize_dropdown' ] );

// adding the section
add_settings_section( $section_id, __( 'Uninstall Settings', 'wp-user-frontend' ), [ $this, 'uninstall_section_callback' ], 'wpuf_tools' );

foreach ( $uninstall_fields as $field ) {
$field_id = $field['id'];
$field_label = $field['label'];

$args = [
'id' => $field_id,
'class' => ! empty( $field['class'] ) ? $field['class'] : '',
'label_for' => "{$section_id}[{$field['id']}]",
'desc' => ! empty( $field['desc'] ) ? $field['desc'] : '',
'name' => $field_label,
'section' => $section_id,
'size' => ! empty( $field['size'] ) ? $field['size'] : null,
'options' => ! empty( $field['options'] ) ? $field['options'] : '',
'type' => ! empty( $field['type'] ) ? $field['type'] : '',
];

add_settings_field( $section_id . '["' . $field_id . '"]', $field_label, [ $this, $field['callback'] ], 'wpuf_tools', $section_id, $args );
}

$this->uninstall_settings = get_option( $section_id );
}

/**
* Sanitize callback for yes/no dropdown
*
* @since WPUF
*
* @return array
*/
public function sanitize_dropdown( $input ) {
if ( ! $input ) {
return $input;
}

$options = [ 'delete_settings', 'delete_database', 'delete_forms', 'delete_pages' ];

// Create our array for storing the validated options
$output = [];

// Loop through each of the incoming options
foreach ( $input as $key => $value ) {
// Check to see if the current option has a value. If so, process it.
if ( in_array( $key, $options, true ) && 'on' === $value ) {
$output[ $key ] = $value;
}
}

return $output;
}

/**
* Hanlde tools page action
*
Expand Down Expand Up @@ -426,6 +525,92 @@ public function handle_tools_action() {
exit;
}

/**
* Delete settings checkbox HTML markup
*
* @since WPUF
*
* @return void
*/
public function delete_settings_markup() {
$value = ! empty( $this->uninstall_settings['delete_settings'] ) ? $this->uninstall_settings['delete_settings'] : 'off';
?>
<label class="switch">
<input type="checkbox" id="wpuf_uninstall[delete_settings]" name="wpuf_uninstall[delete_settings]" <?php checked( $value, 'on' ); ?>>
<span class="slider round"></span>
</label>
<?php
}

/**
* Delete database checkbox HTML markup
*
* @since WPUF
*
* @return void
*/
public function delete_database_markup() {
$value = ! empty( $this->uninstall_settings['delete_database'] ) ? $this->uninstall_settings['delete_database'] : 'off';
?>
<label class="switch">
<input type="checkbox" id="wpuf_uninstall[delete_database]" name="wpuf_uninstall[delete_database]" <?php checked( $value, 'on' ); ?>>
<span class="slider round"></span>
</label>
<?php
}

/**
* Delete WPUF forms checkbox HTML markup
*
* @since WPUF
*
* @return void
*/
public function delete_forms_markup() {
$value = ! empty( $this->uninstall_settings['delete_forms'] ) ? $this->uninstall_settings['delete_forms'] : 'off';
?>
<label class="switch">
<input type="checkbox" id="wpuf_uninstall[delete_forms]" name="wpuf_uninstall[delete_forms]" <?php checked( $value, 'on' ); ?>>
<span class="slider round"></span>
</label>
<?php
}

/**
* Delete WPUF pages checkbox HTML markup
*
* @since WPUF
*
* @return void
*/
public function delete_pages_markup() {
$value = ! empty( $this->uninstall_settings['delete_pages'] ) ? $this->uninstall_settings['delete_pages'] : 'off';
?>
<label class="switch">
<input type="checkbox" id="wpuf_uninstall[delete_pages]" name="wpuf_uninstall[delete_pages]" <?php checked( $value, 'on' ); ?>>
<span class="slider round"></span>
</label>
<?php
}

/**
* The HTML section for User Frontend > Tools > Uninstall Settings
*
* @since WPUF
*
* @param $args
*
* @return void
*/
public function uninstall_section_callback( $args ) {
?>
<p style="background: #EF4444; color: #FFF; padding: 10px; font-size: 1.1em">
<strong><?php esc_html_e( 'Caution:', 'wp-user-frontend' ); ?></strong>
<?php esc_html_e( ' Check this to remove WP User Frontend related data and table from the database upon deleting the plugin. When you delete the WP User Frontend free version, it will also remove all the data related to WP User Frontend Pro as well. This won\'t happen when the plugins are deactivated.', 'wp-user-frontend' ); ?>
</p>
<?php
}

/**
* Delete all posts by a post type
*
Expand Down
72 changes: 72 additions & 0 deletions admin/class-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,66 @@ public function tool_page() {

<?php } ?>

<style>
.danger {
color: #BB0000;
}
.switch {
position: relative;
display: inline-block;
width: 47px;
height: 20px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 15px;
width: 15px;
left: 3px;
bottom: 3px;
background-color: #FFF;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
.form-table th {
width: 250px;
}
</style>

<div class="metabox-holder">
<div class="postbox">
<h3><?php esc_html_e( 'Page Installation', 'wp-user-frontend' ); ?></h3>
Expand Down Expand Up @@ -363,6 +423,18 @@ public function tool_page() {
<a class="button button-primary" href="<?php echo esc_url( wp_nonce_url( add_query_arg( [ 'wpuf_action' => 'clear_transaction' ], 'admin.php?page=wpuf_tools&action=tools' ), 'wpuf-tools-action' ) ); ?>" onclick="return confirm('<?php echo esc_attr( $confirmation_message ); ?>');"><?php esc_html_e( 'Delete Transactions', 'wp-user-frontend' ); ?></a>
</div>
</div>

<div class="postbox">
<div class="inside">
<form method="POST" action="options.php">
<?php
settings_fields( 'wpuf_uninstall' );
do_settings_sections( 'wpuf_tools' );
submit_button();
?>
</form>
</div>
</div>
</div>
<?php
}
Expand Down
Loading