-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
uninstall.php
90 lines (82 loc) · 2.26 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
90
<?php
/**
* Translation Tools uninstall file to clean all settings and transient data from the database.
*
* @package Translation Tools
*
* @since 1.0.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_tools_blog ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
switch_to_blog( $translation_tools_blog );
translation_tools_uninstall();
}
restore_current_blog();
} else {
translation_tools_uninstall();
}
/**
* Removes ALL plugin data if set in the settings.
*
* @since 1.0.0
*
* @return void
*/
function translation_tools_uninstall() {
$option = get_option( 'translation_tools_settings' );
// Check if Delete Data on Uninstall is set.
if ( empty( $option['delete_data_on_uninstall'] ) ) {
return;
} else {
if ( is_multisite() ) {
// Delete option in Multisite.
delete_site_option( 'translation_tools_settings' );
} else {
// Delete option.
delete_option( 'translation_tools_settings' );
}
// Delete transients.
translation_tools_uninstall_delete_transients( 'translation_tools_' );
}
}
/**
* Removes ALL transiantes on uninstall.
*
* @since 1.0.0
*
* @param string $search Transient search term.
*
* @return void
*/
function translation_tools_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_' ) ) );
}
}
}
}