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

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