<?php
/**
 * Echo footer fragments.
 *
 * @package SplendorStudio\Framework\Templates\Fragments
 *
 * @since   1.0.0
 */
class Fancify_Lite_Footer_Fragment extends Fancify_Lite_Template_Parts {
	/**
	 * Constructor
	 *
	 * @since    1.0.0
	 */
	function __construct() {
		$this->register_hook_callbacks();
	}

	/**
	 * Register callbacks for actions and filters
	 *
	 * @since    1.0.0
	 */
	protected function register_hook_callbacks() {
		// Check for coming soon
		if ( fancify_lite_is_coming_soon() ) {
			return;
		}
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_footer', array( $this, 'footer_content' ) );
		Fancify_Lite_Actions::add_smart_action( 'wp_footer', array( $this, 'replace_nojs_class' ) );
	}

	/**
	 * Echo the footer content.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function footer_content() {
		// Get page ID
		$page_id = get_queried_object_id();
		
		// Check custom footer
		$custom_footer = Fancify_Lite_ACF::get_field( 'page_option_enable_custom_footer', false, $page_id );

		if ( $custom_footer ) {
			$footer_layout_ID = Fancify_Lite_ACF::get_field( 'page_option_select_footer', false, $page_id );
			$footer_layout_ID = $footer_layout_ID->ID;
		} else {
			$footer_layout_ID = get_theme_mod( 'footer_layout', false );
		}

		if ( $footer_layout_ID && !empty ( $footer_layout_ID ) ) {

			// Get content
			$footer_content = get_post( $footer_layout_ID );
			if ( ! empty ( $footer_content ) ) {

				if ( did_action( 'elementor/loaded' ) ) {
					echo \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $footer_layout_ID );// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped before
				} else {
					echo wp_kses_post( $footer_content->post_content );
				}

			}
		} else {
			echo fancify_lite_get_widget_area_output( 'footer_widgets_column' );// phpcs:ignore WordPress.Security.EscapeOutput.DeprecatedWhitelistCommentFound -- XSS ok.
		}

	}

	/**
	 * Print inline JavaScript in the footer to replace the 'no-js' class with 'js'.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public function replace_nojs_class() {
		?><script type="text/javascript">
			(function() {
				document.body.className = document.body.className.replace('no-js','js');
			}());
		</script>
		<?php
	}

}

Fancify_Lite_Footer_Fragment::get_instance();
