-
-
Save peterwilsoncc/286025a3ca49b90c4d4f2e06741396a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Delete dt_subscription posts and associated postmeta from the database. | |
*/ | |
function delete_site_post_data() { | |
global $wpdb; | |
// Get all subscription IDs. | |
$subscription_post_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'dt_subscription';" ); | |
// Delete subscription meta. | |
$wpdb->query( | |
$wpdb->prepare( | |
sprintf( | |
"DELETE FROM $wpdb->postmeta WHERE post_id IN ( %s );", | |
implode( ',', array_fill( 0, count( $subscription_post_ids ), '%s' ) ) | |
), | |
$subscription_post_ids | |
) | |
); | |
// Delete subscription posts. | |
$wpdb->query( | |
$wpdb->prepare( | |
sprintf( | |
"DELETE FROM $wpdb->posts WHERE ID IN ( %s );", | |
implode( ',', array_fill( 0, count( $subscription_post_ids ), '%s' ) ) | |
), | |
$subscription_post_ids | |
) | |
); | |
// Clear the post cache. | |
wp_cache_set_posts_last_changed(); | |
wp_cache_delete_multiple( $subscription_post_ids, 'posts' ); | |
wp_cache_delete_multiple( $subscription_post_ids, 'post_meta' ); | |
} | |
/** | |
* Delete all options from a single site. | |
*/ | |
function delete_site_options() { | |
global $wpdb; | |
$option_prefixes = array( | |
'dt_', | |
'_transient_dt_', | |
'_transient_timeout_dt_', | |
); | |
if ( ! is_multisite() ) { | |
$option_prefixes[] = '_site_transient_dt_'; | |
$option_prefixes[] = '_site_transient_timeout_dt_'; | |
} | |
$options_to_delete = $wpdb->get_results( | |
$wpdb->prepare( | |
sprintf( | |
"SELECT option_name, autoload FROM $wpdb->options WHERE 1 = 1 AND ( %s );", | |
implode( ' OR ', array_fill( 0, count( $option_prefixes ), 'option_name LIKE %s' ) ) | |
), | |
array_map( function( $prefix ) use ( $wpdb ) { | |
return $wpdb->esc_like( $prefix ) . '%'; | |
}, $option_prefixes ) | |
) | |
); | |
$autoload_values = function_exists( 'wp_autoload_values_to_autoload' ) | |
? wp_autoload_values_to_autoload() | |
: array( 'yes' ); | |
$autoloaded_options = array_filter( $options_to_delete, function( $option ) use ( $autoload_values ) { | |
return in_array( $option->autoload, $autoload_values ); | |
} ); | |
$not_autoloaded_options = array_filter( $options_to_delete, function( $option ) use ( $autoload_values ) { | |
return ! in_array( $option->autoload, $autoload_values ); | |
} ); | |
// Delete all the options using the retrieved option names. | |
$wpdb->query( | |
$wpdb->prepare( | |
sprintf( | |
"DELETE FROM $wpdb->options WHERE 1 = 1 AND ( %s );", | |
implode( ' OR ', array_fill( 0, count( $options_to_delete ), 'option_name = %s' ) ) | |
), | |
array_map( function( $option ) { | |
return $option->option_name; | |
}, $options_to_delete ) | |
) | |
); | |
// Flush the alloptions cache if any options were autoloaded. | |
if ( ! empty( $autoloaded_options ) ) { | |
wp_cache_delete( 'alloptions', 'options' ); | |
} | |
// Flush the individual caches for options that were not autoloaded. | |
$not_all_option_names = wp_list_pluck( $not_autoloaded_options, 'option_name' ); | |
wp_cache_delete_multiple( $not_all_option_names, 'options' ); | |
} | |
/** | |
* Delete all site options from a multisite install. | |
*/ | |
function delete_multisite_site_options() { | |
global $wpdb; | |
$option_prefixes = array( | |
'dt_', | |
'_site_transient_dt_', | |
'_site_transient_timeout_dt_', | |
); | |
$options_to_delete = $wpdb->get_results( | |
$wpdb->prepare( | |
sprintf( | |
"SELECT meta_key FROM $wpdb->sitemeta WHERE 'site_id' = %d AND ( %s );", | |
get_current_network_id(), | |
implode( ' OR ', array_fill( 0, count( $option_prefixes ), 'meta_key LIKE %s' ) ) | |
), | |
array_map( function( $prefix ) use ( $wpdb ) { | |
return $wpdb->esc_like( $prefix ) . '%'; | |
}, $option_prefixes ) | |
) | |
); | |
// Delete all the options using the retrieved option names. | |
$wpdb->query( | |
$wpdb->prepare( | |
sprintf( | |
"DELETE FROM $wpdb->sitemeta WHERE 'site_id' = %d AND ( %s );", | |
get_current_network_id(), | |
implode( ' OR ', array_fill( 0, count( $options_to_delete ), 'meta_key = %s' ) ) | |
), | |
array_map( function( $option ) { | |
return $option->meta_key; | |
}, $options_to_delete ) | |
) | |
); | |
// Flush the option caches. | |
$option_names = wp_list_pluck( $options_to_delete, 'meta_key' ); | |
$option_cache_keys = array_map( function( $option_name ) { | |
return get_current_network_id() . ":$option_name"; | |
}, $option_names ); | |
wp_cache_delete_multiple( $option_cache_keys, 'site-options' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment