<?php
/**
 * Xpro Theme Builder compatibility.
 *
 * Renders Xpro header, footer, singular, and archive templates on theme hooks
 * instead of the default Eronex markup when those templates are enabled.
 *
 * @package Eronex
 * @since   0.0.1
 */

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

if ( ! class_exists( 'Eronex_Xpro_Compatibility' ) ) {

	/**
	 * Xpro Theme Builder compatibility.
	 *
	 * @since 0.0.1
	 */
	class Eronex_Xpro_Compatibility {

		/**
		 * Singleton instance.
		 *
		 * @since 0.0.1
		 * @var Eronex_Xpro_Compatibility|null
		 */
		private static $instance;

		/**
		 * Returns the singleton instance.
		 *
		 * @since 0.0.1
		 *
		 * @return Eronex_Xpro_Compatibility
		 */
		public static function get_instance() {
			if ( ! isset( self::$instance ) ) {
				self::$instance = new self();
			}

			return self::$instance;
		}

		/**
		 * Constructor.
		 *
		 * @since 0.0.1
		 *
		 * @return void
		 */
		public function __construct() {
			$this->disable_default_compat();
			add_action( 'wp', array( $this, 'hooks' ) );
		}

		/**
		 * Stops Xpro default compatibility from replacing get_header / get_footer.
		 *
		 * @since 0.0.1
		 *
		 * @return void
		 */
		protected function disable_default_compat() {
			if ( ! class_exists( 'Xpro_Default_Compatibility' ) ) {
				return;
			}

			remove_action( 'wp', array( Xpro_Default_Compatibility::instance(), 'hooks' ) );
		}

		/**
		 * Replaces theme markup when Xpro Theme Builder templates are active.
		 *
		 * @since 0.0.1
		 *
		 * @return void
		 */
		public function hooks() {
			if ( ! function_exists( 'xpro_theme_builder_is_singular_enabled' ) ) {
				return;
			}

			if ( get_post_type() === 'xpro-themer' ) {
				add_filter( 'single_template', array( $this, 'blank_template' ) );
				return;
			}

			if ( xpro_theme_builder_header_enabled() ) {
				remove_action( 'eronex_header', 'eronex_header_markup' );
				add_action( 'eronex_header', 'xpro_theme_builder_render_header' );
			}

			if ( xpro_theme_builder_footer_enabled() ) {
				remove_action( 'eronex_footer', 'eronex_footer_markup' );
				add_action( 'eronex_footer', 'xpro_theme_builder_render_footer' );
			}

			if ( function_exists( 'xpro_theme_builder_is_singular_enabled' ) && xpro_theme_builder_is_singular_enabled() && ! $this->elementor_location_active( 'single' ) ) {

				remove_action( 'eronex_banner', 'eronex_banner_markup' );
				remove_action( 'eronex_sidebar', 'eronex_get_sidebar' );

				add_filter( 'page_template', array( $this, 'empty_template' ) );
				add_filter( 'single_template', array( $this, 'empty_template' ) );
				add_filter( '404_template', array( $this, 'empty_template' ) );
				add_filter( 'frontpage_template', array( $this, 'empty_template' ) );

				if ( defined( 'WOOCOMMERCE_VERSION' ) && is_woocommerce() ) {
					add_action( 'template_redirect', array( $this, 'woo_template' ), 999 );
					add_action( 'template_include', array( $this, 'woo_template' ), 999 );
				}
			}

			if ( function_exists( 'xpro_theme_builder_is_archive_enabled' ) && xpro_theme_builder_is_archive_enabled() && ! $this->elementor_location_active( 'archive' ) ) {

				remove_action( 'eronex_banner', 'eronex_banner_markup' );
				remove_action( 'eronex_sidebar', 'eronex_get_sidebar' );

				add_filter( 'search_template', array( $this, 'empty_template' ) );
				add_filter( 'date_template', array( $this, 'empty_template' ) );
				add_filter( 'author_template', array( $this, 'empty_template' ) );
				add_filter( 'archive_template', array( $this, 'empty_template' ) );
				add_filter( 'category_template', array( $this, 'empty_template' ) );
				add_filter( 'tag_template', array( $this, 'empty_template' ) );
				add_filter( 'home_template', array( $this, 'empty_template' ) );

				if ( defined( 'WOOCOMMERCE_VERSION' ) && is_woocommerce() ) {
					add_action( 'template_redirect', array( $this, 'woo_template' ), 999 );
					add_action( 'template_include', array( $this, 'woo_template' ), 999 );
				}
			}
		}

		public function blank_template( $template ) {
			if ( defined( 'XPRO_THEME_BUILDER_DIR' ) && file_exists( XPRO_THEME_BUILDER_DIR . 'inc/templates/blank.php' ) ) {
				return XPRO_THEME_BUILDER_DIR . 'inc/templates/blank.php';
			}

			return $template;
		}

		/**
		 * Uses Xpro empty wrapper for views without eronex content hooks.
		 *
		 * @since 0.0.1
		 *
		 * @param string $template Template path.
		 * @return string
		 */
		public function empty_template( $template ) {
			if ( defined( 'XPRO_THEME_BUILDER_DIR' ) && file_exists( XPRO_THEME_BUILDER_DIR . 'inc/templates/empty.php' ) ) {
				return XPRO_THEME_BUILDER_DIR . 'inc/templates/empty.php';
			}

			return $template;
		}

		/**
		 * Uses Xpro WooCommerce wrapper for shop / product views.
		 *
		 * @since 0.0.1
		 *
		 * @param string $template Template path.
		 * @return string
		 */
		public function woo_template( $template ) {
			if ( defined( 'XPRO_THEME_BUILDER_DIR' ) && file_exists( XPRO_THEME_BUILDER_DIR . 'inc/templates/woo.php' ) ) {
				return XPRO_THEME_BUILDER_DIR . 'inc/templates/woo.php';
			}

			return $template;
		}

		/**
		 * Whether Elementor Pro Theme Builder already owns a location.
		 *
		 * @since 0.0.1
		 *
		 * @param string $location Location slug.
		 * @return bool
		 */
		protected function elementor_location_active( $location ) {
			return class_exists( 'Eronex_Elementor_Compatibility' ) && Eronex_Elementor_Compatibility::location_exists( $location );
		}
	}

	Eronex_Xpro_Compatibility::get_instance();
}
