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

Add delete transients #139

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions assets/js/safety-net-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
const deactivatePluginsButton = document.getElementById( 'safety-net-deactivate-plugins' );
const deleteUsersButton = document.getElementById( 'safety-net-delete-users' );
const settingsTitle = document.getElementById( 'safety-net-settings-title' );
const deleteTransientsButton = document.getElementById( 'safety-net-delete-transients' );

function deleteTransients() {
if ( ! confirm( 'Are you sure you want to delete transients? This cannot be undone!') ) {
return;
}

ajax({
action: 'safety_net_delete_transients',
nonce: deleteTransientsButton.dataset.nonce,
});
}

function scrubOptions() {
if ( ! confirm( 'Are you sure you want to scrub options? This cannot be undone!') ) {
Expand Down Expand Up @@ -148,4 +160,9 @@
if (deleteUsersButton) {
deleteUsersButton.addEventListener('click', deleteUsers);
}

// If the delete transients button exists, add a click event listener.
if (deleteTransientsButton) {
deleteTransientsButton.addEventListener('click', deleteTransients);
}
})(window, document, jQuery);
42 changes: 41 additions & 1 deletion includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use function SafetyNet\DeactivatePlugins\deactivate_plugins;
use function SafetyNet\Delete\delete_users_and_orders;
use function SafetyNet\Utilities\is_production;
use function SafetyNet\DeleteTransients\delete_transients;

add_filter( 'init', __NAMESPACE__ . '\add_admin_hooks' );

Expand All @@ -24,6 +25,7 @@ function add_admin_hooks() {
add_action( 'wp_ajax_safety_net_scrub_options', __NAMESPACE__ . '\handle_ajax_scrub_options' );
add_action( 'wp_ajax_safety_net_deactivate_plugins', __NAMESPACE__ . '\handle_ajax_deactivate_plugins' );
add_action( 'wp_ajax_safety_net_delete_users', __NAMESPACE__ . '\handle_ajax_delete_users' );
add_action( 'wp_ajax_safety_net_delete_transients', __NAMESPACE__ . '\handle_ajax_delete_transients' );
add_filter( 'plugin_action_links_' . SAFETY_NET_BASENAME, __NAMESPACE__ . '\add_action_links' );
}
add_action( 'action_scheduler_pre_init', __NAMESPACE__ . '\pause_renewal_actions' );
Expand Down Expand Up @@ -122,7 +124,7 @@ function settings_init() {

add_settings_field(
'safety_net_delete_users',
esc_html__( 'Delete All Users, Orders, and Subscriptions', 'safety-net' ),
esc_html__( 'Delete Users, Orders, and Subscriptions', 'safety-net' ),
__NAMESPACE__ . '\render_field',
'safety_net_options',
'safety_net_option',
Expand All @@ -134,6 +136,20 @@ function settings_init() {
)
);

add_settings_field(
'safety_net_delete_transients',
esc_html__( 'Delete Transients', 'safety-net' ),
__NAMESPACE__ . '\render_field',
'safety_net_options',
'safety_net_option',
array(
'type' => 'button',
'id' => 'safety-net-delete-transients',
'button_text' => esc_html__( 'Delete', 'safety-net' ),
'description' => esc_html__( 'Deletes all transients.', 'safety-net' ),
)
);

add_settings_field(
'safety_net_pause_renewal_actions_toggle',
esc_html__( 'Pause renewal actions', 'safety-net' ),
Expand Down Expand Up @@ -334,6 +350,30 @@ function handle_ajax_delete_users() {
die();
}

/**
* Handles the AJAX request for deleting transients.
*
* @return void
*/
function handle_ajax_delete_transients() {

// Permissions and security checks.
check_the_permissions();
check_the_nonce( $_POST['nonce'], 'safety-net-delete-transients' ); // phpcs:ignore WordPress.Security.NonceVerification

// Checks passed. Delete the transients.
delete_transients();

echo wp_json_encode(
array(
'success' => true,
'message' => esc_html__( 'Transients have been deleted.' ),
)
);

die();
}

/**
* Checks if the user has the correct permissions, and sends the AJAX response if they don't.
*
Expand Down
21 changes: 20 additions & 1 deletion includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
add_action( 'safety_net_loaded', __NAMESPACE__ . '\maybe_scrub_options' );
add_action( 'safety_net_loaded', __NAMESPACE__ . '\maybe_deactivate_plugins' );
add_action( 'safety_net_loaded', __NAMESPACE__ . '\maybe_delete_data' );

add_action( 'safety_net_loaded', __NAMESPACE__ . '\maybe_delete_transients' );
/**
* Determines if we should set the 'Pause renewal actions' toggle when first loading the plugin.
*
Expand All @@ -31,6 +31,25 @@ function maybe_pause_renewal_actions() {
update_option( 'safety_net_pause_renewal_actions_toggle', 'on' );
}

/**
* Determines if transients should be deleted.
*
* Transients will be deleted if we're on staging, development, or local AND they haven't already been deleted.
*/
function maybe_delete_transients() {
// If transients have already been deleted, skip.
if ( get_option( 'safety_net_transients_deleted' ) ) {
return;
}

// If we're not on staging, development, or a local environment, return.
if ( is_production() ) {
return;
}

do_action( 'safety_net_delete_transients' );
}

/**
* Determines if options should be scrubbed.
*
Expand Down
15 changes: 15 additions & 0 deletions includes/classes/cli/class-safetynet-cli.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use function SafetyNet\Delete\delete_users_and_orders;
use function SafetyNet\DeleteTransients\delete_transients;

/**
* Anonymizer command line utilities.
Expand All @@ -21,6 +22,20 @@ public function delete() {
WP_CLI::success( __( 'Users and their data have been deleted' ) );
}

/**
* Delete all transients
*
* ## EXAMPLES
*
* wp safety-net delete-transients
*
*/
public function delete_transients() {
delete_transients();

WP_CLI::success( __( 'Transients have been deleted' ) );
}

/**
* Clear options such as API keys so that plugins won't talk to 3rd parties
*
Expand Down
23 changes: 23 additions & 0 deletions includes/delete-transients.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace SafetyNet\DeleteTransients;

add_action( 'safety_net_delete_transients', __NAMESPACE__ . '\delete_transients' );

/**
* Deletes all transients.
*
* @return void
*/
function delete_transients() {

global $wpdb;

// Delete all transients
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_%'" );

// Set option so this function doesn't run again.
update_option( 'safety_net_transients_deleted', true );

wp_cache_flush();
}
1 change: 1 addition & 0 deletions safety-net.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/common.php';
require_once __DIR__ . '/includes/delete.php';
require_once __DIR__ . '/includes/delete-transients.php';
require_once __DIR__ . '/includes/utilities.php';
require_once __DIR__ . '/includes/deactivate-plugins.php';
require_once __DIR__ . '/includes/scrub-options.php';
Expand Down