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

function haup_add_matomo_tracking_tag_in_footer(){
	if( isset( GetOP03_ana_ad()['login-user-not-output-matomo_tag'] )){
		$ignore_admin = absint( GetOP03_ana_ad()['login-user-not-output-matomo_tag'] );
	}else{
		$ignore_admin = '';
	}

	//IDをセットする
	if(isset( GetOP03_ana_ad()['matomo-site-id'] )){
		$site_id = esc_html( GetOP03_ana_ad()['matomo-site-id'] );
	}else{
		$site_id = '';
	}

	if(empty($site_id)){
		$matomo_id = '1';
	}else{
		$matomo_id = $site_id;
	}

	if(isset( GetOP03_ana_ad()['matomo-url'] )){
		$url = esc_html( GetOP03_ana_ad()['matomo-url'] );
	}else{
		$url = '';
	}

	$tag_a = "var _paq = window._paq = window._paq || [];_paq.push(['trackPageView']);_paq.push(['enableLinkTracking']);(function() {";
	$tag_b = 'var u="'.$url.'";';
	$tag_c = "_paq.push(['setTrackerUrl', u+'matomo.php']);_paq.push(['setSiteId', '".$matomo_id."']);var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);})();";

	if($ignore_admin =='1'){
		if(!is_user_logged_in()){
			echo '<script>';
			echo "var _paq = window._paq = window._paq || [];_paq.push(['trackPageView']);_paq.push(['enableLinkTracking']);(function() {var u=";
			echo '"';
			echo esc_html($url);
			echo '";';
			echo "_paq.push(['setTrackerUrl', u+'matomo.php']);_paq.push(['setSiteId', '";
			echo esc_html($matomo_id);
			echo "']);var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);})();";
			echo '</script>';
		}
	}else{
			echo '<script>';
			echo "var _paq = window._paq = window._paq || [];_paq.push(['trackPageView']);_paq.push(['enableLinkTracking']);(function() {var u=";
			echo '"';
			echo esc_html($url);
			echo '";';
			echo "_paq.push(['setTrackerUrl', u+'matomo.php']);_paq.push(['setSiteId', '";
			echo esc_html($matomo_id);
			echo "']);var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);})();";
			echo '</script>';
	}

}
add_action('wp_footer','haup_add_matomo_tracking_tag_in_footer');