<?php


/**
 * Class Tribe__Events__Integrations__WPML__Category_Translation
 *
 * Translates category links on the site front-end.
 */
class Tribe__Events__Integrations__WPML__Category_Translation {

	/**
	 * @var static
	 */
	protected static $instance;

	/**
	 * @return Tribe__Events__Integrations__WPML__Category_Translation
	 */
	public static function instance() {
		if ( empty( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Filters the `tribe_events_category_slug` to return the category slug that's WPML aware.
	 *
	 * WPML does not currently support translation of custom taxonomies root ,e.g. `category` in
	 * The Events Calendar case. But we do take WPML-managed translations of the `category` slug
	 * into account in our rewrite rules and try to show a localized version of the `category` slug
	 * in the permalinks.
	 *
	 * @param string $slug The original, possibily translated, category slug.
	 *
	 * @return string The category slug in its ENG form if the Events Category translation is not active
	 *                or in a translation that The Events Calendar supports.
	 */
	public function filter_tribe_events_category_slug( $slug ) {
		/** @var SitePress $sitepress */
		global $sitepress;

		$tax_sync_options     = $sitepress->get_setting( 'taxonomies_sync_option' );
		$translate_event_cat  = ! empty( $tax_sync_options[ Tribe__Events__Main::TAXONOMY ] );
		$using_lang_query_var = $sitepress->get_setting( 'language_negotiation_type' ) == 3;

		if ( ! $translate_event_cat || $using_lang_query_var ) {
			$slug = 'category';
		} else {
			$lang            = $sitepress->get_locale( ICL_LANGUAGE_CODE );
			$tec_translation = Tribe__Events__Integrations__WPML__Utils::get_wpml_i18n_strings( array( 'category' ), $lang );
			$slug            = ! empty( $tec_translation[0] ) ? end( $tec_translation[0] ) : $slug;

			$remove_accents = true;

			/**
			 * Whether accents should be removed from the translated slug or not.
			 *
			 * Returning a falsy value here will prevent accents from being removed.
			 * E.g. "catégorie" would become "categorie" if this filter value is truthy;
			 * it would instead remain unchanged if this filters returns a falsy value.
			 *
			 * @param bool $remove_accents Defaults to `true`.
			 */
			$remove_accents = apply_filters( 'tribe_events_integrations_category_slug_remove_accents', $remove_accents );

			if ( $remove_accents ) {
				$slug = remove_accents( urldecode( $slug ) );
			}
		}

		return $slug;
	}
}
