<?php
    /*
        Template Name: Top fresh node
    */

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

    $redirect = array();

    $last = array_map(
        function($post){
            return $post->ID;
        },
        get_posts(
            array(
                'posts_per_page' => 100,
                'post_status' => 'publish',
                'post_type' => array('anew','profile_article','yellow'),
                'orderby' => 'date',
                'order' => 'DESC',
                'date_query' => array(
                    array(
                        'after' => '24 hours ago'
                    )
                )
            )
        )
    );

    $top = json_decode(get_option('ppp_options'));
    $top =  array_reduce($top, function ($result, $item) {
        $item = (array)$item;

        $result[$item['id']] = $item['count'];

        return $result;
    }, array());

    arsort($top);

    $top = array_keys($top);
    foreach ($top as $t_item){
        foreach ($last as $l_item){
            if ($l_item == $t_item){
                array_push($redirect, $l_item);
            }
            if(count($redirect) >= 5){
                break;
            }
        }
    }

    wp_redirect(get_permalink($redirect[rand(0,count($redirect)-1)]), 301);

    exit;