Skip to content

Commit

Permalink
delete transients
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercasares committed Sep 23, 2024
1 parent 1913bb6 commit 8176a67
Showing 1 changed file with 51 additions and 4 deletions.
55 changes: 51 additions & 4 deletions wpvulnerability-run.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ function wpvulnerability_expired_database_data() {
// Check and update core, plugins, and themes vulnerabilities if cache has expired.
$components = array(
'core' => 'wpvulnerability-core-cache',
'plugins' => 'wpvulnerability-plugins-cache',
'themes' => 'wpvulnerability-themes-cache',
'plugin' => 'wpvulnerability-plugins-cache',
'theme' => 'wpvulnerability-themes-cache',
);

foreach ( $components as $component => $cache_key ) {
Expand Down Expand Up @@ -284,19 +284,65 @@ function wpvulnerability_deactivation() {
$delete_option_func( $option );
}

wpvulnerability_delete_transients();

// Unschedule and remove scheduled wp-cron jobs.
$cron_jobs = array(
'wpvulnerability_notification',
'wpvulnerability_update_database',
'wpvulnerability_pull_db_data_event',
);

foreach ( $cron_jobs as $job ) {
wp_unschedule_event( wp_next_scheduled( $job ), $job );
wp_clear_scheduled_hook( $job );
}
}

/**
* Deletes all transients that start with 'wpvulnerability_'.
*
* @since 3.5.0
*
* @return void
*/
function wpvulnerability_delete_transients() {
global $wpdb;

// Determine if the site is multisite.
$is_multisite = is_multisite();

// Define the prefix according to whether it is multisite or not.
$transient_prefix = $is_multisite ? '_site_transient_wpvulnerability_' : '_transient_wpvulnerability_';

// Prepare the LIKE pattern securely.
$like_pattern = $wpdb->esc_like($transient_prefix) . '%';

// Secure query to get all transients starting with the specified prefix.
$transients = $wpdb->get_col(
$wpdb->prepare(
"SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s",
$like_pattern
)
);

if (empty($transients)) {
// No matching transients found.
return;
}

foreach ($transients as $transient) {
if ($is_multisite) {
// For multisite, delete using delete_site_transient.
$transient_name = str_replace('_site_transient_', '', $transient);
delete_site_transient($transient_name);
} else {
// For single sites, delete using delete_transient.
$transient_name = str_replace('_transient_', '', $transient);
delete_transient($transient_name);
}
}
}

/**
* Callback function to run when the plugin is uninstalled.
* Deletes options and removes scheduled wp-cron jobs.
Expand Down Expand Up @@ -356,12 +402,13 @@ function wpvulnerability_uninstall() {
// Delete config data.
$delete_option_func( 'wpvulnerability-config' );

wpvulnerability_delete_transients();

// Unschedule and remove scheduled wp-cron jobs.
$cron_jobs = array(
'wpvulnerability_notification',
'wpvulnerability_update_database',
);

foreach ( $cron_jobs as $job ) {
wp_unschedule_event( wp_next_scheduled( $job ), $job );
wp_clear_scheduled_hook( $job );
Expand Down

0 comments on commit 8176a67

Please sign in to comment.