<?php
    /*
        Template Name: Popular node list
    */



    $ref = filter_input(INPUT_SERVER, 'HTTP_REFERER');
    $isFromSF = mb_stripos($ref, 'seo-fast.ru');

    $posts = json_decode(get_option('ppp_options'));
    $ratios = array_map(function($obj){$obj->rate = get_post_meta($obj->id,'popular_hide', true) == 1 ? 0 : get_post_meta($obj->id,'popular_ratio', true); return $obj;}, $posts);
    $rates = array();

    foreach ($ratios as $item){
        $rate = 100 * $item->rate/array_sum(array_map(function($obj){return $obj->rate;}, $ratios));
        if($rate > 0 && count($rates <= 10)){
            $rates[$item->id] = $rate;
        }
    }



    $count = (int)get_option('popularnode_list_count');
    $count = $count+1;
    update_option('popularnode_list_count', $count);

?>
<?php get_header();?>
<div class="row clearfix d-block">
    <div class="col-12 col-xl-9 float-left">
        <div class="newslist">
            <div class="newslist__result">
                <?php foreach ($rates as $post => $rate): ?>

                    <div class="newslist__item">
                        <div class="d-flex">
                            <?php if (has_post_thumbnail($post) && show_thumbnail($post)): ?>
                                <div class="newslist__frame">
                                    <a href="<?php echo esc_url( get_permalink($post) ); ?>">
                                        <img loading="lazy" <?php if(wp_is_mobile()): ?>src="<?= get_template_directory_uri() ?>/assets/img/Profile_lazyload_m.webp"<?php else: ?>src="<?= get_template_directory_uri() ?>/assets/img/Profile_lazyload.jpg"<?php endif; ?> class="lazyload" data-src="<?php echo get_the_post_thumbnail_url($post); ?>" alt="<?php the_title($post); ?>" />
                                    </a>
                                </div>
                            <?php endif; ?>
                            <div>
                                <div class="newslist__body">
                                    <h2 class="newslist__title">
                                        <a href="<?php echo esc_url( get_permalink($post) ); ?>"><?php echo get_the_title($post); ?></a>
                                    </h2>
                                </div>
                                <div class="publication__data">
                                    <?php echo get_the_date("j.m.Y H:i", $post); ?>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
    <div class="col-12 col-xl-3 float-left d-none d-xl-block">
        <div class="right-side">
            <?php dynamic_sidebar('right_sidebar'); ?></div>
            <div class="js-stickybit">
                <?php get_template_part( 'content', 'ad' ); ?>
            </div>
        </div>
    </div>
</div>
<?php if($isFromSF !== false): ?>
    <script>
        setTimeout('location="<?php echo get_permalink(chance($rates)); ?>"', 2000);
    </script>
<?php endif; ?>
<?php get_footer(); ?>