<?php
/**
 * The Fancify Templates API allows to load Fancify template files as well as loading the entire document.
 *
 * @package SplendorStudio\Framework\API\Template
 *
 * @since   1.0.0
 */
class Fancify_Lite_Templates extends Fancify_Lite_Controller {
	/**
	 * Constructor.
	 *
	 * @since 1.0.0
	 */
	function __construct() {}

	/**
	 * Register callbacks for actions and filters
	 *
	 * @since 1.0.0
	 */
	protected function register_hook_callbacks() {}

	/**
	 * Load and render the entire document (web page).
	 *
	 * Here are some guidelines for calling this function:
	 *
	 *      - Call it from a primary template file, e.g. single.php, page.php, home.php, archive.php, etc.
	 *      - Do all modifications and customizations before calling this function.
	 *      - Put this function on the last line of code in the template file.
	 *
	 * @since 1.0.0
	 *
	 * @return void
	 */
	public static function fancify_lite_load_document() {
		/**
		 * Fires before the document is loaded.
		 *
		 * @since 1.0.0
		 */
		do_action('fancify_lite_before_load_document');

		/**
		 * Fires when the document loads.
		 *
		 * This hook is the root of SplendorStudio's framework hierarchy. It all starts here!
		 *
		 * @since 1.0.0
		 */
		do_action('fancify_lite_load_document');

		/**
		 * Fires after the document is loaded.
		 *
		 * @since 1.0.0
		 */
		do_action('fancify_lite_after_load_document');
	}

	/**
	 * Loads a secondary template file.
	 *
	 * This function loads SplendorStudio's default template file. It must be called from a secondary template file
	 * (e.g. comments.php) and must be the last function to be called. All modifications must be done before calling
	 * this function. This includes modifying markup, attributes, fragments, etc.
	 *
	 * The default template files contain the hook on which the fragments are attached to. Bypassing this function
	 * will completely remove the default content.
	 *
	 * @since 1.0.0
	 *
	 * @param string $file The filename of the secondary template files. __FILE__ is usually to argument to pass.
	 *
	 * @return bool True on success, false on failure.
	 */
	public static function fancify_lite_load_default_template($file) {
		$file = FANCIFY_STRUCTURE_PATH . basename($file);
		if (!file_exists($file)) {
			return false;
		}

		require_once $file;// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- valid usecase

		return true;
	}

	/**
	 * Loads a secondary template file.
	 *
	 * This function loads SplendorStudio's template files. It must be called from a secondary template file
	 * (e.g. comments.php) and must be the last function to be called. All modifications must be done before calling
	 * this function. This includes modifying markup, attributes, fragments, etc.
	 *
	 * The default template files contain the hook on which the fragments are attached to. Bypassing this function
	 * will completely remove the default content.
	 *
	 * @since 1.0.0
	 *
	 * @param string $file The filename of the secondary template files. __FILE__ is usually to argument to pass.
	 *
	 * @return bool True on success, false on failure.
	 */
	public static function fancify_lite_load_template($file, $array = NULL) {
		$file = FANCIFY_TEMPLATES_PATH . $file;
		extract($array);
		if (!file_exists($file)) {
			return false;
		}

		require_once $file;// phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound -- valid usecase

		return true;
	}

	/**
	 * Render the current comment's HTML markup.
	 *
	 * This function is a callback that is registered to {@see wp_list_comments()}.  It adds the args and depth to the
	 * global comment, renders the opening <li> tag, and fires the "fancify_lite_comment" event to render the comment.
	 *
	 * @since 1.0.0
	 *
	 * @see   wp_list_comments()
	 *
	 * @param WP_Comment $comment Instance of the current comment, i.e. which is also the global comment.
	 * @param array      $args    Array of arguments.
	 * @param int        $depth   Depth of the comment in reference to its parents.
	 *
	 * @return void
	 */
	public static function fancify_lite_comment_callback($comment, array $args, $depth) {
		// To give us access, add the args and depth as public properties on the comment's global instance.
		global $comment;
		$comment->args  = $args;
		$comment->depth = $depth;

		// Render the opening <li> tag.
		$comment_class = empty($args['has_children']) ? '' : 'parent';
		printf('<li id="comment-%d" %s>',
			(int) get_comment_ID(),
			comment_class($comment_class, $comment, null, false)
		);

		/**
		 * Render the comment's HTML markup.
		 *
		 * @since 1.0.0
		 */
		do_action('fancify_lite_comment');

		// The </li> tag is intentionally omitted.
	}

}
