<?php if ( !defined( 'ABSPATH' ) ) exit;

//plugin Checkで以下のメッセージが出ないようにするコメント
//Functions declared in the global namespace by a theme/plugin should start with the theme/plugin prefix. Found: "hautility_input_html_js_code_editor".
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
// ここから警告が無視される↓↓

/***** サニタイズ関数 *****/
/* 組み込み */
// 文字列に限定
// sanitize_text_field()

//正の整数に限定
//absint()

//URLに限定
//sanitize_url()

//メールアドレスに限定
//sanitize_email()

//#付のHex色に限定
//sanitize_hex_color()

//#なしのHex色に限定
//sanitize_hex_color_no_hash()

/* チェックボックス */
/* https://github.com/WPTT/code-examples/blob/master/customizer/sanitization-callbacks.php */
if ( !function_exists( 'hau_sanitize_checkbox' ) ){
function hau_sanitize_checkbox( $checked ) {
	// Boolean check.
	return ( ( isset( $checked ) && true == $checked ) ? true : false );
}
}

/* 画像アップロード(URL) */
/* https://gist.github.com/dmtrmrv/495ae39d68ba22fa0a8d2f75b150042e */
if ( !function_exists( 'hau_sanitize_image' ) ){
function hau_sanitize_image( $input ) {
	$filetype = wp_check_filetype( $input );
	if ( $filetype['ext'] && wp_ext2type( $filetype['ext'] ) === 'image' ) {
		return esc_url( $input );
	}
	return '';
}
}

/* CSS */
if ( !function_exists( 'hau_sanitize_css' ) ){
function hau_sanitize_css( $input ) {
	return wp_strip_all_tags( $input );
}
}

/* JS他 エンコード・デコードするだけなので完全ではない */
/* 入力 */
if ( !function_exists( 'hau_sanitize_js_encode' ) ){
function hau_sanitize_js_encode($input){
	return base64_encode($input);
}
}

/* 出力 */
if ( !function_exists( 'hau_sanitize_js_decode' ) ){
function hau_sanitize_js_decode($input){
	return base64_decode($input);
}
}

/* HTMLタグ */
if ( !function_exists( 'hau_sanitize_html_tags' ) ){
function hau_sanitize_html_tags( $input ){
$allowed_tags = array(
	'a' => array(
		'class' => array(),
		'href'  => array(),
		'rel'   => array(),
		'title' => array(),
	),
	'aside' => array(
		'class' => array(),
		'style' => array(),
	),
	'b' => array(),
	'blockquote' => array(
		'cite'  => array(),
	),
	'br' => array(),
	'button' => array(
		'id' => array(),
		'class'  => array(),
		'style' => array(),
	),
	'cite' => array(
		'title' => array(),
	),
	'code' => array(),
	'del' => array(
		'datetime' => array(),
		'title' => array(),
	),
	'dd' => array(),
	'div' => array(
		'id' => array(),
		'class' => array(),
		'title' => array(),
		'style' => array(),
	),
	'dl' => array(),
	'dt' => array(),
	'em' => array(),
	'h1' => array(
		'class' => array(),
		'style' => array(),
	),
	'h2' => array(
		'class' => array(),
		'style' => array(),
	),
	'h3' => array(
		'class' => array(),
		'style' => array(),
	),
	'h4' => array(
		'class' => array(),
		'style' => array(),
	),
	'h5' => array(
		'class' => array(),
		'style' => array(),
	),
	'h6' => array(
		'class' => array(),
		'style' => array(),
	),
	'i' => array(),
	'img' => array(
		'alt'    => array(),
		'class'  => array(),
		'height' => array(),
		'src'    => array(),
		'width'  => array(),
	),
	'li' => array(
		'class' => array(),
		'style' => array(),
	),
	'nav' => array(
		'id' => array(),
		'class' => array(),
		'style' => array(),
	),
	'ol' => array(
		'class' => array(),
		'style' => array(),
	),
	'p' => array(
		'class' => array(),
		'style' => array(),
	),
	'q' => array(
		'cite' => array(),
		'title' => array(),
	),
	'section' => array(
		'id' => array(),
		'class' => array(),
		'style' => array(),
	),
	'span' => array(
		'class' => array(),
		'title' => array(),
		'style' => array(),
	),
	'strong' => array(),
	'ul' => array(
		'class' => array(),
		'style' => array(),
	),
);

	return wp_kses($input, $allowed_tags);
}
}

