-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathdashboard.php
992 lines (852 loc) · 27.6 KB
/
dashboard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
<?php
/**
* Create an ElasticPress dashboard page.
*
* @package elasticpress
* @since 1.9
*/
namespace ElasticPress\Dashboard;
use ElasticPress\AdminNotices;
use ElasticPress\Elasticsearch;
use ElasticPress\Features;
use ElasticPress\Installer;
use ElasticPress\Screen;
use ElasticPress\Stats;
use ElasticPress\Utils;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Setup actions and filters for all things settings
*
* @since 2.1
*/
function setup() {
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { // Must be network admin in multisite.
add_action( 'network_admin_menu', __NAMESPACE__ . '\action_admin_menu' );
add_action( 'admin_bar_menu', __NAMESPACE__ . '\action_network_admin_bar_menu', 50 );
}
add_action( 'admin_menu', __NAMESPACE__ . '\action_admin_menu' );
add_action( 'wp_ajax_ep_save_feature', __NAMESPACE__ . '\action_wp_ajax_ep_save_feature' );
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\action_admin_enqueue_dashboard_scripts' );
add_action( 'admin_init', __NAMESPACE__ . '\maybe_clear_es_info_cache' );
add_action( 'admin_init', __NAMESPACE__ . '\maybe_skip_install' );
add_action( 'wp_ajax_ep_notice_dismiss', __NAMESPACE__ . '\action_wp_ajax_ep_notice_dismiss' );
add_action( 'admin_notices', __NAMESPACE__ . '\maybe_notice' );
add_action( 'network_admin_notices', __NAMESPACE__ . '\maybe_notice' );
add_filter( 'plugin_action_links', __NAMESPACE__ . '\filter_plugin_action_links', 10, 2 );
add_filter( 'network_admin_plugin_action_links', __NAMESPACE__ . '\filter_plugin_action_links', 10, 2 );
add_action( 'ep_add_query_log', __NAMESPACE__ . '\log_version_query_error' );
add_filter( 'ep_analyzer_language', __NAMESPACE__ . '\use_language_in_setting', 10, 2 );
add_filter( 'wp_kses_allowed_html', __NAMESPACE__ . '\filter_allowed_html', 10, 2 );
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\block_assets' );
if ( version_compare( get_bloginfo( 'version' ), '5.8', '>=' ) ) {
add_action( 'block_categories_all', __NAMESPACE__ . '\block_categories' );
} else {
add_action( 'block_categories', __NAMESPACE__ . '\block_categories' );
}
/**
* Filter whether to show 'ElasticPress Indexing' option on Multisite in admin UI or not.
*
* @since 3.6.0
* @hook ep_show_indexing_option_on_multisite
* @param {bool} $show True to show.
* @return {bool} New value
*/
$show_indexing_option_on_multisite = apply_filters( 'ep_show_indexing_option_on_multisite', defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK );
if ( $show_indexing_option_on_multisite ) {
add_filter( 'wpmu_blogs_columns', __NAMESPACE__ . '\filter_blogs_columns', 10, 1 );
add_action( 'manage_sites_custom_column', __NAMESPACE__ . '\add_blogs_column', 10, 2 );
add_action( 'wp_ajax_ep_site_admin', __NAMESPACE__ . '\action_wp_ajax_ep_site_admin' );
}
}
/**
* Add ep-html kses context
*
* @param array $allowedtags HTML tags
* @param string $context Context string
* @since 3.0
* @return array
*/
function filter_allowed_html( $allowedtags, $context ) {
global $allowedposttags;
if ( 'ep-html' === $context ) {
$ep_tags = $allowedposttags;
$atts = [
'type' => true,
'checked' => true,
'selected' => true,
'disabled' => true,
'value' => true,
'href' => true,
'class' => true,
'data-*' => true,
'data-field-name' => true,
'data-ep-notice' => true,
'data-feature' => true,
'id' => true,
'style' => true,
'title' => true,
'name' => true,
'placeholder' => '',
];
$ep_tags['input'] = $atts;
$ep_tags['select'] = $atts;
$ep_tags['textarea'] = $atts;
$ep_tags['option'] = $atts;
$ep_tags['form'] = [
'action' => true,
'accept' => true,
'accept-charset' => true,
'enctype' => true,
'method' => true,
'name' => true,
'target' => true,
];
$ep_tags['a'] = array_merge(
$atts,
[ 'target' => true ]
);
return $ep_tags;
}
return $allowedtags;
}
/**
* Stores the results of the version query.
*
* @param array $query The version query.
* @since 3.0
*/
function log_version_query_error( $query ) {
// Ignore fake requests like the autosuggest template generation
if ( ! empty( $query['request'] ) && is_array( $query['request'] ) && ! empty( $query['request']['is_ep_fake_request'] ) ) {
return;
}
$logging_key = 'logging_ep_es_info';
$logging = Utils\get_transient( $logging_key );
// Are we logging the version query results?
if ( '1' === $logging ) {
/**
* Filter how long results of Elasticsearch version query are stored
*
* @since 23.0
* @hook ep_es_info_cache_expiration
* @param {int} Time in seconds
* @return {int} New time in seconds
*/
$cache_time = apply_filters( 'ep_es_info_cache_expiration', ( 5 * MINUTE_IN_SECONDS ) );
$response_code_key = 'ep_es_info_response_code';
$response_error_key = 'ep_es_info_response_error';
$response_code = 0;
$response_error = '';
if ( ! empty( $query['request'] ) ) {
$response_code = absint( wp_remote_retrieve_response_code( $query['request'] ) );
$response_error = wp_remote_retrieve_response_message( $query['request'] );
if ( empty( $response_error ) && is_wp_error( $query['request'] ) ) {
$response_error = $query['request']->get_error_message();
}
}
// Store the response code, and remove the flag that says
// we're logging the response code so we don't log additional
// queries.
Utils\set_transient( $response_code_key, $response_code, $cache_time );
Utils\set_transient( $response_error_key, $response_error, $cache_time );
Utils\delete_transient( $logging_key );
}
}
/**
* Allow user to skip install process.
*
* @since 3.5
*/
function maybe_skip_install() {
if ( ! is_admin() && ! is_network_admin() ) {
return;
}
if ( empty( $_GET['ep-skip-install'] ) || empty( $_GET['nonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['nonce'] ), 'ep-skip-install' ) || ! in_array( Screen::factory()->get_current_screen(), [ 'install' ], true ) ) {
return;
}
if ( ! empty( $_GET['ep-skip-features'] ) ) {
$features = \ElasticPress\Features::factory()->registered_features;
foreach ( $features as $slug => $feature ) {
\ElasticPress\Features::factory()->deactivate_feature( $slug );
}
}
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
$redirect_url = network_admin_url( 'admin.php?page=elasticpress' );
} else {
$redirect_url = admin_url( 'admin.php?page=elasticpress' );
}
Utils\update_option( 'ep_skip_install', true );
wp_safe_redirect( $redirect_url );
exit;
}
/**
* Clear ES info cache whenever EP dash or settings page is viewed. Also clear cache
* when "try again" notification link is clicked.
*
* @since 2.3.1
*/
function maybe_clear_es_info_cache() {
if ( ! is_admin() && ! is_network_admin() ) {
return;
}
$isset_retry = ! empty( $_GET['ep-retry'] ) &&
! empty( $_GET['ep_retry_nonce'] ) &&
wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['ep_retry_nonce'] ) ), 'ep_retry_nonce' );
if ( ! $isset_retry && ! in_array( Screen::factory()->get_current_screen(), [ 'dashboard', 'settings', 'install' ], true ) ) {
return;
}
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
delete_site_transient( 'ep_es_info' );
} else {
delete_transient( 'ep_es_info' );
}
if ( $isset_retry ) {
wp_safe_redirect( remove_query_arg( [ 'ep-retry', 'ep_retry_nonce' ] ) );
exit();
}
}
/**
* Show ElasticPress in network admin menu bar
*
* @param object $admin_bar WP_Admin Bar reference.
* @since 2.2
*/
function action_network_admin_bar_menu( $admin_bar ) {
$admin_bar->add_menu(
array(
'id' => 'network-admin-elasticpress',
'parent' => 'network-admin',
'title' => 'ElasticPress',
'href' => esc_url( network_admin_url( 'admin.php?page=elasticpress' ) ),
)
);
}
/**
* Output dashboard link in plugin actions
*
* @param array $plugin_actions Array of HTML.
* @param string $plugin_file Path to plugin file.
* @since 2.1
* @return array
*/
function filter_plugin_action_links( $plugin_actions, $plugin_file ) {
if ( is_network_admin() ) {
$url = admin_url( 'network/admin.php?page=elasticpress' );
if ( ! defined( 'EP_IS_NETWORK' ) || ! EP_IS_NETWORK ) {
return $plugin_actions;
}
} else {
$url = admin_url( 'admin.php?page=elasticpress' );
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
return $plugin_actions;
}
}
$new_actions = [];
if ( basename( EP_PATH ) . '/elasticpress.php' === $plugin_file ) {
$new_actions['ep_dashboard'] = sprintf( '<a href="%s">%s</a>', esc_url( $url ), __( 'Dashboard', 'elasticpress' ) );
}
return array_merge( $new_actions, $plugin_actions );
}
/**
* Output variety of dashboard notices.
*
* @param bool $force Force ES info hard lookup.
* @since 3.0
*/
function maybe_notice( $force = false ) {
if ( ! current_user_can( Utils\get_capability() ) ) {
return false;
}
/**
* Filter how long results of Elasticsearch version query are stored
*
* @since 23.0
* @hook ep_es_info_cache_expiration
* @param {int} Time in seconds
* @return {int} New time in seconds
*/
$cache_time = apply_filters( 'ep_es_info_cache_expiration', ( 5 * MINUTE_IN_SECONDS ) );
Utils\set_transient(
'logging_ep_es_info',
'1',
$cache_time
);
// Fetch ES version
Elasticsearch::factory()->get_elasticsearch_version( $force );
AdminNotices::factory()->process_notices();
$notices = AdminNotices::factory()->get_notices();
foreach ( $notices as $notice_key => $notice ) {
?>
<div data-ep-notice="<?php echo esc_attr( $notice_key ); ?>" class="notice notice-<?php echo esc_attr( $notice['type'] ); ?> <?php
if ( $notice['dismiss'] ) :
?>
is-dismissible<?php endif; ?>">
<p>
<?php echo wp_kses( $notice['html'], 'ep-html' ); ?>
</p>
</div>
<?php
}
wp_enqueue_script( 'ep_notice_script' );
return $notices;
}
/**
* Dismiss notice via ajax
*
* @since 2.2
*/
function action_wp_ajax_ep_notice_dismiss() {
if ( empty( $_POST['notice'] ) || ! check_ajax_referer( 'ep_admin_nonce', 'nonce', false ) ) {
wp_send_json_error();
exit;
}
if ( ! current_user_can( Utils\get_capability() ) ) {
wp_send_json_error();
exit;
}
AdminNotices::factory()->dismiss_notice( sanitize_key( $_POST['notice'] ) );
wp_send_json_success();
}
/**
* Getting the status of ongoing index fired by WP CLI
*
* @since 2.1
*/
function action_wp_ajax_ep_cli_index() {
_deprecated_function( __CLASS__, '3.6.0', '\ElasticPress\Screen::factory()->sync_screen->action_wp_ajax_ep_cli_index()' );
}
/**
* Continue index
*
* @since 2.1
*/
function action_wp_ajax_ep_index() {
_deprecated_function( __CLASS__, '3.6.0', '\ElasticPress\Screen::factory()->sync_screen->action_wp_ajax_ep_index()' );
}
/**
* Cancel index
*
* @since 2.1
*/
function action_wp_ajax_ep_cancel_index() {
_deprecated_function( __CLASS__, '3.6.0', '\ElasticPress\Screen::factory()->sync_screen->action_wp_ajax_ep_cancel_index()' );
}
/**
* Save individual feature settings
*
* @since 2.2
*/
function action_wp_ajax_ep_save_feature() {
$post = wp_unslash( $_POST );
if ( empty( $post['feature'] ) || empty( $post['settings'] ) || ! check_ajax_referer( 'ep_dashboard_nonce', 'nonce', false ) ) {
wp_send_json_error();
exit;
}
if ( Utils\is_indexing() ) {
$error = new \WP_Error( 'is_indexing' );
wp_send_json_error( $error );
exit;
}
$data = Features::factory()->update_feature( $post['feature'], $post['settings'] );
// Since we deactivated, delete auto activate notice.
if ( empty( $post['settings']['active'] ) ) {
Utils\delete_option( 'ep_feature_auto_activated_sync' );
}
wp_send_json_success( $data );
}
/**
* Register and Enqueue JavaScripts for dashboard
*
* @since 2.2
*/
function action_admin_enqueue_dashboard_scripts() {
if ( isset( get_current_screen()->id ) && strpos( get_current_screen()->id, 'sites-network' ) !== false ) {
wp_enqueue_style( 'wp-components' );
wp_enqueue_script(
'ep_admin_sites_scripts',
EP_URL . 'dist/js/sites-admin-script.js',
Utils\get_asset_info( 'sites-admin-script', 'dependencies' ),
Utils\get_asset_info( 'sites-admin-script', 'version' ),
true
);
wp_set_script_translations( 'ep_admin_sites_scripts', 'elasticpress' );
$data = [
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'epsa' ),
];
wp_localize_script( 'ep_admin_sites_scripts', 'epsa', $data );
}
if ( in_array( Screen::factory()->get_current_screen(), [ 'dashboard', 'settings', 'install', 'health', 'weighting', 'synonyms', 'sync', 'status-report' ], true ) ) {
wp_enqueue_style(
'ep_admin_styles',
EP_URL . 'dist/css/dashboard-styles.css',
Utils\get_asset_info( 'dashboard-styles', 'dependencies' ),
Utils\get_asset_info( 'dashboard-styles', 'version' )
);
wp_enqueue_script(
'ep_admin_script',
EP_URL . 'dist/js/admin-script.js',
Utils\get_asset_info( 'admin-script', 'dependencies' ),
Utils\get_asset_info( 'admin-script', 'version' ),
true
);
wp_set_script_translations( 'ep_admin_script', 'elasticpress' );
}
if ( 'weighting' === Screen::factory()->get_current_screen() ) {
wp_enqueue_style(
'ep_weighting_styles',
EP_URL . 'dist/css/weighting-script.css',
[ 'wp-components', 'wp-edit-post' ],
Utils\get_asset_info( 'weighting-script', 'version' )
);
wp_enqueue_script(
'ep_weighting_script',
EP_URL . 'dist/js/weighting-script.js',
Utils\get_asset_info( 'weighting-script', 'dependencies' ),
Utils\get_asset_info( 'weighting-script', 'version' ),
true
);
$weighting = Features::factory()->get_registered_feature( 'search' )->weighting;
$api_url = esc_url_raw( rest_url( 'elasticpress/v1/weighting' ) );
$meta_mode = $weighting->get_meta_mode();
$weightable_fields = $weighting->get_weightable_fields();
$weighting_configuration = $weighting->get_weighting_configuration_with_defaults();
/**
* Filter weighting dashboard options.
*
* @hook ep_weighting_options
* @param {array} $data Weighting dashboard options
* @return {array} New options array
* @since 5.1.0
*/
$data = apply_filters(
'ep_weighting_options',
[
'apiUrl' => $api_url,
'metaMode' => $meta_mode,
'weightableFields' => $weightable_fields,
'weightingConfiguration' => $weighting_configuration,
]
);
wp_localize_script(
'ep_weighting_script',
'epWeighting',
$data
);
wp_set_script_translations( 'ep_weighting_script', 'elasticpress' );
}
if ( in_array( Screen::factory()->get_current_screen(), [ 'dashboard', 'install' ], true ) ) {
wp_enqueue_script(
'ep_dashboard_scripts',
EP_URL . 'dist/js/dashboard-script.js',
Utils\get_asset_info( 'dashboard-script', 'dependencies' ),
Utils\get_asset_info( 'dashboard-script', 'version' ),
true
);
wp_set_script_translations( 'ep_dashboard_scripts', 'elasticpress' );
$sync_url = Utils\get_sync_url( true );
$skip_url = ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) ?
network_admin_url( 'admin.php?page=elasticpress' ) :
admin_url( 'admin.php?page=elasticpress' );
$data = array(
'skipUrl' => add_query_arg(
array(
'ep-skip-install' => 1,
'ep-skip-features' => 1,
'nonce' => wp_create_nonce( 'ep-skip-install' ),
),
$skip_url
),
'syncUrl' => $sync_url,
);
wp_localize_script( 'ep_dashboard_scripts', 'epDash', $data );
}
if ( in_array( Screen::factory()->get_current_screen(), [ 'health' ], true ) && ! empty( Utils\get_host() ) ) {
Stats::factory()->build_stats();
$data = Stats::factory()->get_localized();
wp_enqueue_script(
'ep_stats',
EP_URL . 'dist/js/stats-script.js',
Utils\get_asset_info( 'stats-script', 'dependencies' ),
Utils\get_asset_info( 'stats-script', 'version' ),
true
);
wp_set_script_translations( 'ep_stats', 'elasticpress' );
wp_localize_script( 'ep_stats', 'epChartData', $data );
}
wp_register_script(
'ep_notice_script',
EP_URL . 'dist/js/notice-script.js',
Utils\get_asset_info( 'notice-script', 'dependencies' ),
Utils\get_asset_info( 'notice-script', 'version' ),
true
);
wp_set_script_translations( 'ep_notice_script', 'elasticpress' );
wp_localize_script(
'ep_notice_script',
'epAdmin',
array(
'nonce' => wp_create_nonce( 'ep_admin_nonce' ),
)
);
}
/**
* Output current ElasticPress dashboard screen
*
* @since 3.0
*/
function resolve_screen() {
Screen::factory()->output();
}
/**
* Admin menu actions
*
* Adds options page to admin menu.
*
* @since 1.9
* @return void
*/
function action_admin_menu() {
Installer::factory()->calculate_install_status();
if ( true !== Installer::factory()->get_install_status() && ! Utils\is_top_level_admin_context() ) {
return;
}
if ( ! Utils\is_site_indexable() && ! is_network_admin() ) {
return;
}
$capability = ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) ? Utils\get_network_capability() : Utils\get_capability();
add_menu_page(
'ElasticPress',
'ElasticPress',
$capability,
'elasticpress',
__NAMESPACE__ . '\resolve_screen',
'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgNzMgNzEuMyIgc3R5bGU9ImVuYWJsZS1iYWNrZ3JvdW5kOm5ldyAwIDAgNzMgNzEuMzsiIHhtbDpzcGFjZT0icHJlc2VydmUiPjxwYXRoIGQ9Ik0zNi41LDQuN0MxOS40LDQuNyw1LjYsMTguNiw1LjYsMzUuN2MwLDEwLDQuNywxOC45LDEyLjEsMjQuNWw0LjUtNC41YzAuMS0wLjEsMC4xLTAuMiwwLjItMC4zbDAuNy0wLjdsNi40LTYuNGMyLjEsMS4yLDQuNSwxLjksNy4xLDEuOWM4LDAsMTQuNS02LjUsMTQuNS0xNC41cy02LjUtMTQuNS0xNC41LTE0LjVTMjIsMjcuNiwyMiwzNS42YzAsMi44LDAuOCw1LjMsMi4xLDcuNWwtNi40LDYuNGMtMi45LTMuOS00LjYtOC43LTQuNi0xMy45YzAtMTIuOSwxMC41LTIzLjQsMjMuNC0yMy40czIzLjQsMTAuNSwyMy40LDIzLjRTNDkuNCw1OSwzNi41LDU5Yy0yLjEsMC00LjEtMC4zLTYtMC44bC0wLjYsMC42bC01LjIsNS40YzMuNiwxLjUsNy42LDIuMywxMS44LDIuM2MxNy4xLDAsMzAuOS0xMy45LDMwLjktMzAuOVM1My42LDQuNywzNi41LDQuN3oiLz48L3N2Zz4='
);
if ( ! Utils\is_top_level_admin_context() ) {
return;
}
add_submenu_page(
'elasticpress',
esc_html__( 'ElasticPress Features', 'elasticpress' ),
esc_html__( 'Features', 'elasticpress' ),
$capability,
'elasticpress',
__NAMESPACE__ . '\resolve_screen'
);
add_submenu_page(
'elasticpress',
esc_html__( 'ElasticPress Settings', 'elasticpress' ),
esc_html__( 'Settings', 'elasticpress' ),
$capability,
'elasticpress-settings',
__NAMESPACE__ . '\resolve_screen'
);
add_submenu_page(
'elasticpress',
'ElasticPress ' . esc_html__( 'Sync', 'elasticpress' ),
esc_html__( 'Sync', 'elasticpress' ),
$capability,
'elasticpress-sync',
__NAMESPACE__ . '\resolve_screen'
);
add_submenu_page(
'elasticpress',
esc_html__( 'ElasticPress Index Health', 'elasticpress' ),
esc_html__( 'Index Health', 'elasticpress' ),
$capability,
'elasticpress-health',
__NAMESPACE__ . '\resolve_screen'
);
add_submenu_page(
'elasticpress',
esc_html__( 'ElasticPress Status Report', 'elasticpress' ),
esc_html__( 'Status Report', 'elasticpress' ),
$capability,
'elasticpress-status-report',
__NAMESPACE__ . '\resolve_screen'
);
}
/**
* Languages supported in Elasticsearch mappings.
*
* If $format is 'elasticsearch', the array format is `Elasticsearch analyzer name => [ WordPress language package names ]`.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-lang-analyzer.html
* @since 4.7.0
* @param string $format Format of the return ('locales' or 'elasticsearch' )
* @return array
*/
function get_available_languages( string $format = 'elasticsearch' ): array {
/**
* Filter available languages in Elasticsearch.
*
* The returned array should follow the format `Elasticsearch analyzer name => [ WordPress language package names ]`.
*
* @since 4.7.0
* @hook ep_available_languages
* @param {bool} $available_languages List of available languages
* @return {bool} New list
*/
$es_languages = apply_filters(
'ep_available_languages',
[
'arabic' => [ 'ar', 'ary' ],
'armenian' => [ 'hy' ],
'basque' => [ 'eu' ],
'bengali' => [ 'bn', 'bn_BD' ],
'brazilian' => [ 'pt_BR' ],
'bulgarian' => [ 'bg', 'bg_BG' ],
'catalan' => [ 'ca' ],
'cjk' => [], // CJK characters (not a language)
'czech' => [ 'cs', 'cs_CZ' ],
'danish' => [ 'da', 'da_DK' ],
'dutch' => [ 'nl_NL_formal', 'nl_NL', 'nl_BE' ],
'english' => [ 'en', 'en_AU', 'en_GB', 'en_NZ', 'en_CA', 'en_US', 'en_ZA' ],
'estonian' => [ 'et' ],
'finnish' => [ 'fi' ],
'french' => [ 'fr', 'fr_CA', 'fr_FR', 'fr_BE' ],
'galician' => [ 'gl_ES' ],
'german' => [ 'de', 'de_DE', 'de_DE_formal', 'de_CH', 'de_CH_informal', 'de_AT' ],
'greek' => [ 'el' ],
'hindi' => [ 'hi_IN' ],
'hungarian' => [ 'hu_HU' ],
'indonesian' => [ 'id_ID' ],
'irish' => [], // WordPress doesn't support Irish as an active locale currently
'italian' => [ 'it_IT' ],
'latvian' => [ 'lv' ],
'lithuanian' => [ 'lt_LT' ],
'norwegian' => [ 'nb_NO' ],
'persian' => [ 'fa_IR' ],
'portuguese' => [ 'pt', 'pt_AO', 'pt_PT', 'pt_PT_ao90' ],
'romanian' => [ 'ro_RO' ],
'russian' => [ 'ru_RU' ],
'sorani' => [ 'ckb' ],
'spanish' => [ 'es_CR', 'es_MX', 'es_VE', 'es_AR', 'es_CL', 'es_GT', 'es_PE', 'es_ES', 'es_UY', 'es_CO' ],
'swedish' => [ 'sv_SE' ],
'turkish' => [ 'tr_TR' ],
'thai' => [ 'th' ],
]
);
if ( 'locales' === $format ) {
$arr = array_reduce(
$es_languages,
function ( $acc, $lang ) {
$lang = array_filter(
$lang,
function ( $locale ) {
// English is always added. This removes the duplicates
return ! in_array( $locale, [ 'en', 'en_US' ], true );
}
);
$acc = array_merge( $acc, $lang );
return $acc;
},
[]
);
return $arr;
}
return $es_languages;
}
/**
* Uses the language from EP settings in mapping.
*
* @param string $language The current language.
* @param string $context The context where the function is running.
* @return string The updated language.
*/
function use_language_in_setting( $language = 'english', $context = '' ) {
global $locale, $wp_local_package;
// Get the currently set language.
$ep_language = Utils\get_language();
// Bail early if no EP language is set.
if ( empty( $ep_language ) ) {
return $language;
}
/**
* WordPress does not reset the language when switch_blog() is called.
*
* @see https://core.trac.wordpress.org/ticket/49263
*/
if ( 'site-default' === $ep_language ) {
$locale = null;
$wp_local_package = null;
$ep_language = get_locale();
}
require_once ABSPATH . 'wp-admin/includes/translation-install.php';
$translations = wp_get_available_translations();
// Default to en_US if not in the array of available translations.
if ( ! empty( $translations[ $ep_language ]['english_name'] ) ) {
$wp_language = $translations[ $ep_language ]['language'];
} else {
$wp_language = 'en_US';
}
$es_languages = get_available_languages();
/**
* Languages supported in Elasticsearch snowball token filters.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-snowball-tokenfilter.html
*/
$es_snowball_languages = [
'Armenian',
'Basque',
'Catalan',
'Danish',
'Dutch',
'English',
'Finnish',
'French',
'German',
'German2', // currently unused
'Hungarian',
'Italian',
'Kp', // currently unused
'Lithuanian',
'Lovins', // currently unused
'Norwegian',
'Porter', // currently unused
'Portuguese',
'Romanian',
'Russian',
'Spanish',
'Swedish',
'Turkish',
];
$es_snowball_similar = [
'Brazilian' => 'Portuguese',
];
foreach ( $es_languages as $analyzer_name => $analyzer_language_codes ) {
if ( in_array( $wp_language, $analyzer_language_codes, true ) ) {
$language = $analyzer_name;
break;
}
}
if ( 'filter_ewp_snowball' === $context ) {
$uc_first_language = ucfirst( $language );
if ( in_array( $uc_first_language, $es_snowball_languages, true ) ) {
return $uc_first_language;
}
return $es_snowball_similar[ $uc_first_language ] ?? 'English';
}
if ( 'filter_ep_stop' === $context ) {
return "_{$language}_";
}
return $language;
}
/**
* Add column to sites admin table.
*
* @param string[] $columns Array of columns.
*
* @return string[]
*/
function filter_blogs_columns( $columns ) {
$columns['elasticpress'] = esc_html__( 'ElasticPress Indexing', 'elasticpress' );
return $columns;
}
/**
* Populate column with checkbox/switch.
*
* @param string $column_name The name of the current column.
* @param int $blog_id The blog ID.
*
* @return void | string
*/
function add_blogs_column( $column_name, $blog_id ) {
if ( 'elasticpress' !== $column_name ) {
return;
}
$site = get_site( $blog_id );
if ( $site->deleted || $site->archived || $site->spam ) {
return;
}
$is_indexable = get_site_meta( $blog_id, 'ep_indexable', true );
$is_indexable = '' !== $is_indexable ? $is_indexable : 'yes';
printf(
'<input %1$s class="index-toggle" data-blog-id="%2$s" disabled type="checkbox">',
checked( $is_indexable, 'yes', false ),
esc_attr( $blog_id )
);
}
/**
* AJAX callback to update ep_indexable site option.
*/
function action_wp_ajax_ep_site_admin() {
$blog_id = ( ! empty( $_POST['blog_id'] ) ) ? absint( wp_unslash( $_POST['blog_id'] ) ) : - 1;
$checked = ( ! empty( $_POST['checked'] ) ) ? sanitize_text_field( wp_unslash( $_POST['checked'] ) ) : 'no';
if ( - 1 === $blog_id || ! check_ajax_referer( 'epsa', 'nonce', false ) ) {
return wp_send_json_error();
}
/**
* NOTE: This will be removed in ElasticPress 5.0.0. Implementations should rely on site_meta since 4.7.0.
*/
$result = update_blog_option( $blog_id, 'ep_indexable', $checked );
$result = update_site_meta( $blog_id, 'ep_indexable', $checked );
$data = [
'blog_id' => $blog_id,
'result' => $result,
];
return wp_send_json_success( $data );
}
/**
* Handle the fetch for indexing status
*/
function handle_indexing_status() {
$indexing_status = \ElasticPress\Utils\get_indexing_status();
$status = array(
'method' => '',
'items_indexed' => 0,
'total_items' => 0,
'indexable' => '',
);
if ( ! empty( $indexing_status ) ) {
if ( isset( $indexing_status['method'] ) && 'cli' === $indexing_status['method'] ) {
$status['method'] = $indexing_status['method'];
$status['items_indexed'] = $indexing_status['items_indexed'];
$status['total_items'] = $indexing_status['total_items'];
$status['indexable'] = $indexing_status['slug'];
} else {
$status['method'] = 'dashboard';
$status['items_indexed'] = isset( $indexing_status['offset'] ) ? $indexing_status['offset'] : 0;
$status['total_items'] = isset( $indexing_status['found_items'] ) ? $indexing_status['found_items'] : 0;
$status['indexable'] = isset( $indexing_status['current_sync_item']['indexable'] ) ? $indexing_status['current_sync_item']['indexable'] : '';
}
}
return $status;
}
/**
* Add an ElasticPress block category.
*
* @param array $block_categories Array of categories for block types.
* @return array Array of categories for block types.
*/
function block_categories( $block_categories ) {
$block_categories[] = [
'slug' => 'elasticpress',
'title' => 'ElasticPress',
];
return $block_categories;
}
/**
* Enqueue shared block editor assets.
*
* @return void
*/
function block_assets() {
wp_enqueue_script(
'elasticpress-blocks',
EP_URL . 'dist/js/blocks-script.js',
Utils\get_asset_info( 'blocks-script', 'dependencies' ),
Utils\get_asset_info( 'blocks-script', 'version' ),
true
);
wp_localize_script(
'elasticpress-blocks',
'epBlocks',
[
'syncUrl' => Utils\get_sync_url(),
]
);
}