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

/***** プログラム *****/
function haup_qloop_related_posts( $query, $block ) {
	$block = $block->parsed_block;

// 現在の投稿に関連付けられたカテゴリーを取得
	$categories = get_the_category();
	$category_ids = array();

//投稿タイプの指定
$post_types ='post';//初期値（投稿と固定ページのみ）をセット
$post_types = apply_filters('haup_related_posts_singular',$post_types);
//20250902 explodeだけでは空文字などがあった場合に不具合が出る可能性があるので修正
$post_types_array = array_map('trim', explode(',', $posttypes));
$post_types_array = array_filter($post_types_array);
//$post_types_array = explode(',', $posttypes);//「,」で投稿タイプを分割

	if ($categories) {
		foreach ($categories as $category) {
			$category_ids[] = $category->cat_ID;
		}
	}

	if (
		isset( $block['attrs']['className'] )
		&& false !== strpos( $block['attrs']['className'], 'haup-related-posts' )
	) {
		$query['post_type'] = $post_types_array;
		$query['cat'] = $category_ids;
		$query['orderby'] = 'rand';
	}

	return $query;
}

add_filter( 'query_loop_block_query_vars', 'haup_qloop_related_posts', 10, 2 );



/***** パターンの登録 *****/
function haup_related_posts_pattern_init(){
	$slug = 'hima-art-utility/related-posts-pattern';
	$title = __('Related Content','hima-art-utility');

	$pattern = [
		"title" => $title,
		"categories" => ["hima-art-utility/original-patterns"],
		"content" => '
			<!-- wp:query {"queryId":1,"query":{"perPage":"4","pages":"4","offset":0,"postType":"post","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false}} -->
<div class="wp-block-query"><!-- wp:post-template {"className":"haup-related-posts","layout":{"type":"grid","columnCount":2}} -->
<!-- wp:group {"style":{"spacing":{"padding":{"top":"8px","bottom":"8px","left":"8px","right":"8px"}},"dimensions":{"minHeight":"100%"},"border":{"radius":"4px","width":"1px"}},"borderColor":"ha-separate","layout":{"type":"constrained"}} -->
<div class="wp-block-group has-border-color has-ha-separate-border-color" style="border-width:1px;border-radius:4px;min-height:100%;padding-top:8px;padding-right:8px;padding-bottom:8px;padding-left:8px"><!-- wp:post-featured-image {"isLink":true,"height":"200px"} /-->

<!-- wp:group {"layout":{"type":"flex","flexWrap":"nowrap","justifyContent":"right"}} -->
<div class="wp-block-group"><!-- wp:post-date {"textAlign":"right","isLink":true,"displayType":"modified","className":"list-modday","fontSize":"small"} /-->

<!-- wp:post-date {"isLink":true,"className":"list-pubday","fontSize":"small"} /--></div>
<!-- /wp:group -->

<!-- wp:post-title {"level":4,"isLink":true,"fontSize":"medium"} /--></div>
<!-- /wp:group -->
<!-- /wp:post-template -->

<!-- wp:query-no-results -->
<!-- wp:paragraph {"placeholder":"クエリーが結果を返さない場合に表示するテキスト、またはブロックを追加してください。"} -->
<p>関連するページがありません</p>
<!-- /wp:paragraph -->
<!-- /wp:query-no-results --></div>
<!-- /wp:query -->
		',
	];

	register_block_pattern($slug, $pattern);
}
add_action('admin_init', 'haup_related_posts_pattern_init');


/***** スタイルコードの出力 *****/
function haup_related_posts_pattern_style_init(){

	//公開日
	if(isset( GetOP01_addcontent()['related-post-pattern-postdate-alt'] )){
		$post_date = GetOP01_addcontent()['related-post-pattern-postdate-alt'];
	}else{
		$post_date = '';
	}

	$pd = __('Publish date/','hima-art-utility');
	if(!empty($post_date)){
		$pd = $post_date;
	}else{
		$pd = __('Publish date/','hima-art-utility');
	}

	//最終更新日
	if(isset( GetOP01_addcontent()['related-post-pattern-moddate-alt'] )){
		$mod_date = GetOP01_addcontent()['related-post-pattern-moddate-alt'];
	}else{
		$mod_date = '';
	}
	$md = __('Last updated/','hima-art-utility');
	if(!empty($mod_date)){
		$md = $mod_date;
	}else{
		$md = __('Last updated/','hima-art-utility');
	}

//スタイルコードをまとめる
$custom_css_pre = '
/* HAUP Related Posts Style */
.list-modday:before {
    content: "'.$md.'";
}
.list-pubday:before {
    content: "'.$pd.'";
}
';

//タブを除去
$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 );
//追加したスタイルコードをサイトエディタ側へインライン出力
	wp_add_inline_style( 'wp-edit-blocks', $custom_css );

}
add_action( 'init', 'haup_related_posts_pattern_style_init');
add_action( 'wp_enqueue_scripts', 'haup_related_posts_pattern_style_init');