<?php
/**
 * Loads the Reltify template parts.
 *
 * The template parts contain the structural markup and hooks to which the fragments are attached.
 *
 * @package SplendorStudio\Framework\Render
 *
 * @since   1.0.0
 */
class Fancify_Lite_Template_Parts extends Fancify_Lite_Controller {
	
	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
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_load_document', array( $this, 'header_template' ), 5 );
		if ( ! fancify_lite_is_coming_soon() ) {
			Fancify_Lite_Actions::add_smart_action( 'fancify_lite_site_prepend_markup', array( $this, 'header_partial_template' ) );
		}
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_load_document', array( $this, 'content_template' ) );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_content',  array( $this, 'loop_template' ) );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_post_after_markup', array( $this, 'comments_template' ), 15 );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_comment', array( $this, 'comment_template' ) );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_primary_after_markup', array( $this, 'sidebar_primary_template' ) );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_widget_area', array( $this, 'widget_area_template' ) );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_primary_after_markup', array( $this, 'sidebar_secondary_template' ) );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_site_append_markup', array( $this, 'footer_partial_template' ) );
		Fancify_Lite_Actions::add_smart_action( 'fancify_lite_load_document', array( $this, 'footer_template' ) );

	}

	/**
	 * Echo header template part.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public static function header_template() {
		get_header();
	}

	/**
	 * Echo header partial template part.
	 *
	 * @since 1.3.0
	 *
	 * @return void
	 */
	function header_partial_template() {
		// Allow overwrite.
		if ( '' !== locate_template( 'header-partial.php', true, false ) ) {
			return;
		}

		echo static::render_template(// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- valid usecase
			'structure/header-partial.php',
			array()
		);

		return;
	}

	/**
	 * Echo main content template part.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	function content_template() {
		// Allow overwrite.
		if ( '' !== locate_template( 'content.php', true ) ) {
			return;
		}

		require_once FANCIFY_STRUCTURE_PATH . 'content.php';// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- valid usecase
	}

	/**
	 * Echo loop template part.
	 *
	 * @since 1.0.0
	 *
	 * @param string $id Optional. The loop ID is used to filter the loop WP_Query arguments.
	 *
	 * @return void
	 */
	function loop_template( $id = false ) {
		// Set default loop id.
		if ( ! $id ) {
			$id = 'main';
		}

		// Only run new query if a filter is set.
		$_has_filter = Fancify_Lite_Filters::has_filters( "fancify_lite_loop_query_args[_{$id}]" );

		if ( $_has_filter ) {
			global $wp_query;

			/**
			 * Filter the beans loop query. This can be used for custom queries.
			 *
			 * @since 1.0.0
			 */
			$args     = Fancify_Lite_Filters::apply_filters( "fancify_lite_loop_query_args[_{$id}]", false );
			$wp_query = new WP_Query( $args ); // @codingStandardsIgnoreLine // phpcs:ignore WordPress.Variables.GlobalVariables.OverrideProhibited -- Used inside a function scope.

		}
		
		// Allow overwrite. Require the default loop.php if no overwrite is found.
		if ( '' === locate_template( 'loop.php', true, false ) ) {
			require FANCIFY_STRUCTURE_PATH . 'loop.php';// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- valid usecase
		}

		// Only reset the query if a filter is set.
		if ( $_has_filter ) {
			wp_reset_query();//phpcs:ignore WordPress.WP.DiscouragedFunctions.wp_reset_query_wp_reset_query -- Ensure the main query has been reset to the original main query.
		}
	}

	/**
	 * Echo comments template part.
	 *
	 * The comments template part only loads if comments are active to prevent unnecessary memory usage.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	function comments_template() {
		global $post;

		$shortcircuit_conditions = array(
			fancify_lite_get( 'ID', $post ) && ! ( comments_open() || get_comments_number() ),
			! post_type_supports( fancify_lite_get( 'post_type', $post ), 'comments' ),
		);

		if ( in_array( true, $shortcircuit_conditions, true ) ) {
			return;
		}

		comments_template();
	}

	/**
	 * Echo comment template part.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	function comment_template() {
		// Allow overwrite.
		if ( '' !== locate_template( 'comment.php', true, false ) ) {
			return;
		}

		require FANCIFY_STRUCTURE_PATH . 'comment.php';// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- valid usecase
	}

	/**
	 * Echo widget area template part.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	function widget_area_template() {
		// Allow overwrite.
		if ( '' !== locate_template( 'widget-area.php', true, false ) ) {
			return;
		}

		require FANCIFY_STRUCTURE_PATH . 'widget-area.php';// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- valid usecase
	}

	/**
	 * Echo primary sidebar template part.
	 *
	 * The primary sidebar template part only loads if the layout set includes it. This prevents unnecessary memory usage.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	function sidebar_primary_template() {
		if ( false === stripos( fancify_lite_get_layout(), 'sp' ) || ! fancify_lite_has_widget_area( 'sidebar_primary' ) ) {
			return;
		}

		get_sidebar( 'primary' );
	}

	/**
	 * Echo secondary sidebar template part.
	 *
	 * The secondary sidebar template part only loads if the layout set includes it. This prevents unnecessary memory usage.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	function sidebar_secondary_template() {
		if ( false === stripos( fancify_lite_get_layout(), 'ss' ) || ! fancify_lite_has_widget_area( 'sidebar_secondary' ) ) {
			return;
		}

		get_sidebar( 'secondary' );
	}

	/**
	 * Echo footer partial template part.
	 *
	 * @since 1.3.0
	 *
	 * @return void
	 */
	function footer_partial_template() {
		// Allow overwrite.
		if ( '' !== locate_template( 'footer-partial.php', true, false ) ) {
			return;
		}

		require FANCIFY_STRUCTURE_PATH . 'footer-partial.php';// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- valid usecase
	}

	/**
	 * Echo footer template part.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public static function footer_template() {
		get_footer();
	}

}

Fancify_Lite_Template_Parts::get_instance();
/**
 * Set the content width based on the SplendorStudio default layout.
 *
 * This is mainly added to align to WordPress.org requirements.
 *
 * @since 1.2.0
 *
 * @ignore
 * @access private
 */
if ( ! isset( $content_width ) ) {
	$content_width = 800; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Valid use case.
}