PluginProbe
ElasticPress / 4.3.1
ElasticPress v4.3.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 / AdminNotices.php

AdminNotices.php in ElasticPress 4.3.1, at includes/classes/AdminNotices.php

831 lines 22.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ElasticPress admin notice handler
4 *
5 * phpcs:disable WordPress.WP.I18n.MissingTranslatorsComment
6 *
7 * @since 3.0
8 * @package elasticpress
9 */
10
11 namespace ElasticPress;
12
13 use ElasticPress\Utils;
14 use ElasticPress\Elasticsearch;
15 use ElasticPress\Screen;
16 use ElasticPress\Features;
17 use ElasticPress\Indexables;
18 use ElasticPress\Stats;
19
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit; // Exit if accessed directly.
22 }
23
24 /**
25 * Admin notices class
26 */
27 class AdminNotices {
28
29 /**
30 * Notice keys
31 *
32 * @since 3.0
33 * @var array
34 */
35 protected $notice_keys = [
36 'host_error',
37 'es_below_compat',
38 'es_above_compat',
39 'different_server_type',
40 'need_setup',
41 'no_sync',
42 'upgrade_sync',
43 'auto_activate_sync',
44 'using_autosuggest_defaults',
45 'maybe_wrong_mapping',
46 'yellow_health',
47 ];
48
49 /**
50 * Notices that should be shown on a screen
51 *
52 * @var array
53 * @since 3.0
54 */
55 protected $notices = [];
56
57 /**
58 * Process all notifications and prepare ones that should be displayed
59 *
60 * @since 3.0
61 */
62 public function process_notices() {
63 $this->notices = [];
64
65 foreach ( $this->notice_keys as $notice ) {
66 $output = call_user_func( [ $this, 'process_' . $notice . '_notice' ] );
67
68 if ( ! empty( $output ) ) {
69 $this->notices[ $notice ] = $output;
70 }
71 }
72 }
73
74 /**
75 * Autosuggest defaults are being used. Feature must be active.
76 *
77 * Type: notice
78 * Dismiss: Everywhere
79 * Show: Settings and dashboard only
80 *
81 * @since 3.0
82 * @return array|bool
83 */
84 protected function process_using_autosuggest_defaults_notice() {
85 $feature = Features::factory()->get_registered_feature( 'autosuggest' );
86 if ( ! $feature instanceof Feature ) {
87 return false;
88 }
89
90 if ( ! $feature->is_active() ) {
91 return false;
92 }
93
94 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
95 $last_sync = get_site_option( 'ep_last_sync', false );
96 } else {
97 $last_sync = get_option( 'ep_last_sync', false );
98 }
99
100 if ( empty( $last_sync ) ) {
101 return false;
102 }
103
104 $host = Utils\get_host();
105
106 if ( empty( $host ) ) {
107 return false;
108 }
109
110 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
111 $dismiss = get_site_option( 'ep_hide_using_autosuggest_defaults_notice', false );
112 } else {
113 $dismiss = get_option( 'ep_hide_using_autosuggest_defaults_notice', false );
114 }
115
116 if ( $dismiss ) {
117 return false;
118 }
119
120 $screen = Screen::factory()->get_current_screen();
121
122 if ( ! in_array( $screen, [ 'dashboard', 'settings' ], true ) ) {
123 return false;
124 }
125
126 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
127 $url = admin_url( 'network/admin.php?page=elasticpress&do_sync' );
128 } else {
129 $url = admin_url( 'admin.php?page=elasticpress&do_sync' );
130 }
131
132 return [
133 'html' => sprintf( esc_html__( 'Autosuggest feature is enabled. If documents feature is enabled, your media will also become searchable in the frontend.', 'elasticpress' ) ),
134 'type' => 'info',
135 'dismiss' => true,
136 ];
137 }
138
139 /**
140 * An EP feature was auto-activated that requires a sync.
141 *
142 * Type: warning
143 * Dismiss: Everywhere except ep dash and settings
144 * Show: All screens except install. Dont show during sync ever
145 *
146 * @since 3.0
147 * @return array|bool
148 */
149 protected function process_auto_activate_sync_notice() {
150 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
151 $need_upgrade_sync = get_site_option( 'ep_need_upgrade_sync', false );
152 } else {
153 $need_upgrade_sync = get_option( 'ep_need_upgrade_sync', false );
154 }
155
156 // need_upgrade_sync takes priority over this notice
157 if ( $need_upgrade_sync ) {
158 return false;
159 }
160
161 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
162 $auto_activate_sync = get_site_option( 'ep_feature_auto_activated_sync', false );
163 } else {
164 $auto_activate_sync = get_option( 'ep_feature_auto_activated_sync', false );
165 }
166
167 if ( ! $auto_activate_sync ) {
168 return false;
169 }
170
171 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
172 $last_sync = get_site_option( 'ep_last_sync', false );
173 } else {
174 $last_sync = get_option( 'ep_last_sync', false );
175 }
176
177 if ( empty( $last_sync ) ) {
178 return false;
179 }
180
181 $host = Utils\get_host();
182
183 if ( empty( $host ) ) {
184 return false;
185 }
186
187 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
188 $dismiss = get_site_option( 'ep_hide_auto_activate_sync_notice', false );
189 } else {
190 $dismiss = get_option( 'ep_hide_auto_activate_sync_notice', false );
191 }
192
193 $screen = Screen::factory()->get_current_screen();
194
195 if ( 'install' === $screen ) {
196 return false;
197 }
198
199 if ( $dismiss && ! in_array( $screen, [ 'dashboard', 'settings' ], true ) ) {
200 return false;
201 }
202
203 if ( isset( $_GET['do_sync'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
204 return false;
205 }
206
207 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
208 $url = admin_url( 'network/admin.php?page=elasticpress-sync&do_sync' );
209 } else {
210 $url = admin_url( 'admin.php?page=elasticpress-sync&do_sync' );
211 }
212
213 $feature = Features::factory()->get_registered_feature( $auto_activate_sync );
214
215 if ( defined( 'EP_DASHBOARD_SYNC' ) && ! EP_DASHBOARD_SYNC ) {
216 $html = sprintf( esc_html__( 'Dashboard sync is disabled. The ElasticPress %s feature has been auto-activated! You will need to reindex using WP-CLI for it to work.', 'elasticpress' ), esc_html( is_object( $feature ) ? $feature->title : '' ) );
217 } else {
218 $html = sprintf( __( 'The ElasticPress %1$s feature has been auto-activated! You will need to <a href="%2$s">run a sync</a> for it to work.', 'elasticpress' ), esc_html( is_object( $feature ) ? $feature->title : '' ), esc_url( $url ) );
219 }
220
221 return [
222 'html' => $html,
223 'type' => 'warning',
224 'dismiss' => ! in_array( $screen, [ 'dashboard', 'settings' ], true ),
225 ];
226 }
227
228 /**
229 * EP was upgraded to or past a version that requires a sync.
230 *
231 * Type: warning
232 * Dismiss: Everywhere except ep dash and settings
233 * Show: All screens except install. Dont show during sync ever
234 *
235 * @since 3.0
236 * @return array|bool
237 */
238 protected function process_upgrade_sync_notice() {
239 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
240 $need_upgrade_sync = get_site_option( 'ep_need_upgrade_sync', false );
241 } else {
242 $need_upgrade_sync = get_option( 'ep_need_upgrade_sync', false );
243 }
244
245 if ( ! $need_upgrade_sync ) {
246 return false;
247 }
248
249 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
250 $last_sync = get_site_option( 'ep_last_sync', false );
251 } else {
252 $last_sync = get_option( 'ep_last_sync', false );
253 }
254
255 if ( empty( $last_sync ) ) {
256 return false;
257 }
258
259 $host = Utils\get_host();
260
261 if ( empty( $host ) ) {
262 return false;
263 }
264
265 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
266 $dismiss = get_site_option( 'ep_hide_upgrade_sync_notice', false );
267 } else {
268 $dismiss = get_option( 'ep_hide_upgrade_sync_notice', false );
269 }
270
271 $screen = Screen::factory()->get_current_screen();
272
273 if ( 'install' === $screen ) {
274 return false;
275 }
276
277 if ( $dismiss && ! in_array( $screen, [ 'dashboard', 'settings' ], true ) ) {
278 return false;
279 }
280
281 if ( isset( $_GET['do_sync'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
282 return false;
283 }
284
285 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
286 $url = admin_url( 'network/admin.php?page=elasticpress-sync&do_sync' );
287 } else {
288 $url = admin_url( 'admin.php?page=elasticpress-sync&do_sync' );
289 }
290
291 if ( defined( 'EP_DASHBOARD_SYNC' ) && ! EP_DASHBOARD_SYNC ) {
292 $html = esc_html__( 'Dashboard sync is disabled. The new version of ElasticPress requires that you delete all data and start a fresh sync using WP-CLI.', 'elasticpress' );
293 } else {
294 $html = sprintf( __( 'The new version of ElasticPress requires that you <a href="%s">delete all data and start a fresh sync</a>.', 'elasticpress' ), esc_url( $url ) );
295 }
296
297 $notice = esc_html__( 'Please note that some ElasticPress functionality may be impaired and/or content may not be searchable until the full sync has been performed.', 'elasticpress' );
298
299 return [
300 'html' => '<span class="dashicons dashicons-warning"></span> ' . $html . ' ' . $notice,
301 'type' => 'error',
302 'dismiss' => ! in_array( $screen, [ 'dashboard', 'settings' ], true ),
303 ];
304 }
305
306 /**
307 * EP has no never had a sync. We assume the user is on step 3 of the install.
308 *
309 * Type: notice
310 * Dismiss: Everywhere except ep dash and settings
311 * Show: All screens except install. Dont show during sync ever
312 *
313 * @since 3.0
314 * @return array|bool
315 */
316 protected function process_no_sync_notice() {
317 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
318 $last_sync = get_site_option( 'ep_last_sync', false );
319 } else {
320 $last_sync = get_option( 'ep_last_sync', false );
321 }
322
323 if ( ! empty( $last_sync ) ) {
324 return false;
325 }
326
327 $host = Utils\get_host();
328
329 if ( empty( $host ) ) {
330 return false;
331 }
332
333 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
334 $dismiss = get_site_option( 'ep_hide_no_sync_notice', false );
335 } else {
336 $dismiss = get_option( 'ep_hide_no_sync_notice', false );
337 }
338
339 $screen = Screen::factory()->get_current_screen();
340
341 if ( 'install' === $screen ) {
342 return false;
343 }
344
345 if ( $dismiss && ! in_array( $screen, [ 'dashboard', 'settings' ], true ) ) {
346 return false;
347 }
348
349 if ( isset( $_GET['do_sync'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
350 return false;
351 }
352
353 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
354 $url = admin_url( 'network/admin.php?page=elasticpress-sync' );
355 } else {
356 $url = admin_url( 'admin.php?page=elasticpress-sync' );
357 }
358
359 if ( defined( 'EP_DASHBOARD_SYNC' ) && ! EP_DASHBOARD_SYNC ) {
360 $html = esc_html__( 'Dashboard sync is disabled, but ElasticPress is almost ready to go. Trigger a sync from WP-CLI.', 'elasticpress' );
361 } else {
362 $html = sprintf( __( 'ElasticPress is almost ready to go. You just need to <a href="%s">sync your content</a>.', 'elasticpress' ), esc_url( $url ) );
363 }
364
365 return [
366 'html' => $html,
367 'type' => 'info',
368 'dismiss' => ! in_array( $screen, [ 'dashboard', 'settings' ], true ),
369 ];
370 }
371
372 /**
373 * EP has no host set. We assume the user needs to run an install.
374 *
375 * Type: notice
376 * Dismiss: Anywhere except EP dashboard
377 * Show: All screens except settings and install
378 *
379 * @since 3.0
380 * @return array|bool
381 */
382 protected function process_need_setup_notice() {
383 $host = Utils\get_host();
384
385 if ( ! empty( $host ) ) {
386 return false;
387 }
388
389 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
390 $dismiss = get_site_option( 'ep_hide_need_setup_notice', false );
391 } else {
392 $dismiss = get_option( 'ep_hide_need_setup_notice', false );
393 }
394
395 $screen = Screen::factory()->get_current_screen();
396
397 if ( in_array( $screen, [ 'settings', 'install' ], true ) ) {
398 return false;
399 }
400
401 if ( $dismiss && 'dashboard' !== $screen ) {
402 return false;
403 }
404
405 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
406 $url = admin_url( 'network/admin.php?page=elasticpress-settings' );
407 } else {
408 $url = admin_url( 'admin.php?page=elasticpress-settings' );
409 }
410
411 return [
412 'html' => sprintf( __( 'ElasticPress is almost ready to go. You just need to <a href="%s">enter your settings</a>.', 'elasticpress' ), esc_url( $url ) ),
413 'type' => 'info',
414 'dismiss' => 'dashboard' !== $screen,
415 ];
416 }
417
418 /**
419 * Below ES compat error.
420 *
421 * Type: error
422 * Dismiss: Anywhere
423 * Show: All screens
424 *
425 * @since 3.0
426 * @return array|bool
427 */
428 protected function process_es_below_compat_notice() {
429 if ( Utils\is_epio() ) {
430 return false;
431 }
432
433 $host = Utils\get_host();
434
435 if ( empty( $host ) ) {
436 return false;
437 }
438
439 $es_version = Elasticsearch::factory()->get_elasticsearch_version();
440
441 if ( false === $es_version ) {
442 return false;
443 }
444
445 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
446 $dismiss = get_site_option( 'ep_hide_es_below_compat_notice', false );
447 } else {
448 $dismiss = get_option( 'ep_hide_es_below_compat_notice', false );
449 }
450
451 if ( $dismiss ) {
452 return false;
453 }
454
455 // First reduce version to major version i.e. 7.10 not 7.10.1.
456 $major_es_version = preg_replace( '#^([0-9]+\.[0-9]+).*#', '$1', $es_version );
457
458 // pad a version to have at least two parts (7 -> 7.0)
459 $parts = explode( '.', $major_es_version );
460
461 if ( 1 === count( $parts ) ) {
462 $parts[] = 0;
463 }
464
465 $major_es_version = implode( '.', $parts );
466
467 if ( 1 === version_compare( EP_ES_VERSION_MIN, $major_es_version ) ) {
468 return [
469 'html' => sprintf( __( 'Your Elasticsearch version %1$s is below the minimum required Elasticsearch version %2$s. ElasticPress may or may not work properly.', 'elasticpress' ), esc_html( $es_version ), esc_html( EP_ES_VERSION_MIN ) ),
470 'type' => 'error',
471 'dismiss' => true,
472 ];
473 }
474
475 return false;
476 }
477
478 /**
479 * Above ES compat error.
480 *
481 * Type: error
482 * Dismiss: Anywhere
483 * Show: All screens
484 *
485 * @since 3.0
486 * @return array|bool
487 */
488 protected function process_es_above_compat_notice() {
489 if ( Utils\is_epio() ) {
490 return false;
491 }
492
493 $host = Utils\get_host();
494
495 if ( empty( $host ) ) {
496 return false;
497 }
498
499 $es_version = Elasticsearch::factory()->get_elasticsearch_version();
500
501 if ( false === $es_version ) {
502 return false;
503 }
504
505 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
506 $dismiss = get_site_option( 'ep_hide_es_above_compat_notice', false );
507 } else {
508 $dismiss = get_option( 'ep_hide_es_above_compat_notice', false );
509 }
510
511 if ( $dismiss ) {
512 return false;
513 }
514
515 // First reduce version to major version i.e. 7.10 not 7.10.1.
516 $major_es_version = preg_replace( '#^([0-9]+\.[0-9]+).*#', '$1', $es_version );
517
518 if ( -1 === version_compare( EP_ES_VERSION_MAX, $major_es_version ) ) {
519 return [
520 'html' => sprintf( __( 'Your Elasticsearch version %1$s is above the maximum required Elasticsearch version %2$s. ElasticPress may or may not work properly.', 'elasticpress' ), esc_html( $es_version ), esc_html( EP_ES_VERSION_MAX ) ),
521 'type' => 'warning',
522 'dismiss' => true,
523 ];
524 }
525 }
526
527 /**
528 * Server software different from Elasticsearch warning.
529 *
530 * Type: warning
531 * Dismiss: Anywhere
532 * Show: All screens
533 *
534 * @since 4.2.1
535 * @return array|bool
536 */
537 protected function process_different_server_type_notice() {
538 if ( Utils\is_epio() ) {
539 return false;
540 }
541
542 $host = Utils\get_host();
543
544 if ( empty( $host ) ) {
545 return false;
546 }
547
548 $server_type = Elasticsearch::factory()->get_server_type();
549
550 if ( false === $server_type || 'elasticsearch' === $server_type ) {
551 return false;
552 }
553
554 $dismiss = Utils\get_option( 'ep_hide_different_server_type_notice', false );
555
556 if ( $dismiss ) {
557 return false;
558 }
559
560 $doc_url = 'https://10up.github.io/ElasticPress/tutorial-compatibility.html';
561 $html = sprintf(
562 /* translator: Document page URL */
563 __( 'Your server software is not supported. To learn more about server compatibility please <a href="%s">visit our documentation</a>.', 'elasticpress' ),
564 esc_url( $doc_url )
565 );
566
567 return [
568 'html' => $html,
569 'type' => 'warning',
570 'dismiss' => true,
571 ];
572 }
573
574 /**
575 * Host error notification. Shows when EP can't reach ES host.
576 *
577 * Type: error
578 * Dismiss: Only on non-EP screens
579 * Show: All screens except install
580 *
581 * @since 3.0
582 * @return array|bool
583 */
584 protected function process_host_error_notice() {
585 $host = Utils\get_host();
586
587 if ( empty( $host ) ) {
588 return false;
589 }
590
591 $screen = Screen::factory()->get_current_screen();
592
593 if ( 'install' === $screen ) {
594 return false;
595 }
596
597 $es_version = Elasticsearch::factory()->get_elasticsearch_version( false );
598
599 if ( false !== $es_version ) {
600 return false;
601 }
602
603 // Only dismissable on non-EP screens
604 if ( ! in_array( $screen, [ 'settings', 'dashboard' ], true ) ) {
605 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
606 $dismiss = get_site_option( 'ep_hide_host_error_notice', false );
607 } else {
608 $dismiss = get_option( 'ep_hide_host_error_notice', false );
609 }
610
611 if ( $dismiss ) {
612 return false;
613 }
614 }
615
616 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
617 $url = admin_url( 'network/admin.php?page=elasticpress-settings' );
618 $response_code = get_site_transient( 'ep_es_info_response_code' );
619 $response_error = get_site_transient( 'ep_es_info_response_error' );
620 } else {
621 $url = admin_url( 'admin.php?page=elasticpress-settings' );
622 $response_code = get_transient( 'ep_es_info_response_code' );
623 $response_error = get_transient( 'ep_es_info_response_error' );
624 }
625
626 $html = sprintf( __( 'There is a problem with connecting to your Elasticsearch host. ElasticPress can <a href="%1$s">try your host again</a>, or you may need to <a href="%2$s">change your settings</a>.', 'elasticpress' ), esc_url( add_query_arg( 'ep-retry', 1 ) ), esc_url( $url ) );
627
628 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
629 if ( ! empty( $response_code ) ) {
630 $html .= '<span class="notice-error-es-response-code"> ' . sprintf( __( 'Response Code: %s', 'elasticpress' ), esc_html( $response_code ) ) . '</span>';
631 }
632
633 if ( ! empty( $response_error ) ) {
634 $html .= '<span class="notice-error-es-response-error"> ' . sprintf( __( 'Response error: %s', 'elasticpress' ), esc_html( $response_error ) ) . '</span>';
635 }
636 }
637
638 return [
639 'html' => $html,
640 'type' => 'error',
641 'dismiss' => ( ! in_array( Screen::factory()->get_current_screen(), [ 'settings', 'dashboard' ], true ) ) ? true : false,
642 ];
643 }
644
645 /**
646 * Determine if the wrong mapping might be installed
647 *
648 * Type: error
649 * Dismiss: Always dismissable per es_version as custom mapping could exist
650 * Show: All screens
651 *
652 * @since 3.6.2
653 * @return array|bool
654 */
655 protected function process_maybe_wrong_mapping_notice() {
656 $screen = Screen::factory()->get_current_screen();
657
658 if ( 'install' === $screen ) {
659 return false;
660 }
661
662 // we might have this dismissed
663 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
664 $dismiss = get_site_option( 'ep_hide_maybe_wrong_mapping_notice', false );
665 } else {
666 $dismiss = get_option( 'ep_hide_maybe_wrong_mapping_notice', false );
667 }
668
669 // we need a host
670 $host = Utils\get_host();
671 if ( empty( $host ) ) {
672 return false;
673 }
674
675 // we also need a version
676 $es_version = Elasticsearch::factory()->get_elasticsearch_version( false );
677
678 if ( false === $es_version || $dismiss === $es_version ) {
679 return false;
680 }
681
682 // we also likely need a sync to have a mapping
683 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
684 $last_sync = get_site_option( 'ep_last_sync', false );
685 } else {
686 $last_sync = get_option( 'ep_last_sync', false );
687 }
688
689 if ( empty( $last_sync ) ) {
690 return false;
691 }
692
693 $post_indexable = Indexables::factory()->get( 'post' );
694
695 $mapping_file_wanted = $post_indexable->get_mapping_name();
696 $mapping_file_current = $post_indexable->determine_mapping_version();
697 if ( is_wp_error( $mapping_file_current ) ) {
698 return false;
699 }
700
701 if ( ! $mapping_file_current || $mapping_file_wanted !== $mapping_file_current ) {
702 $html = sprintf(
703 /* translators: 1. <em>; 2. </em> */
704 esc_html__( 'It seems the mapping data in your index does not match the Elasticsearch version used. We recommend to reindex your content using the sync button on the top of the screen or through wp-cli by adding the %1$s--setup%2$s flag', 'elasticpress' ),
705 '<em>',
706 '</em>'
707 );
708
709 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
710 $html .= '<span class="notice-error-es-response-code"> ' . sprintf( esc_html__( 'Current mapping: %1$s. Expected mapping: %2$s', 'elasticpress' ), esc_html( $mapping_file_current ), esc_html( $mapping_file_wanted ) ) . '</span>';
711 }
712
713 return [
714 'html' => $html,
715 'type' => 'error',
716 'dismiss' => true,
717 ];
718
719 }
720
721 }
722
723 /**
724 * Single node notification. Shows when index health is yellow.
725 *
726 * Type: warning
727 * Dismiss: Anywhere
728 * Show: All screens except install
729 *
730 * @since 3.2
731 * @return array|bool
732 */
733 protected function process_yellow_health_notice() {
734 $host = Utils\get_host();
735
736 if ( empty( $host ) ) {
737 return false;
738 }
739
740 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
741 $last_sync = get_site_option( 'ep_last_sync', false );
742 } else {
743 $last_sync = get_option( 'ep_last_sync', false );
744 }
745
746 if ( empty( $last_sync ) ) {
747 return false;
748 }
749
750 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
751 $dismiss = get_site_option( 'ep_hide_yellow_health_notice', false );
752 } else {
753 $dismiss = get_option( 'ep_hide_yellow_health_notice', false );
754 }
755
756 $screen = Screen::factory()->get_current_screen();
757
758 if ( ! in_array( $screen, [ 'dashboard', 'settings' ], true ) || $dismiss ) {
759 return false;
760 }
761
762 $nodes = Stats::factory()->get_nodes();
763
764 if ( false !== $nodes && $nodes < 2 && $nodes > 0 ) {
765 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
766 $url = network_admin_url( 'admin.php?page=elasticpress-health' );
767 } else {
768 $url = admin_url( 'admin.php?page=elasticpress-health' );
769 }
770
771 return [
772 'html' => sprintf( __( 'It looks like one or more of your indices are running on a single node. While this won\'t prevent you from using ElasticPress, depending on your site\'s specific needs this can represent a performance issue. Please check the <a href="%1$s">Index Health</a> page where you can check the health of all of your indices.', 'elasticpress' ), $url ),
773 'type' => 'warning',
774 'dismiss' => true,
775 ];
776 }
777 }
778
779 /**
780 * Get notices that should be displayed
781 *
782 * @since 3.0
783 * @return array
784 */
785 public function get_notices() {
786 /**
787 * Filter admin notices
788 *
789 * @hook ep_admin_notices
790 * @param {array} $notices Admin notices
791 * @return {array} New notices
792 */
793 return apply_filters( 'ep_admin_notices', $this->notices );
794 }
795
796 /**
797 * Dismiss a notice given a notice key.
798 *
799 * @param string $notice Notice key
800 * @since 3.0
801 */
802 public function dismiss_notice( $notice ) {
803 $value = true;
804 // allow version dependent dismissal
805 if ( in_array( $notice, [ 'maybe_wrong_mapping' ], true ) ) {
806 $value = Elasticsearch::factory()->get_elasticsearch_version( false );
807 }
808 if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
809 update_site_option( 'ep_hide_' . $notice . '_notice', $value );
810 } else {
811 update_option( 'ep_hide_' . $notice . '_notice', $value );
812 }
813 }
814
815 /**
816 * Return singleton instance of class
817 *
818 * @return object
819 * @since 0.1.0
820 */
821 public static function factory() {
822 static $instance = false;
823
824 if ( ! $instance ) {
825 $instance = new self();
826 }
827
828 return $instance;
829 }
830 }
831