function allow_tags_list(){
	$tags = __('Tag','hima-art-utility');
	$att = __('Attribute','hima-art-utility');
	$title = __('See list of available tags','hima-art-utility');
	$message = __('The following tags and attributes can be used (frequently used tags are in red)','hima-art-utility');
	$alert = __('*Please note that any other tags or anything that seems to be written incorrectly will be deleted when saving or displaying.','hima-art-utility');
	$alert2 = __('When using attributes, empty ("") designations are removed on save','hima-art-utility');

$content = '
<details class="accordion"  style="margin: 5px 0px 0px 0px;overflow: hidden;">
<summary class="accordion__title" style="cursor: pointer;">'.esc_html($title).'</summary>
<div class="accordion__content">
<p>'.esc_html($message).'<p>
<p style="color:red;font-weight:700;">'.esc_html($alert).'</p>
<p style="color:green;font-weight:700;">'.esc_html($alert2).'</p>
<table class="haup-allow-tags-list">
	<tbody>
		<tr class="haup-allow-tags-list-title">
			<td>'.esc_html($tags).'</td>
			<td>'.esc_html($att).'</td>
			<td>'.esc_html($tags).'</td>
			<td>'.esc_html($att).'</td>
			<td>'.esc_html($tags).'</td>
			<td>'.esc_html($att).'</td>
		</tr>
		<tr>
			<td class="haup-allow-tags-list-red">a</td>
				<td class="haup-allow-tags-list-red">class href rel title</td>
			<td class="haup-allow-tags-list-red">aside</td>
				<td class="haup-allow-tags-list-red">class style</td>
			<td>b</td>
				<td></td>
		</tr>
		<tr>
			<td>blockquote</td>
				<td>cite</td>
			<td>br</td>
				<td></td>
			<td>button</td>
				<td>id class style</td>
		</tr>
		<tr>
			<td>cite</td>
				<td>title</td>
			<td>code</td>
				<td></td>
			<td>del</td>
				<td>datetime title</td>
		</tr>
		<tr>
			<td>dd</td>
				<td></td>
			<td class="haup-allow-tags-list-red">div</td>
				<td class="haup-allow-tags-list-red">id class title style</td>
			<td>dl</td>
				<td></td>
		</tr>
		<tr>
			<td>dt</td>
				<td></td>
			<td>em</td>
				<td></td>
		</tr>
		<tr>
			<td>h1</td>
				<td>class style</td>
			<td class="haup-allow-tags-list-red">h2</td>
				<td class="haup-allow-tags-list-red">class style</td>
			<td class="haup-allow-tags-list-red">h3</td>
				<td class="haup-allow-tags-list-red">class style</td>
		</tr>
		<tr>
			<td class="haup-allow-tags-list-red">h4</td>
				<td class="haup-allow-tags-list-red">class style</td>
			<td>h5</td>
				<td>class style</td>
			<td>h6</td>
				<td>class style</td>
		</tr>
		<tr>
			<td>i</td>
				<td></td>
			<td>img</td>
				<td>alt class height src width</td>
			<td class="haup-allow-tags-list-red">li</td>
				<td class="haup-allow-tags-list-red">class style</td>
		</tr>
		<tr>
			<td>nav</td>
				<td>id class style</td>
			<td class="haup-allow-tags-list-red">ol</td>
				<td class="haup-allow-tags-list-red">class style</td>
			<td class="haup-allow-tags-list-red">p</td>
				<td class="haup-allow-tags-list-red">class style</td>
		</tr>
		<tr>
			<td>q</td>
				<td>cite title</td>
			<td>section</td>
				<td>id class style</td>
			<td class="haup-allow-tags-list-red">span</td>
				<td class="haup-allow-tags-list-red">class title style</td>
		</tr>
		<tr>
			<td>strong</td>
				<td></td>
			<td class="haup-allow-tags-list-red">ul</td>
				<td class="haup-allow-tags-list-red">class style</td>
		</tr>
	</tbody>
</table>
</div>
</details>
';

	return $content;
}

