<?php if ( !defined( 'ABSPATH' ) ) exit;//PHPの宣言と直接アクセスの拒否

/* https://www.benjaminslap.be/make-your-own-cookie-bar-with-jquery/ */

/***** スクリプトの読み込み *****/
function haup_cookie_consent_js_init(){
	wp_enqueue_script('haup-jquery-cookie-js'); 
	wp_enqueue_script('haup-cookie-consent-js');
}
add_action('wp_enqueue_scripts','haup_cookie_consent_js_init');

/***** フッターへコンテンツを出力 *****/
function haup_cookie_consent_content_in_footer(){
//メッセージの変数化
	//プライバシーポリシーページへのリンク
	$privacy_text_before = __( 'For more information, please refer to the linked page below.', 'hima-art-utility' );
	$privacy_link = get_the_privacy_policy_link();
	$link_html = '&emsp;-&nbsp;'.$privacy_link.'&nbsp;-&emsp;';
	$accepted_message = esc_html(__( 'By clicking, this guide will be hidden for about one year.', 'hima-art-utility' ));

	/*** Cookie利用のメッセージ ***/
	//代替文字列入力されたもの
	if(isset( GetOP10_addcontent()['gdpr-cookie-alt-text'] )){
		$cookie_alttext = sanitize_text_field(GetOP10_addcontent()['gdpr-cookie-alt-text']);
	}else{
		$cookie_alttext = '';
	}

	if(empty($cookie_alttext)){
		$cookie_message = esc_html(__( 'This site uses cookies to improve the convenience of the site.', 'hima-art-utility' ));
	}else{
		$cookie_message = esc_html($cookie_alttext);
	}

	/*** フォーム入力時の情報取得メッセージ ***/
	//代替文字列入力されたもの
		if(isset( GetOP10_addcontent()['gdpr-input-form-alt-text'] )){
		$form_alttext = sanitize_text_field(GetOP10_addcontent()['gdpr-input-form-alt-text']);
	}else{
		$form_alttext = '';
	}

	if(empty($form_alttext)){
		$input_message = esc_html(__( 'Various information may be saved at the same time as the content entered from the form', 'hima-art-utility' ));
	}else{
		$input_message = esc_html($form_alttext);
	}
	//出力する内容をまとめる
	if(isset( GetOP10_addcontent()['gdpr-show-get-input-info'] )){
		$form_output = GetOP10_addcontent()['gdpr-show-get-input-info'];
	}else{
		$form_output = '';
	}

	if($form_output == '1'){
		if(!haup_is_mobile()){
			$input_message_li ='<li>'.$input_message.'</li>';
		}else{
			$input_message_li ='';
		}
	}

	/*** 広告使用のメッセージ ***/
	//代替文字列入力されたもの
	if(isset( GetOP10_addcontent()['gdpr-input-ad-ana-alt-text'] )){
		$add_alttext = sanitize_text_field(GetOP10_addcontent()['gdpr-input-ad-ana-alt-text']);
	}else{
		$add_alttext = '';
	}

	if(empty($add_alttext)){
		$ad_message = esc_html(__( 'In addition, some information is provided to external services for access analysis and advertisement display.', 'hima-art-utility' ));
	}else{
		$ad_message = esc_html($add_alttext);
	}
	
	//出力する内容をまとめる
	if(isset( GetOP10_addcontent()['gdpr-show-provision-to-third-parties'] )){
		$add_output = GetOP10_addcontent()['gdpr-show-provision-to-third-parties'];
	}else{
		$add_output = '';
	}

	if($add_output == '1'){
		if(!haup_is_mobile()){
			$ad_message_li ='<li>'.$ad_message.'</li>';
		}else{
			$ad_message_li ='';
		}
	}

	//了承ボタン
	$privacy_button = esc_html(__( 'I have read and understand the content', 'hima-art-utility' ));

	//閉じるボタン
	if(isset( GetOP10_addcontent()['gdpr-show-close-button'] )){
		$close_button_show = absint(GetOP10_addcontent()['gdpr-show-close-button']);
	}else{
		$close_button_show = '';
	}

	if($close_button_show == '1'){
		if(!haup_is_mobile()){
			$close_button = esc_html(__( 'Close', 'hima-art-utility' ));
		}else{
			$close_button = 'X';
		}
	
	$close_content = hau_sanitize_html_tags('<div class="gdpr-close-button">
	<button class="gdpr-button-close">'.$close_button.'</button>
	</div>');
	}

	//プライバシーポリシーページへのリンク
	if(!haup_is_mobile()){
		$privacy_page = '<li>'.$privacy_text_before.$link_html.'</li>';
	}else{
		$privacy_page = '<li>'.$link_html.'</li>';
	}

$string =hau_sanitize_html_tags('<aside><div id="gdpr-box" class="cookies-accept"><div class="gdpr-container">'.$close_content.'
			<div class="gdpr-content">
				<ul class="gdpr-message">
					<li>'.$cookie_message.'</li>
					'.$input_message_li.$ad_message_li.$privacy_page.'
				</ul>
				<div class="gdpr-button-content">
					<button class="gdpr-button-accept">'.$privacy_button.'</button>
					<p class="gdpr-micro-text">'.$accepted_message.'</p>
			</div>
		</div>
</div></div></aside>');

$gdpr_content = preg_replace("/\s+/", " ", $string);

echo wp_kses_post($gdpr_content);
}
add_action('wp_footer','haup_cookie_consent_content_in_footer');

/***** インラインでスタイルを出力 *****/
function haup_cookie_consent_style_inline_init(){

/* 変数指定 */
//背景色
	if(isset( GetOP10_addcontent()['gdpr-bg-color'] )){
		$background_option = GetOP10_addcontent()['gdpr-bg-color'];
	}else{
		$background_option = '';
	}

	if ( empty( $background_option ) ) {
		$background = sanitize_hex_color('#000000');
	}else{
		$background = sanitize_hex_color($background_option);
	}

//背景の透明度
	if(isset( GetOP10_addcontent()['gdpr-bg-transparency'] )){
		$background_opacity_option = GetOP10_addcontent()['gdpr-bg-transparency'];
	}else{
		$background_opacity_option = '';
	}

	if ( empty( $background_opacity_option ) ) {
		$background_opa = absint('90');
	}else{
		$background_opa = absint($background_opacity_option);
	}

//文字色・リンク色
	if(isset( GetOP10_addcontent()['gdpr-text-color'] )){
		$text_link_color_option = GetOP10_addcontent()['gdpr-text-color'];
	}else{
		$text_link_color_option = '';
	}

	if ( empty( $text_link_color_option ) ) {
		$text_link_color = sanitize_hex_color('#ffffff');
	}else{
		$text_link_color = sanitize_hex_color($text_link_color_option);
	}

//ボタン背景色
	if(isset( GetOP10_addcontent()['gdpr-button-color'] )){
		$button_bg_color_option = GetOP10_addcontent()['gdpr-button-color'];
	}else{
		$button_bg_color_option = '';
	}

	if ( empty( $button_bg_color_option ) ) {
		$button_bg_color = sanitize_hex_color('#555555');
	}else{
		$button_bg_color = sanitize_hex_color($button_bg_color_option);
	}

//ボタン枠線色
	if(isset( GetOP10_addcontent()['gdpr-button-border-color'] )){
		$button_border_color_option = GetOP10_addcontent()['gdpr-button-border-color'];
	}else{
		$button_border_color_option = '';
	}

	if ( empty( $button_border_color_option ) ) {
		$button_border_color = sanitize_hex_color('#ffffff');
	}else{
		$button_border_color = sanitize_hex_color($button_border_color_option);
	}

//ボタン文字色
	if(isset( GetOP10_addcontent()['gdpr-button-text-color'] )){
		$button_text_color_option = GetOP10_addcontent()['gdpr-button-text-color'];
	}else{
		$button_text_color_option = '';
	}

	if ( empty( $button_text_color_option ) ) {
		$button_text_color = sanitize_hex_color('#ffffff');
	}else{
		$button_text_color = sanitize_hex_color($button_text_color_option);
	}

//ホーバー時のボタン背景色
	if(isset( GetOP10_addcontent()['gdpr-button-color-hover'] )){
		$button_bg_color_hover_option = GetOP10_addcontent()['gdpr-button-color-hover'];
	}else{
		$button_bg_color_hover_option = '';
	}

	if ( empty( $button_bg_color_hover_option ) ) {
		$button_bg_color_hover = sanitize_hex_color('#000000');
	}else{
		$button_bg_color_hover = sanitize_hex_color($button_bg_color_hover_option);
	}

//ホーバー時のボタン枠線色
	if(isset( GetOP10_addcontent()['gdpr-button-border-color-hover'] )){
		$button_border_color_hover_option = GetOP10_addcontent()['gdpr-button-border-color-hover'];
	}else{
		$button_border_color_hover_option = '';
	}

	if ( empty( $button_border_color_hover_option ) ) {
		$button_border_color_hover = sanitize_hex_color('#ffffff');
	}else{
		$button_border_color_hover = sanitize_hex_color($button_border_color_hover_option);
	}

//ホーバー時のボタン文字色
	if(isset( GetOP10_addcontent()['gdpr-button-text-color-hover'] )){
		$button_text_color_hover_option = GetOP10_addcontent()['gdpr-button-text-color-hover'];
	}else{
		$button_text_color_hover_option = '';
	}

	if ( empty( $button_text_color_hover_option ) ) {
		$button_text_color_hover = sanitize_hex_color('#ffffff');
	}else{
		$button_text_color_hover = sanitize_hex_color($button_text_color_hover_option);
	}

//「閉じる」ボタン背景色
	if(isset( GetOP10_addcontent()['gdpr-close-button-color'] )){
		$close_button_bg_color_option = GetOP10_addcontent()['gdpr-close-button-color'];
	}else{
		$close_button_bg_color_option = '';
	}

	if ( empty( $close_button_bg_color_option ) ) {
		$close_button_bg_color = sanitize_hex_color('#eee');
	}else{
		$close_button_bg_color = sanitize_hex_color($close_button_bg_color_option);
	}

//「閉じる」ボタン文字色
	if(isset( GetOP10_addcontent()['gdpr-close-button-text-color'] )){
		$close_button_text_color_option = GetOP10_addcontent()['gdpr-close-button-text-color'];
	}else{
		$close_button_text_color_option = '';
	}

	if ( empty( $close_button_text_color_option ) ) {
		$close_button_text_color = sanitize_hex_color('#000000');
	}else{
		$close_button_text_color = sanitize_hex_color($close_button_text_color_option);
	}

//ホーバー時の「閉じる」ボタン背景色
	if(isset( GetOP10_addcontent()['gdpr-close-button-color-hover'] )){
		$close_button_bg_color_option_hover = GetOP10_addcontent()['gdpr-close-button-color-hover'];
	}else{
		$close_button_bg_color_option_hover = '';
	}

	if ( empty( $close_button_bg_color_option_hover ) ) {
		$close_button_bg_color_hover = sanitize_hex_color('#000000');
	}else{
		$close_button_bg_color_hover = sanitize_hex_color($close_button_bg_color_option_hover);
	}

//ホーバー時の「閉じる」ボタン文字色
	if(isset( GetOP10_addcontent()['gdpr-close-button-text-color-hover'] )){
		$close_button_text_color_option_hover = GetOP10_addcontent()['gdpr-close-button-text-color-hover'];
	}else{
		$close_button_text_color_option_hover = '';
	}

	if ( empty( $close_button_text_color_option_hover ) ) {
		$close_button_text_color_hover = sanitize_hex_color('#ffffff');
	}else{
		$close_button_text_color_hover = sanitize_hex_color($close_button_text_color_option_hover);
	}

//パソコン時（タブレット除く）の横幅指定
	if(isset( GetOP10_addcontent()['cookie-consent-width-fit'] )){
		$fit_content = absint(GetOP10_addcontent()['cookie-consent-width-fit']);
	}else{
		$fit_content = '';
	}

if($fit_content == '1'){
	if(!wp_is_mobile()){
		$pc_container_width = '
		.gdpr-container {
			width: var(--wp--style--global--wide-size);
			margin: 0 auto;
		}
	';
	}else{
		$pc_container_width = '';
	}
}else{
	$pc_container_width = '';
}


/* CSS */
$custom_css_pre = hau_sanitize_css("
/* HAUP Cookie Consent */
#gdpr-box {
	display: none;
	width: 100%;
	background: ".$background.$background_opa.";
	color: ".$text_link_color.";
	position: fixed;
	bottom: 0;
	z-index: 1;
	font-size:0.8rem;
}

#gdpr-box a {
	color: ".$text_link_color.";
}

#gdpr-box button.gdpr-button-accept {
	background: none;
	color: ".$button_text_color.";
	font-size: 12px;
}

".$pc_container_width."

.gdpr-content {
	width: 80%;
	margin: 0 auto;
}

ul.gdpr-message {
	list-style: none;
	text-align: center;
}

	.gdpr-button-content {
	text-align: center;
}

p.gdpr-micro-text {
	margin: 0.2rem;
	font-size: 0.7rem;
}

.gdpr-button-close {
cursor: pointer;
background: ".$close_button_bg_color.";
color: ".$close_button_text_color.";
border-radius:4px;
}

.gdpr-button-close:hover {
cursor: pointer;
background: ".$close_button_bg_color_hover.";
color: ".$close_button_text_color_hover.";
}

.gdpr-close-button {
	padding: 8px;
	position: absolute;
}

button.gdpr-button-close {
	padding: 4px;
}

#gdpr-box button.gdpr-button-accept,
#gdpr-box a.gdpr-button-settings {
	display: inline-block;
	border: 1px solid ".$button_border_color.";
	background: ".$button_bg_color.";
	padding: 6px 5px 4px;
	text-decoration: none;
	border-radius: 2px;
	font-weight: 800;
	font-size: 12px;
	-webkit-transition: all .5s;
	-moz-transition: all .5s;
	-o-transition: all .5s;
	transition: all .5s;
}

