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

class Sifency_Before_Footer_Loader {

    private static $rendered = false;

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

    public function init() {

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

        if ( ! $template_id ) {
            return;
        }

        /**
         * Insert before-footer template BEFORE theme footer.
         *
         * get_footer hook runs before wp_footer,
         * so we hook into get_footer to print before-footer content.
         */
        add_action( 'get_footer', [ $this, 'render_before_footer' ], 1 );
    }

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

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

        $template_id = Sifency_Frontend_Helper::find_matching_template( 'before-footer' );
        if ( ! $template_id ) return;

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

}

new Sifency_Before_Footer_Loader();
