-
Notifications
You must be signed in to change notification settings - Fork 27
/
uninstall.php
161 lines (129 loc) · 3.15 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Uninstall
*/
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
/**
* Uninstall class
*
* @class wp_ulike_uninstall
* @since 1.0.0
*/
class wp_ulike_uninstall {
/**
* Constructor
*
* @since 1.0.0
* @access public
*/
public function __construct() {
if ( is_multisite() ) {
$this->uninstall_sites();
} else {
$this->uninstall_site();
}
}
/**
* Process uninstall on each sites (multisite)
*
* @since 1.0.0
* @access public
*/
public function uninstall_sites() {
global $wpdb;
// Save current blog ID.
$current = $wpdb->blogid;
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );
// Create tables for each blog ID.
foreach ( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );
$this->uninstall_site();
}
// Go back to current blog.
switch_to_blog( $current );
}
/**
* Process uninstall on current site
*
* @since 1.0.0
* @access public
*/
public function uninstall_site() {
/*
* Only remove ALL data if WP_ULIKE_REMOVE_ALL_DATA constant is set to true in user's
* wp-config.php. This is to prevent data loss when deleting the plugin from the backend
* and to ensure only the site owner can perform this action.
*/
if ( defined( 'WP_ULIKE_REMOVE_ALL_DATA' ) && true === WP_ULIKE_REMOVE_ALL_DATA ) {
$this->drop_tables();
$this->delete_transients();
$this->delete_options();
$this->delete_files();
}
}
/**
* Drop plugin custom tables from current site
*
* @since 1.0.0
* @access public
*/
public function drop_tables() {
global $wpdb;
$wpdb->query(
"DROP TABLE IF EXISTS
{$wpdb->prefix}ulike,
{$wpdb->prefix}ulike_comments,
{$wpdb->prefix}ulike_activities,
{$wpdb->prefix}ulike_forums,
{$wpdb->prefix}ulike_meta"
);
}
/**
* Delete plugin transients from current site
*
* @since 1.0.0
* @access public
*/
public function delete_transients() {
global $wpdb;
// Delete all plugin metadata.
$wpdb->query( "DELETE from $wpdb->options WHERE option_name LIKE '_transient_wp-ulike%'" );
$wpdb->query( "DELETE from $wpdb->options WHERE option_name LIKE '_transient_timeout_wp-ulike%'" );
$wpdb->query( "DELETE from $wpdb->options WHERE option_name LIKE '_transient_wp_ulike%'" );
$wpdb->query( "DELETE from $wpdb->options WHERE option_name LIKE '_transient_timeout_wp_ulike%'" );
}
/**
* Delete plugin options from current site
*
* @since 1.0.0
* @access public
*/
public function delete_options() {
delete_option( 'wp_ulike_dbVersion' );
delete_option( 'widget_wp_ulike' );
delete_option( 'wp_ulike_settings' );
delete_option( 'wp_ulike_use_inline_custom_css' );
delete_option( 'wp_ulike_customize' );
}
/**
* Delete plugin files
*
* @since 1.0.0
* @access public
*/
public function delete_files() {
global $wp_filesystem;
// Get filesystem.
if ( empty( $wp_filesystem ) ) {
if ( ! function_exists( 'WP_Filesystem' ) ) {
require_once ABSPATH . '/wp-admin/includes/file.php';
}
WP_Filesystem();
}
$wp_content = $wp_filesystem->wp_content_dir();
$wp_filesystem->delete( $wp_content . '/uploads/wp-ulike', true );
}
}
new wp_ulike_uninstall();