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

    $posts = json_decode(get_option('ppp_options'));
    $ratios = array_slice(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), 0, 10);
    $rates = array();

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

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

    wp_redirect(get_permalink(chance($rates)), 301);
    exit;

?>