PluginProbe
ElasticPress / 4.7.0
ElasticPress v4.7.0
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / uninstall.php

uninstall.php in ElasticPress 4.7.0, at uninstall.php

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