<?php
    /*
        Template Name: Random node
    */

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



    $last = get_posts(
        array(
            'post_status' => 'publish',
            'post_type' => array('anew','profile_article','yellow'),
            'posts_per_page' => 5,
            'orderby' => 'date',
            'order' => 'DESC'
        )
    );

    $posts = json_decode(get_option('ppp_options'));

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

    //var_dump();
    //die;
    $popular = get_posts(
        array(
            'post__in' => array_map(function($obj){return $obj->id;},$posts),
            'ignore_sticky_posts' => 1,
            'post_type' => array('anew', 'yellow', 'profile_article'),
            'posts_per_page' => 20
        )
    );

    foreach ($popular as $key => $item){
        if(get_post_meta($item->id,'popular_hide', true) == 1){ unset($popular[$key]); }
    }


    $posts = array_merge($last, array_slice($popular,0,5));

    //var_dump($posts);

    //wp_redirect(, 301);//exit;
?>
<?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 ($posts as $post): ?>
                    <?php the_post($post); ?>
                    <?php get_template_part( 'content', 'news' ); ?>
                <?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($posts[rand(0,count($posts))]); ?>"', 2000);
    </script>
<?php endif; ?>
<?php get_footer(); ?>
