| 1 |
<?php |
| 2 |
/** |
| 3 |
* ElasticPress uninstaller |
| 4 |
* |
| 5 |
* Used when clicking "Delete" from inside of WordPress's plugins page. |
| 6 |
* |
| 7 |
* @package elasticpress |
| 8 |
* @since 1.7 |
| 9 |
*/ |
| 10 |
|
| 11 |
use ElasticPress\Utils; |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
require_once __DIR__ . '/includes/utils.php'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class EP_Uninstaller |
| 19 |
*/ |
| 20 |
class EP_Uninstaller { |
| 21 |
|
| 22 |
/** |
| 23 |
* List of option keys that need to be deleted when uninstalling the plugin. |
| 24 |
* |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
protected $options = [ |
| 28 |
'ep_host', |
| 29 |
'ep_index_meta', |
| 30 |
'ep_feature_settings', |
| 31 |
'ep_feature_settings_draft', |
| 32 |
'ep_version', |
| 33 |
'ep_intro_shown', |
| 34 |
'ep_last_sync', |
| 35 |
'ep_need_upgrade_sync', |
| 36 |
'ep_feature_requirement_statuses', |
| 37 |
'ep_feature_auto_activated_sync', |
| 38 |
'ep_hide_intro_shown_notice', |
| 39 |
'ep_skip_install', |
| 40 |
'ep_last_cli_index', |
| 41 |
'ep_credentials', |
| 42 |
'ep_prefix', |
| 43 |
'ep_language', |
| 44 |
'ep_bulk_setting', |
| 45 |
'ep_sync_history', |
| 46 |
|
| 47 |
'elasticpress_weighting', |
| 48 |
|
| 49 |
// Admin notices options |
| 50 |
'ep_hide_host_error_notice', |
| 51 |
'ep_hide_es_below_compat_notice', |
| 52 |
'ep_hide_es_above_compat_notice', |
| 53 |
'ep_hide_need_setup_notice', |
| 54 |
'ep_hide_no_sync_notice', |
| 55 |
'ep_hide_upgrade_sync_notice', |
| 56 |
'ep_hide_auto_activate_sync_notice', |
| 57 |
'ep_hide_using_autosuggest_defaults_notice', |
| 58 |
'ep_hide_yellow_health_notice', |
| 59 |
]; |
| 60 |
|
| 61 |
/** |
| 62 |
* List of transient keys that need to be deleted when uninstalling the plugin. |
| 63 |
* |
| 64 |
* @var array |
| 65 |
*/ |
| 66 |
protected $transients = [ |
| 67 |
'ep_autosuggest_query_request_cache', |
| 68 |
'ep_elasticpress_io_messages', |
| 69 |
'ep_es_info', |
| 70 |
'ep_es_info_response_code', |
| 71 |
'ep_es_info_response_error', |
| 72 |
'ep_meta_field_keys', |
| 73 |
'ep_wpcli_sync', |
| 74 |
'ep_wpcli_sync_interrupted', |
| 75 |
'logging_ep_es_info', |
| 76 |
]; |
| 77 |
|
| 78 |
/** |
| 79 |
* Initialize uninstaller |
| 80 |
* |
| 81 |
* Perform some checks to make sure plugin can/should be uninstalled |
| 82 |
* |
| 83 |
* @since 1.7 |
| 84 |
*/ |
| 85 |
public function __construct() { |
| 86 |
// Exit if accessed directly. |
| 87 |
if ( ! defined( 'ABSPATH' ) ) { |
| 88 |
$this->exit_uninstaller(); |
| 89 |
} |
| 90 |
|
| 91 |
// If testing, do not do anything automatically |
| 92 |
if ( defined( 'EP_UNIT_TESTS' ) && EP_UNIT_TESTS ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
// EP_MANUAL_SETTINGS_RESET is used by the `settings-reset` WP-CLI command. |
| 97 |
if ( ! defined( 'EP_MANUAL_SETTINGS_RESET' ) || ! EP_MANUAL_SETTINGS_RESET ) { |
| 98 |
// Not uninstalling. |
| 99 |
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) || ! WP_UNINSTALL_PLUGIN ) { |
| 100 |
$this->exit_uninstaller(); |
| 101 |
} |
| 102 |
|
| 103 |
// Not uninstalling this plugin. |
| 104 |
if ( dirname( WP_UNINSTALL_PLUGIN ) !== dirname( plugin_basename( __FILE__ ) ) ) { |
| 105 |
$this->exit_uninstaller(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// Uninstall ElasticPress. |
| 110 |
$this->clean_options_and_transients(); |
| 111 |
$this->clean_site_meta(); |
| 112 |
$this->remove_elasticpress_capability(); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Delete all the options in a single site context. |
| 117 |
*/ |
| 118 |
protected function delete_options() { |
| 119 |
foreach ( $this->options as $option ) { |
| 120 |
delete_option( $option ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Delete all the transients in a single site context. |
| 126 |
*/ |
| 127 |
protected function delete_transients() { |
| 128 |
foreach ( $this->transients as $transient ) { |
| 129 |
delete_transient( $transient ); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Delete remaining transients by their option names. |
| 135 |
* |
| 136 |
* @since 4.7.0 |
| 137 |
*/ |
| 138 |
protected function delete_transients_by_option_name() { |
| 139 |
global $wpdb; |
| 140 |
|
| 141 |
$transients = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery |
| 142 |
"SELECT option_name |
| 143 |
FROM {$wpdb->prefix}options |
| 144 |
WHERE |
| 145 |
option_name LIKE '_transient_ep_index_settings_%' |
| 146 |
OR option_name LIKE '_transient_ep_related_posts_%' |
| 147 |
" |
| 148 |
); |
| 149 |
|
| 150 |
foreach ( $transients as $transient ) { |
| 151 |
$transient_name = str_replace( '_transient_', '', $transient ); |
| 152 |
delete_site_transient( $transient_name ); |
| 153 |
delete_transient( $transient_name ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* DEPRECATED. Delete all transients of the Related Posts feature. |
| 159 |
*/ |
| 160 |
protected function delete_related_posts_transients() { |
| 161 |
_deprecated_function( __METHOD__, '4.7.0', '\EP_Uninstaller::delete_transients_by_name()' ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* DEPRECATED. Delete all transients of the total fields limit. |
| 166 |
*/ |
| 167 |
protected function delete_total_fields_limit_transients() { |
| 168 |
_deprecated_function( __METHOD__, '4.7.0', '\EP_Uninstaller::delete_transients_by_name()' ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Cleanup options and transients |
| 173 |
* |
| 174 |
* Deletes ElasticPress options and transients. |
| 175 |
* |
| 176 |
* @since 4.2.0 |
| 177 |
*/ |
| 178 |
protected function clean_options_and_transients() { |
| 179 |
if ( is_multisite() ) { |
| 180 |
foreach ( $this->options as $option ) { |
| 181 |
delete_site_option( $option ); |
| 182 |
} |
| 183 |
foreach ( $this->transients as $transient ) { |
| 184 |
delete_site_transient( $transient ); |
| 185 |
} |
| 186 |
|
| 187 |
$sites = \get_sites(); |
| 188 |
|
| 189 |
foreach ( $sites as $site ) { |
| 190 |
switch_to_blog( $site->blog_id ); |
| 191 |
|
| 192 |
$this->delete_options(); |
| 193 |
$this->delete_transients(); |
| 194 |
$this->delete_transients_by_option_name(); |
| 195 |
|
| 196 |
restore_current_blog(); |
| 197 |
} |
| 198 |
} else { |
| 199 |
$this->delete_options(); |
| 200 |
$this->delete_transients(); |
| 201 |
$this->delete_transients_by_option_name(); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Delete all site meta |
| 207 |
* |
| 208 |
* @since 4.7.0 |
| 209 |
*/ |
| 210 |
protected function clean_site_meta() { |
| 211 |
if ( ! is_multisite() ) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
$sites = Utils\get_sites(); |
| 216 |
foreach ( $sites as $site ) { |
| 217 |
delete_site_meta( $site['blog_id'], 'ep_indexable' ); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Remove the ElasticPress' capability |
| 223 |
* |
| 224 |
* @since 4.5.0 |
| 225 |
*/ |
| 226 |
protected function remove_elasticpress_capability() { |
| 227 |
$role = get_role( 'administrator' ); |
| 228 |
$role->remove_cap( Utils\get_capability() ); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Cleanup options (deprecated, kept for documenting reasons.) |
| 233 |
* |
| 234 |
* @since 1.7 |
| 235 |
* @return void |
| 236 |
* @see clean_options_and_transients |
| 237 |
*/ |
| 238 |
protected static function clean_options() { |
| 239 |
_deprecated_function( __FUNCTION__, '4.2.0', '\EP_Uninstaller->clean_options_and_transients()' ); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Exit uninstaller |
| 244 |
* |
| 245 |
* Gracefully exit the uninstaller if we should not be here |
| 246 |
* |
| 247 |
* @since 1.7 |
| 248 |
* @return void |
| 249 |
*/ |
| 250 |
protected function exit_uninstaller() { |
| 251 |
status_header( 404 ); |
| 252 |
exit; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
new EP_Uninstaller(); |
| 257 |
|