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

/**
* OGPタグ出力ファンクション
* OGPタグをheadタグ内に出力します。
* 
* ■出力するタグ
* ・OGPタグ
* ・Twitterカード
* ・Facebook
*/
function haup_ogp_meta_head_insert(){
global $post;

//Twitterのアカウント名 (@xxx)
	if(isset( GetOP03_optimize()['ogp-twitter-ac-name'] )){
		$twitter_account_name = GetOP03_optimize()['ogp-twitter-ac-name'];
	}else{
		$twitter_account_name = '';
	}

	if(isset( GetOP03_optimize()['ogp-facebook-id'] )){
		$facebook_id = GetOP03_optimize()['ogp-facebook-id'];
	}else{
		$facebook_id = '';
	}

	$twitter_site = $twitter_account_name;

	//カードサイズ
	if(isset( GetOP03_optimize()['ogp-card-size'] )){
		$card_size = GetOP03_optimize()['ogp-card-size'];
	}else{
		$card_size = '';
	}

	if(empty($card_size) || $card_size == '1'){
		$twitter_card = 'summary_large_image';
	}else{
		$twitter_card = 'summary';
	}

	$facebook_app_id = $facebook_id;// Facebook APP ID

	$locale_set = get_bloginfo( 'language' );
		if($locale_set == 'ja'){
			$locale = 'ja_JP';
		}else{
			$locale = get_bloginfo( 'language' );
		}

if(is_home() || is_front_page()){//フロントページと一覧
	$ogp_title = get_bloginfo('name');
	$ogp_description = get_bloginfo('description');
	$ogp_url = home_url();
	$content_type = 'website';

	if(isset( GetOP01_optimize()['ogp-default-image'] )){
		$default_image = GetOP01_optimize()['ogp-default-image'];
	}else{
		$default_image = '';
	}
	
	if(!empty( $default_image )){
		$ogp_image = $default_image;//テーマ画像
	}else{
		$ogp_image = get_template_directory_uri().'/screenshot.png';//テーマ画像
	}
}else if(haup_is_single_without_front()){//個別ページ
	$ogp_description = mb_substr(get_the_excerpt(), 0, 100);
	$ogp_url = get_permalink();
	$content_type = 'article';

	/* タイトル */
	$sch_ctt = get_post_meta($post->ID, 'seo_title', true);
	if(empty($sch_ctt)){ //カスタムフィールドに値がないときの処理
		$ogp_title = get_the_title();
	}else{
		$ogp_title = get_post_meta($post->ID , 'seo_title' ,true);
	}

	/* ディスクリプション */
	$sch_ctdc = esc_html(get_post_meta($post->ID, 'meta_description', true));
	if(empty($sch_ctdc)){ //カスタムフィールドに値がないときの処理
		$ogp_description = mb_substr(get_the_excerpt(), 0, 100);
	}else{
		$ogp_description = get_post_meta($post->ID, 'meta_description', true);
	}

	/* SNSアカウント */
	$author_id = $post->post_author;
	// x
	$author_x_meta = get_the_author_meta('ha-user-x-url',$author_id);
		if(!empty($author_x_meta)){
			$twitter_site = $author_x_meta;
		}else{
			$twitter_site = $twitter_account_name;
		}

	// Facebook
	$author_fb_meta = get_the_author_meta('ha-user-facebook-url',$author_id);
		if(!empty($author_fb_meta)){
			$facebook_app_id = $author_fb_meta;
		}else{
			$facebook_app_id = $facebook_id;
		}



	if (has_post_thumbnail()) {
		$attachment_id = get_post_thumbnail_id();
		$image_attributes = wp_get_attachment_image_src( $attachment_id , 'large',true);
		//$ps_thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'medium',true);
		$ogp_image = $image_attributes[0];
	}elseif(!empty( $default_image )){
		$ogp_image = $default_image;
	}else{
		$ogp_image = get_template_directory_uri().'/screenshot.png';//画像
	}

}else{
	if(isset( $_SERVER["HTTP_HOST"] )){
		$http_host = sanitize_text_field(wp_unslash($_SERVER["HTTP_HOST"]));
	}

	if(isset( $_SERVER["REQUEST_URI"] )){
		$request_url = sanitize_text_field(wp_unslash($_SERVER["REQUEST_URI"]));
	}
	
	$ogp_url=(empty($_SERVER["HTTPS"]) ? "http://" : "https://") . sanitize_text_field(wp_unslash( $http_host )) . sanitize_text_field(wp_unslash( $request_url ));
	$ogp_title = get_bloginfo('name');
	$ogp_description = get_bloginfo('description');
	$content_type = 'article';

	if(!empty( $default_image )){
		$ogp_image = $default_image;//テーマ画像
	}else{
		$ogp_image = get_template_directory_uri().'/screenshot.png';//テーマ画像
	}
}

//OGPタグを出力
echo "\n";
echo '<!-- HAUP OGP TAG -->';
echo "\n";
echo '<meta property="og:title" content="';
echo esc_html($ogp_title);
echo '" />';
echo "\n";
echo '<meta property="og:description" content="';
echo esc_html($ogp_description);
echo '" />';
echo "\n";
echo '<meta property="og:type" content="';
echo esc_html($content_type);
echo '" />';
echo "\n";
echo '<meta property="og:url" content="';
echo esc_url($ogp_url);
echo '" />';
echo "\n";
echo '<meta property="og:image" content="';
echo esc_url($ogp_image);
echo '" />';
echo "\n";
echo '<meta property="og:site_name" content="';
echo esc_html(get_bloginfo('name'));
echo '" />';
echo "\n";
echo '<meta name="twitter:card" content="';
echo esc_html($twitter_card);
echo '" />';
echo "\n";
echo '<meta property="og:locale" content="';
echo esc_html($locale);
echo '" />';
echo "\n";

if ($twitter_site != "") {
	echo '<meta property="twitter:site" content="';
	echo '@'.esc_html($twitter_site);
	echo '">';
	echo "\n";
}

if ($facebook_app_id != "") {
	echo '<meta property="fb:app_id" content="';
	echo esc_html($facebook_app_id);
	echo '">';
	echo "\n";
}


}//end function
add_action('wp_head', 'haup_ogp_meta_head_insert',3);