#gdpr-box button.gdpr-button-accept:hover {
	cursor: pointer;
}

#gdpr-box button.gdpr-button-accept:hover,
#gdpr-box a.gdpr-button-settings:hover {
	background: ".$button_bg_color_hover.";
	border:1px solid ".$button_border_color_hover.";
	color:".$button_text_color_hover.";
}
");

//タブを除去
$custom_css_deltab = preg_replace('/\t+/', '', $custom_css_pre);
//改行を除去
$custom_css = str_replace(PHP_EOL, '', $custom_css_deltab);

//インラインで出力
	wp_add_inline_style( 'haup-front-and-blocks-style', $custom_css );
}
add_action('wp_enqueue_scripts','haup_cookie_consent_style_inline_init');

/*** Cookie同意コンテンツをテストモードにする ***/
/* https://www.wpbeginner.com/wp-tutorials/how-to-set-get-and-delete-wordpress-cookies-like-a-pro/ */
/* https://wpmudev.com/blog/set-get-delete-cookies/ */
function haup_cookie_consent_sandbox(){
unset( $_COOKIE['haup_cookiebar'] );
setcookie( 'haup_cookiebar', '', time() - ( 15 * 60 ) );
}

if(isset( GetOP10_addcontent()['is-enable-test-mode'] ) && GetOP10_addcontent()['is-enable-test-mode'] == 1 ){
add_action('init','haup_cookie_consent_sandbox');
}