<?php

/**
 * The internationalization functionality of the plugin.
 *
 * @link       https://wpconfiguratorbuilder.com/
 * @since      1.0.0
 *
 * @package    wcb-configurator-builder
 * @subpackage wcb-configurator-builder/includes
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * The internationalization functionality of the plugin.
 *
 * Registers all post strings to the WPML and Polylang string translations tabs
 *
 * @package    wcb-configurator-builder
 * @subpackage wcb-configurator-builder/admin
 * @author     WPCB <support@wpconfiguratorbuilder.com>
 */

class WPCB_i18n {

	/**
	 * Constructor.
	 */
	public function __construct() {
		
		add_action( 'plugins_loaded', array( $this, 'register_textdomain' ) );

		add_filter( 'pll_get_post_types', array( $this, 'mark_post_types_translatable' ), 20, 2 );
		add_action( 'carbon_fields_container_settings_after_sidebar', array( $this, 'display_side_meta_boxes' ) );

		add_action( 'admin_init', array( $this, 'register_string_translation_metabox' ), 10 );
		add_action( 'admin_post_do_wpcb_string_translation_action', array( $this, 'wpcb_string_translation_action' ) );

	}

	/**
	 * Register WPCB Configurator text domain
	 *
	 * @since    1.0.0
	 */
	public function register_textdomain() {
		load_plugin_textdomain(
			'wcb-configurator-builder',
			false,
			WPCB_BASE . 'languages/'
		);
	}

	/**
	 * Register WPCB Configurator post types
	 *
	 * @since    1.0.0
	 */
	public function mark_post_types_translatable($post_types, $hide) {
		if ( defined( 'WPCB_STRING_TRANSLATION' ) ) {
			return $post_types;
		}

		$post_types['wpcb_configuration'] = 'wpcb_configuration';
		$post_types['wpcb_choice'] = 'wpcb_choice';
		$post_types['wpcb_input'] = 'wpcb_input';

		return $post_types;
	}

	public function register_string_translation_metabox() {
		global $pagenow;

		if ( ! defined( 'WPCB_STRING_TRANSLATION' ) ) {
			return;
		}

		if ( ! function_exists( 'icl_register_string' ) ) {
			return; // no translation function available.
		}

		if ($pagenow === 'edit.php' && isset($_GET['page']) && $_GET['page'] === 'wpcb-settings') {
			// Add meta box
			add_meta_box( 'wpcb-translation-metabox', 'Translations', array( $this, 'display_translation_metabox_form' ), 'wpcb_configuration_page_wpcb-settings', 'side' );
		}
	}

	public function display_side_meta_boxes() {
		global $screen;
		do_meta_boxes( $screen, 'side', 'wpcb_configuration_page_wpcb-settings' );
	}

	public function display_translation_metabox_form() {
		$nonce = wp_create_nonce('wpcb_translation_nonce');
		?>
		<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post" style="display: inline-block;">
		</form>
		<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post" style="display: inline-block;">
			<p><?php esc_attr_e( 'Click the button below to register all your configurator, choice and input string translations to your plugin string translation page.', 'wcb-configurator-builder' ); ?></p>
			<input type="hidden" name="referrer" value="<?php echo esc_url( admin_url('/edit.php?post_type=wpcb_configuration&page=wpcb-settings') ); ?>">
			<input type="hidden" name="nonce" value="<?php echo esc_attr( $nonce ); ?>">
			<input type="hidden" name="action" value="do_wpcb_string_translation_action">
			<input type="submit" value="<?php esc_attr_e( 'Register strings', 'wcb-configurator-builder' ); ?>" class="button button-primary button-large" id="register_strings" name="register_strings">
		</form>
		<?php
	}

	/**
	 * Handle WPCB Configurator strings action
	 *
	 * @since    1.0.0
	 */
	public function wpcb_string_translation_action() {
		$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';

		if ( wp_verify_nonce( $nonce, 'wpcb_translation_nonce' ) ) {
			// register all strings
			$this->register_wpcb_configuration_strings();
			$this->register_wpcb_choice_strings();
			$this->register_wpcb_input_strings();
		}

		if ( isset( $_POST['referrer'] ) ) {
			$url = sanitize_url( wp_unslash($_POST['referrer']) );
			wp_safe_redirect( $url );
			exit;
		}
	}

