<?php
/**
 * Class Noesia Cs Template Loader.
 *
 * @package Noesia_Commerce_Suite
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class implementation.
 */
class Noesia_CS_Template_Loader {
	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $template Template.
	 * @param mixed $args Args.
	 */
	public static function render( $template, $args = array() ) {
		$template  = ltrim( sanitize_text_field( $template ), '/\\' );
		$path      = NOESIA_CS_PATH . 'templates/' . $template;

		if ( ! file_exists( $path ) ) {
			return false;
		}

		if ( is_array( $args ) && ! empty( $args ) ) {
			extract( $args, EXTR_SKIP ); // phpcs:ignore WordPress.PHP.DontExtract.extract_extract
		}

		include $path;
		return true;
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $template Template.
	 * @param mixed $args Args.
	 */
	public static function get( $template, $args = array() ) {
		ob_start();
		$rendered  = self::render( $template, $args );
		$content   = ob_get_clean();

		return $rendered ? $content : '';
	}
}
