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

/*** パソコンでは小さい画像、モバイルでは大きい画像のカードを表示 ***/
function haup_set_embed_thumbnail_image_shape() {
	if(!haup_is_mobile()){
		return esc_html('square');
	}else{
		return esc_html('rectangular');
	}
}

if(isset( GetOP03_blockoption()['card-size-unity'] ) && GetOP03_blockoption()['card-size-unity'] == 1 ){
add_filter( 'embed_thumbnail_image_shape', 'haup_set_embed_thumbnail_image_shape' );
}

/*** 出力する画像サイズを「中」に統一 ***/
function haup_set_embed_thumbnail_image_size() {
  return esc_html('medium');
}
if(isset( GetOP03_blockoption()['image-size-unity'] ) && GetOP03_blockoption()['image-size-unity'] == 1 ){
add_filter( 'embed_thumbnail_image_size', 'haup_set_embed_thumbnail_image_size' );
}

/*** ホーバー時に拡大する（出力する画像サイズを「中」に統一した場合のみ） ***/
function haup_embed_thumbnail_image_hover_style(){
if(isset( GetOP03_blockoption()['image-size-unity'] )){
	$image_square = GetOP03_blockoption()['image-size-unity'];
}else{
	$image_square = '';
}

if(isset( GetOP03_blockoption()['image-expand-on-hover'] )){
	$image_hover = GetOP03_blockoption()['image-expand-on-hover'];
}else{
	$image_hover = '';
}

if($image_square == '1' && $image_hover == '1'){
if(!haup_is_mobile()){
?>
<style>
.wp-embed-featured-image.square {
    margin-right: unset;
    width: 160px;
overflow:hidden;
}

.wp-embed-featured-image {
    margin-bottom: unset;
}

.wp-embed-excerpt {
    margin-left: 180px;
}

.wp-embed-footer {
    margin-top: 50px;
}

.wp-embed-featured-image.square img{
	transition: 1s all;
}

.wp-embed-featured-image.square img:hover{
	transform: scale(1.1,1.1);
	transition: 1s all;
}
</style>
<?php
}
}
}
add_action('embed_head','haup_embed_thumbnail_image_hover_style');

/*** 抜粋の長さを変更 ***/
/* 手動抜粋文を入力していない場合のみ指示した文字数を切り出すことに注意 */
function haup_change_embed_excerpt_length( $length ) {
	$options = GetOP03_blockoption();

	// GetOP03_blockoption() が配列であることを確認
	if ( !is_array( $options ) || !isset( $options['link-card-change-excerpt-length'] ) ) {
			return $length; // デフォルトの55文字
	}

	$option = $options['link-card-change-excerpt-length'];

	if ( !is_embed() ) {
		return $length; // デフォルトの55文字
	}

	// もしオプションが空でなければ、その値を返す
	if(empty( $option )){
		return $length;
	}else{
		return $option;
	}
}
add_filter( 'excerpt_length', 'haup_change_embed_excerpt_length' );

/*** embedに日付を表示 ***/
function haup_add_embed_card_date() {
	if ( ! is_single() ) {
		return;
	}

	$pub_message = __( 'Posted on ', 'hima-art-utility' );
	$mod_message = __( 'Last Mod ', 'hima-art-utility' );

	$published_date = get_the_date( DATE_W3C );
	$modified_date = get_the_modified_date( DATE_W3C );

	echo '<p class="ha-embed-pub-mod-date" style="text-align:right;margin-top:4px;">';

	// 最終更新日と公開日が異なる場合
	if ( $published_date !== $modified_date ) {
		echo esc_html($mod_message);
		echo '<time datetime="' . esc_attr( $modified_date ) . '">' . esc_html( get_the_modified_date() ) . '</time>';
		echo '&nbsp;/&nbsp;';
	}

	echo esc_html($pub_message);
	echo '<time datetime="' . esc_attr( $published_date ) . '">' . esc_html( get_the_date() ) . '</time>';
	echo '</p>';
}

if(isset( GetOP03_blockoption()['add-embed-card-pubmod-date'] ) && GetOP03_blockoption()['add-embed-card-pubmod-date'] == 1 ){
add_action( 'embed_content', 'haup_add_embed_card_date' );
}





