-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
uninstall.php
89 lines (81 loc) · 2.34 KB
/
uninstall.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* Translation Stats uninstall file to clean all settings and transient data from the database.
*
* @package Translation_Stats
*
* @since 0.8.0
*/
// Exit if accessed directly.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
die;
}
// Check if it is a multisite uninstall - if so, run the uninstall function for each blog id.
if ( is_multisite() ) {
global $wpdb;
foreach ( $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ) as $translation_stats_blog ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
switch_to_blog( $translation_stats_blog );
translation_stats_uninstall();
}
restore_current_blog();
} else {
translation_stats_uninstall();
}
/**
* Removes ALL plugin data if set in the settings.
*
* @since 0.8.0
*
* @return void
*/
function translation_stats_uninstall() {
$option = get_option( 'tstats_settings' );
// Check if Delete Data on Uninstall is set.
// TODO: turn setting to boolean instead of string 'true'.
if ( ! empty( $option['settings']['delete_data_on_uninstall'] ) && 'true' === $option['settings']['delete_data_on_uninstall'] ) {
if ( is_multisite() ) {
// Delete option in Multisite.
delete_site_option( 'tstats_settings' );
} else {
// Delete option.
delete_option( 'tstats_settings' );
}
// Delete transients.
translation_stats_uninstall_delete_transients( 'translation_stats_plugin_' );
}
}
/**
* Removes ALL transiantes on uninstall.
*
* @since 0.8.0
*
* @param string $search Transient search term.
*
* @return void
*/
function translation_stats_uninstall_delete_transients( $search ) {
global $wpdb;
$transients = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->prepare(
"SELECT option_name AS name FROM $wpdb->options WHERE option_name LIKE %s",
'%_transient_' . $search . '%'
)
);
$transients = array_map(
function ( $transient_object ) {
return $transient_object->name;
},
$transients
);
if ( is_array( $transients ) ) {
foreach ( $transients as $transient ) {
if ( is_multisite() ) {
// Delete transients in Multisite.
delete_site_transient( substr( $transient, strlen( '_transient_' ) ) );
} else {
// Delete transients.
delete_transient( substr( $transient, strlen( '_transient_' ) ) );
}
}
}
}