<?php
/**
 * Create an ElasticPress dashboard page.
 *
 * @package elasticpress
 * @since   1.9
 */

namespace ElasticPress\Dashboard;

use ElasticPress\Utils as Utils;
use ElasticPress\Elasticsearch;
use ElasticPress\Features;
use ElasticPress\Indexables;
use ElasticPress\Installer;
use ElasticPress\AdminNotices;
use ElasticPress\Screen;
use ElasticPress\Stats;

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 );
	} else {
		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__ . '\action_admin_init' );
	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( 'manage_blogs_custom_column', __NAMESPACE__ . '\add_blogs_column', 10, 2 );
	add_action( 'rest_api_init', __NAMESPACE__ . '\setup_endpoint' );

	/**
	 * 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', true );

	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'] = $atts;

		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 ) {
	$is_network = defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK;

	$logging_key = 'logging_ep_es_info';

	if ( $is_network ) {
		$logging = get_site_transient( $logging_key );
	} else {
		$logging = 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.
		if ( $is_network ) {
			set_site_transient( $response_code_key, $response_code, $cache_time );
			set_site_transient( $response_error_key, $response_error, $cache_time );
			delete_site_transient( $logging_key );
		} else {
			set_transient( $response_code_key, $response_code, $cache_time );
			set_transient( $response_error_key, $response_error, $cache_time );
			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 ) ) { // phpcs:ignore WordPress.Security.NonceVerification
		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;
	}

	if ( empty( $_GET['ep-retry'] ) && ! in_array( Screen::factory()->get_current_screen(), [ 'dashboard', 'settings', 'install' ], true ) ) { // phpcs:ignore WordPress.Security.NonceVerification
		return;
	}

	if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
		delete_site_transient( 'ep_es_info' );
	} else {
		delete_transient( 'ep_es_info' );
	}

	if ( ! empty( $_GET['ep-retry'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
		wp_safe_redirect( remove_query_arg( 'ep-retry' ) );
		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 ) {
	// Admins only.
	if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
		if ( ! is_super_admin() ) {
			return false;
		}
	} else {
		if ( ! current_user_can( 'manage_options' ) ) {
			return false;
		}
	}

	// If in network mode, don't output notice in admin and vice-versa.
	if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
		if ( ! is_network_admin() ) {
			return false;
		}
	} else {
		if ( is_network_admin() ) {
			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 ) );

	if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
		set_site_transient(
			'logging_ep_es_info',
			'1',
			$cache_time
		);
	} else {
		$a = 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( 'manage_options' ) ) {
		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.min.js',
			Utils\get_asset_info( 'sites-admin-script', 'dependencies' ),
			Utils\get_asset_info( 'sites-admin-script', 'version' ),
			true
		);

		$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' ], true ) ) {
		wp_enqueue_style(
			'ep_admin_styles',
			EP_URL . 'dist/css/dashboard-styles.min.css',
			Utils\get_asset_info( 'dashboard-styles', 'dependencies' ),
			Utils\get_asset_info( 'dashboard-styles', 'version' )
		);
	}

	if ( in_array( Screen::factory()->get_current_screen(), [ 'weighting', 'install' ], true ) ) {
		wp_enqueue_script(
			'ep_weighting_script',
			EP_URL . 'dist/js/weighting-script.min.js',
			Utils\get_asset_info( 'weighting-script', 'dependencies' ),
			Utils\get_asset_info( 'weighting-script', 'version' ),
			true
		);
	}

	if ( in_array( Screen::factory()->get_current_screen(), [ 'dashboard', 'install' ], true ) ) {
		wp_enqueue_script(
			'ep_dashboard_scripts',
			EP_URL . 'dist/js/dashboard-script.min.js',
			Utils\get_asset_info( 'dashboard-script', 'dependencies' ),
			Utils\get_asset_info( 'dashboard-script', 'version' ),
			true
		);

		$sync_url = ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) ?
				network_admin_url( 'admin.php?page=elasticpress-sync&do_sync' ) :
				admin_url( 'admin.php?page=elasticpress-sync&do_sync' );

		$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(), [ 'settings' ], true ) ) {
		wp_enqueue_script(
			'ep_settings_scripts',
			EP_URL . 'dist/js/settings-script.min.js',
			Utils\get_asset_info( 'settings-script', 'dependencies' ),
			Utils\get_asset_info( 'settings-script', 'version' ),
			true
		);
	}

	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.min.js',
			Utils\get_asset_info( 'stats-script', 'dependencies' ),
			Utils\get_asset_info( 'stats-script', 'version' ),
			true
		);

		wp_localize_script( 'ep_stats', 'epChartData', $data );
	}

	wp_register_script(
		'ep_notice_script',
		EP_URL . 'dist/js/notice-script.min.js',
		Utils\get_asset_info( 'notice-script', 'dependencies' ),
		Utils\get_asset_info( 'notice-script', 'version' ),
		true
	);

	wp_localize_script(
		'ep_notice_script',
		'epAdmin',
		array(
			'nonce' => wp_create_nonce( 'ep_admin_nonce' ),
		)
	);
}

/**
 * Admin-init actions
 *
 * Sets up Settings API.
 *
 * @since 1.9
 * @return void
 */
