<?php
declare(strict_types=1);
if ( ! defined( 'ABSPATH' ) ) { exit; }
/**
 * JSON-LD Output.
 * @package WTE_Business_Card
 */
function wte_output_json_ld(): void {
	$schemas = [];
	$org = wte_build_organization_schema();
	if ( ! empty( $org ) ) {
		$graph = [
			'@context' => 'https://schema.org',
			'@graph' => [
				$org,
				[ '@type' => 'WebSite', '@id' => home_url( '/#website' ), 'url' => home_url( '/' ), 'name' => get_bloginfo( 'name' ), 'description' => get_bloginfo( 'description' ) ],
				[ '@type' => 'WebPage', '@id' => get_permalink() . '#webpage', 'url' => get_permalink(), 'isPartOf' => [ '@id' => home_url( '/#website' ) ], 'about' => [ '@id' => home_url( '/#organization' ) ] ],
			],
		];
		$schemas[] = $graph;
	}
	if ( is_front_page() ) {
		$faq = wte_build_faqpage_schema();
		if ( ! empty( $faq ) ) { $schemas[] = $faq; }
	}
	$person = wte_build_person_schema();
	if ( ! empty( $person ) ) {
		$schemas[] = [ '@context' => 'https://schema.org' ] + $person;
	}
	foreach ( $schemas as $schema ) {
		printf( '<script type="application/ld+json">%s</script>' . PHP_EOL,
			wp_json_encode( $schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) );
	}
}
add_action( 'wp_head', 'wte_output_json_ld', 1 );