	/**
	 * Register WPCB Configurator strings
	 *
	 * @since    1.0.0
	 */
	public function register_wpcb_configuration_strings() {
		$configurator_ids = get_posts( array(
			'post_type' => 'wpcb_configuration',
			'posts_per_page' => 999999,
			'no_found_rows' => true,
			'update_post_meta_cache' => false,
			'update_post_term_cache' => false,
			'fields' => 'ids',
		));

		foreach ( $configurator_ids as $configurator_id ) {
			$meta = get_post_meta( $configurator_id );
			$label = wpcb_sanitize_title( get_the_title( $configurator_id ) );

			icl_register_string( 'wcb-configurator-builder', $label . '_title', get_the_title( $configurator_id ) );

			if ( isset( $meta['_short_description'] ) ) {
				icl_register_string( 'wcb-configurator-builder', $label . '_short_description', $meta['_short_description'][0] );
			}
			if ( isset( $meta['_gallery_label_controls'] ) ) {
				icl_register_string( 'wcb-configurator-builder', $label . '_gallery_label_controls', $meta['_gallery_label_controls'][0] );
			}
			if ( isset( $meta['_model_label_controls'] ) ) {
				icl_register_string( 'wcb-configurator-builder', $label . '_model_label_controls', $meta['_wpcb_model_label_controls'][0] );
			}

			if ( isset( $meta['_wpcb_model_hotspots'] ) ) {
				foreach ( $meta['_wpcb_model_hotspots'][0] as $index => $hotspot ) {
					icl_register_string( 'wcb-configurator-builder', $label . '_model_hotspot_' . $index, trim( wpcb_get_value( $hotspot['hotspot_text'] ) ) );
				}
			}

			if ( isset( $meta['_email_prefix'] ) ) {
				icl_register_string( 'wcb-configurator-builder', $label . '_email_prefix', $meta['_email_prefix'][0] );
			}
			if ( isset( $meta['_email_postfix'] ) ) {
				icl_register_string( 'wcb-configurator-builder', $label . '_email_postfix', $meta['_email_postfix'][0] );
			}

			// Translate blocks.
			$post = get_post( $configurator_id ); 
			if ( ! has_blocks( $post->post_content ) ) {
				continue;
			}
		
			$configurator_fields = parse_blocks( $post->post_content );
			if ( ! is_array( $configurator_fields ) || 0 === count( $configurator_fields ) ) {
				continue;
			}

			foreach ( $configurator_fields as $index => $configurator_field ) {
				if ( 'carbon-fields/wcb-accordeon' === $configurator_field['blockName'] ) {

					icl_register_string( 'wcb-configurator-builder', 'accordeon_' . $index . '_title', $configurator_field['attrs']['data']['wpcb_accordeon_title'] );
					icl_register_string( 'wcb-configurator-builder', 'accordeon_' . $index . '_description', $configurator_field['attrs']['data']['wpcb_accordeon_description'] );

				} else if ( 'carbon-fields/wcb-tab' === $configurator_field['blockName'] ) {

					icl_register_string( 'wcb-configurator-builder', 'tab_' . $index . '_title', $configurator_field['attrs']['data']['wpcb_accordeon_title'] );

				} else if ( 'carbon-fields/wcb-step' === $configurator_field['blockName'] ) {

					icl_register_string( 'wcb-configurator-builder', 'step_' . $index . '_title', $configurator_field['attrs']['data']['wpcb_step_title'] );
					icl_register_string( 'wcb-configurator-builder', 'step_' . $index . '_description', $configurator_field['attrs']['data']['wpcb_step_description'] );
					icl_register_string( 'wcb-configurator-builder', 'step_' . $index . '_button', $configurator_field['attrs']['data']['wpcb_step_button_text'] );

				} else if ( 'carbon-fields/wcb-group' === $configurator_field['blockName'] ) {

					icl_register_string( 'wcb-configurator-builder', 'fieldgroup_' . $index . '_label', $configurator_field['attrs']['data']['wpcb_fieldgroup_label'] );
					icl_register_string( 'wcb-configurator-builder', 'fieldgroup_' . $index . '_description', $configurator_field['attrs']['data']['wpcb_fieldgroup_description'] );

				} else if ( 'carbon-fields/wcb-filter' === $configurator_field['blockName'] ) {

					icl_register_string( 'wcb-configurator-builder', 'fieldfilter_' . $index . '_label', $configurator_field['attrs']['data']['wpcb_fieldgroup_label'] );
					icl_register_string( 'wcb-configurator-builder', 'fieldfilter_' . $index . '_clear_label', $configurator_field['attrs']['data']['wpcb_fieldfilter_clear_label'] );
					icl_register_string( 'wcb-configurator-builder', 'fieldfilter_' . $index . '_description', $configurator_field['attrs']['data']['wpcb_fieldfilter_description'] );
					icl_register_string( 'wcb-configurator-builder', 'fieldfilter_' . $index . '_button', $configurator_field['attrs']['data']['wpcb_fieldfilter_button'] );

				} else if ( 'carbon-fields/wcb-repeater' === $configurator_field['blockName'] ) {

					icl_register_string( 'wcb-configurator-builder', 'repeater_' . $index . '_label', $configurator_field['attrs']['data']['wpcb_repeater_label'] );
					icl_register_string( 'wcb-configurator-builder', 'repeater_' . $index . '_empty_note', $configurator_field['attrs']['data']['wpcb_repeater_empty_note'] );
					icl_register_string( 'wcb-configurator-builder', 'repeater_' . $index . '_add_tex', $configurator_field['attrs']['data']['wpcb_repeater_add_text'] );

				}
			}
		}
	}

