PluginProbe
ElasticPress / 4.5.1
ElasticPress v4.5.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.5.1, at uninstall.php

228 lines 5.4 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( "SELECT option_name FROM {$wpdb->prefix}options WHERE option_name LIKE '_transient_ep_related_posts_%'" );
133
134 foreach ( $related_posts_transients as $related_posts_transient ) {
135 $related_posts_transient = str_replace( '_transient_', '', $related_posts_transient );
136 delete_site_transient( $related_posts_transient );
137 delete_transient( $related_posts_transient );
138 }
139 }
140
141 /**
142 * Delete all transients of the total fields limit.
143 */
144 protected function delete_total_fields_limit_transients() {
145 global $wpdb;
146
147 $related_posts_transients = $wpdb->get_col( "SELECT option_name FROM {$wpdb->prefix}options WHERE option_name LIKE '_transient_ep_total_fields_limit_%'" );
148
149 foreach ( $related_posts_transients as $related_posts_transient ) {
150 $related_posts_transient = str_replace( '_transient_', '', $related_posts_transient );
151 delete_site_transient( $related_posts_transient );
152 delete_transient( $related_posts_transient );
153 }
154 }
155
156 /**
157 * Cleanup options and transients
158 *
159 * Deletes ElasticPress options and transients.
160 *
161 * @since 4.2.0
162 */
163 protected function clean_options_and_transients() {
164 if ( is_multisite() ) {
165 foreach ( $this->options as $option ) {
166 delete_site_option( $option );
167 }
168 foreach ( $this->transients as $transient ) {
169 delete_site_transient( $transient );
170 }
171
172 $sites = get_sites();
173
174 foreach ( $sites as $site ) {
175 switch_to_blog( $site->blog_id );
176
177 $this->delete_options();
178 $this->delete_transients();
179 $this->delete_related_posts_transients();
180 $this->delete_total_fields_limit_transients();
181
182 restore_current_blog();
183 }
184 } else {
185 $this->delete_options();
186 $this->delete_transients();
187 $this->delete_related_posts_transients();
188 $this->delete_total_fields_limit_transients();
189 }
190 }
191
192 /**
193 * Remove the ElasticPress' capability
194 *
195 * @since 4.5.0
196 */
197 protected function remove_elasticpress_capability() {
198 $role = get_role( 'administrator' );
199 $role->remove_cap( Utils\get_capability() );
200 }
201
202 /**
203 * Cleanup options (deprecated, kept for documenting reasons.)
204 *
205 * @since 1.7
206 * @return void
207 * @see clean_options_and_transients
208 */
209 protected static function clean_options() {
210 _deprecated_function( __FUNCTION__, '4.2.0', '\EP_Uninstaller->clean_options_and_transients()' );
211 }
212
213 /**
214 * Exit uninstaller
215 *
216 * Gracefully exit the uninstaller if we should not be here
217 *
218 * @since 1.7
219 * @return void
220 */
221 protected function exit_uninstaller() {
222 status_header( 404 );
223 exit;
224 }
225 }
226
227 new EP_Uninstaller();
228