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 / AdminNotices.php

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

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