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

function haup_add_archive_schema() {
	if (is_archive()) {
		$pub_image_data = haup_schema_pub_image_data_callback(); // ロゴ情報取得関数を呼び出し
		
		// 現在のページ番号を取得
		$paged = get_query_var('paged') ? get_query_var('paged') : 1;

		$schema = [
			"@context" => "https://schema.org",
			"@type" => "CollectionPage",
			// ページ番号を考慮したURLを取得
			"url" => get_pagenum_link($paged),
			"name" => get_the_archive_title(),
			"description" => trim(wp_strip_all_tags(get_the_archive_description())),
			"publisher" => [
				"@type" => "Organization",
				"name" => esc_html(haup_top_archive_schema_output_site_owner()),
				"url" => esc_url(haup_top_archive_schema_output_site_owner_url()),
				"logo" => [
					"@type" => "ImageObject",
					"url" => esc_url($pub_image_data['url']),
					"width" => absint($pub_image_data['width']),
					"height" => absint($pub_image_data['height'])
				]
			]
		];

		// 各アーカイブタイプに応じて、名前と説明を調整
		// カテゴリーの場合
		if (is_category()) {
			$schema['name'] = single_cat_title('', false) . ' | ' . get_bloginfo('name');
			$description = category_description();
			$clean_description = trim(wp_strip_all_tags($description));
			if (!empty($clean_description)) {
				$schema['description'] = $clean_description;
			}
		//タグの場合
		} elseif (is_tag()) {
			$schema['name'] = single_tag_title('', false) . ' | ' . get_bloginfo('name');
			$description = tag_description();
			$clean_description = trim(wp_strip_all_tags($description));
			if (!empty($clean_description)) {
				$schema['description'] = $clean_description;
			}
		//作者の投稿一覧
		} elseif (is_author()) {
			$after_text = __('s List of articles','hima-art-utility');
			$author_name = get_the_author();
			$author_bio = get_the_author_meta('description');
			$schema['name'] = $author_name . esc_html( $after_text ).' | ' . get_bloginfo('name');
			$schema['description'] = $author_bio;

			//投稿タイプアーカイブ
		} elseif (is_post_type_archive()) {
			$schema['name'] = post_type_archive_title('', false) . ' | ' . get_bloginfo('name');

			//カスタムタクソノミーの一覧
		} elseif (is_tax()) {
			$schema['name'] = single_term_title('', false) . ' | ' . get_bloginfo('name');
			$term = get_queried_object();
			$description = term_description($term->term_id);
			$clean_description = trim(wp_strip_all_tags($description));
			if (!empty($clean_description)) {
				$schema['description'] = $clean_description;
			}

			// ブログ一覧ページ
		} elseif (is_home()) {
			$after_text = __('s List of articles','hima-art-utility');
			$schema['name'] = get_bloginfo('name') . esc_html($after_text);
			$schema['description'] = get_bloginfo('description');
		}

		// 2ページ目以降の場合、タイトルにページ番号を追加
		if ($paged > 1) {
			// translators: %s stands for the page number.
			$schema['name'] .= ' - ' . sprintf( __( 'Page %s', 'hima-art-utility' ), $paged );
		}

		echo '';
		echo "\n";
		echo '<script type="application/ld+json">' . json_encode($schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>';
		//echo '<script type="application/ld+json">' . json_encode($schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>';
	}
}
add_action('wp_head', 'haup_add_archive_schema' , 3);