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

/***** アドセンスの広告タグ出力（管理者では出力しない場合） *****/
/* 公開済み・アドセンスIDあり・フロントでは出力しない */
function haup_auto_ad_tag_for_anonimous_without_front(){
	//アドセンスのID
	if(isset( GetOP04_ana_ad()['adsense-user-id'] )){
		$adsense_uid = esc_html(GetOP04_ana_ad()['adsense-user-id']);
	}else{
		$adsense_uid = '';
	}

	//遅延処理をする
	if(isset( GetOP04_ana_ad()['google-adsense-delay'] )){
		$delay = absint(GetOP04_ana_ad()['google-adsense-delay']);
	}else{
		$delay = '';
	}

	//トップページでは出力しない
	if(isset( GetOP04_ana_ad()['disable-adsense-autotag-frontpage'] )){
		$dis_front = absint(GetOP04_ana_ad()['disable-adsense-autotag-frontpage']);
	}else{
		$dis_front = '';
	}

if(get_post_status( get_the_ID() ) == 'publish' && !empty($adsense_uid) && $dis_front=='1'){
if(!is_front_page() || !is_home()){
if($delay == '1'){
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
	echo "<!-- Adsense --><script>(function(window, document) {function main() {var ad = document.createElement('script');ad.type = 'text/javascript';ad.defer = true;ad.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=";
echo esc_html($adsense_uid);
echo "';ad.crossorigin = 'anonymous';var sc = document.getElementsByTagName('script')[0];sc.parentNode.insertBefore(ad, sc);}var lazyLoad = false;function onLazyLoad() {if (lazyLoad === false) {lazyLoad = true;window.removeEventListener('scroll', onLazyLoad);window.removeEventListener('mousemove', onLazyLoad);window.removeEventListener('mousedown', onLazyLoad);window.removeEventListener('touchstart', onLazyLoad);window.removeEventListener('keydown', onLazyLoad);main();}}window.addEventListener('scroll', onLazyLoad);window.addEventListener('mousemove', onLazyLoad);window.addEventListener('mousedown', onLazyLoad);window.addEventListener('touchstart', onLazyLoad);window.addEventListener('keydown', onLazyLoad);window.addEventListener('load', function() {if (window.pageYOffset) {onLazyLoad();}window.setTimeout(onLazyLoad,3500)});})(window, document);</script>";
}else{
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
	echo '<script defer src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=';
echo esc_html($adsense_uid);
echo '" crossorigin="anonymous"></script>';
}
}
}
}
add_action('wp_footer','haup_auto_ad_tag_for_anonimous_without_front');

/* 公開済み・アドセンスIDあり・フロントも出力する */
function haup_auto_ad_tag_for_anonimous_output_all_pages(){
	//アドセンスのID
	if(isset( GetOP04_ana_ad()['adsense-user-id'] )){
		$adsense_uid = esc_html(GetOP04_ana_ad()['adsense-user-id']);
	}else{
		$adsense_uid = '';
	}

	//遅延処理をする
	if(isset( GetOP04_ana_ad()['google-adsense-delay'] )){
		$delay = absint(GetOP04_ana_ad()['google-adsense-delay']);
	}else{
		$delay = '';
	}

	//トップページでは出力しない
	if(isset( GetOP04_ana_ad()['disable-adsense-autotag-frontpage'] )){
		$dis_front = absint(GetOP04_ana_ad()['disable-adsense-autotag-frontpage']);
	}else{
		$dis_front = '';
	}

if(get_post_status( get_the_ID() ) == 'publish' && !empty($adsense_uid) && empty($dis_front)){
if($delay == '1'){
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
	echo "<!-- Adsense --><script>(function(window, document) {function main() {var ad = document.createElement('script');ad.type = 'text/javascript';ad.defer = true;ad.src = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=";
echo esc_html($adsense_uid);
echo "';ad.crossorigin = 'anonymous';var sc = document.getElementsByTagName('script')[0];sc.parentNode.insertBefore(ad, sc);}var lazyLoad = false;function onLazyLoad() {if (lazyLoad === false) {lazyLoad = true;window.removeEventListener('scroll', onLazyLoad);window.removeEventListener('mousemove', onLazyLoad);window.removeEventListener('mousedown', onLazyLoad);window.removeEventListener('touchstart', onLazyLoad);window.removeEventListener('keydown', onLazyLoad);main();}}window.addEventListener('scroll', onLazyLoad);window.addEventListener('mousemove', onLazyLoad);window.addEventListener('mousedown', onLazyLoad);window.addEventListener('touchstart', onLazyLoad);window.addEventListener('keydown', onLazyLoad);window.addEventListener('load', function() {if (window.pageYOffset) {onLazyLoad();}window.setTimeout(onLazyLoad,3500)});})(window, document);</script>";
}else{
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
	echo '<script defer src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=';
echo esc_html($adsense_uid);
echo '" crossorigin="anonymous"></script>';
}
}
}
add_action('wp_footer','haup_auto_ad_tag_for_anonimous_output_all_pages');

