| 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 |
/** |
| 12 |
* Class EP_Uninstaller |
| 13 |
*/ |
| 14 |
class EP_Uninstaller { |
| 15 |
|
| 16 |
/** |
| 17 |
* List of option keys that need to be deleted when uninstalling the plugin. |
| 18 |
* |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
protected $options = [ |
| 22 |
'ep_host', |
| 23 |
'ep_index_meta', |
| 24 |
'ep_feature_settings', |
| 25 |
'ep_version', |
| 26 |
'ep_intro_shown', |
| 27 |
'ep_last_sync', |
| 28 |
'ep_need_upgrade_sync', |
| 29 |
'ep_feature_requirement_statuses', |
| 30 |
'ep_feature_auto_activated_sync', |
| 31 |
'ep_hide_intro_shown_notice', |
| 32 |
'ep_skip_install', |
| 33 |
'ep_last_cli_index', |
| 34 |
'ep_credentials', |
| 35 |
'ep_prefix', |
| 36 |
'ep_language', |
| 37 |
'ep_bulk_setting', |
| 38 |
'ep_last_index', |
| 39 |
|
| 40 |
// Admin notices options |
| 41 |
'ep_hide_host_error_notice', |
| 42 |
'ep_hide_es_below_compat_notice', |
| 43 |
'ep_hide_es_above_compat_notice', |
| 44 |
'ep_hide_need_setup_notice', |
| 45 |
'ep_hide_no_sync_notice', |
| 46 |
'ep_hide_upgrade_sync_notice', |
| 47 |
'ep_hide_auto_activate_sync_notice', |
| 48 |
'ep_hide_using_autosuggest_defaults_notice', |
| 49 |
'ep_hide_yellow_health_notice', |
| 50 |
]; |
| 51 |
|
| 52 |
/** |
| 53 |
* List of transient keys that need to be deleted when uninstalling the plugin. |
| 54 |
* |
| 55 |
* @var array |
| 56 |
*/ |
| 57 |
protected $transients = [ |
| 58 |
'ep_es_info_response_code', |
| 59 |
'ep_es_info_response_error', |
| 60 |
'logging_ep_es_info', |
| 61 |
'ep_wpcli_sync_interrupted', |
| 62 |
'ep_wpcli_sync', |
| 63 |
'ep_es_info', |
| 64 |
'ep_autosuggest_query_request_cache', |
| 65 |
]; |
| 66 |
|
| 67 |
/** |
| 68 |
* Initialize uninstaller |
| 69 |
* |
| 70 |
* Perform some checks to make sure plugin can/should be uninstalled |
| 71 |
* |
| 72 |
* @since 1.7 |
| 73 |
* @return EP_Uninstaller |
| 74 |
*/ |
| 75 |
public function __construct() { |
| 76 |
|
| 77 |
// Exit if accessed directly. |
| 78 |
if ( ! defined( 'ABSPATH' ) ) { |
| 79 |
$this->exit_uninstaller(); |
| 80 |
} |
| 81 |
|
| 82 |
// EP_MANUAL_SETTINGS_RESET is used by the `settings-reset` WP-CLI command. |
| 83 |
if ( ! defined( 'EP_MANUAL_SETTINGS_RESET' ) || ! EP_MANUAL_SETTINGS_RESET ) { |
| 84 |
// Not uninstalling. |
| 85 |
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) || ! WP_UNINSTALL_PLUGIN ) { |
| 86 |
$this->exit_uninstaller(); |
| 87 |
} |
| 88 |
|
| 89 |
// Not uninstalling this plugin. |
| 90 |
if ( dirname( WP_UNINSTALL_PLUGIN ) !== dirname( plugin_basename( __FILE__ ) ) ) { |
| 91 |
$this->exit_uninstaller(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// Uninstall ElasticPress. |
| 96 |
$this->clean_options_and_transients(); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Delete all the options in a single site context. |
| 101 |
*/ |
| 102 |
protected function delete_options() { |
| 103 |
foreach ( $this->options as $option ) { |
| 104 |
delete_option( $option ); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Delete all the transients in a single site context. |
| 110 |
*/ |
| 111 |
protected function delete_transients() { |
| 112 |
foreach ( $this->transients as $transient ) { |
| 113 |
delete_transient( $transient ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Delete all transients of the Related Posts feature. |
| 119 |
*/ |
| 120 |
protected function delete_related_posts_transients() { |
| 121 |
global $wpdb; |
| 122 |
|
| 123 |
$related_posts_transients = $wpdb->get_col( "SELECT option_name FROM {$wpdb->prefix}options WHERE option_name LIKE '_transient_ep_related_posts_%'" ); |
| 124 |
|
| 125 |
foreach ( $related_posts_transients as $related_posts_transient ) { |
| 126 |
$related_posts_transient = str_replace( '_transient_', '', $related_posts_transient ); |
| 127 |
delete_site_transient( $related_posts_transient ); |
| 128 |
delete_transient( $related_posts_transient ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Cleanup options and transients |
| 134 |
* |
| 135 |
* Deletes ElasticPress options and transients. |
| 136 |
* |
| 137 |
* @since 4.2.0 |
| 138 |
*/ |
| 139 |
protected function clean_options_and_transients() { |
| 140 |
if ( is_multisite() ) { |
| 141 |
foreach ( $this->options as $option ) { |
| 142 |
delete_site_option( $option ); |
| 143 |
} |
| 144 |
foreach ( $this->transients as $transient ) { |
| 145 |
delete_site_transient( $transient ); |
| 146 |
} |
| 147 |
|
| 148 |
$sites = get_sites(); |
| 149 |
|
| 150 |
foreach ( $sites as $site ) { |
| 151 |
switch_to_blog( $site->blog_id ); |
| 152 |
|
| 153 |
$this->delete_options(); |
| 154 |
$this->delete_transients(); |
| 155 |
$this->delete_related_posts_transients(); |
| 156 |
|
| 157 |
restore_current_blog(); |
| 158 |
} |
| 159 |
} else { |
| 160 |
$this->delete_options(); |
| 161 |
$this->delete_transients(); |
| 162 |
$this->delete_related_posts_transients(); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Cleanup options (deprecated, kept for documenting reasons.) |
| 168 |
* |
| 169 |
* @since 1.7 |
| 170 |
* @return void |
| 171 |
* @see clean_options_and_transients |
| 172 |
*/ |
| 173 |
protected static function clean_options() { |
| 174 |
_deprecated_function( __FUNCTION__, '4.2.0', '\EP_Uninstaller->clean_options_and_transients()' ); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Exit uninstaller |
| 179 |
* |
| 180 |
* Gracefully exit the uninstaller if we should not be here |
| 181 |
* |
| 182 |
* @since 1.7 |
| 183 |
* @return void |
| 184 |
*/ |
| 185 |
protected function exit_uninstaller() { |
| 186 |
status_header( 404 ); |
| 187 |
exit; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
new EP_Uninstaller(); |
| 192 |
|