	/**
	 * Register WPCB Configurator strings
	 *
	 * @since    1.0.0
	 */
	public function register_wpcb_choice_strings() {
		$choice_ids = get_posts( array(
			'post_type' => 'wpcb_choice',
			'posts_per_page' => 999999,
			'no_found_rows' => true,
			'update_post_meta_cache' => false,
			'update_post_term_cache' => false,
			'fields' => 'ids',
		));

		foreach ( $choice_ids as $choice_id ) {
			$meta = get_post_meta( $choice_id );
			$label = wpcb_sanitize_title( get_the_title( $choice_id ) );

			icl_register_string( 'wcb-configurator-builder', $label . '_label', $meta['_label'][0] );
			if ( isset( $meta['_note'] ) ) {
				icl_register_string( 'wcb-configurator-builder', $label . '_note', $meta['_note'][0] );
			}
			if ( isset( $meta['_description'] ) ) {
				icl_register_string( 'wcb-configurator-builder', $label . '_description', $meta['_description'][0] );
			}
			if ( isset( $meta['_placeholder'] ) ) {
				icl_register_string( 'wcb-configurator-builder', $label . '_placeholder', $meta['_placeholder'][0] );
			}

			$options = unserialize( $meta['_options'][0] );
			if ( count( $options ) > 0 ) {
				foreach ( $options as $index => $option ) {
					$key = $index + 1;

					icl_register_string( 'wcb-configurator-builder', $label . '_' . $key . '_label', wpcb_get_value( $option['label'] ) );

					if ( isset( $option['note'] ) ) {
						icl_register_string( 'wcb-configurator-builder', $label . '_' . $key . '_note', wpcb_get_value( $option['note'] ) );
					}
					if ( isset( $option['description'] ) ) {
						icl_register_string( 'wcb-configurator-builder', $label . '_' . $key . '_description', wpcb_get_value( $option['description'] ) );
					}
				}
			}
		}
	}

	/**
	 * Register WPCB Configurator strings
	 *
	 * @since    1.0.0
	 */
	public function register_wpcb_input_strings() {
		$input_ids = get_posts( array(
			'post_type' => 'wpcb_input',
			'posts_per_page' => 999999,
			'no_found_rows' => true,
			'update_post_meta_cache' => false,
			'update_post_term_cache' => false,
			'fields' => 'ids',
		));

		foreach ( $input_ids as $input_id ) {
			$meta = get_post_meta( $input_id );
			$label = wpcb_sanitize_title( get_the_title( $input_id ) );

			$keys = array('_label', '_placeholder', '_default_value', '_unit', '_note', '_description');
			foreach ( $keys as $key ) {
				if ( isset( $meta[$key] ) ) {
					icl_register_string( 'wcb-configurator-builder', $label . $key, $meta[$key][0] );
				}
			}
		}
	}
}
