From a9301b89575e29013742208251f550d50e092a37 Mon Sep 17 00:00:00 2001 From: ninomihovilic Date: Mon, 21 Oct 2024 10:38:30 +0200 Subject: [PATCH] Add delete transients --- assets/js/safety-net-admin.js | 17 ++++++++ includes/admin.php | 42 +++++++++++++++++++- includes/bootstrap.php | 21 +++++++++- includes/classes/cli/class-safetynet-cli.php | 15 +++++++ includes/delete-transients.php | 23 +++++++++++ safety-net.php | 1 + 6 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 includes/delete-transients.php diff --git a/assets/js/safety-net-admin.js b/assets/js/safety-net-admin.js index 7e575a3..5434608 100644 --- a/assets/js/safety-net-admin.js +++ b/assets/js/safety-net-admin.js @@ -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!') ) { @@ -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); diff --git a/includes/admin.php b/includes/admin.php index 8f9d45f..66c3f58 100644 --- a/includes/admin.php +++ b/includes/admin.php @@ -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' ); @@ -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' ); @@ -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', @@ -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' ), @@ -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. * diff --git a/includes/bootstrap.php b/includes/bootstrap.php index 56aa214..cbeaed7 100644 --- a/includes/bootstrap.php +++ b/includes/bootstrap.php @@ -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. * @@ -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. * diff --git a/includes/classes/cli/class-safetynet-cli.php b/includes/classes/cli/class-safetynet-cli.php index 947f761..a898403 100644 --- a/includes/classes/cli/class-safetynet-cli.php +++ b/includes/classes/cli/class-safetynet-cli.php @@ -1,6 +1,7 @@ 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(); +} diff --git a/safety-net.php b/safety-net.php index d1bdca1..4577074 100644 --- a/safety-net.php +++ b/safety-net.php @@ -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';