/***** カード内(iframe内)のスタイル *****/
/*** 設定画面内 ***/
function haup_link_card_style_custom_init(){
//フッターを非表示にする
if(isset( GetOP03_blockoption()['hide-card-footer-content'] )){
	$hidden_footer = GetOP03_blockoption()['hide-card-footer-content'];
}else{
	$hidden_footer = '';
}

if(!empty($hidden_footer)){
	$hidden_footer_style = hau_sanitize_css('.wp-embed-footer {display: none!important;}');
}else{
	$hidden_footer_style = '';
}

//文字種を本体と共通にする
if(isset( GetOP03_blockoption()['card-text-type-inherit'] )){
	$font = GetOP03_blockoption()['card-text-type-inherit'];
}else{
	$font = '';
}

if(!empty($font)){
	$font_style = hau_sanitize_css('.wp-embed{font-family: var(--wp--preset--font-family--system-font)!important;}');
}else{
	$font_style = '';
}

//文字サイズを本体と共通にする
if(isset( GetOP03_blockoption()['card-text-size-inherit'] )){
	$font_size = GetOP03_blockoption()['card-text-size-inherit'];
}else{
	$font_size = '';
}

if(!empty($font_size)){
	$font_size_style = hau_sanitize_css('.wp-embed{font-size: var(--wp--preset--font-size--medium)!important;}');
}else{
	$font_size_style = '';
}

//タイトルの色
if(isset( GetOP03_blockoption()['link-card-title-color'] )){
	$title_color = GetOP03_blockoption()['link-card-title-color'];
}else{
	$title_color = '';
}

if(!empty($title_color)){
	$title_style = hau_sanitize_css('.wp-embed-heading a {color:'.$title_color.'!important;}');
}else{
	$title_style = '';
}

//コンテンツの文字色
if(isset( GetOP03_blockoption()['link-card-content-color'] )){
	$content_text_color = GetOP03_blockoption()['link-card-content-color'];
}else{
	$content_text_color = '';
}

if(!empty($content_text_color)){
	$content_text_color_style = hau_sanitize_css('.wp-embed{color: '.$content_text_color.'!important;}');
}else{
	$content_text_color_style = '';
}

//続きを読むの色
if(isset( GetOP03_blockoption()['link-card-readmore-color'] )){
	$readmore_color = GetOP03_blockoption()['link-card-readmore-color'];
}else{
	$readmore_color = '';
}

if(!empty($readmore_color)){
	$readmore_style = hau_sanitize_css('.wp-embed .wp-embed-more {color:'.$readmore_color.'!important;}');
}else{
	$readmore_style = '';
}

//背景色
if(isset( GetOP03_blockoption()['link-card-bg-color'] )){
	$bg_color = GetOP03_blockoption()['link-card-bg-color'];
}else{
	$bg_color = '';
}

if(!empty($bg_color)){
	$bg_style = hau_sanitize_css('.wp-embed{background: '.$bg_color.'!important;}');
}else{
	$bg_style = '';
}

//背景画像の効果
if(isset( GetOP03_blockoption()['link-card-background-image-blend'] )){
	$image_blend = GetOP03_blockoption()['link-card-background-image-blend'];
}else{
	$image_blend = '';
}

if($image_blend == '2'){
	$ib_style = esc_html('lighten');
}elseif($image_blend == '3'){
	$ib_style = esc_html('darken');
}else{
	$ib_style = esc_html('normal');
}

//背景画像
if(isset( GetOP03_blockoption()['link-card-background-image'] )){
	$back_image = GetOP03_blockoption()['link-card-background-image'];
}else{
	$back_image = '';
}

if(!empty($back_image)){
$bi_style =hau_sanitize_css('
.wp-embed{
background-image: url('.$back_image.')!important;
	background-position: center center!important;
	background-size: cover!important;
	background-repeat: no-repeat!important;
	background-attachment: fixed!important;
	background-blend-mode:'.$ib_style.'!important;
}
');
}else{
	$bi_style = '';
}


//枠線の色
if(isset( GetOP03_blockoption()['link-card-border-color'] )){
	$border_color = GetOP03_blockoption()['link-card-border-color'];
}else{
	$border_color = '';
}

if(!empty($border_color)){
	$border_style = '.wp-embed{border: 1px solid '.$border_color.'!important;}';
}else{
	$border_style = '';
}

//枠線の影の透明度
if(isset( GetOP03_blockoption()['link-card-border-shadow-trans'] )){
	$border_shadow_trans = GetOP03_blockoption()['link-card-border-shadow-trans'];
}else{
	$border_shadow_trans = '';
}

if(empty($border_shadow_trans)){
	$border_trans_style = absint('50');
}else{
	$border_trans_style = absint($border_shadow_trans);
}

//枠線の影の色
if(isset( GetOP03_blockoption()['link-card-border-shadow-color'] )){
	$border_shadow_color = GetOP03_blockoption()['link-card-border-shadow-color'];
}else{
	$border_shadow_color = '';
}

if(!empty($border_shadow_color)){
	$border_shadow_style = '.wp-embed{box-shadow: 0 1px 1px '.$border_shadow_color.$border_trans_style.'!important;}';
}else{
	$border_shadow_style = '';
}

//アイコンの色
if(isset( GetOP03_blockoption()['link-card-icon-color'] )){
	$icon_color = GetOP03_blockoption()['link-card-icon-color'];
}else{
	$icon_color = '';
}

if(!empty($icon_color)){
	$icon_color_style = urlencode($icon_color);
	$icon_color_style_b = $icon_color;
}else{
	$icon_color_style = 'inherit';
	$icon_color_style_b = 'inherit';
}


$icon_style = hau_sanitize_css('
.wp-embed-site-title a , .wp-embed-meta a{
    color: '.$icon_color_style_b.'!important;
}
.dashicons-admin-comments {
    background-image: url(data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%2020%2020%27%3E%3Cpath%20d%3D%27M5%202h9q.82%200%201.41.59T16%204v7q0%20.82-.59%201.41T14%2013h-2l-5%205v-5H5q-.82%200-1.41-.59T3%2011V4q0-.82.59-1.41T5%202z%27%20fill%3D%27'.$icon_color_style.'%27%2F%3E%3C%2Fsvg%3E);
}
.dashicons-share {
    background-image: url(data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%2020%2020%27%3E%3Cpath%20d%3D%27M14.5%2012q1.24%200%202.12.88T17.5%2015t-.88%202.12-2.12.88-2.12-.88T11.5%2015q0-.34.09-.69l-4.38-2.3Q6.32%2013%205%2013q-1.24%200-2.12-.88T2%2010t.88-2.12T5%207q1.3%200%202.21.99l4.38-2.3q-.09-.35-.09-.69%200-1.24.88-2.12T14.5%202t2.12.88T17.5%205t-.88%202.12T14.5%208q-1.3%200-2.21-.99l-4.38%202.3Q8%209.66%208%2010t-.09.69l4.38%202.3q.89-.99%202.21-.99z%27%20fill%3D%27'.$icon_color_style.'%27%2F%3E%3C%2Fsvg%3E);
}

');

//ホーバー時のアイコンの色
if(isset( GetOP03_blockoption()['link-card-icon-hover-color'] )){
	$icon_hover_color = GetOP03_blockoption()['link-card-icon-hover-color'];
}else{
	$icon_hover_color = '';
}

if(!empty($icon_hover_color)){
	$icon_hover_color_style = urlencode($icon_hover_color);
	$icon_hover_color_style_b = $icon_hover_color;
}else{
	$icon_hover_color_style = 'inherit';
	$icon_hover_color_style_b = 'inherit';
}

$icon_hover_style = hau_sanitize_css('
.wp-embed-site-title a:hover {
    color: '.$icon_hover_color_style_b.'!important;
}
.wp-embed-comments a:hover .dashicons-admin-comments {
    background-image: url(data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%2020%2020%27%3E%3Cpath%20d%3D%27M5%202h9q.82%200%201.41.59T16%204v7q0%20.82-.59%201.41T14%2013h-2l-5%205v-5H5q-.82%200-1.41-.59T3%2011V4q0-.82.59-1.41T5%202z%27%20fill%3D%27'.$icon_hover_color_style.'%27%2F%3E%3C%2Fsvg%3E);
}
.wp-embed-share-dialog-open:hover .dashicons-share {
    background-image: url(data:image/svg+xml;charset=utf8,%3Csvg%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20viewBox%3D%270%200%2020%2020%27%3E%3Cpath%20d%3D%27M14.5%2012q1.24%200%202.12.88T17.5%2015t-.88%202.12-2.12.88-2.12-.88T11.5%2015q0-.34.09-.69l-4.38-2.3Q6.32%2013%205%2013q-1.24%200-2.12-.88T2%2010t.88-2.12T5%207q1.3%200%202.21.99l4.38-2.3q-.09-.35-.09-.69%200-1.24.88-2.12T14.5%202t2.12.88T17.5%205t-.88%202.12T14.5%208q-1.3%200-2.21-.99l-4.38%202.3Q8%209.66%208%2010t-.09.69l4.38%202.3q.89-.99%202.21-.99z%27%20fill%3D%27'.$icon_hover_color_style.'%27%2F%3E%3C%2Fsvg%3E);
}
');

?>
<style>
<?php echo esc_html($hidden_footer_style); ?>
<?php echo esc_html($bg_style); ?>
<?php echo esc_html($border_shadow_style); ?>
<?php echo esc_html($border_style); ?>
<?php echo esc_html($font_size_style); ?>
<?php echo esc_html($font_style); ?>
<?php echo esc_html($content_text_color_style); ?>
<?php echo esc_html($bi_style); ?>

/* フッターの文字の大きさは変えない */
.wp-embed-footer {
    font-size: 14px!important;
}
<?php echo esc_html($title_style); ?>
<?php echo esc_html($readmore_style); ?>
<?php echo esc_html($icon_style); ?>
<?php echo esc_html($icon_hover_style); ?>

</style>
<?php
}
add_filter('embed_head','haup_link_card_style_custom_init');

/*** フリースタイル（embed_head内へ任意のコードを出力） ***/
function haup_link_card_style_custom_code_init(){
if(isset( GetOP03_blockoption()['add-link-card-custom-css'] )){
	$css = GetOP03_blockoption()['add-link-card-custom-css'];
}else{
	$css = '';
}

?>
<style>
<?php echo esc_html($css); ?>
</style>
<?php
}
add_filter('embed_head','haup_link_card_style_custom_code_init',9999);