Skip to content

Commit

Permalink
improvise the code
Browse files Browse the repository at this point in the history
  • Loading branch information
faisal-alvi committed Sep 5, 2024
1 parent f2f9b73 commit e0530b9
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,34 @@

/**
* Function to delete all relevant data from the site (single or multisite).
*
* @param bool $is_multisite Whether it's a multisite installation.
*/
function dt_delete_data( $is_multisite = false ) {
global $wpdb;

// Delete post meta and posts of type 'dt_subscription'.
$subscription_post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'dt_subscription';" );
$subscription_post_ids = $wpdb->get_col(
"SELECT ID FROM $wpdb->posts WHERE post_type = %s",
'dt_subscription'
);

if ( ! empty( $subscription_post_ids ) ) {
$ids_string = implode( ',', array_map( 'intval', $subscription_post_ids ) );

// Delete subscription meta.
$wpdb->query( "DELETE FROM $wpdb->postmeta WHERE post_id IN ($ids_string);" );
$wpdb->query(
$wpdb->prepare(
"DELETE FROM $wpdb->postmeta WHERE post_id IN ($ids_string);"
)
);

// Delete subscription posts.
$wpdb->query( "DELETE FROM $wpdb->posts WHERE ID IN ($ids_string);" );
$wpdb->query(
$wpdb->prepare(
"DELETE FROM $wpdb->posts WHERE ID IN ($ids_string);"
)
);

// Clear the post cache.
wp_cache_set_posts_last_changed();
Expand All @@ -50,6 +63,8 @@ function dt_delete_data( $is_multisite = false ) {

/**
* Delete all relevant options from a site (single or multisite).
*
* @param bool $is_multisite Whether it's a multisite installation.
*/
function delete_site_options( $is_multisite = false ) {
global $wpdb;
Expand All @@ -60,22 +75,19 @@ function delete_site_options( $is_multisite = false ) {
'_transient_timeout_dt_',
);

// Include multisite-specific prefixes if not a single site.
if ( ! $is_multisite ) {
$option_prefixes[] = '_site_transient_dt_';
$option_prefixes[] = '_site_transient_timeout_dt_';
}

// Determine the appropriate table and column based on multisite or single site.
// Include multisite-specific prefixes if it's not a single site.
// Also determine the appropriate table and column based on multisite or single site.
if ( $is_multisite ) {
$table = $wpdb->sitemeta;
$id_column = 'meta_id';
$option_prefixes[] = '_site_transient_dt_';
$option_prefixes[] = '_site_transient_timeout_dt_';
$table = $wpdb->sitemeta;
$id_column = 'meta_id';
$key_column = 'meta_key';
$site_column = 'site_id';
$site_id = get_current_network_id();
$site_id = get_current_network_id();
} else {
$table = $wpdb->options;
$id_column = 'option_id';
$table = $wpdb->options;
$id_column = 'option_id';
$key_column = 'option_name';
}

Expand Down Expand Up @@ -104,7 +116,11 @@ function delete_site_options( $is_multisite = false ) {
$ids_string = implode( ',', array_map( 'intval', $options_to_delete ) );

// Delete the options using the retrieved IDs.
$wpdb->query( "DELETE FROM $table WHERE $id_column IN ($ids_string);" );
$wpdb->query(
$wpdb->prepare(
"DELETE FROM $table WHERE $id_column IN ($ids_string);"
)
);

// Flush the relevant caches.
$cache_group = $is_multisite ? 'site-options' : 'options';
Expand Down

0 comments on commit e0530b9

Please sign in to comment.