function action_admin_init() {

	// Save options for multisite.
	if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK && isset( $_POST['ep_language'] ) ) {
		check_admin_referer( 'elasticpress-options' );

		// phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
		$language = sanitize_text_field( $_POST['ep_language'] );
		Utils\update_option( 'ep_language', $language );

		if ( isset( $_POST['ep_host'] ) ) {
			$host = esc_url_raw( trim( $_POST['ep_host'] ) );
			Utils\update_option( 'ep_host', $host );
		}
		// phpcs:enable WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

		if ( isset( $_POST['ep_prefix'] ) ) {
			$prefix = ( isset( $_POST['ep_prefix'] ) ) ? sanitize_text_field( wp_unslash( $_POST['ep_prefix'] ) ) : '';
			Utils\update_option( 'ep_prefix', $prefix );
		}

		if ( isset( $_POST['ep_credentials'] ) ) {
			// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
			$credentials = ( isset( $_POST['ep_credentials'] ) ) ? Utils\sanitize_credentials( $_POST['ep_credentials'] ) : [
				'username' => '',
				'token'    => '',
			];

			Utils\update_option( 'ep_credentials', $credentials );
		}

		if ( isset( $_POST['ep_bulk_setting'] ) ) {
			Utils\update_option( 'ep_bulk_setting', intval( $_POST['ep_bulk_setting'] ) );
		}
	} else {
		register_setting( 'elasticpress', 'ep_host', 'esc_url_raw' );
		register_setting( 'elasticpress', 'ep_prefix', 'sanitize_text_field' );
		register_setting( 'elasticpress', 'ep_credentials', 'ep_sanitize_credentials' );
		register_setting( 'elasticpress', 'ep_language', 'sanitize_text_field' );
		register_setting(
			'elasticpress',
			'ep_bulk_setting',
			[
				'type'              => 'integer',
				'sanitize_callback' => __NAMESPACE__ . '\sanitize_bulk_settings',
			]
		);
	}
}

/**
 * Sanitize bulk settings.
 *
 * @param int $bulk_settings Number of bulk content items
 * @return int
 */
function sanitize_bulk_settings( $bulk_settings = 350 ) {
	$bulk_settings = absint( $bulk_settings );

	return ( 0 === $bulk_settings ) ? 350 : $bulk_settings;
}

/**
 * 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() {
	$capability = 'manage_options';

	if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
		$capability = 'manage_network';
	}

	add_menu_page(
		'Enterprise Search', // VIP: Rename page title
		'Enterprise Search', // VIP: Rename page menu
		$capability,
		'elasticpress',
		__NAMESPACE__ . '\resolve_screen',
		'dashicons-search' // VIP: Change menu icon
	);

	add_submenu_page(
		'elasticpress',
		esc_html__( 'Search Features', 'elasticpress' ), // VIP: Rename page menu.
		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'
	);

	// VIP: Remove "Sync" page from menu

	add_submenu_page(
		'elasticpress',
		esc_html__( 'ElasticPress Index Health', 'elasticpress' ),
		esc_html__( 'Index Health', 'elasticpress' ),
		$capability,
		'elasticpress-health',
		__NAMESPACE__ . '\resolve_screen'
	);
}

/**
 * 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 = '' ) {
	// Get the currently set language.
	$ep_language = Utils\get_language();

	// Bail early if no EP language is set.
	if ( empty( $ep_language ) ) {
		return $language;
	}

	require_once ABSPATH . 'wp-admin/includes/translation-install.php';
	$translations = wp_get_available_translations();

	// Bail early if not in the array of available translations.
	if ( empty( $translations[ $ep_language ]['english_name'] ) ) {
		return $language;
	}

	$wp_language = $translations[ $ep_language ]['language'];

	/**
	 * Languages supported in Elasticsearch mappings.
	 * Array format: Elasticsearch analyzer name => WordPress language package name
	 *
	 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-lang-analyzer.html
	 */
	$es_languages = [
		'arabic'     => [ 'ar', 'ary' ],
		'armenian'   => [ 'hy' ],
		'basque'     => [ 'eu' ],
		'bengali'    => [ 'bn', 'bn_BD' ],
		'brazilian'  => [ 'pt_BR' ],
		'bulgarian'  => [ 'bg' ],
		'catalan'    => [ 'ca' ],
		'cjk'        => [], // CJK characters (not a language)
		'czech'      => [ 'cs' ],
		'danish'     => [ 'da' ],
		'dutch'      => [ 'nl_NL_formal', 'nl_NL', 'nl_BE' ],
		'english'    => [ 'en', 'en_AU', 'en_GB', 'en_NZ', 'en_CA', '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' ],
	];

	/**
	 * 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',
	];

	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 ) {
		if ( in_array( ucfirst( $language ), $es_snowball_languages, true ) ) {
			return ucfirst( $language );
		}

		return 'English';
	}

	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 ) {
	$site = get_site( $blog_id );
	if ( $site->deleted || $site->archived || $site->spam ) {
		return;
	}
	if ( 'elasticpress' === $column_name ) {
		$is_indexable = get_blog_option( $blog_id, 'ep_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 )
		);
	}

	return $column_name;
}

/**
 * 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();
	}
	$old    = get_blog_option( $blog_id, 'ep_indexable' );
	$result = update_blog_option( $blog_id, 'ep_indexable', $checked );
	$data   = [
		'blog_id' => $blog_id,
		'result'  => $result,
	];

	return wp_send_json_success( $data );
}

/**
 * Registers the API endpoint
 */
function setup_endpoint() {
	register_rest_route(
		'elasticpress/v1',
		'indexing_status',
		[
			'methods'             => 'GET',
			'callback'            => __NAMESPACE__ . '\handle_indexing_status',
			'permission_callback' => '__return_true',
		]
	);
}

/**
 * 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;
}
