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