<?php
/**
 * Loads Fancify fragments.
 *
 * @package SplendorStudio\Framework\Render
 *
 * @since   1.0.0
 */
class Fancify_Lite_Fragments extends Fancify_Lite_Loader {
	
	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	function __construct() {
		Fancify_Lite_Actions::add_smart_action( 'template_redirect', array( $this, 'load_global_fragments' ), 1 );
		Fancify_Lite_Actions::add_smart_action( 'comments_template', array( $this, 'load_comments_fragment' ) );
		Fancify_Lite_Actions::add_smart_action( 'dynamic_sidebar_before', array( $this, 'load_widget_fragment' ), -1 );
		Fancify_Lite_Actions::add_smart_action( 'pre_get_search_form', array( $this, 'load_search_form_fragment' ) );
	}

	// Filter.
	/**
	 * Load global fragments and dynamic views.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	function load_global_fragments() {
		parent::fancify_lite_load_fragment_file( 'footer' );
		parent::fancify_lite_load_fragment_file( 'header' );
		parent::fancify_lite_load_fragment_file( 'menu' );
		parent::fancify_lite_load_fragment_file( 'post-shortcodes' );
		parent::fancify_lite_load_fragment_file( 'post' );
		parent::fancify_lite_load_fragment_file( 'widget-area' );
		parent::fancify_lite_load_fragment_file( 'embed' );
	}

	// Filter.
	/**
	 * Load comments fragments.
	 *
	 * The comments fragments only loads if comments are active to prevent unnecessary memory usage.
	 *
	 * @since 1.0.0
	 *
	 * @param string $template The template filename.
	 *
	 * @return string The template filename.
	 */
	function load_comments_fragment( $template ) {
		if ( empty( $template ) ) {
			return;
		}

		parent::fancify_lite_load_fragment_file( 'comments' );

		return $template;
	}

	/**
	 * Load widget fragments.
	 *
	 * The widget fragments only loads if a sidebar is active to prevent unnecessary memory usage.
	 *
	 * @since 1.0.0
	 *
	 * @return bool True on success, false on failure.
	 */
	function load_widget_fragment() {
		return parent::fancify_lite_load_fragment_file( 'widget' );
	}

	/**
	 * Load search form fragments.
	 *
	 * The search form fragments only loads if search is active to prevent unnecessary memory usage.
	 *
	 * @since 1.0.0
	 *
	 * @return bool True on success, false on failure.
	 */
	function load_search_form_fragment() {
		return parent::fancify_lite_load_fragment_file( 'searchform' );
	}

}

new Fancify_Lite_Fragments;