| @@ -7,8 +7,14 @@ | ||
| 7 | 7 | * @package elasticpress |
| 8 | 8 | * @since 1.7 |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | +use ElasticPress\Utils; | |
| 12 | + | |
| 13 | +defined( 'ABSPATH' ) || exit; | |
| 14 | + | |
| 15 | +require_once __DIR__ . '/includes/utils.php'; | |
| 16 | + | |
| 11 | 17 | /** |
| 12 | 18 | * Class EP_Uninstaller |
| 13 | 19 | */ |
| 14 | 20 | class EP_Uninstaller { |
| @@ -21,8 +27,9 @@ | ||
| 21 | 27 | protected $options = [ |
| 22 | 28 | 'ep_host', |
| 23 | 29 | 'ep_index_meta', |
| 24 | 30 | 'ep_feature_settings', |
| 31 | + 'ep_feature_settings_draft', | |
| 25 | 32 | 'ep_version', |
| 26 | 33 | 'ep_intro_shown', |
| 27 | 34 | 'ep_last_sync', |
| 28 | 35 | 'ep_need_upgrade_sync', |
| @@ -34,10 +41,12 @@ | ||
| 34 | 41 | 'ep_credentials', |
| 35 | 42 | 'ep_prefix', |
| 36 | 43 | 'ep_language', |
| 37 | 44 | 'ep_bulk_setting', |
| 38 | - 'ep_last_index', | |
| 45 | + 'ep_sync_history', | |
| 39 | 46 | |
| 47 | + 'elasticpress_weighting', | |
| 48 | + | |
| 40 | 49 | // Admin notices options |
| 41 | 50 | 'ep_hide_host_error_notice', |
| 42 | 51 | 'ep_hide_es_below_compat_notice', |
| 43 | 52 | 'ep_hide_es_above_compat_notice', |
| @@ -54,15 +63,17 @@ | ||
| 54 | 63 | * |
| 55 | 64 | * @var array |
| 56 | 65 | */ |
| 57 | 66 | protected $transients = [ |
| 67 | + 'ep_autosuggest_query_request_cache', | |
| 68 | + 'ep_elasticpress_io_messages', | |
| 69 | + 'ep_es_info', | |
| 58 | 70 | 'ep_es_info_response_code', |
| 59 | 71 | 'ep_es_info_response_error', |
| 72 | + 'ep_meta_field_keys', | |
| 73 | + 'ep_wpcli_sync', | |
| 74 | + 'ep_wpcli_sync_interrupted', | |
| 60 | 75 | 'logging_ep_es_info', |
| 61 | - 'ep_wpcli_sync_interrupted', | |
| 62 | - 'ep_wpcli_sync', | |
| 63 | - 'ep_es_info', | |
| 64 | - 'ep_autosuggest_query_request_cache', | |
| 65 | 76 | ]; |
| 66 | 77 | |
| 67 | 78 | /** |
| 68 | 79 | * Initialize uninstaller |
| @@ -69,17 +80,20 @@ | ||
| 69 | 80 | * |
| 70 | 81 | * Perform some checks to make sure plugin can/should be uninstalled |
| 71 | 82 | * |
| 72 | 83 | * @since 1.7 |
| 73 | - * @return EP_Uninstaller | |
| 74 | 84 | */ |
| 75 | 85 | public function __construct() { |
| 76 | - | |
| 77 | 86 | // Exit if accessed directly. |
| 78 | 87 | if ( ! defined( 'ABSPATH' ) ) { |
| 79 | 88 | $this->exit_uninstaller(); |
| 80 | 89 | } |
| 81 | 90 | |
| 91 | + // If testing, do not do anything automatically | |
| 92 | + if ( defined( 'EP_UNIT_TESTS' ) && EP_UNIT_TESTS ) { | |
| 93 | + return; | |
| 94 | + } | |
| 95 | + | |
| 82 | 96 | // EP_MANUAL_SETTINGS_RESET is used by the `settings-reset` WP-CLI command. |
| 83 | 97 | if ( ! defined( 'EP_MANUAL_SETTINGS_RESET' ) || ! EP_MANUAL_SETTINGS_RESET ) { |
| 84 | 98 | // Not uninstalling. |
| 85 | 99 | if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) || ! WP_UNINSTALL_PLUGIN ) { |
| @@ -93,8 +107,10 @@ | ||
| 93 | 107 | } |
| 94 | 108 | |
| 95 | 109 | // Uninstall ElasticPress. |
| 96 | 110 | $this->clean_options_and_transients(); |
| 111 | + $this->clean_site_meta(); | |
| 112 | + $this->remove_elasticpress_capability(); | |
| 97 | 113 | } |
| 98 | 114 | |
| 99 | 115 | /** |
| 100 | 116 | * Delete all the options in a single site context. |
| @@ -114,23 +130,46 @@ | ||
| 114 | 130 | } |
| 115 | 131 | } |
| 116 | 132 | |
| 117 | 133 | /** |
| 118 | - * Delete all transients of the Related Posts feature. | |
| 134 | + * Delete remaining transients by their option names. | |
| 135 | + * | |
| 136 | + * @since 4.7.0 | |
| 119 | 137 | */ |
| 120 | - protected function delete_related_posts_transients() { | |
| 138 | + protected function delete_transients_by_option_name() { | |
| 121 | 139 | global $wpdb; |
| 122 | 140 | |
| 123 | - $related_posts_transients = $wpdb->get_col( "SELECT option_name FROM {$wpdb->prefix}options WHERE option_name LIKE '_transient_ep_related_posts_%'" ); | |
| 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 | + ); | |
| 124 | 149 | |
| 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 ); | |
| 150 | + foreach ( $transients as $transient ) { | |
| 151 | + $transient_name = str_replace( '_transient_', '', $transient ); | |
| 152 | + delete_site_transient( $transient_name ); | |
| 153 | + delete_transient( $transient_name ); | |
| 129 | 154 | } |
| 130 | 155 | } |
| 131 | 156 | |
| 132 | 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 | + /** | |
| 133 | 172 | * Cleanup options and transients |
| 134 | 173 | * |
| 135 | 174 | * Deletes ElasticPress options and transients. |
| 136 | 175 | * |
| @@ -144,9 +183,9 @@ | ||
| 144 | 183 | foreach ( $this->transients as $transient ) { |
| 145 | 184 | delete_site_transient( $transient ); |
| 146 | 185 | } |
| 147 | 186 | |
| 148 | - $sites = get_sites(); | |
| 187 | + $sites = \get_sites(); | |
| 149 | 188 | |
| 150 | 189 | foreach ( $sites as $site ) { |
| 151 | 190 | switch_to_blog( $site->blog_id ); |
| 152 | 191 | |
| @@ -151,9 +190,9 @@ | ||
| 151 | 190 | switch_to_blog( $site->blog_id ); |
| 152 | 191 | |
| 153 | 192 | $this->delete_options(); |
| 154 | 193 | $this->delete_transients(); |
| 155 | - $this->delete_related_posts_transients(); | |
| 194 | + $this->delete_transients_by_option_name(); | |
| 156 | 195 | |
| 157 | 196 | restore_current_blog(); |
| 158 | 197 | } |
| 159 | 198 | } else { |
| @@ -158,10 +197,36 @@ | ||
| 158 | 197 | } |
| 159 | 198 | } else { |
| 160 | 199 | $this->delete_options(); |
| 161 | 200 | $this->delete_transients(); |
| 162 | - $this->delete_related_posts_transients(); | |
| 201 | + $this->delete_transients_by_option_name(); | |
| 163 | 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() ); | |
| 164 | 229 | } |
| 165 | 230 | |
| 166 | 231 | /** |
| 167 | 232 | * Cleanup options (deprecated, kept for documenting reasons.) |