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

/* スクリプトの読み込み */
function haup_slidein_content_script_init() {
	wp_enqueue_script('haup-slidein-content-js');
}

/** 再利用ブロックIDの有無・モバイルの表示スイッチで判断 **/
if(isset( GetOP08_addcontent()['slidein-content-reuse-id'] ) && !empty( GetOP08_addcontent()['slidein-content-reuse-id'] )){
	if(isset( GetOP08_addcontent()['slidein-content-mobile'] ) && GetOP08_addcontent()['slidein-content-mobile'] == 1 ){
		if(!haup_is_mobile()){
			add_action( 'wp_enqueue_scripts', 'haup_slidein_content_script_init' );
		}
	}else{
		add_action( 'wp_enqueue_scripts', 'haup_slidein_content_script_init' );
	}
}

/***** スライド内のコンテンツ（再利用ブロック）呼び出し *****/
/* コンテンツ取得のコールバック */
/* echo出力するのでコールバック化（一応） */
function haup_get_block_slide_reuse( ) {
	if(isset( GetOP08_addcontent()['slidein-content-reuse-id'] )){
		$reuse_id = absint(GetOP08_addcontent()['slidein-content-reuse-id'] );
	}else{
		$reuse_id = '';
	}

	if(empty($reuse_id)){
		$content ='';
	}else{
		$content_post = get_post( $reuse_id );
		$content = $content_post->post_content;
	}

	return $content;
}

/* フッターへ出力 */
function haup_slide_block_content_footer() {
	//再利用ブロックをコールバック関数から呼び出す
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
	$content = apply_filters( 'the_content', haup_get_block_slide_reuse() );

	//ダッシュアイコン
	//開くボタン
	if(isset( GetOP08_addcontent()['slidein-content-open-button-dashicon'] )){
		$open_button = esc_html(GetOP08_addcontent()['slidein-content-open-button-dashicon']);
	}else{
		$open_button = '';
	}
	
	if(!empty($open_button)){
		$open_button_icon = sanitize_text_field($open_button);
	}else{
		$open_button_icon = sanitize_text_field('icon-hauicon-webmark33');
	}
	//閉じるボタン
	if(isset( GetOP08_addcontent()['slidein-content-close-button-dashicon'] )){
		$close_button = esc_html(GetOP08_addcontent()['slidein-content-close-button-dashicon']);
	}else{
		$close_button = '';
	}

	if(!empty($close_button)){
		$close_button_icon = sanitize_text_field($close_button);
	}else{
		$close_button_icon = sanitize_text_field('icon-hauicon-arrow12');
	}

if(isset( GetOP08_addcontent()['slidein-content-reuse-id'] ) && !empty( GetOP08_addcontent()['slidein-content-reuse-id'] ) ){
echo '<div id="ha-slide-nav-openbtn"  class="'.esc_html($open_button_icon).'"></div>';
echo '<aside>';
echo '<div id="ha-slide-nav">';
echo '<div id="ha-slide-nav-closebtn"  class="'.esc_html($close_button_icon).'"></div>';
echo '<div id="ha-slide-nav-content">';
echo wp_kses_post($content);
echo '</div>';
echo '</div>';
echo '</aside>';

}

}

/** モバイルの表示スイッチで判断 **/
if(isset( GetOP08_addcontent()['disable-slidein-content-mobile'] ) && GetOP08_addcontent()['disable-slidein-content-mobile'] == 1 ){
		if(!haup_is_mobile()){
			add_action('wp_footer','haup_slide_block_content_footer');
		}
	}else{
		add_action('wp_footer','haup_slide_block_content_footer');
	}

