<?php
/**
 * Upgrades Handler.
 *
 * @author     ThemeFusion
 * @copyright  (c) Copyright by ThemeFusion
 * @link       https://avada.com
 * @package    Avada
 * @subpackage Core
 */

// Do not allow directly accessing this file.
if ( ! defined( 'ABSPATH' ) ) {
	exit( 'Direct script access denied.' );
}

/**
 * Handle migrations for Avada 7.15.3
 *
 * @since 7.15.3
 */
class Avada_Upgrade_7153 extends Avada_Upgrade_Abstract {

	/**
	 * The version.
	 *
	 * @access protected
	 * @since 7.15.3
	 * @var string
	 */
	protected $version = '7.15.3';

	/**
	 * An array of all available languages.
	 *
	 * @static
	 * @access private
	 * @since 7.15.3
	 * @var array
	 */
	private static $available_languages = [];

	/**
	 * The actual migration process.
	 *
	 * @access protected
	 * @since 7.15.3
	 * @return void
	 */
	protected function migration_process() {
		$available_languages       = Fusion_Multilingual::get_available_languages();
		self::$available_languages = ( ! empty( $available_languages ) ) ? $available_languages : [ '' ];

		$this->migrate_options();
	}

	/**
	 * Migrate options.
	 *
	 * @since 7.15.3
	 * @access protected
	 */
	protected function migrate_options() {
		$available_langs = self::$available_languages;

		$options = get_option( $this->option_name, [] );
		$options = $this->migrate_custom_tracking_code( $options );

		update_option( $this->option_name, $options );

		foreach ( $available_langs as $language ) {

			// Skip langs that are already done.
			if ( '' === $language ) {
				continue;
			}

			$options = get_option( $this->option_name . '_' . $language, [] );
			$options = $this->migrate_custom_tracking_code( $options );

			update_option( $this->option_name . '_' . $language, $options );
		}
	}

	/**
	 * Migrate the google_analytics Custom Tracking Code field into
	 * the first entry of the new custom_code_snippets repeater.
	 *
	 * @access private
	 * @since 7.15.3
	 * @param array $options The Global Options array.
	 * @return array         The updated Global Options array.
	 */
	private function migrate_custom_tracking_code( $options ) {
		$tracking_code = isset( $options['google_analytics'] ) ? $options['google_analytics'] : '';

		// Only migrate if the old field had a value and the new repeater is not yet set.
		if ( ! empty( $tracking_code ) && empty( $options['custom_code_snippets'] ) ) {

			// Preserve the old blocking behaviour: hard-block only when privacy embeds were
			// enabled AND the legacy "tracking" type was selected in Privacy Consent Types.
			$privacy_embeds_on = isset( $options['privacy_embeds'] ) && '1' === (string) $options['privacy_embeds'];
			$selected_types    = isset( $options['privacy_embed_types'] ) ? (array) $options['privacy_embed_types'] : [];
			$tracking_selected = in_array( 'tracking', $selected_types, true );
			$block_type        = ( $privacy_embeds_on && $tracking_selected ) ? 'hard_block' : 'soft_block';

			// Store in the columnar format fusionredux repeaters use.
			$options['custom_code_snippets'] = [
				'fusionredux_repeater_data' => [ 0 => [ 'title' => '' ] ],
				'label'                     => [ 0 => esc_html__( 'Custom Tracking Code', 'Avada' ) ],
				'code'                      => [ 0 => $tracking_code ],
				'category'                  => [ 0 => 'tracking' ],
				'placement'                 => [ 0 => 'head' ],
				'snippet_description'       => [ 0 => '' ],
				'lifespan'                  => [ 0 => '' ],
				'block_type'                => [ 0 => $block_type ],
				'prechecked'                => [ 0 => '0' ],
			];

			// Clear the old field.
			//unset( $options['google_analytics'] );
		}

		return $options;
	}
}
