<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Sifency_Pre_Before_Footer_Loader {

	private static $rendered = false;

	public function __construct() {
		add_action( 'wp', array( $this, 'init' ), 50 );
	}

	public function init() {

		$template_id = Sifency_Addons_Frontend_Helper::find_matching_template( 'pre-before-footer' );

		if ( ! $template_id ) {
			return;
		}

		/**
		 * Insert pre-before-footer template BEFORE before-footer.
		 *
		 * get_footer hook runs before wp_footer,
		 * so we hook into get_footer with priority 0 to print pre-before-footer content
		 * before the before-footer content (which uses priority 1).
		 */
		add_action( 'get_footer', array( $this, 'render_pre_before_footer' ), 0 );
	}

	/**
	 * Output Elementor Pre Before Footer template
	 */
	public function render_pre_before_footer() {

		if ( self::$rendered ) {
			return;
		}
		self::$rendered = true;

		$template_id = Sifency_Addons_Frontend_Helper::find_matching_template( 'pre-before-footer' );
		if ( ! $template_id ) {
			return;
		}

		echo "\n<!-- Sifency Pre Before Footer Start -->\n";
		echo '<div id="sifency-pre-before-footer" class="sifency-pre-before-footer">';
		Sifency_Addons_Frontend_Helper::render_elementor_content( $template_id );
		echo '</div>';
		echo "\n<!-- Sifency Pre Before Footer End -->\n";
	}
}

new Sifency_Pre_Before_Footer_Loader();
