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 / includes / classes / Upgrades.php

Upgrades.php in ElasticPress 4.5.1, at includes/classes/Upgrades.php

332 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle upgrades.
4 *
5 * @since 3.x
6 * @package elasticpress
7 */
8
9 namespace ElasticPress;
10
11 use ElasticPress\Utils;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly.
15 }
16
17 /**
18 * Class Upgrades
19 *
20 * @package ElasticPress
21 */
22 class Upgrades {
23
24 /**
25 * Store the version number before performing upgrades.
26 * Set in the `setup()` method.
27 *
28 * @var null|string
29 */
30 protected $old_version;
31
32 /**
33 * Initialize class
34 */
35 public function setup() {
36 $this->old_version = Utils\get_option( 'ep_version', false );
37
38 /**
39 * An array with the upgrades routines.
40 * Indexes are the ElasticPress version and values
41 * are an array with the method name and, if needed,
42 * the action name where it should be hooked.
43 */
44 $routines = [
45 '3.5.2' => [ 'upgrade_3_5_2', 'init' ],
46 '3.5.3' => [ 'upgrade_3_5_3', 'init' ],
47 '3.6.6' => [ 'upgrade_3_6_6', 'init' ],
48 '4.2.2' => [ 'upgrade_4_2_2', 'init' ],
49 '4.4.0' => [ 'upgrade_4_4_0', 'init' ],
50 '4.5.0' => [ 'upgrade_4_5_0', 'init' ],
51 ];
52
53 array_walk( $routines, [ $this, 'run_upgrade_routine' ] );
54
55 /**
56 * Check if a reindex is needed.
57 */
58 add_action( 'plugins_loaded', [ $this, 'check_reindex_needed' ], 5 );
59
60 /**
61 * Update the version number.
62 * Note: if a upgrade routine method is hooked to some action,
63 * this code will be executed *earlier* than the routine method.
64 */
65 Utils\update_option( 'ep_version', sanitize_text_field( EP_VERSION ) );
66
67 add_filter( 'ep_admin_notices', [ $this, 'resync_notice_4_0_0_instant_results' ] );
68 }
69
70 /**
71 * Run the upgrade routine, if needed.
72 *
73 * @param array $routine Array with the info about the method to call.
74 * If needed to be run in an action, it'll contain the action name.
75 * @param string $version The version number to be tested.
76 */
77 protected function run_upgrade_routine( $routine, $version ) {
78 if ( version_compare( $this->old_version, $version, '<' ) ) {
79 $function_name = $routine[0];
80 $action_tag = $routine[1];
81 $priority = ( ! empty( $routine[2] ) ) ? $routine[2] : 10;
82 if ( $action_tag ) {
83 add_action( $action_tag, [ $this, $function_name ], $priority );
84 } else {
85 $this->$function_name();
86 }
87 }
88 }
89
90 /**
91 * Upgrade routine of v3.5.2.
92 *
93 * If weighting options exist and the WooCommerce feature is enabled,
94 * this method will enable the SKU field.
95 */
96 public function upgrade_3_5_2() {
97 $weighting_options = get_option( 'elasticpress_weighting', [] );
98 if ( empty( $weighting_options ) ) {
99 return;
100 }
101
102 $woocommerce = Features::factory()->get_registered_feature( 'woocommerce' );
103 if ( ! $woocommerce->is_active() ) {
104 return;
105 }
106
107 if ( empty( $weighting_options['product'] ) ) {
108 return;
109 }
110
111 if ( ! empty( $weighting_options['product']['author_name'] ) ) {
112 unset( $weighting_options['product']['author_name'] );
113 }
114
115 $weighting_options['product']['meta._sku.value'] = array(
116 'enabled' => true,
117 'weight' => 1,
118 );
119
120 update_option( 'elasticpress_weighting', $weighting_options );
121 }
122
123 /**
124 * Upgrade routine of v3.5.3.
125 *
126 * Check if synonyms post has the correct post type, otherwise,
127 * change it to the correct one.
128 */
129 public function upgrade_3_5_3() {
130 delete_option( 'elasticpress_synonyms_post_id' );
131 delete_site_option( 'elasticpress_synonyms_post_id' );
132 }
133
134 /**
135 * Upgrade routine of v3.6.6.
136 *
137 * Delete all synonyms that have the post content identical to the example we set.
138 * In previous versions we had a bug that created several posts with it.
139 *
140 * @see https://github.com/10up/ElasticPress/issues/2516
141 */
142 public function upgrade_3_6_6() {
143 global $wpdb;
144
145 $synonyms = \ElasticPress\Features::factory()->get_registered_feature( 'search' )->synonyms;
146
147 if ( ! $synonyms ) {
148 return;
149 }
150
151 $synonyms_example_ids = $wpdb->get_col(
152 $wpdb->prepare(
153 "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_content = %s LIMIT 100",
154 $synonyms::POST_TYPE_NAME,
155 $synonyms->example_synonym_list()
156 )
157 );
158
159 if ( ! $synonyms_example_ids ) {
160 return;
161 }
162
163 foreach ( $synonyms_example_ids as $synonym_post_id ) {
164 wp_delete_post( $synonym_post_id, true );
165 }
166 }
167
168 /**
169 * Upgrade routine of v4.2.2.
170 *
171 * Delete the transient with ES info, so EP is forced to fetch it again,
172 * determining the correct software type (elasticsearch or opensearch, for example)
173 *
174 * @see https://github.com/10up/ElasticPress/issues/2882
175 */
176 public function upgrade_4_2_2() {
177 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
178 delete_site_transient( 'ep_es_info' );
179 } else {
180 delete_transient( 'ep_es_info' );
181 }
182 }
183
184 /**
185 * Upgrade routine of v4.4.0.
186 *
187 * Delete the ep_prefix option, as that is now obtained via ep_credentials
188 *
189 * @see https://github.com/10up/ElasticPress/issues/2739
190 */
191 public function upgrade_4_4_0() {
192 Utils\delete_option( 'ep_prefix' );
193 }
194
195 /**
196 * Upgrade routine of v4.5.0.
197 *
198 * Add the ElasticPress capability to admins
199 *
200 * @see https://github.com/10up/ElasticPress/pull/3313
201 */
202 public function upgrade_4_5_0() {
203 setup_roles();
204 }
205
206 /**
207 * Adjust the upgrade sync notice to warn users about Instant Results.
208 *
209 * As 4.0.0 introduces this new feature and it requires a resync, admin users
210 * might want to enable the feature before the resync (and then resync only once.)
211 *
212 * @since 4.0.0
213 * @param array $notices All admin notices
214 * @return array
215 */
216 public function resync_notice_4_0_0_instant_results( $notices ) {
217 if ( ! isset( $notices['upgrade_sync'] ) ) {
218 return $notices;
219 }
220
221 $instant_results = \ElasticPress\Features::factory()->get_registered_feature( 'instant-results' );
222 if ( $instant_results->is_active() ) {
223 return $notices;
224 }
225
226 $feature_status = $instant_results->requirements_status();
227 $appended_message = '';
228 if ( 1 >= $feature_status->code ) {
229 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
230 $features_url = admin_url( 'network/admin.php?page=elasticpress' );
231 } else {
232 $features_url = admin_url( 'admin.php?page=elasticpress' );
233 }
234
235 $appended_message = wp_kses_post(
236 sprintf(
237 /* translators: 1: <a> tag (Zendesk article); 2. </a>; 3: <a> tag (link to Features screen); 4. </a>; */
238 __( '%1$sInstant Results%2$s is now available in ElasticPress, but requires a re-sync before activation. If you would like to use Instant Results, click %3$shere%4$s to activate the feature and start your sync.', 'elasticpress' ),
239 '<a href="https://elasticpress.zendesk.com/hc/en-us/articles/360050447492#instant-results">',
240 '</a>',
241 '<a href="' . $features_url . '">',
242 '</a>'
243 )
244 );
245 } else {
246 $appended_message = wp_kses_post(
247 sprintf(
248 /* translators: 1: <a> tag (Zendesk article about Instant Results); 2. </a>; 3: <a> tag (Zendesk article about self hosted Elasticsearch setups); 4. </a>; */
249 __( '%1$sInstant Results%2$s is now available in ElasticPress, but requires a re-sync before activation. If you would like to use Instant Results, since you are not using ElasticPress.io, you will also need to %3$sinstall and configure a PHP proxy%4$s.', 'elasticpress' ),
250 '<a href="https://elasticpress.zendesk.com/hc/en-us/articles/360050447492#instant-results">',
251 '</a>',
252 '<a href="https://elasticpress.zendesk.com/hc/en-us/articles/4413938931853-Considerations-for-self-hosted-Elasticsearch-setups">',
253 '</a>'
254 )
255 );
256 }
257
258 $notices['upgrade_sync']['html'] .= '<br><br>' . $appended_message;
259
260 return $notices;
261 }
262
263 /**
264 * Check if a reindex is needed based on the version number.
265 */
266 public function check_reindex_needed() {
267 if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
268 return;
269 }
270
271 $last_sync = Utils\get_option( 'ep_last_sync', 'never' );
272
273 // No need to upgrade since we've never synced.
274 if ( empty( $last_sync ) || 'never' === $last_sync ) {
275 return;
276 }
277
278 /**
279 * Reindex if we cross a reindex version in the upgrade
280 */
281 $reindex_versions = apply_filters(
282 'ep_reindex_versions',
283 array(
284 '2.2',
285 '2.3.1',
286 '2.4',
287 '2.5.1',
288 '2.6',
289 '2.7',
290 '3.0',
291 '3.1',
292 '3.3',
293 '3.4',
294 '3.6.0',
295 '3.6.1',
296 '4.0.0-beta.1',
297 '4.0.0',
298 )
299 );
300
301 $need_upgrade_sync = false;
302
303 if ( false !== $this->old_version ) {
304 $last_reindex_version = $reindex_versions[ count( $reindex_versions ) - 1 ];
305
306 if ( -1 === version_compare( $this->old_version, $last_reindex_version ) && 0 <= version_compare( EP_VERSION, $last_reindex_version ) ) {
307 $need_upgrade_sync = true;
308 }
309 }
310
311 if ( $need_upgrade_sync ) {
312 Utils\update_option( 'ep_need_upgrade_sync', true );
313 }
314 }
315
316 /**
317 * Return singleton instance of class
318 *
319 * @return self
320 */
321 public static function factory() {
322 static $instance = false;
323
324 if ( ! $instance ) {
325 $instance = new self();
326 $instance->setup();
327 }
328
329 return $instance;
330 }
331 }
332