| 1 |
<?php |
| 2 |
/** |
| 3 |
* Manages the uninstallation of the plugin. |
| 4 |
* |
| 5 |
* @package WP_Stream |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Stream; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class - Uninstall |
| 12 |
*/ |
| 13 |
class Uninstall { |
| 14 |
/** |
| 15 |
* Holds Instance of plugin object |
| 16 |
* |
| 17 |
* @var Plugin |
| 18 |
*/ |
| 19 |
public $plugin; |
| 20 |
|
| 21 |
/** |
| 22 |
* Hold the array of option keys to uninstall |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
public $options; |
| 27 |
|
| 28 |
/** |
| 29 |
* Hold the array of user meta keys to uninstall |
| 30 |
* |
| 31 |
* @var array |
| 32 |
*/ |
| 33 |
public $user_meta; |
| 34 |
|
| 35 |
/** |
| 36 |
* Class constructor |
| 37 |
* |
| 38 |
* @param Plugin $plugin Instance of plugin object. |
| 39 |
*/ |
| 40 |
public function __construct( $plugin ) { |
| 41 |
$this->plugin = $plugin; |
| 42 |
|
| 43 |
$this->user_meta = array( |
| 44 |
'edit_stream_per_page', |
| 45 |
'stream_last_read', // Deprecated. |
| 46 |
'stream_unread_count', // Deprecated. |
| 47 |
'stream_user_feed_key', // Deprecated. |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Uninstall Stream by deleting its data |
| 53 |
*/ |
| 54 |
public function uninstall() { |
| 55 |
$this->options = array( |
| 56 |
$this->plugin->install->option_key, |
| 57 |
$this->plugin->settings->option_key, |
| 58 |
$this->plugin->settings->network_options_key, |
| 59 |
); |
| 60 |
|
| 61 |
// Verify current user's permissions before proceeding. |
| 62 |
if ( ! current_user_can( $this->plugin->admin->settings_cap ) ) { |
| 63 |
wp_die( |
| 64 |
esc_html__( "You don't have sufficient privileges to do this action.", 'stream' ) |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
if ( defined( 'DISALLOW_FILE_MODS' ) && true === DISALLOW_FILE_MODS ) { |
| 69 |
wp_die( |
| 70 |
esc_html__( "You don't have sufficient file permissions to do this action.", 'stream' ) |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
// Prevent this action from firing. |
| 75 |
remove_action( 'deactivate_plugin', array( 'Connector_Installer', 'callback' ), null ); |
| 76 |
|
| 77 |
// Just in case. |
| 78 |
if ( ! function_exists( 'is_plugin_active_for_network' ) ) { |
| 79 |
require_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Drop everything on single site installs or when network activated |
| 84 |
* Otherwise only delete data relative to the current blog. |
| 85 |
*/ |
| 86 |
if ( ! is_multisite() || $this->plugin->is_network_activated() ) { |
| 87 |
$this->delete_all_records(); |
| 88 |
$this->delete_all_options(); |
| 89 |
$this->delete_all_user_meta(); |
| 90 |
} else { |
| 91 |
$blog_id = get_current_blog_id(); |
| 92 |
|
| 93 |
$this->delete_blog_records( $blog_id ); |
| 94 |
$this->delete_blog_options( $blog_id ); |
| 95 |
$this->delete_blog_user_meta( $blog_id ); |
| 96 |
} |
| 97 |
|
| 98 |
$this->delete_all_cron_events(); |
| 99 |
|
| 100 |
$this->deactivate(); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Delete the Stream database tables |
| 105 |
*/ |
| 106 |
private function delete_all_records() { |
| 107 |
global $wpdb; |
| 108 |
|
| 109 |
$wpdb->query( "DROP TABLE {$wpdb->stream}" ); |
| 110 |
$wpdb->query( "DROP TABLE {$wpdb->streammeta}" ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Delete records and record meta from a specific blog |
| 115 |
* |
| 116 |
* @param int $blog_id Blog ID (optional). |
| 117 |
*/ |
| 118 |
private function delete_blog_records( $blog_id = 1 ) { |
| 119 |
if ( empty( $blog_id ) || ! is_int( $blog_id ) ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
global $wpdb; |
| 124 |
|
| 125 |
$wpdb->query( |
| 126 |
$wpdb->prepare( |
| 127 |
"DELETE `records`, `meta` |
| 128 |
FROM {$wpdb->stream} AS `records` |
| 129 |
LEFT JOIN {$wpdb->streammeta} AS `meta` |
| 130 |
ON `meta`.`record_id` = `records`.`ID` |
| 131 |
WHERE blog_id = %d;", |
| 132 |
$blog_id |
| 133 |
) |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Delete all options |
| 139 |
*/ |
| 140 |
private function delete_all_options() { |
| 141 |
global $wpdb; |
| 142 |
|
| 143 |
// Wildcard matches. |
| 144 |
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '%wp_stream%';" ); |
| 145 |
|
| 146 |
// Specific options. |
| 147 |
foreach ( $this->options as $option ) { |
| 148 |
delete_site_option( $option ); // Supports both multisite and single site installs. |
| 149 |
} |
| 150 |
|
| 151 |
// Single site installs can stop here. |
| 152 |
if ( ! is_multisite() ) { |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
// Wildcard matches on network options. |
| 157 |
$wpdb->query( "DELETE FROM {$wpdb->sitemeta} WHERE meta_key LIKE '%wp_stream%';" ); |
| 158 |
|
| 159 |
// Delete options from each blog on network. |
| 160 |
foreach ( wp_stream_get_sites() as $blog ) { |
| 161 |
$this->delete_blog_options( absint( $blog->blog_id ) ); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Delete options from a specific blog |
| 167 |
* |
| 168 |
* @param int $blog_id Blog ID (optional). |
| 169 |
*/ |
| 170 |
private function delete_blog_options( $blog_id = 1 ) { |
| 171 |
if ( empty( $blog_id ) || ! is_int( $blog_id ) ) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
global $wpdb; |
| 176 |
|
| 177 |
// Wildcard matches. |
| 178 |
$wpdb->query( "DELETE FROM {$wpdb->prefix}options WHERE option_name LIKE '%wp_stream%';" ); |
| 179 |
|
| 180 |
// Specific options. |
| 181 |
foreach ( $this->options as $option ) { |
| 182 |
delete_blog_option( $blog_id, $option ); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Delete all user meta |
| 188 |
*/ |
| 189 |
private function delete_all_user_meta() { |
| 190 |
global $wpdb; |
| 191 |
|
| 192 |
// Wildcard matches. |
| 193 |
$wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE '%wp_stream%';" ); |
| 194 |
|
| 195 |
// Specific user meta. |
| 196 |
foreach ( $this->user_meta as $meta_key ) { |
| 197 |
$wpdb->query( |
| 198 |
$wpdb->prepare( "DELETE FROM {$wpdb->usermeta} WHERE meta_key = %s;", $meta_key ) |
| 199 |
); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Delete user meta from a specific blog |
| 205 |
* |
| 206 |
* @param int $blog_id Blog ID (optional). |
| 207 |
*/ |
| 208 |
private function delete_blog_user_meta( $blog_id = 1 ) { |
| 209 |
if ( empty( $blog_id ) || ! is_int( $blog_id ) ) { |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
global $wpdb; |
| 214 |
|
| 215 |
// Wildcard matches. |
| 216 |
$wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE '{$wpdb->prefix}%wp_stream%';" ); |
| 217 |
|
| 218 |
// Specific user meta. |
| 219 |
foreach ( $this->user_meta as $meta_key ) { |
| 220 |
$wpdb->query( |
| 221 |
$wpdb->prepare( "DELETE FROM {$wpdb->usermeta} WHERE meta_key = {$wpdb->prefix}%s;", $meta_key ) |
| 222 |
); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Delete scheduled cron event hooks |
| 228 |
*/ |
| 229 |
private function delete_all_cron_events() { |
| 230 |
wp_clear_scheduled_hook( 'wp_stream_auto_purge' ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Deactivate the plugin and redirect to the plugins screen |
| 235 |
*/ |
| 236 |
private function deactivate() { |
| 237 |
deactivate_plugins( $this->plugin->locations['plugin'] ); |
| 238 |
|
| 239 |
wp_safe_redirect( |
| 240 |
add_query_arg( |
| 241 |
array( |
| 242 |
'deactivate' => true, |
| 243 |
), |
| 244 |
self_admin_url( 'plugins.php' ) |
| 245 |
) |
| 246 |
); |
| 247 |
|
| 248 |
exit; |
| 249 |
} |
| 250 |
} |
| 251 |
|