/***** アドセンス 広告ブロックによる損失収益の回復 *****/
/*** 公開済み・アドセンスIDあり・フロントでは出力しない ***/
function haup_adsense_ad_block_recover_for_anonimous_without_home(){
	//アドセンスのID
	if(isset( GetOP04_ana_ad()['adsense-user-id'] )){
		$adsense_uid = esc_html(GetOP04_ana_ad()['adsense-user-id']);
	}else{
		$adsense_uid = '';
	}

	//遅延処理をする
	if(isset( GetOP04_ana_ad()['google-adsense-delay'] )){
		$delay = absint(GetOP04_ana_ad()['google-adsense-delay']);
	}else{
		$delay = '';
	}

	//トップページでは出力しない
	if(isset( GetOP04_ana_ad()['disable-adsense-autotag-frontpage'] )){
		$dis_front = absint(GetOP04_ana_ad()['disable-adsense-autotag-frontpage']);
	}else{
		$dis_front = '';
	}

	//アドセンス 広告ブロックによる損失収益の回復タグを出力する
	if(isset( GetOP04_ana_ad()['adsense-ad-blocking-recovery-messages'] )){
		$block_tag = absint(GetOP04_ana_ad()['adsense-ad-blocking-recovery-messages']);
	}else{
		$block_tag = '';
	}

	//回復タグのID
	if(isset( GetOP04_ana_ad()['adsense-recovery-messages-id'] )){
		$recovery_id = esc_html(GetOP04_ana_ad()['adsense-recovery-messages-id']);
	}else{
		$recovery_id = '';
	}

if($block_tag == '1'){
if(get_post_status( get_the_ID() ) == 'publish' && !empty($adsense_uid) && $dis_front=='1'){
if(!is_front_page() || !is_home()){
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
echo '<script async src="https://fundingchoicesmessages.google.com/i/';
echo esc_html($recovery_id);
echo '?ers=1" nonce="yLRV4a4_3OQpglNwvNp0rw"></script>';
echo '<script nonce="yLRV4a4_3OQpglNwvNp0rw">';
echo "(function() {function signalGooglefcPresent() {if (!window.frames['googlefcPresent']) {if (document.body) {const iframe = document.createElement('iframe'); iframe.style = 'width: 0; height: 0; border: none; z-index: -1000; left: -1000px; top: -1000px;'; iframe.style.display = 'none'; iframe.name = 'googlefcPresent'; document.body.appendChild(iframe);} else {setTimeout(signalGooglefcPresent, 0);}}}signalGooglefcPresent();})();";
echo '</script>';
}
}
}
}
add_action('wp_footer','haup_adsense_ad_block_recover_for_anonimous_without_home');

/*** 公開済み・アドセンスIDあり・フロントも出力する ***/
function haup_adsense_ad_block_recover_for_anonimous_all_pages(){
	//アドセンスのID
	if(isset( GetOP04_ana_ad()['adsense-user-id'] )){
		$adsense_uid = esc_html(GetOP04_ana_ad()['adsense-user-id']);
	}else{
		$adsense_uid = '';
	}

	//遅延処理をする
	if(isset( GetOP04_ana_ad()['google-adsense-delay'] )){
		$delay = absint(GetOP04_ana_ad()['google-adsense-delay']);
	}else{
		$delay = '';
	}

	//トップページでは出力しない
	if(isset( GetOP04_ana_ad()['disable-adsense-autotag-frontpage'] )){
		$dis_front = absint(GetOP04_ana_ad()['disable-adsense-autotag-frontpage']);
	}else{
		$dis_front = '';
	}

	//アドセンス 広告ブロックによる損失収益の回復タグを出力する
	if(isset( GetOP04_ana_ad()['adsense-ad-blocking-recovery-messages'] )){
		$block_tag = absint(GetOP04_ana_ad()['adsense-ad-blocking-recovery-messages']);
	}else{
		$block_tag = '';
	}

	//回復タグのID
	if(isset( GetOP04_ana_ad()['adsense-recovery-messages-id'] )){
		$recovery_id = esc_html(GetOP04_ana_ad()['adsense-recovery-messages-id']);
	}else{
		$recovery_id = '';
	}

if(empty($block_tag)){
if(get_post_status( get_the_ID() ) == 'publish' && !empty($adsense_uid) && empty($dis_front)){
// phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
echo '<script async src="https://fundingchoicesmessages.google.com/i/';
echo esc_html($recovery_id);
echo '?ers=1" nonce="yLRV4a4_3OQpglNwvNp0rw"></script>';
echo '<script nonce="yLRV4a4_3OQpglNwvNp0rw">';
echo "(function() {function signalGooglefcPresent() {if (!window.frames['googlefcPresent']) {if (document.body) {const iframe = document.createElement('iframe'); iframe.style = 'width: 0; height: 0; border: none; z-index: -1000; left: -1000px; top: -1000px;'; iframe.style.display = 'none'; iframe.name = 'googlefcPresent'; document.body.appendChild(iframe);} else {setTimeout(signalGooglefcPresent, 0);}}}signalGooglefcPresent();})();";
echo '</script>';
}
}
}
add_action('wp_footer','haup_adsense_ad_block_recover_for_anonimous_all_pages');