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

/***** 再利用ブロックの内容を出力 *****/
/*** ショートコードプログラムを無効にしている場合にも有効になるよう追加 ***/
if ( !function_exists( 'haup_get_block' ) ){
function haup_get_block( $id ) {
	$content_post = get_post( $id );
	$content = $content_post->post_content;
	return $content;
}
}

/* haup_display_block( 再利用ブロックのID );でテンプレート内出力 */
if ( !function_exists( 'haup_display_block' ) ){
function haup_display_block( $id ) {
	//echo apply_filters( 'the_content', haup_get_block( $id ) );
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
	$content = apply_filters( 'the_content', haup_get_block( $id ) );
	return $content;
}
}

/* スクリプトの読み込み */
function haup_sticky_header_script_include() {
	if(!haup_is_mobile()){
	wp_enqueue_script( 'haup-sticky-header-js');


//PHPからJSへ値を渡す
if(isset( GetOP04_addcontent()['sticky-header-start-position'] )){
	$start_position = GetOP04_addcontent()['sticky-header-start-position'];
}else{
	$start_position = '';
}

if(isset( GetOP04_addcontent()['sticky-header-fadein-speed'] )){
	$fadein_speed = GetOP04_addcontent()['sticky-header-fadein-speed'];
}else{
	$fadein_speed = '';
}

if(isset( GetOP04_addcontent()['sticky-header-fadeout-speed'] )){
	$fadeout_speed = GetOP04_addcontent()['sticky-header-fadeout-speed'];
}else{
	$fadeout_speed = '';
}


	if(empty($start_position)){
		$sposition = '200';
	}else{
		$sposition = $start_position;
	}

	if(empty($fadein_speed)){
		$inspeed = '400';
	}else{
		$inspeed = $fadein_speed;
	}

	if(empty($fadeout_speed)){
		$outspeed = '400';
	}else{
		$outspeed = $fadeout_speed;
	}

		$localize_val = array(
		'scroll_fadein' => $inspeed,
		'scroll_fadeout' => $outspeed,
		'top_distance' => $sposition
		);

		wp_localize_script('haup-sticky-header-js', 'StickHeaderVal', $localize_val);

}
}
add_action( 'wp_enqueue_scripts', 'haup_sticky_header_script_include' );

/***** コンテンツをフッターへ出力 *****/
function haup_header_content_init(){
	//固定ヘッダー開始タグ
	$sheader_content_before = '<div class="ha-sticky-header is_fix">
	<div class="ha-sticky-header-inner">';

	//再利用ブロック指定なしの場合のコンテンツ
	$logo = get_custom_logo();
	$home_url = get_home_url( '/' );
	
	//タイトルを設定
	if(isset( GetOP04_addcontent()['sticky-header-s-title-alt-text'] ) && !empty( GetOP04_addcontent()['sticky-header-s-title-alt-text'] )){
		$bloginfo_name = GetOP04_addcontent()['sticky-header-s-title-alt-text'];
	}else{
		$bloginfo_name = get_bloginfo( 'name' );
	}

	//キャッチフレーズを設定
	if(isset( GetOP04_addcontent()['sticky-header-s-descri-alt-text'] ) && !empty( GetOP04_addcontent()['sticky-header-s-descri-alt-text'] )){
		$bloginfo_des = GetOP04_addcontent()['sticky-header-s-descri-alt-text'];
	}else{
		$bloginfo_des = get_bloginfo( 'description' );
	}

	$default_content = '
	<div class="haup-sheader-parent">
		<div class="inner-left">'.$logo.'<a href="'.$home_url.'">'.$bloginfo_name.'</a></div>
		<div class="inner-right"><a href="'.$home_url.'">'.$bloginfo_des.'</a></div>
	</div>
	';

	//設定画面にある再利用ブロックのIDを取得
	if(isset( GetOP04_addcontent()['sticky-header-reuse-id'] )){
		$reuse_id = GetOP04_addcontent()['sticky-header-reuse-id'];
	}else{
		$reuse_id = '';
	}

	//設定画面の再利用ブロックIDの有無によってコンテンツを切り替え
	if(isset( GetOP04_addcontent()['sticky-header-reuse-id'] ) && !empty( GetOP04_addcontent()['sticky-header-reuse-id'] )){
		$sheader_content = haup_display_block($reuse_id);
	}else{
		$sheader_content = $default_content;
	}

	//固定ヘッダー終了タグ
	$sheader_content_after = '</div></div>';



	//コンテンツを出力
	echo wp_kses_post($sheader_content_before.$sheader_content.$sheader_content_after);
}
if(!haup_is_mobile()){
add_action('wp_footer','haup_header_content_init');
}

