<?php

namespace CarDealerPress\includes\helpers;


use CarDealerPress\includes\functions\Logger;

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

//https://github.com/WordPress/WordPress/blob/master/wp-includes/kses.php
class Allowed_Html {
	use Logger;

	public $tags;
	public $content;

	function __construct() {
		add_filter( 'wp_kses_allowed_html', array( $this, 'custom_wpkses_post_tags_iframe' ), 10, 2 );
		add_filter( 'wp_kses_allowed_html', array( $this, 'custom_wpkses_post_tags_basics' ), 10, 2 );
		add_filter( 'wp_kses_allowed_html', array( $this, 'custom_wpkses_post_tags_table' ), 10, 2 );
		add_filter( 'wp_kses_allowed_html', array( $this, 'custom_wpkses_post_tags_lists' ), 10, 2 );
		add_filter( 'wp_kses_allowed_html', array( $this, 'custom_wpkses_post_tags_images' ), 10, 2 );

		add_filter( 'safe_style_css', function ( $styles ) {
			$styles[] = 'display';
			return $styles;
		} );
	}

	static $default_attribs = array(
		'id'    => true,
		'class' => true,
		'style' => array(),
		'data'  => true,
		'data-*'  => true,
	);
	static $schema = array(
		'itemprop'  => true,
		'itemtype'  => true,
		'itemscope' => true,
	);

	/**
	 * Add iFrame to allowed wp_kses_post tags
	 *
	 * @param array $tags Allowed tags, attributes, and/or entities.
	 * @param string $context Context to judge allowed tags by. Allowed values are 'post'.
	 *
	 * @return array
	 */
	function custom_wpkses_post_tags_iframe( $tags, $context ) {
		if ( 'post' === $context ) {
			$tags['iframe']   =array_merge( self::$default_attribs, array(
				'src'             => true,
				'height'          => true,
				'width'           => true,
				'frameborder'     => true,
				'allowfullscreen' => true,
				'onload' => true,
				'loading' => true,
			) );
			$tags['noscript'] = array();
			$tags['script']   = array();
		}

		return $tags;
	}

	function custom_wpkses_post_tags_basics( $tags, $context ) {
		if ( 'post' === $context ) {
			$tags['center']     = array();
			$tags['div']      = array_merge( self::$default_attribs, array(
				'hidden'  => true,
				'src'     => true,
				'name'    => true,
				'title'   => true,
				'active'  => true,
				'onclick' => true,
			), self::$schema );
			$tags['form']     = array(
				'action'   => true,
				'class'    => true,
				'id'       => true,
				'name'     => true,
				'onsubmit' => true,
			);
			$tags['input']    = array_merge( self::$default_attribs, array(
				'value'       => true,
				'type'        => true,
				'name'        => true,
				'placeholder' => true,
				'checked'     => true,
				'required'    => true,
				'pattern'     => true,
				'step'        => true,
				'min'         => true,
				'max'         => true,
				'hidden'      => true,
				'disabled'    => true,
			) );
			$tags['fieldset'] = array(
				'class' => true,
			);
			$tags['meta']     = array(
				'content'  => true,
				'name'     => true,
				'property' => true,
			);
			$tags['nav']      = array_merge( self::$default_attribs );
			$tags['p']        = array_merge( self::$default_attribs );
			$tags['section']  = array_merge( self::$default_attribs );
			$tags['style']  = array();
			$tags['span']     = array_merge( self::$default_attribs, array(
				'hidden'  => true,
				'title'   => true,
				'content' => true,
			), self::$schema );
			$tags['textarea'] = array_merge( self::$default_attribs, array(
				'cols'     => true,
				'rows'     => true,
				'disabled' => true,
				'name'     => true,
			) );

			//$tags['script']   = array();
		}

		return $tags;
	}

	function custom_wpkses_post_tags_lists( $tags, $context ) {
		if ( 'post' === $context ) {
			$tags['ul']     = array_merge( self::$default_attribs, array(
				'type' => true,
			) );
			$tags['li']     = array_merge( self::$default_attribs, array(
				'data-*' => true,
			) );
			$tags['select'] = array_merge( self::$default_attribs, array(
				'onchange' => true,
				'multiple' => true,
				'name'     => true,
			) );
			$tags['option'] = array_merge( self::$default_attribs, array(
				'value'    => true,
				'selected' => true,
			) );
			$tags['datalist']    = self::$default_attribs;
		}

		return $tags;
	}

	function custom_wpkses_post_tags_table( $tags, $context ) {
		if ( 'post' === $context ) {
			$tags['table'] = array_merge( self::$default_attribs, array(
				'align'       => true,
				'bgcolor'     => true,
				'border'      => true,
				'cellpadding' => true,
				'cellspacing' => true,
				'rules'       => true,
				'summary'     => true,
				'width'       => true,
				'data-*' => true,
			) );
			$tags['tbody'] = array_merge( self::$default_attribs, array(
				'align'   => true,
				'char'    => true,
				'charoff' => true,
				'valign'  => true,
				'data-*' => true,
			) );
			$tags['td']    = array_merge( self::$default_attribs, array(
				'abbr'    => true,
				'align'   => true,
				'axis'    => true,
				'bgcolor' => true,
				'char'    => true,
				'charoff' => true,
				'colspan' => true,
				'headers' => true,
				'height'  => true,
				'nowrap'  => true,
				'rowspan' => true,
				'scope'   => true,
				'valign'  => true,
				'width'   => true,
				'required' => true,
				'data-*' => true,
				'label'  => true,
				'name'   => true,
				'tag'    => true,
			) );
			$tags['th']    = array_merge(self::$default_attribs, array(
				'abbr'    => true,
				'align'   => true,
				'axis'    => true,
				'bgcolor' => true,
				'char'    => true,
				'charoff' => true,
				'colspan' => true,
				'headers' => true,
				'height'  => true,
				'nowrap'  => true,
				'rowspan' => true,
				'scope'   => true,
				'valign'  => true,
				'width'   => true,
			));
			$tags['tr']    = array_merge( self::$default_attribs, array(
				'label'  => true,
				'name'   => true,
				'tag'    => true,
			) );
		}

		return $tags;
	}

	function custom_wpkses_post_tags_images( $tags, $context ) {
		if ( 'post' === $context ) {
			$tags['img'] = array_merge( self::$default_attribs, array(
				'alt'      => true,
				'align'    => true,
				'border'   => true,
				'height'   => true,
				'hspace'   => true,
				'loading'  => true,
				'longdesc' => true,
				'vspace'   => true,
				'src'      => true,
				'usemap'   => true,
				'width'    => true,
				'title'  => true,
			), self::$schema );

			$tags['svg']  = array(
				'xmlns'   => true,
				'viewbox' => true,
				'width'   => true,
				'height'  => true,
			);
			$tags['path'] = array(
				'd' => true,
			);
		}

		return $tags;
	}

}