<?php
/**
 * WPML and Polylang compatibility functions.
 *
 * @package Artech
 * @since 1.0
 */

/**
 * Filter to translate strings
 */
function glides_translate_single_string( $original_value, $domain ) {
	if ( is_customize_preview() ) {
		$wpml_translation = $original_value;
	} else {
		$wpml_translation = apply_filters( 'wpml_translate_single_string', $original_value, $domain, $original_value );
		if ( $wpml_translation === $original_value && function_exists( 'pll__' ) ) {
			return pll__( $original_value );
		}
	}
	return $wpml_translation;
}
add_filter( 'glides_translate_single_string', 'glides_translate_single_string', 10, 2 );

/**
 * Helper to register pll string.
 *
 * @param String    $theme_mod Theme mod name.
 * @param bool/json $default Default value.
 * @param String    $name Name for polylang backend.
 */
function glides_pll_string_register_helper( $theme_mod ) {
	if ( ! function_exists( 'pll_register_string' ) ) {
		return;
	}
	
	$repeater_content = get_theme_mod( $theme_mod );
	$repeater_content = json_decode( $repeater_content );
	if ( ! empty( $repeater_content ) ) {
		foreach ( $repeater_content as $repeater_item ) {
			foreach ( $repeater_item as $field_name => $field_value ) {
				if ( $field_value !== 'undefined' ) {
					if ( $field_name !== 'id' ) {
						$f_n = ucfirst( $field_name );
						pll_register_string( $f_n, $field_value);
					}
				}
			}
		}
	}
}


/**
 * Header Social
 */
 
function glides_header_social_register_strings() {
	$default = themes_get_social_icon_default();
	glides_pll_string_register_helper( 'social_icons', $default, 'Header Social' );
}


/**
 * Slider Section
 */
 
function glides_slider_register_strings() {
	$default = themes_get_slider_default();
	glides_pll_string_register_helper( 'slider', $default, 'Slider section' );
	
}


/**
 * Service Section
 */
 
function glides_service_register_strings() {
	$default = themes_get_service_default();
	glides_pll_string_register_helper( 'service_contents', $default, 'Service section' );
}


/**
 * Features Section
 */
 
function glides_features_register_strings() {
	$default = themes_get_feature_default();
	glides_pll_string_register_helper( 'features_contents', $default, 'Features section' );
}

/**
 * Funfact Section
 */
 
function glides_funfact_register_strings() {
	$default = themes_get_funfact_default();
	glides_pll_string_register_helper( 'funfact_contents', $default, 'Funfact section' );
}


if ( function_exists( 'pll_register_string' ) ) {
	add_action( 'after_setup_theme', 'glides_slider_register_strings', 11 );
	add_action( 'after_setup_theme', 'glides_service_register_strings', 11 );
	add_action( 'after_setup_theme', 'glides_features_register_strings', 11 );
	add_action( 'after_setup_theme', 'glides_funfact_register_strings', 11 );
}

if (function_exists('pll_register_string')) {
	/* About Page */
    pll_register_string('service_title', 'Mind Blowing Service', 'glides');
    pll_register_string('service_subtitle', 'Our Service', 'glides');
	
	pll_register_string('feature_title', 'Mind Blowing Feature', 'glides');
    pll_register_string('feature_subtitle', 'Our Feature', 'glides');
	
	pll_register_string('blog_title', 'What People Writes', 'glides');
    pll_register_string('blog_subtitle', 'Outstanding Blog', 'glides');
}