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

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

334 lines 8.8 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 // phpcs:disable WordPress.DB.DirectDatabaseQuery
152 $synonyms_example_ids = $wpdb->get_col(
153 $wpdb->prepare(
154 "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s AND post_content = %s LIMIT 100",
155 $synonyms::POST_TYPE_NAME,
156 $synonyms->example_synonym_list()
157 )
158 );
159 // phpcs:enable WordPress.DB.DirectDatabaseQuery
160
161 if ( ! $synonyms_example_ids ) {
162 return;
163 }
164
165 foreach ( $synonyms_example_ids as $synonym_post_id ) {
166 wp_delete_post( $synonym_post_id, true );
167 }
168 }
169
170 /**
171 * Upgrade routine of v4.2.2.
172 *
173 * Delete the transient with ES info, so EP is forced to fetch it again,
174 * determining the correct software type (elasticsearch or opensearch, for example)
175 *
176 * @see https://github.com/10up/ElasticPress/issues/2882
177 */
178 public function upgrade_4_2_2() {
179 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
180 delete_site_transient( 'ep_es_info' );
181 } else {
182 delete_transient( 'ep_es_info' );
183 }
184 }
185
186 /**
187 * Upgrade routine of v4.4.0.
188 *
189 * Delete the ep_prefix option, as that is now obtained via ep_credentials
190 *
191 * @see https://github.com/10up/ElasticPress/issues/2739
192 */
193 public function upgrade_4_4_0() {
194 Utils\delete_option( 'ep_prefix' );
195 }
196
197 /**
198 * Upgrade routine of v4.5.0.
199 *
200 * Add the ElasticPress capability to admins
201 *
202 * @see https://github.com/10up/ElasticPress/pull/3313
203 */
204 public function upgrade_4_5_0() {
205 setup_roles();
206 }
207
208 /**
209 * Adjust the upgrade sync notice to warn users about Instant Results.
210 *
211 * As 4.0.0 introduces this new feature and it requires a resync, admin users
212 * might want to enable the feature before the resync (and then resync only once.)
213 *
214 * @since 4.0.0
215 * @param array $notices All admin notices
216 * @return array
217 */
218 public function resync_notice_4_0_0_instant_results( $notices ) {
219 if ( ! isset( $notices['upgrade_sync'] ) ) {
220 return $notices;
221 }
222
223 $instant_results = \ElasticPress\Features::factory()->get_registered_feature( 'instant-results' );
224 if ( $instant_results->is_active() ) {
225 return $notices;
226 }
227
228 $feature_status = $instant_results->requirements_status();
229 $appended_message = '';
230 if ( 1 >= $feature_status->code ) {
231 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
232 $features_url = admin_url( 'network/admin.php?page=elasticpress' );
233 } else {
234 $features_url = admin_url( 'admin.php?page=elasticpress' );
235 }
236
237 $appended_message = wp_kses_post(
238 sprintf(
239 /* translators: 1: <a> tag (Zendesk article); 2. </a>; 3: <a> tag (link to Features screen); 4. </a>; */
240 __( '%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' ),
241 '<a href="https://elasticpress.zendesk.com/hc/en-us/articles/360050447492#instant-results">',
242 '</a>',
243 '<a href="' . $features_url . '">',
244 '</a>'
245 )
246 );
247 } else {
248 $appended_message = wp_kses_post(
249 sprintf(
250 /* translators: 1: <a> tag (Zendesk article about Instant Results); 2. </a>; 3: <a> tag (Zendesk article about self hosted Elasticsearch setups); 4. </a>; */
251 __( '%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' ),
252 '<a href="https://elasticpress.zendesk.com/hc/en-us/articles/360050447492#instant-results">',
253 '</a>',
254 '<a href="https://elasticpress.zendesk.com/hc/en-us/articles/4413938931853-Considerations-for-self-hosted-Elasticsearch-setups">',
255 '</a>'
256 )
257 );
258 }
259
260 $notices['upgrade_sync']['html'] .= '<br><br>' . $appended_message;
261
262 return $notices;
263 }
264
265 /**
266 * Check if a reindex is needed based on the version number.
267 */
268 public function check_reindex_needed() {
269 if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
270 return;
271 }
272
273 $last_sync = Utils\get_option( 'ep_last_sync', 'never' );
274
275 // No need to upgrade since we've never synced.
276 if ( empty( $last_sync ) || 'never' === $last_sync ) {
277 return;
278 }
279
280 /**
281 * Reindex if we cross a reindex version in the upgrade
282 */
283 $reindex_versions = apply_filters(
284 'ep_reindex_versions',
285 array(
286 '2.2',
287 '2.3.1',
288 '2.4',
289 '2.5.1',
290 '2.6',
291 '2.7',
292 '3.0',
293 '3.1',
294 '3.3',
295 '3.4',
296 '3.6.0',
297 '3.6.1',
298 '4.0.0-beta.1',
299 '4.0.0',
300 )
301 );
302
303 $need_upgrade_sync = false;
304
305 if ( false !== $this->old_version ) {
306 $last_reindex_version = $reindex_versions[ count( $reindex_versions ) - 1 ];
307
308 if ( -1 === version_compare( $this->old_version, $last_reindex_version ) && 0 <= version_compare( EP_VERSION, $last_reindex_version ) ) {
309 $need_upgrade_sync = true;
310 }
311 }
312
313 if ( $need_upgrade_sync ) {
314 Utils\update_option( 'ep_need_upgrade_sync', true );
315 }
316 }
317
318 /**
319 * Return singleton instance of class
320 *
321 * @return self
322 */
323 public static function factory() {
324 static $instance = false;
325
326 if ( ! $instance ) {
327 $instance = new self();
328 $instance->setup();
329 }
330
331 return $instance;
332 }
333 }
334