PluginProbe
ElasticPress / 4.2.0
ElasticPress v4.2.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 / includes / classes / Upgrades.php

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

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