<?php

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

if ( ! class_exists( 'tipcg_form' ) ) {

	class tipcg_form {

		/**
		 * Returns the HTML elements and attributes generated by this form builder.
		 *
		 * @return array Allowed HTML for wp_kses().
		 */
		public function get_allowed_html() {

			static $allowed_html = array(
				'a'        => array(
					'id'     => true,
					'class'  => true,
					'href'   => true,
					'target' => true,
					'rel'    => true,
				),
				'b'        => array(),
				'br'       => array(),
				'div'      => array(
					'id'    => true,
					'class' => true,
				),
				'form'     => array(
					'action'  => true,
					'method'  => true,
					'enctype' => true,
				),
				'h2'       => array(
					'id'    => true,
					'class' => true,
				),
				'h4'       => array(
					'id'    => true,
					'class' => true,
				),
				'h5'       => array(
					'id'    => true,
					'class' => true,
				),
				'input'    => array(
					'id'                 => true,
					'class'              => true,
					'type'               => true,
					'name'               => true,
					'value'              => true,
					'placeholder'        => true,
					'min'                => true,
					'max'                => true,
					'step'               => true,
					'readonly'           => true,
					'aria-label'         => true,
					'data-suffix'        => true,
					'data-default-color' => true,
				),
				'label'    => array(
					'id'  => true,
					'for' => true,
				),
				'li'       => array(
					'id'    => true,
					'class' => true,
					'value' => true,
				),
				'option'   => array(
					'value'    => true,
					'selected' => true,
				),
				'p'        => array(
					'id'    => true,
					'class' => true,
				),
				'select'   => array(
					'id'    => true,
					'class' => true,
					'name'  => true,
				),
				'span'     => array(
					'id'    => true,
					'class' => true,
				),
				'strong'   => array(),
				'table'    => array(
					'id'          => true,
					'class'       => true,
					'cellspacing' => true,
					'cellpadding' => true,
				),
				'tbody'    => array(
					'id'    => true,
					'class' => true,
				),
				'td'       => array(
					'id'    => true,
					'class' => true,
				),
				'textarea' => array(
					'id'          => true,
					'class'       => true,
					'name'        => true,
					'placeholder' => true,
				),
				'th'       => array(
					'id'    => true,
					'class' => true,
				),
				'tr'       => array(
					'id'    => true,
					'class' => true,
				),
				'ul'       => array(
					'id'    => true,
					'class' => true,
				),
			);

			return $allowed_html;

		}

		/**
		 * ELEMENT START
		 */

		public function elementStart( $tag, $id = false, $class = false ) {
			$id_attr    = ( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
			$class_attr = ( $class ) ? ' class="' . esc_attr( $class ) . '"' : '';

			return '<' . esc_attr( $tag ) . $id_attr . $class_attr . '>';
		}

		/**
		 * ELEMENT END
		 */

		public function elementEnd( $tag ) {

			$html = '</';
			$html .= $tag !== FALSE ? esc_attr($tag) : '' ;
			$html .= '>';

			return $html;

		}

		/**
		 * ELEMENT
		 */

		public function element( $tag, $id = false, $class = false, $content = '' ) {

			$allowed_tags = array(
				'b'      => array(),
				'strong' => array(),
				'br'     => array(),
			);

			$html = '<';
			$html .= $tag !== FALSE ? esc_attr($tag) : '' ;
			$html .= $id !== FALSE ? ' id="' . esc_attr($id) . '" ' : '' ;
			$html .= $class !== FALSE ? ' class="' . esc_attr($class) . '" ' : '' ;
			$html .= '>';
			$html .= $content !== FALSE ? wp_kses( $content, $allowed_tags ) : '';
			$html .= '</';
			$html .= $tag !== FALSE ? esc_attr($tag) : '' ;
			$html .= '>';

			return $html;

			}

		/**
		 * LINK
		 */

		public function link( $url, $id = false, $class = false, $target = false, $rel = false, $content = '' ) {

			$html = '<a ';
			$html .= $url !== FALSE ? ' href="' . esc_url($url) . '" ' : '' ;
			$html .= $id !== FALSE ? ' id="' . esc_attr($id) . '" ' : '' ;
			$html .= $class !== FALSE ? ' class="' . esc_attr($class) . '" ' : '' ;
			$html .= $target !== FALSE ? ' target="' . esc_attr($target) . '" ' : '' ;
			$html .= $rel !== FALSE ? ' rel="' . esc_attr($rel) . '" ' : '' ;
			$html .= '>';
			$html .= $content !== FALSE ? esc_html($content) : '' ;
			$html .= '</a>';

			return $html;

		}

		/**
		 * FORM START
		 */

		public function formStart( $method = 'post', $action = '' ) {
			return '<form enctype="multipart/form-data" method="' . esc_attr( $method ) . '" action="' . esc_url( $action ) . '">';
		}

		/**
		 * FORM END
		 */

		public function formEnd() {
			return '</form>';
		}

		/**
		 * FORM INPUT
		 */

		public function input( $name, $id = false, $class = false, $type = 'text', $value = '', $placeholder = '', $attrs = '' ) {
			$id_attr          = ( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
			$class_attr       = ( $class ) ? ' class="' . esc_attr( $class ) . '"' : '';
			$placeholder_attr = ( '' !== $placeholder ) ? ' placeholder="' . esc_attr( $placeholder ) . '"' : '';
			$attrs            = ( '' !== $attrs ) ? ' ' . wp_kses_data( trim( $attrs ) ) : '';

			return '<input type="' . esc_attr( $type ) . '" name="' . esc_attr( $name ) . '"' . $id_attr . $class_attr . ' value="' . esc_attr( $value ) . '"' . $placeholder_attr . $attrs . ' />';
		}

		/**
		 * TEXTAREA
		 */

		public function textarea( $name, $id = false, $class = false, $value = '', $placeholder = '' ) {
			$id_attr          = ( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
			$class_attr       = ( $class ) ? ' class="' . esc_attr( $class ) . '"' : '';
			$placeholder_attr = ( '' !== $placeholder ) ? ' placeholder="' . esc_attr( $placeholder ) . '"' : '';

			return '<textarea name="' . esc_attr( $name ) . '"' . $id_attr . $class_attr . $placeholder_attr . '>' . esc_textarea( $value ) . '</textarea>';
		}

		/**
		 * SELECT
		 */

		public function select( $name, $id = false, $class = false, $options = array(), $selected = '', $attrs = '' ) {
			$id_attr    = ( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
			$class_attr = ( $class ) ? ' class="' . esc_attr( $class ) . '"' : '';

			$html = '<select name="' . esc_attr( $name ) . '"' . $id_attr . $class_attr;
			$html .= ( $attrs ) ? ' ' . wp_kses_data( trim( $attrs ) ) : '';
			$html .= '>';

			foreach ( $options as $value => $label ) {
				$html .= '<option value="' . esc_attr( $value ) . '"' . selected( $selected, $value, false ) . '>' . esc_html( $label ) . '</option>';
			}

			$html .= '</select>';
			return $html;
		}

		/**
		 * LIST
		 */

		public function htmlList( $type, $id, $class, $values, $current ) {

			$html  = '<' . esc_attr($type);
			$html .= $id !== FALSE ? ' id="' . esc_attr($id) . '"' : '' ;
			$html .= $class !== FALSE ? ' class="' . esc_attr($class) . '"' : '' ;
			$html .= '>';

			foreach ($values as $k => $v ) {
			
				$html .= '<li';
				$html .= (str_replace(' ', '', $k) === $current) ? ' class="ui-state-active"' : '' ;
				$html .= ' value="' . esc_attr( $k ) . '"';
				$html .= '>';
				
				if (0 === strpos($k, 'http')) {
					$html .= $this->link($k, FALSE, FALSE, '_BLANK', FALSE, $v);
				} else {
					$html .= $this->link(esc_url('admin.php?page=tipcg_panel&tab=' . str_replace(' ', '', $k)), FALSE, FALSE, '_SELF', FALSE, $v);
				}
				
				$html .= '</li>';
			}
			
			$html .= '<li class="clear"></li>';
			$html .= '</' . esc_attr($type) . '>';

			return $html;

		}

		/**
		 * LABEL
		 */

		public function label($id, $text) {

			$html  = '<label';
			$html .= $id !== FALSE ? ' id="' . esc_attr($id) . '_label"' : '' ;
			$html .= $id !== FALSE ? ' for="' . esc_attr($id) . '"' : '' ;
			$html .= '>';
			$html .= $text !== FALSE ? esc_html($text) : '' ;
			$html .= '</label>';

			return $html;

		}

		/**
		 * TABLE START
		 */

		public function tableStart($id, $class, $cellspacing, $cellpadding) {

			$html  = '<table ';
			$html .= $id !== FALSE ? ' id="' . esc_attr($id) . '"' : '' ;
			$html .= $class !== FALSE ? ' class="' . esc_attr($class) . '"' : '' ;
			$html .= $cellspacing !== FALSE ? ' cellspacing="' . esc_attr($cellspacing) . '"' : '' ;
			$html .= $cellpadding !== FALSE ? ' cellpadding="' . esc_attr($cellpadding) . '"' : '' ;
			$html .= '>';

			return $html;

		}

		/**
		 * TABLE END
		 */

		public function tableEnd() {

			return '</table>';

		}

		/**
		 * TABLE ELEMENT
		 */

		public function tableElement($name, $id, $class) {

			$html  = '<'.esc_attr($name).' ';
			$html .= $id !== FALSE ? ' id="' . esc_attr($id) . '"' : '' ;
			$html .= $class !== FALSE ? ' class="' . esc_attr($class) . '"' : '' ;
			$html .= '>';

			$html .= '</'.esc_attr($name).'>';

			return $html;

		}

		/**
		 * TABLE ELEMENT START
		 */

		public function tableElementStart($name, $id, $class) {

			$html  = '<'.esc_attr($name).' ';
			$html .= $id !== FALSE ? ' id="' . esc_attr($id) . '"' : '' ;
			$html .= $class !== FALSE ? ' class="' . esc_attr($class) . '"' : '' ;
			$html .= '>';

			return $html;

		}

		/**
		 * TABLE ELEMENT END
		 */

		public function tableElementEnd($name) {

			return '</'.esc_attr($name).'>';

		}

	}

}

?>