/***** スタイルをインライン出力 *****/
function haup_sticky_header_vstyle_init(){
	//表示幅
	if(isset( GetOP04_addcontent()['fix-header-contents-width'] )){
		$width = GetOP04_addcontent()['fix-header-contents-width'];
	}else{
		$width = '';
	}

	if(empty($width) || $width == '1'){
		$width_style = '
	.ha-sticky-header-inner {
		margin: 10px 20px;
	}';
	}else{
		$width_style = '
	.ha-sticky-header-inner {
		margin: 0 auto;
		width: var(--wp--style--global--wide-size);
		padding: 20px 10px;
	}
		';
	}

	//リンクの文字色
	if(isset( GetOP04_addcontent()['sticky-header-contents-a-color'] ) && !empty( GetOP04_addcontent()['sticky-header-contents-a-color'] )){
		$link_color = GetOP04_addcontent()['sticky-header-contents-a-color'] ;
	}else{
		$link_color = '#ffffff';
	}

	//タイトルの大きさ
	if(isset( GetOP04_addcontent()['sticky-header-title-text-size'] ) && !empty( GetOP04_addcontent()['sticky-header-title-text-size'] )){
		$title_size = GetOP04_addcontent()['sticky-header-title-text-size'] ;
	}else{
		$title_size = '1.2';
	}

	//キャッチフレーズの大きさ
	if(isset( GetOP04_addcontent()['sticky-header-catch-text-size'] ) && !empty( GetOP04_addcontent()['sticky-header-catch-text-size'] )){
		$catch_size = GetOP04_addcontent()['sticky-header-catch-text-size'] ;
	}else{
		$catch_size = '0.9';
	}

	//サイトアイコンの大きさ
	if(isset( GetOP04_addcontent()['sticky-header-site-icon-size'] ) && !empty( GetOP04_addcontent()['sticky-header-site-icon-size'] )){
		$icon_size = GetOP04_addcontent()['sticky-header-site-icon-size'] ;
	}else{
		$icon_size = '0.8';
	}

	//背景色
	if(isset( GetOP04_addcontent()['sticky-header-bg-color'] ) && !empty( GetOP04_addcontent()['sticky-header-bg-color'] )){
		$background_color = GetOP04_addcontent()['sticky-header-bg-color'] ;
	}else{
		$background_color = '#000000';
	}

	//コンテンツの透明度
	if(isset( GetOP04_addcontent()['sticky-header-bg-opacity'] ) && !empty( GetOP04_addcontent()['sticky-header-bg-opacity'] )){
		$back_trans = GetOP04_addcontent()['sticky-header-bg-opacity'] ;
	}else{
		$back_trans = '80';
	}

	//オフセット
	if(isset( GetOP04_addcontent()['sticky-header-scroll-offset-val'] )){
		$offset_value = GetOP04_addcontent()['sticky-header-scroll-offset-val'];
	}else{
		$offset_value = '';
	}

	if(isset( GetOP04_addcontent()['use-sticky-header_offset'] )){
		$enable_offset = GetOP04_addcontent()['use-sticky-header_offset'];
	}else{
		$enable_offset = '';
	}

	if(isset( GetOP04_addcontent()['use-sticky-header'] )){
		$enable_fix_header = GetOP04_addcontent()['use-sticky-header'];
	}else{
		$enable_fix_header = '';
	}

	$offset_style = '';

	if(!haup_is_mobile()){
		if($enable_fix_header == '1' && $enable_offset  == '1'){
			if(empty($offset_value)){
				$offset_style = 'h2, h3, h4, h5, h6 {scroll-margin-top: 100px;}';
			}else{
				$offset_style = 'h2, h3, h4, h5, h6 {scroll-margin-top: '.$offset_value.'px;}';
			}
		}
	}

$custom_css_pre = hau_sanitize_css('
/* HAUP Sticky Header */
.ha-sticky-header{
	display:none;
}

.is_fix{
	position:fixed;
	top: 0;
	margin: 0 calc(50% - 50vw);
	padding: 0;
	width:100vw;
	z-index:30;
}

.haup-sheader-parent{
	display: flex;
	justify-content: space-between;
	align-items: baseline;
	padding: 4px 40px;
}

.ha-sticky-header img.custom-logo {
	width: '.$icon_size.'rem;
	height: '.$icon_size.'rem;
	margin-right: 0.5em;
}

.inner-left a{
	font-size:'.$title_size.'rem;
	text-decoration:none;
}

.inner-right a{
	font-size:'.$catch_size.'rem;
	text-decoration:none;
}

.is_fix{
	background:'.$background_color.$back_trans.'
}

'.$width_style.'


.ha-sticky-header-inner a {
	color:'.$link_color.'!important;
	text-decoration:none;
}

.inner-left a{
	color:'.$link_color.';
}

.inner-right a{
	color:'.$link_color.';
}

'.$offset_style.'
');

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

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

/* 管理バーがある場合に表示をずらす */
function haup_sticky_header_admin_bar(){
//スタイルコードをまとめる
$custom_css_pre = hau_sanitize_css('
/* HAUP Has Front Adminbar Margin For Sticky Header */
.ha-sticky-header{
margin-top:32px!important;
}
');

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

//インラインで出力
if ( is_admin_bar_showing() && !haup_is_mobile()) {
	wp_add_inline_style( 'haup-front-and-blocks-style', $custom_css );
}
}
add_action('wp_enqueue_scripts','haup_sticky_header_admin_bar');