<?php
/**
 * @package The7
 */

defined( 'ABSPATH' ) || exit;

/**
 * Import Slider Revolution demo sliders.
 */
class The7_Revslider_Importer {

	/**
	 * @var null|RevSlider
	 */
	protected $revslider;

	/**
	 * @var array
	 */
	protected $existed_sliders = [];

	/**
	 * @var The7_Content_Importer|null
	 */
	protected $content_importer;

	/**
	 * The7_Revslider_Importer constructor.
	 *
	 * @param The7_Content_Importer|null $content_importer Demo content importer.
	 */
	public function __construct( $content_importer = null ) {
		$this->content_importer = $content_importer;

		if ( class_exists( 'RevSliderSlider', false ) && method_exists( 'RevSliderSlider', 'importSliderFromPost' ) ) {
			$this->revslider = new RevSlider();

			foreach ( (array) $this->revslider->getArrSliders() as $rev_slider ) {
				$this->existed_sliders[] = $rev_slider->getAlias();
			}
		}
	}

	/**
	 * Import $slider file.
	 *
	 * @param string $slider_alias Slider alias.
	 * @param string $slider_file Absolute path to slider.
	 *
	 * @return array|bool
	 */
	public function import_slider( $slider_alias, $slider_file ) {
		if ( $this->revslider === null || in_array( $slider_alias, $this->existed_sliders, true ) ) {
			return false;
		}

		$this->existed_sliders[] = $slider_alias;

		$this->add_import_filters();

		try {
			$status = $this->revslider->importSliderFromPost( true, true, $slider_file, false, false, true );
		} finally {
			$this->remove_import_filters();
		}

		return $status;
	}

	/**
	 * Remap URLs in slider-level import data.
	 *
	 * @param array  $data          Slider data.
	 * @param string $download_path Import temp path.
	 * @param object $importer      RevSlider importer instance.
	 *
	 * @return array
	 */
	public function remap_slider_import_data( $data, $download_path, $importer ) {
		unset( $download_path, $importer );

		return $this->remap_urls_recursive( $data );
	}

	/**
	 * Remap URLs in slide import data.
	 *
	 * @param array  $data          Slide data.
	 * @param string $slide_type    Slide type.
	 * @param string $download_path Import temp path.
	 * @param object $importer      RevSlider importer instance.
	 *
	 * @return array
	 */
	public function remap_slide_import_data( $data, $slide_type, $download_path, $importer ) {
		unset( $slide_type, $download_path, $importer );

		return $this->remap_urls_recursive( $data );
	}

	/**
	 * Add temporary RevSlider import filters.
	 *
	 * @return void
	 */
	protected function add_import_filters() {
		add_filter( 'revslider_importSliderFromPost_modify_slider_data', [ $this, 'remap_slider_import_data' ], 10, 3 );
		add_filter( 'revslider_importSliderFromPost_modify_data', [ $this, 'remap_slide_import_data' ], 10, 4 );
	}

	/**
	 * Remove temporary RevSlider import filters.
	 *
	 * @return void
	 */
	protected function remove_import_filters() {
		remove_filter( 'revslider_importSliderFromPost_modify_slider_data', [ $this, 'remap_slider_import_data' ] );
		remove_filter( 'revslider_importSliderFromPost_modify_data', [ $this, 'remap_slide_import_data' ] );
	}

	/**
	 * Recursively remap demo upload URLs in arrays and objects.
	 *
	 * @param mixed $value Value to process.
	 *
	 * @return mixed
	 */
	protected function remap_urls_recursive( $value ) {
		if ( is_array( $value ) ) {
			foreach ( $value as $key => $item ) {
				$value[ $key ] = $this->remap_urls_recursive( $item );
			}

			return $value;
		}

		if ( is_object( $value ) ) {
			foreach ( get_object_vars( $value ) as $key => $item ) {
				$value->$key = $this->remap_urls_recursive( $item );
			}

			return $value;
		}

		if ( is_string( $value ) ) {
			if ( ! $this->content_importer ) {
				return $value;
			}

			return $this->content_importer->get_remapped_media_url( $value );
		}

		return $value;
	}
}
