<?php

/**
 * WPML and Polylang compatibility for the Offer custom post type.
 *
 * @link       https://nudgesells.com
 * @since      1.0.0
 *
 * @package    NudgeSells
 * @subpackage NudgeSells/includes
 */

if (! defined('ABSPATH')) {
	exit;
}

/**
 * NudgeSells_Multilingual
 *
 * Registers the Offer post type with WPML/Polylang so offers can be translated like any other content.
 *
 * @since      1.0.0
 * @package    NudgeSells
 * @subpackage NudgeSells/includes
 * @author     NudgeSells <support@nudgesells.com>
 */
class NudgeSells_Multilingual
{

	/**
	 * Register polylang post type.
	 *
	 * @param mixed $post_types
	 * @param mixed $is_settings
	 *
	 * @return mixed
	 * @since 1.0.0
	 */
	public function register_polylang_post_type($post_types, $is_settings)
	{
		$post_types[NudgeSells_CPT::POST_TYPE] = NudgeSells_CPT::POST_TYPE;
		return $post_types;
	}

	/**
	 * Is active.
	 *
	 * @return mixed
	 * @since 1.0.0
	 */
	public static function is_active()
	{
		return function_exists('pll_current_language') || defined('ICL_SITEPRESS_VERSION');
	}

	/**
	 * Current language.
	 *
	 * @return mixed
	 * @since 1.0.0
	 */
	public static function current_language()
	{
		if (function_exists('pll_current_language')) {
			return (string) pll_current_language();
		}
		if (defined('ICL_LANGUAGE_CODE')) {
			return (string) ICL_LANGUAGE_CODE;
		}
		return '';
	}

	/**
	 * Get translated offer id.
	 *
	 * @param mixed $offer_id
	 *
	 * @return mixed
	 * @since 1.0.0
	 */
	public static function get_translated_offer_id($offer_id)
	{
		if (function_exists('wpml_object_id_filter') || has_filter('wpml_object_id')) {
			$translated = apply_filters('wpml_object_id', $offer_id, NudgeSells_CPT::POST_TYPE, false);
			if ($translated) {
				return (int) $translated;
			}
		} elseif (function_exists('pll_get_post')) {
			$translated = pll_get_post($offer_id);
			if ($translated) {
				return (int) $translated;
			}
		}
		return (int) $offer_id;
	}
}