/***** PC用のCSSをインライン出力 *****/
function haup_slidein_content_pc_css_inline_init(){

	//ボタンの位置
	if(isset( GetOP08_addcontent()['slidein-content-open-button-align'] )){
		$button_location = GetOP08_addcontent()['slidein-content-open-button-align'];
	}else{
		$button_location = '';
	}
	
	if(empty($button) || $button_location == '1'){
		$location = '
		#ha-slide-nav-openbtn,
		body.admin-bar #ha-slide-nav-openbtn{
			left: 12px;
			}
		';
	}
	if($button_location == '2'){
		$location ='
		@media screen and (max-width: 1365px) {
			#ha-slide-nav-openbtn,
			body.admin-bar #ha-slide-nav-openbtn{
				left: calc( (100vw - var(--wp--style--global--wide-size)) / 2);
			}
		}

		@media only screen and (min-width: 1366px) and (max-width: 1439px) {
			#ha-slide-nav-openbtn,
			body.admin-bar #ha-slide-nav-openbtn{
				left: calc( (100vw - var(--wp--style--global--wide-size)) / 2 - 2rem);
			}
		}

@media only screen and (min-width: 1440px) {
			#ha-slide-nav-openbtn,
			body.admin-bar #ha-slide-nav-openbtn{
				left: calc( (100vw - var(--wp--style--global--wide-size)) / 2 - 4rem);
			}
		}
	';
	}

	//ボタンの色
	if(isset( GetOP08_addcontent()['slidein-content-button-color'] )){
		$button_color = GetOP08_addcontent()['slidein-content-button-color'];
	}else{
		$button_color = '';
	}

	if(!empty($button_color)){
		$button_icon_color = sanitize_hex_color($button_color);
	}else{
		$button_icon_color = sanitize_hex_color('#ffffff');
	}
	//ボタンの背景色
	if(isset( GetOP08_addcontent()['slidein-content-button-bg-color'] )){
		$button_bg_color = GetOP08_addcontent()['slidein-content-button-bg-color'];
	}else{
		$button_bg_color = '';
	}

	if(!empty($button_bg_color)){
		$button_icon_bg_color = sanitize_hex_color($button_bg_color);
	}else{
		$button_icon_bg_color = sanitize_hex_color('#000000');
	}

	//コンテンツの背景色
	if(isset( GetOP08_addcontent()['slidein-content-bg-color'] )){
		$content_bg_color = GetOP08_addcontent()['slidein-content-bg-color'];
	}else{
		$content_bg_color = '';
	}

	if(!empty($content_bg_color)){
		$content_main_bg_color = sanitize_hex_color($content_bg_color);
	}else{
		$content_main_bg_color = sanitize_hex_color('#eeeeee');
	}

	//コンテンツの幅
	if(isset( GetOP08_addcontent()['slidein-content-pc-width'] )){
		$content_width_pc = GetOP08_addcontent()['slidein-content-pc-width'];
	}else{
		$content_width_pc = '';
	}

	if(!empty($content_width_pc)){
		$content_main_width_pc = absint($content_width_pc);
	}else{
		$content_main_width_pc = absint('40');
	}

$custom_css_pre = hau_sanitize_css("
/* HAUP Slide Content PC */
#ha-slide-nav {
	position:fixed;
	z-index: 90;
	top:0;
	left: -120%;
	width:$content_main_width_pc%;
	height: 100vh;
	background:$content_main_bg_color;
	transition: all 0.6s;
	overflow: auto;
}

#ha-slide-nav.panelactive {
	left: 0;
}

#ha-slide-nav-content{
	padding:20px;
	margin-top:40px;
	margin-bottom:20px;
}

body.admin-bar #ha-slide-nav-content{
	margin-top:72px;
}

#ha-slide-nav-openbtn{
	position:fixed;
	z-index: 90;
	top:18px;
	/*left: 12px;*/
	/*left: calc((100vw - 1280px) / 2 - 2rem);*/
	
	cursor: pointer;
	width:auto;
	height:auto;
	background:$button_icon_bg_color;
	border-radius:4px;
	font-size:2rem;
	color: $button_icon_color;
}