/***** allow_tags_listのスタイル *****/
function haup_allow_tags_list_style_init(){
$custom_css_pre = hau_sanitize_css("
/* HAUP Allow Tags List Style */
table.haup-allow-tags-list{
	width:80%;
	border:2px solid #777;
	border-collapse: collapse;
}

table.haup-allow-tags-list tr,
table.haup-allow-tags-list td{
	border: 1px solid #777;
	border-collapse: collapse;
}

table.haup-allow-tags-list td{
	padding:8px;
}

table.haup-allow-tags-list td:nth-child(1),
table.haup-allow-tags-list td:nth-child(3),
table.haup-allow-tags-list td:nth-child(5){
	width:10%;
	font-weight:500;
}

table.haup-allow-tags-list td:nth-child(2),
table.haup-allow-tags-list td:nth-child(4),
table.haup-allow-tags-list td:nth-child(6){
	border-right:2px solid #777;
	width:20%;
}

table.haup-allow-tags-list tr:nth-child(2n){
	background:#fff;
	color:#777;
}

tr.haup-allow-tags-list-title{
	text-align:center;
}

td.haup-allow-tags-list-red{
	color:red;
	font-weight:700;
	font-size:1.1em;
}
");

//スタイルコードを改行なしで整形する
	$custom_css = str_replace(PHP_EOL, '', $custom_css_pre);

	wp_add_inline_style( 'haup-only-admin-style', $custom_css );//インラインでCSS出力
}
add_action('admin_enqueue_scripts','haup_allow_tags_list_style_init');


/*** サニタイズコールバック関数(配列保存用) ***/
function haumenu_sanitize_options($options) {
	if (!is_array($options)) {
		return [];
	}

	$sanitized_options = [];

	foreach ($options as $key => $value) {
		$sanitized_options[$key] = sanitize_text_field($value);
	}

	return $sanitized_options;
}

/** * wp_kses_post を拡張してSchema.org の属性を許可する ***/
function haup_allow_schema_attributes_for_kses( $allowed_tags, $context ) {
	if ( $context === 'post' ) {
		// すべての許可されたタグに itemprop, itemscope, itemtype を追加
		foreach ( $allowed_tags as $tag => $attributes ) {
			$allowed_tags[ $tag ]['itemprop'] = true;
			$allowed_tags[ $tag ]['itemscope'] = true;
			$allowed_tags[ $tag ]['itemtype'] = true;
			$allowed_tags[ $tag ]['content'] = true; // <meta> タグの content 属性も許可
		}

		// 特に <meta> タグを許可する必要がある場合（すでに許可されている可能性もある）
		if ( ! isset( $allowed_tags['meta'] ) ) {
			$allowed_tags['meta'] = [
				'itemprop'  => true,
				'content'   => true,
			];
		}

		// <link> タグを許可する必要がある場合（すでに許可されている可能性もある）
		if ( ! isset( $allowed_tags['link'] ) ) {
			$allowed_tags['link'] = [
				'itemprop'  => true,
				'href'      => true,
			];
		}
	}

	return $allowed_tags;
}
add_filter( 'wp_kses_allowed_html', 'haup_allow_schema_attributes_for_kses', 10, 2 );

// ここまで警告が無視される↑↑
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound