<?php
namespace AtomicSlider;

if ( ! defined( 'ABSPATH' ) )  exit; // Exit if accessed directly

class Utils {

	/**
	 * A list of safe tage for `get_valid_html_tag` method.
	 */
	const ALLOWED_HTML_WRAPPER_TAGS = [
		'article',
		'aside',
		'div',
		'footer',
		'h1',
		'h2',
		'h3',
		'h4',
		'h5',
		'h6',
		'header',
		'main',
		'nav',
		'p',
		'section',
		'span',
	];

	public static function print_html_attributes(array $attributes) {
		echo esc_html(self::render_html_attributes($attributes)); 
	}

	/**
	 * Render html attributes
	 *
	 * @access public
	 * @static
	 * @param array $attributes
	 *
	 * @return string
	 */
	public static function render_html_attributes(array $attributes) {
		$rendered_attributes = [];

		foreach ($attributes as $attribute_key => $attribute_values) {
			if (is_array($attribute_values)) {
				$attribute_values = implode(' ', $attribute_values);
			}

			$rendered_attributes[] = sprintf('%1$s="%2$s"', $attribute_key, esc_attr($attribute_values));
		}

		return implode(' ', $rendered_attributes);
	}

	public static function readable_num( $size ) {
		$l    = substr( $size, -1 );
		$ret  = substr( $size, 0, -1 );
		$byte = 1024;

		switch ( strtoupper( $l ) ) {
			case 'P':
			$ret *= 1024;
			case 'T':
			$ret *= 1024;
			case 'G':
			$ret *= 1024;
			case 'M':
			$ret *= 1024;
			case 'K':
			$ret *= 1024;
		}
		return $ret;
	}

	/**
	 * Validate an HTML tag against a safe allowed list.
	 * @param string $tag
	 * @return string
	 */
	public static function get_valid_html_tag( $tag ) { 
		return in_array( strtolower( $tag ), self::ALLOWED_HTML_WRAPPER_TAGS ) ? $tag : 'div';
	}

	/**
	 * Get placeholder image source.
	 * Retrieve the source of the placeholder image.
	 * @since 5.7.6
	 * @access public
	 * @static
	 * @return string The source of the default placeholder image used by Elementor.
	 */
	public static function get_placeholder_image_src() {
		$placeholder_image = ELEMENTOR_ASSETS_URL . 'images/placeholder.png';
		
		return $placeholder_image;
	}

	/**
	 * Get timezone string.
	 *
	 * Retrieve timezone string from the WordPress database.
	 *
	 * @since 1.0.0
	 * @access public
	 * @static
	 *
	 * @return string Timezone string.
	 */
	public static function get_timezone_string() {
		$current_offset = (float) get_option( 'gmt_offset' );
		$timezone_string = get_option( 'timezone_string' );

		// Create a UTC+- zone if no timezone string exists.
		if ( empty( $timezone_string ) ) {
			if ( $current_offset < 0 ) {
				$timezone_string = 'UTC' . $current_offset;
			} else {
				$timezone_string = 'UTC+' . $current_offset;
			}
		}

		return $timezone_string;
	}

}
