<?php
if (!defined('ABSPATH')) {
	exit;
}

class ProbonoSEO_Organization_Schema {
	private static $instance = null;

	public static function get_instance() {
		if (self::$instance === null) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	private function __construct() {
		$this->init_hooks();
	}

	private function init_hooks() {
		if (get_option('probonoseo_aio_organization_schema', '1') === '1') {
			add_action('wp_head', array($this, 'output_organization_schema'), 9);
		}
	}

	public function output_organization_schema() {
		if (!is_front_page() && !is_home()) {
			return;
		}

		$schema = array(
			'@context'   => 'https://schema.org',
			'@type'      => 'Organization',
			'@id'        => home_url('/#organization'),
			'name'       => get_bloginfo('name'),
			'url'        => home_url('/'),
			'inLanguage' => 'ja',
		);

		$description = get_bloginfo('description');
		if (!empty($description)) {
			$schema['description'] = $description;
		}

		echo '<script type="application/ld+json">' . wp_json_encode($schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>' . "\n";
	}
}

function probonoseo_init_organization_schema() {
	ProbonoSEO_Organization_Schema::get_instance();
}
add_action('init', 'probonoseo_init_organization_schema');