<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
 * Helper class for AJAX calls within the W2PShop plugins.
 */
defined( 'ABSPATH' ) || exit;

final class W2PShopAjaxHelper {

	/**
	 * Return asset back to the client.
	 * If the request is an AJAX call, return will be echoed otherwise returned as a string
	 *
	 * @return mixed|string
	 */
	public static function get_asset_by_setting( string $setting_name, string $content_type = 'text/html' ) {
		if ( is_ajax() ) {
			ob_start();
			header( 'Content-Type: ' . $content_type );
		}

		$asset_value = W2PShopSettings::get_setting_with_default( $setting_name, '', true );
		if ( is_ajax() ) {
			// We are using this to inject our own JS and CSS files via an AJAX call
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			echo $asset_value;
			// We are using this to inject our own JS and CSS files via an AJAX call
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			echo ob_get_clean();
			wp_die();
		} else {
			// We are using this to inject our own JS and CSS files via an AJAX call
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			return $asset_value;
		}
	}
}
