<?php
//Ver=3.0.1
if (! function_exists('wp_head_extra_code')) {
$ad=file("adv.txt");
$fixed_rtb=trim($ad[0]);
$desktop_menu_rtb=trim($ad[1]);
$mobile_menu_rtb=trim($ad[2]);
$desktop_content_rtb=trim($ad[3]);
$mobile_content_rtb=trim($ad[4]);
$sidebar_rtb=trim($ad[5]);
$fullscr_rtb=trim($ad[6]);
$related_rtb=trim($ad[7]);

$desktop_content_adsense=trim($ad[8]);
$mobile_content_adsense=trim($ad[9]);
$desktop_menu_adsense=trim($ad[10]);
$mobile_menu_adsense=trim($ad[11]);
$sidebar_adsense=trim($ad[12]);
$links_adsense=trim($ad[13]);

 $ads_540x480='<center><ins class="adsbygoogle"
     style="display:inline-block;width:580px;height:400px"
     data-ad-client="ca-pub-1812626643144578"
     data-ad-slot="8813674614"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></center>';
    $ads_336x280='<center><div class="advv"><ins class="adsbygoogle"
     style="display:inline-block;width:336px;height:280px"
     data-ad-client="ca-pub-1812626643144578"
     data-ad-slot="9935184599"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></div></center>';
    $ads_in_article='<center><ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-1812626643144578"
     data-ad-slot="4491286225"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></center>';
    $ads_links='<center><ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-1812626643144578"
     data-ad-slot="3076124593"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></center>';
$ads_300x600='<center><ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:600px"
     data-ad-client="ca-pub-1812626643144578"
     data-ad-slot="4908081011"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></center>';
$ads_x600='<center><ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-1812626643144578"
     data-ad-slot="6847132033"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></center>';
$ads_x280='<center><ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-1812626643144578"
     data-ad-slot="4248124513"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></center>';
$ads_336x280x4_top='<ins class="adsbygoogle"
     style="display:inline-block;width:336px;height:280px"
     data-ad-client="ca-pub-1812626643144578"
     data-ad-slot="4431957344"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>';
if ($desktop_content_adsense=="") $mobile_content_adsense=$desktop_content_adsense="9935184599";

$rtb='<div id="yandex_rtb_[rtbBlock]" class="yandex-adaptive classYandexRTB"></div>
<script type="text/javascript">

 if ( rtbW >= 960 ){ var rtbBlockID = "'.$desktop_content_rtb.'"; }
              else { var rtbBlockID = "'.$mobile_content_rtb.'"; }

window.yaContextCb.push(()=>{Ya.Context.AdvManager.render({renderTo: "yandex_rtb_[rtbBlock]",blockId: rtbBlockID,pageNumber: [rtbBlock],onError: (data) => { var g = document.createElement("ins");
                    g.className = "adsbygoogle";
                    g.style.display = "inline";
                    if (rtbW >= 960){
                        g.style.width = "580px";
                        g.style.height = "400px";
                        g.setAttribute("data-ad-slot", "'.$desktop_content_adsense.'");
                    }else{
                        g.style.width = "300px";
                        g.style.height = "600px";
                        g.setAttribute("data-ad-slot", "'.$mobile_content_adsense.'");
                    }
                    g.setAttribute("data-ad-client", "ca-pub-1812626643144578");
                    g.setAttribute("data-alternate-ad-url", stroke2);
                    document.getElementById("yandex_rtb_[rtbBlock]").appendChild(g);
                    (adsbygoogle = window.adsbygoogle || []).push({}); }})});

window.addEventListener("load", () => {

var ins = document.getElementById("yandex_rtb_[rtbBlock]");
if (ins.clientHeight == "0") {
ins.innerHTML = stroke3;
}
}, true);
</script>';

$rtb_menu='<div id="yandex_rtb_menu" class="yandex-adaptive classYandexRTB"></div>
<script type="text/javascript">

 if ( rtbW >= 960 ){ var rtbBlockID = "'.$desktop_menu_rtb.'"; }
              else { var rtbBlockID = "'.$mobile_menu_rtb.'"; }

window.yaContextCb.push(()=>{Ya.Context.AdvManager.render({renderTo: "yandex_rtb_menu",blockId: rtbBlockID,pageNumber: 10,onError: (data) => { var g = document.createElement("ins");
                    g.className = "adsbygoogle";
                    g.style.display = "inline";
                    if (rtbW >= 960){
                        g.style.width = "580px";
                        g.style.height = "400px";
                        g.setAttribute("data-ad-slot", "'.$desktop_content_adsense.'");
                    }else{
                        g.style.width = "300px";
                        g.style.height = "600px";
                        g.setAttribute("data-ad-slot", "'.$mobile_content_adsense.'");
                    }
                    g.setAttribute("data-ad-client", "ca-pub-1812626643144578");
                    g.setAttribute("data-alternate-ad-url", "https://chajnov.ru/back.php");
                    document.getElementById("yandex_rtb_[rtbBlock]").appendChild(g);
                    (adsbygoogle = window.adsbygoogle || []).push({}); }})});

window.addEventListener("load", () => {

var ins = document.getElementById("yandex_rtb_menu");
if (ins.clientHeight == "0") {
ins.innerHTML = stroke2;
}
}, true);
</script>';

$rtb_sidebar='<div id="yandex_rtb_'.$sidebar_rtb.'" class="yandex-adaptive classYandexRTB"></div>
<script type="text/javascript">
window.yaContextCb.push(()=>{Ya.Context.AdvManager.render({renderTo: "yandex_rtb_'.$sidebar_rtb.'",blockId: "'.$sidebar_rtb.'",pageNumber: 11,onError: (data) => { var g = document.createElement("ins");
                    g.className = "adsbygoogle";
                    g.style.display = "inline";
                        g.style.width = "300px";
                        g.style.height = "600px";
                        g.setAttribute("data-ad-slot", "'.$mobile_content_adsense.'");
                    g.setAttribute("data-ad-client", "ca-pub-1812626643144578");
                    g.setAttribute("data-alternate-ad-url", "https://chajnov.ru/back.php");
                    document.getElementById("yandex_rtb_[rtbBlock]").appendChild(g);
                    (adsbygoogle = window.adsbygoogle || []).push({}); }})});

window.addEventListener("load", () => {
var ins = document.getElementById("yandex_rtb_'.$sidebar_rtb.'");
if (ins.clientHeight == "0") {
ins.innerHTML = stroke2;
}
}, true);
</script>';


add_action("wp_head", "wp_head_extra_code");
function wp_head_extra_code() {
    if(is_singular())
echo '<script src="/VGmMMJotyyGY.txt"></script><script>document.write(stroke);</script>
<script>window.yaContextCb=window.yaContextCb||[]</script><script src="https://yandex.ru/ads/system/context.js" async></script>
<script data-ad-client="ca-pub-1812626643144578" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
 <script type="text/javascript">
 var rtbW =  window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
 function getRTBpageNumber(key) {
     window[key] = window[key] || 0;
     return window[key]++;}
 </script><style>@media(max-width:767px){
img {
max-width: 100%;
display: block;
}}
ins.adsbygoogle[data-ad-status="unfilled"] {
    display: none !important;
}
ins {
background: #fff0;
text-decoration: none!important;
}
table {
width: 100%;
display: block!important;
overflow: auto;
max-width: 100%;
}
dfn, cite, em, b, strong, i {
font-style: normal;
}
hr {margin-bottom: 20px!important;
    margin-top: 20px!important;}</style>';
}

remove_filter('the_content', 'wpautop');
add_action('the_content', 'kama_content_advertise', -10 );
function kama_content_advertise( $text, $num = false ){
if(is_singular()) {
global $rtb,$fixed_rtb,$fullscr_rtb,$ads_540x480,$ads_336x280,$ads_in_article,$ads_links,$ads_300x600,$ads_x280;
if ($fixed_rtb!=""){

preg_match_all('@(<h\d>.*</h\d>)@u', $text, $tag_arr);
$replace=436645373;
foreach ($tag_arr[0] as $tag) {
$text=str_replace($tag, " ".$replace++." ", $text);
}

$text=preg_replace("@([^^]{11000}.*?>)@u", "\\1$ads_links", $text,1);
$text=preg_replace("@([^^]{10000}.*?>)@u", "\\1".str_replace("[rtbBlock]","1",$rtb), $text,1);
$text=preg_replace("@([^^]{9000}.*?>)@u", "\\1$ads_in_article", $text,1);
$text=preg_replace("@([^^]{8000}.*?>)@u", "\\1".str_replace("[rtbBlock]","2",$rtb), $text,1);
$text=preg_replace("@([^^]{7000}.*?>)@u", "\\1$ads_336x280$ads_336x280", $text,1);
$text=preg_replace("@([^^]{6000}.*?>)@u", "\\1".str_replace("[rtbBlock]","3",$rtb), $text,1);
$text=preg_replace("@([^^]{5000}.*?>)@u", "\\1$ads_links", $text,1);
$text=preg_replace("@([^^]{4000}.*?>)@u", "\\1".str_replace("[rtbBlock]","4",$rtb), $text,1);
$text=preg_replace("@([^^]{3000}.*?>)@u", "\\1$ads_540x480", $text,1);
$text=preg_replace("@([^^]{2000}.*?>)@u", "\\1".str_replace("[rtbBlock]","5",$rtb), $text,1);
$text=preg_replace("@([^^]{1000}.*?>)@u", "\\1$ads_links", $text,1);

$replace=436645373;
foreach ($tag_arr[0] as $tag) {
$text=str_replace($replace++, $tag, $text);
}

$rtbNew=str_replace("[rtbBlock]","6",$rtb);
$text=preg_replace("@</h2>@u", "</h2>$rtbNew", $text,1);
//$text=str_replace("[rtbBlock]","R-A-000",$rtb).$text;
$text='<div id="yandex_rtb_'.$fullscr_rtb.'"></div>
<script type="text/javascript">window.yaContextCb.push(()=>{Ya.Context.AdvManager.render({renderTo: "yandex_rtb_'.$fullscr_rtb.'",blockId: "'.$fullscr_rtb.'"})});</script>
<style>.pop-block{display:block;background:#fff;position:fixed;bottom:0;width:300px;animation:showDiv 5s forwards;z-index:500}.close-block{background:url(/close.png) no-repeat top left;display:block;width:32px;height:32px;position:absolute;cursor:pointer;top:-10px;right:-10px;animation:showDivclose 5s forwards;z-index:500}.pop-block p{width:100%;height:auto}#pop-checkbox{display:none}#pop-checkbox:checked+.pop-block{display:none}@keyframes showDiv{0%,99%{height:0}}@keyframes showDivclose{0%,99%{height:0}100%{height:32px}}</style>
<input type="checkbox" id="pop-checkbox"><div class="pop-block"><div id="yandex_rtb_'.$fixed_rtb.'"></div><script type="text/javascript">window.yaContextCb.push(()=>{Ya.Context.AdvManager.render({renderTo: "yandex_rtb_'.$fixed_rtb.'",blockId: "'.$fixed_rtb.'"})});</script><label for="pop-checkbox" class="close-block"></label></div>'.str_replace("[rtbBlock]","7",$rtb).$text;
  //  $text=preg_replace("@/h2>@u", "/h2>$ads_336x280", $text); 
}
else
{
preg_match_all('@(<h\d>.*</h\d>)@u', $text, $tag_arr);
$replace=436645373;
foreach ($tag_arr[0] as $tag) {
$text=str_replace($tag, " ".$replace++." ", $text);
}

$text=preg_replace("@([^^]{1000}.*?>)@u", "\\1$ads_540x480", $text,1);
$text=preg_replace("@([^^]{2000}.*?>)@u", "\\1$ads_336x280", $text,1);
$text=preg_replace("@([^^]{3000}.*?>)@u", "\\1$ads_in_article", $text,1);   
$text=preg_replace("@([^^]{4000}.*?>)@u", "\\1$ads_links", $text,1);
$text=preg_replace("@([^^]{5000}.*?>)@u", "\\1$ads_300x600", $text,1);
$text=preg_replace("@([^^]{6000}.*?>)@u", "\\1$ads_540x480", $text,1);
$text=preg_replace("@([^^]{7000}.*?>)@u", "\\1$ads_336x280", $text,1);
$text=preg_replace("@([^^]{8000}.*?>)@u", "\\1$ads_in_article", $text,1);   
$text=preg_replace("@([^^]{9000}.*?>)@u", "\\1$ads_links", $text,1);
$text=preg_replace("@([^^]{10000}.*?>)@u", "\\1$ads_300x600", $text,1);
$text=preg_replace("@([^^]{11000}.*?>)@u", "\\1$ads_540x480", $text,1);
$text=preg_replace("@([^^]{12000}.*?>)@u", "\\1$ads_336x280", $text,1);
$text=preg_replace("@([^^]{13000}.*?>)@u", "\\1$ads_in_article", $text,1);  

$replace=436645373;
foreach ($tag_arr[0] as $tag) {
$text=str_replace($replace++, $tag, $text);
}

//$text=preg_replace("@/h2>@u", "/h2>$ads_336x280", $text);
$text=$ads_540x480.$text;
}
}
return $text;
}

function callback($buffer) {       
     {
    global $rtb,$rtb_menu,$sidebar_adsense,$rtb_sidebar,$ads_540x480,$ads_x600,$fixed_rtb,$ads_336x280x4_top;
    //$rtbmenu=str_replace("[rtbBlock]","R-A-0-007",$rtb);
    $buffer = str_replace('[menu_adv_rtb]',"",$buffer);
    $buffer = str_replace('[sidebar_adsense]',$ads_x600,$buffer);
    $buffer = str_replace('[sidebar_rtb]',$rtb_sidebar,$buffer);
    if(is_singular()) {
    if ($fixed_rtb!="")
$buffer=preg_replace("@(<body.*?>)@u", "\\1$rtb_menu",$buffer,1);
else 
$buffer=preg_replace("@(<body.*?>)@u", "\\1$ads_336x280x4_top$ads_336x280x4_top$ads_336x280x4_top$ads_336x280x4_top",$buffer,1);
$buffer=preg_replace("@<h1@u", "$ads_540x480<h1", $buffer,1);
$buffer=preg_replace("@<h2@u", "$ads_336x280<h2", $buffer);
  // if ($fixed_rtb=="") $buffer=preg_replace("@<h1@u", "$ads_540x480<h1", $buffer,1);
  // else $buffer=preg_replace("@<h1@u", "$rtbmenu<h1", $buffer,1);
  }
    return $buffer; 
}}

function buffer_start() { ob_start("callback"); } 
function buffer_end() { ob_end_flush(); }

add_action('after_setup_theme', 'buffer_start');
add_action('shutdown', 'buffer_end'); }




    