#ha-slide-nav-closebtn.active {
	position:fixed;
	z-index: 90;
	top:18px;
	left: 12px;
	cursor: pointer;
	width:auto;
	height:auto;
	background:$button_icon_bg_color;
	border-radius:4px;
	font-size:2rem;
	color: $button_icon_color;
}


body.admin-bar #ha-slide-nav-openbtn{
	top:50px;
	/*left: calc((100vw - 1280px) / 2 - 2rem);*/
}

".$location."

body.admin-bar #ha-slide-nav-closebtn.active {
	top:50px;
	left: 12px;
}

#ha-slide-nav-openbtn.active {
	display:none;
}
");

//タブを除去
$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 );//インラインでCSS出力
}
if(!haup_is_mobile()){
add_action('wp_enqueue_scripts','haup_slidein_content_pc_css_inline_init');
}

/***** モバイル用のCSSをインライン出力 *****/
function haup_slidein_content_mobile_css_inline_init(){

	//ボタンの色
	if(isset( GetOP08_addcontent()['slidein-content-button-color'] )){
		$button_color = GetOP08_addcontent()['slidein-content-button-color'];
	}else{
		$button_color = '';
	}

	if(!empty($button_color)){
		$button_icon_color = sanitize_hex_color($button_color);
	}else{
		$button_icon_color = sanitize_hex_color('#ffffff');
	}
	//ボタンの背景色
	if(isset( GetOP08_addcontent()['slidein-content-button-bg-color'] )){
		$button_bg_color = GetOP08_addcontent()['slidein-content-button-bg-color'];
	}else{
		$button_bg_color = '';
	}

	if(!empty($button_bg_color)){
		$button_icon_bg_color = sanitize_hex_color($button_bg_color);
	}else{
		$button_icon_bg_color = sanitize_hex_color('#000000');
	}

	//コンテンツの背景色
	if(isset( GetOP08_addcontent()['slidein-content-bg-color'] )){
		$content_bg_color = GetOP08_addcontent()['slidein-content-bg-color'];
	}else{
		$content_bg_color = '';
	}

	if(!empty($content_bg_color)){
		$content_main_bg_color = sanitize_hex_color($content_bg_color);
	}else{
		$content_main_bg_color = sanitize_hex_color('#eeeeee');
	}

	//コンテンツの幅
	if(isset( GetOP08_addcontent()['slidein-content-mob-width'] )){
		$content_width_mob = GetOP08_addcontent()['slidein-content-mob-width'];
	}else{
		$content_width_mob = '';
	}

	if(!empty($content_width_mob)){
		$content_main_width_mob = absint($content_width_mob);
	}else{
		$content_main_width_mob = absint('80');
	}

$custom_css_pre = hau_sanitize_css("
/* HAUP Slide Content Mobile */
#ha-slide-nav {
	position:fixed;
	z-index: 90;
	top:0;
	left: -120%;
	width:$content_main_width_mob%;
	height: 100vh;
	background:$content_main_bg_color;
	transition: all 0.6s;
	overflow: auto;
}

#ha-slide-nav.panelactive {
	left: 0;
}

#ha-slide-nav-content {
	padding:20px;
	margin-top:40px;
	margin-bottom:20px;
}

#ha-slide-nav-openbtn ,
#ha-slide-nav-closebtn.active {
	position:fixed;
	z-index: 90;
	top:18px;
	left: 12px;
	cursor: pointer;
	width:auto;
	height:auto;
	background:$button_icon_bg_color;
	border-radius:4px;
	font-size:2rem;
	color: $button_icon_color;
}

#ha-slide-nav-openbtn.active {
	display:none;
}
");

//タブを除去
$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 );//インラインでCSS出力
}
if(isset( GetOP08_addcontent()['disable-slidein-content-mobile'] ) && empty( GetOP08_addcontent()['disable-slidein-content-mobile'] ) && haup_is_mobile() ){
add_action('wp_enqueue_scripts','haup_slidein_content_mobile_css_inline_init');
}