<!--[single/post-most-popular]-->

<?php

    $ids = array_map(
        function ($item) {
            return $item->id;
        },
        json_decode(
            get_option("ppp_options")
        )
    );

    $popular_query = new WP_Query(
        [
            "post_type" => ["anew", "yellow"],
            "post_status" => "publish",
            "posts_per_page" => 10,
            "ignore_sticky_posts" => true,
            "post__in" => $ids,
            "post__not_in" => get_queried_object_id(),
            "orderby" => "post__in",
            "meta_query" => [
                "key" => "_thumbnail_id",
                "compare" => "EXISTS"
            ]
        ]
    );

?>

<?php if( $popular_query->have_posts() ): ?>

    <div class="partners" id="popular">

        <div class="partners__header">

            Самое читаемое

        </div>

        <div class="row">

            <?php while( $popular_query->have_posts() ): ?>

                <?php $popular_query->the_post(); ?>

                <?php get_template_part("template-parts/single/post-most-popular-item") ?>

            <?php endwhile; ?>

            <?php wp_reset_postdata(); ?>

        </div>

    </div>

    <script>

        (function () {

            let count = 0;

            let current = <?= get_queried_object_id() ?>;

            let visited = ( getCookie('visited') || "" ).split(',');

            visited.push( current );

            visited = visited.slice(-7);

            setCookie('visited', visited.join(","), {secure: true, 'max-age': 3600});

            document.querySelectorAll("#popular div[data-id]").forEach((elem) => {

                if( !visited.includes( elem.getAttribute("data-id") ) && count < 3 ) {

                    elem.classList.remove("d-none");

                    count++;

                }

            })

        })();

    </script>

<?php endif; ?>

<!--[/single/post-most-popular]-->