<?php
    $post = get_post(get_queried_object_id());
    $adfoxDate = date("U");
    //$promote_post_id = (int)get_option('_promote_post_id');
    $promote_post_id = 1014199;
    //$promote_counter_id = (int)get_option('_promote_counter_id');
    //$promote_counter_id = $promote_counter_id ? $promote_counter_id : 24504371;
    $promote_counter_id = 87422562;
    $mob = wp_is_mobile() ? "01" : "00";
    $pt = get_post_type(get_queried_object_id()) == "profile_article" ? "01" : "00";
    $cache_id = "20";
    $postcat = get_post_primary_category((get_queried_object()->ID));
    $postcat = $postcat['primary_category'];
?>
<script>
    function isInViewport(element) {
        const rect = element.getBoundingClientRect();
        return (
            rect.top >= 0 &&
            rect.left >= 0 &&
            rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
            rect.right <= (window.innerWidth || document.documentElement.clientWidth)
        );
    }
    document.addEventListener('scroll', function () {
        const h1 = document.querySelectorAll('h1.onenews__title');
        [].forEach.call(h1, function(el) {
            if(isInViewport(el) && el.getAttribute('data-url') != window.location.href){
                let permalink = el.getAttribute('data-url');
                let title = 'Журнал "Профиль". '+el.innerText;

                history.pushState(null, title, permalink);

                if(permalink.indexOf('<?=$promote_post_id?>') !== -1 && <?=(int)$promote_post_id?> > 0) {
                    ym(<?=$promote_counter_id?>, "init", {
                        clickmap: true,
                        trackLinks: true,
                        accurateTrackBounce: true,
                        webvisor: true
                    });
                    let options = {
                        "title": title
                    }
                    ym(<?=$promote_counter_id?>, 'hit', permalink, options);
                    //console.log("ym <?=$promote_counter_id?> updated with " + permalink + " " + title);
                    //console.log('promotion counters updated');
                }
                document.title = title;
            }
        });
    });
</script>
    <div class="js-ajax-data">
        <div class="row clearfix">
            <div class="col-12 col-xl-9 float-left">
                <div class="onenews" itemscope itemtype="https://schema.org/NewsArticle">
                    <link itemprop="mainEntityOfPage" href="<?php echo get_permalink(); ?>"/>
                    <div class="d-none" itemprop="publisher" itemscope itemtype="https://schema.org/Organization">
                        <div class="d-none" itemprop="logo" itemscope itemtype="https://schema.org/ImageObject">
                            <link itemprop="url" content="<?= get_template_directory_uri() ?>/assets/img/profil-logo.png" />
                            <link itemprop="contentUrl" content="<?= get_template_directory_uri() ?>/assets/img/profil-logo.png" />
                        </div>
                        <meta itemprop="name" content="<?php bloginfo(); ?>"/>
                        <link itemprop="url" content="<?php echo home_url(); ?>"/>
                    </div>
                    <span itemprop="articleSection" class="d-none"><?php the_category(); ?></span>
                    <meta itemprop="inLanguage" content="ru"/>
                    <meta itemprop="dateModified" content="<?php echo the_modified_date("c  "); ?>"/>
                    <h1 itemprop="name headline" class="onenews__title" data-url="<?php echo esc_url(get_permalink()); ?>">
                        <?php the_title(); ?>
                    </h1>
                    <?php if (has_secondary_title()): ?>
                        <h4 class="onenews__subtitle d-lg-flex">
                            <?php echo get_secondary_title(); ?>
                        </h4>
                    <?php endif; ?>
                    <div class="publication__data">
                                <span class="publication__number" itemprop="datePublished"
                                      content="<?php echo get_the_date("Y-m-d"); ?>">
                                    <?php if (is_array(get_post_custom_values('num'))): ?>
                                        <?php $num = get_post_custom_values('num'); ?>
                                        <?php echo array_shift($num); ?>
                                    <?php else: ?>
                                        <?php echo get_the_date("d.m.Y") ?>
                                        <?php if (get_post_type() == 'anew'): ?>
                                            <?php echo get_the_time("H:i") ?>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                </span>
                        <?php if (in_array(get_post_type(), array('anew', 'yellow'))): ?>
                            <?php get_template_part('author', 'news'); ?>
                        <?php else: ?>
                            <?php get_template_part('author', 'single'); ?>
                        <?php endif; ?>
                    </div>
                    <div class="onenews__body">
                        <div class="stick js-stickybit d-none d-lg-block">
                            <?php echo share_block(get_the_ID()); ?>
                        </div>
                        <?php if (has_post_thumbnail() && show_thumbnail()): ?>
                            <figure itemprop="image" itemscope itemtype="https://schema.org/ImageObject">
                                <link itemprop="url" href="<?php echo get_the_post_thumbnail_url(get_queried_object_id(), 'large'); ?>" />
                                <link itemprop="contentUrl" href="<?php echo get_the_post_thumbnail_url(get_queried_object_id(), 'large'); ?>" />
                                <?php echo get_the_post_thumbnail($post, 'large'); ?>
                                <?php $thumb = get_post(get_post_thumbnail_id()); ?>
                                <figcaption itemprop="description">
                                    <?php if (strlen($thumb->post_content) !== 0): ?>
                                        <p><?php echo $thumb->post_content; ?></p>
                                    <?php endif; ?>
                                    <?php if (strlen($thumb->post_excerpt) !== 0 && !strposa($thumb->post_excerpt, ['instag', 'faceb', 'инстаг'])): ?>
                                        <span>©<?php echo str_replace(array("Фото: ", "Фото:"), "", main_photo_caption($thumb->post_excerpt)); ?></span>
                                    <?php endif; ?>
                                </figcaption>
                            </figure>
                        <?php endif; ?>
                        <div class="micromarking" itemprop="articleBody" data-post-type="<?php echo get_post_type(); ?>">
                            <?php
                            global $more;
                            $more = 1;
                            echo the_content();
                            ?>
                        </div>
                        <div class="stick js-stickybit d-xs-none d-lg-none">
                            <?php echo share_block(get_the_ID()); ?>
                        </div>
                    </div>
                </div>

                <?php get_template_part('content', 'post_tags'); ?>

                <?php
                //                        cache="true"
                //                        cache_id="0001'.get_queried_object_id().$mob.$promote_post_id.'"
                echo do_shortcode('
                        [ajax_load_more
                        post__in="'.$promote_post_id.'"
                        repeater="template_19"
                        post_type="profile_article" 
                        destroy_after="1"
                        cache="true"
                        cache_id="'.$mob.$promote_post_id.'"
                        ]
                    ');
                ?>

            </div>
        </div>
    </div>