PluginProbe
ElasticPress / 4.6.1
ElasticPress v4.6.1
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.6.1, at uninstall.php

232 lines 5.5 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
85 // Exit if accessed directly.
86 if ( ! defined( 'ABSPATH' ) ) {
87 $this->exit_uninstaller();
88 }
89
90 // EP_MANUAL_SETTINGS_RESET is used by the `settings-reset` WP-CLI command.
91 if ( ! defined( 'EP_MANUAL_SETTINGS_RESET' ) || ! EP_MANUAL_SETTINGS_RESET ) {
92 // Not uninstalling.
93 if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) || ! WP_UNINSTALL_PLUGIN ) {
94 $this->exit_uninstaller();
95 }
96
97 // Not uninstalling this plugin.
98 if ( dirname( WP_UNINSTALL_PLUGIN ) !== dirname( plugin_basename( __FILE__ ) ) ) {
99 $this->exit_uninstaller();
100 }
101 }
102
103 // Uninstall ElasticPress.
104 $this->clean_options_and_transients();
105 $this->remove_elasticpress_capability();
106 }
107
108 /**
109 * Delete all the options in a single site context.
110 */
111 protected function delete_options() {
112 foreach ( $this->options as $option ) {
113 delete_option( $option );
114 }
115 }
116
117 /**
118 * Delete all the transients in a single site context.
119 */
120 protected function delete_transients() {
121 foreach ( $this->transients as $transient ) {
122 delete_transient( $transient );
123 }
124 }
125
126 /**
127 * Delete all transients of the Related Posts feature.
128 */
129 protected function delete_related_posts_transients() {
130 global $wpdb;
131
132 $related_posts_transients = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
133 "SELECT option_name FROM {$wpdb->prefix}options WHERE option_name LIKE '_transient_ep_related_posts_%'"
134 );
135
136 foreach ( $related_posts_transients as $related_posts_transient ) {
137 $related_posts_transient = str_replace( '_transient_', '', $related_posts_transient );
138 delete_site_transient( $related_posts_transient );
139 delete_transient( $related_posts_transient );
140 }
141 }
142
143 /**
144 * Delete all transients of the total fields limit.
145 */
146 protected function delete_total_fields_limit_transients() {
147 global $wpdb;
148
149 $related_posts_transients = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
150 "SELECT option_name FROM {$wpdb->prefix}options WHERE option_name LIKE '_transient_ep_total_fields_limit_%'"
151 );
152
153 foreach ( $related_posts_transients as $related_posts_transient ) {
154 $related_posts_transient = str_replace( '_transient_', '', $related_posts_transient );
155 delete_site_transient( $related_posts_transient );
156 delete_transient( $related_posts_transient );
157 }
158 }
159
160 /**
161 * Cleanup options and transients
162 *
163 * Deletes ElasticPress options and transients.
164 *
165 * @since 4.2.0
166 */
167 protected function clean_options_and_transients() {
168 if ( is_multisite() ) {
169 foreach ( $this->options as $option ) {
170 delete_site_option( $option );
171 }
172 foreach ( $this->transients as $transient ) {
173 delete_site_transient( $transient );
174 }
175
176 $sites = get_sites();
177
178 foreach ( $sites as $site ) {
179 switch_to_blog( $site->blog_id );
180
181 $this->delete_options();
182 $this->delete_transients();
183 $this->delete_related_posts_transients();
184 $this->delete_total_fields_limit_transients();
185
186 restore_current_blog();
187 }
188 } else {
189 $this->delete_options();
190 $this->delete_transients();
191 $this->delete_related_posts_transients();
192 $this->delete_total_fields_limit_transients();
193 }
194 }
195
196 /**
197 * Remove the ElasticPress' capability
198 *
199 * @since 4.5.0
200 */
201 protected function remove_elasticpress_capability() {
202 $role = get_role( 'administrator' );
203 $role->remove_cap( Utils\get_capability() );
204 }
205
206 /**
207 * Cleanup options (deprecated, kept for documenting reasons.)
208 *
209 * @since 1.7
210 * @return void
211 * @see clean_options_and_transients
212 */
213 protected static function clean_options() {
214 _deprecated_function( __FUNCTION__, '4.2.0', '\EP_Uninstaller->clean_options_and_transients()' );
215 }
216
217 /**
218 * Exit uninstaller
219 *
220 * Gracefully exit the uninstaller if we should not be here
221 *
222 * @since 1.7
223 * @return void
224 */
225 protected function exit_uninstaller() {
226 status_header( 404 );
227 exit;
228 }
229 }
230
231 new EP_Uninstaller();
232