<?php

$post_title_tag = 'h4';
$style = $opt['style'];
$border = $opt['border'];
/* Reviews */
$review_type = get_custom_option('reviews_first');
$review_avg = $post_data['post_reviews_' . $review_type];
$review_first = get_custom_option('reviews_first');
$review_max = get_custom_option('reviews_max_level');
$review_style = get_custom_option('reviews_style');
$review_color = get_custom_option('review_color');
$review = !empty($review_avg) ? get_review_rating('5stars', $review_avg, $review_max, '#f2c574') : '';
/* /Reviews */
$format_icon = getPostFormatIcon($post_data['post_format']);
$title = '<' . $post_title_tag . ' class="sc_blogger_title sc_title' . (in_array($opt['style'], array('accordion_1', 'accordion_2')) ? ' sc_accordion_title' : '') . '">' . '<a href="' . (in_array($opt['style'], array('accordion_1', 'accordion_2')) ? '#' : $post_data['post_link']) . '">' . (themerex_substr($opt['style'], 0, 6) == 'bubble' ? '<span class="sc_title_bubble_icon ' . ($post_data['post_icon'] != '' ? ' ' . $post_data['post_icon'] : '') . '"' . ($post_data['bubble_color'] != '' ? ' style="background-color:' . $post_data['bubble_color'] . '"' : '') . '></span>' : '') . $post_data['post_title'] . '</a>' . '</' . $post_title_tag . '>';
$info = '<header class="post_info infoPost">';
$info .= $title;
$info .= '<div class="subheader">';
$info .= $review;
$info .= '<span class="icon-clock-1"></span>' . $post_data['post_date'];
$info .= '<span class="separator">|</span>
		 			<a class="icon-comment-1" title="' . sprintf(__('Comments - %s', 'themerex'), $post_data['post_comments']) . '" href="' . $post_data['post_comments_link'] . '">' . $post_data['post_comments'] . '</a>';
$info .= '</div>';
$info .= '</header>';
?>
<article class="sc_blogger_item<?php 
echo (in_array($opt['style'], array('accordion_1', 'accordion_2')) ? ' sc_accordion_item' : '') . ($opt['number'] == $opt['found'] ? ' sc_blogger_item_last' : '') . (sc_param_is_on($opt['scroll']) && ($opt['dir'] == 'vertical' || $opt['style'] == 'date') ? ' sc_scroll_slide swiper-slide' : '') . ($border == "true" ? ' border' : '') . ($opt['number'] == $opt['found'] ? ' last' : '');
?>
">
    if (!empty($review_avg) && !strpos($review_style, 'stars') !== false) {
        echo get_review_rating($review_style, $review_avg, $review_max, $review_color);
    }
    /* /Reviews */
    if ($show_title) {
        ?>
		<h2 class="post_title"><a href="<?php 
        echo $post_data['post_link'];
        ?>
"><?php 
        echo $post_data['post_title'];
        ?>
</a></h2>
		<?php 
        if (!empty($review_avg) && strpos($review_style, 'stars') !== false) {
            echo get_review_rating($review_style, $review_avg, $review_max, $review_color);
        }
        ?>
 
		<?php 
    }
    ?>
		<?php 
    if ($post_data['post_categories_links_colored'] != '') {
        ?>
			<span class="post_cats filled<?php 
        echo count($post_data['post_categories_list']) > 1 ? ' multicat' : '';
        ?>
"> <?php 
        echo $post_data['post_categories_links'];
        ?>
    /**
     * How to display the widget on the screen.
     */
    function widget($args, $instance)
    {
        extract($args);
        global $wp_query, $post;
        /* Our variables from the widget settings. */
        $title = apply_filters('widget_title', isset($instance['title']) ? $instance['title'] : '');
        $title_tabs = array(apply_filters('widget_title', isset($instance['title_latest']) ? $instance['title_latest'] : ''), apply_filters('widget_title', isset($instance['title_popular']) ? $instance['title_popular'] : ''), apply_filters('widget_title', isset($instance['title_commented']) ? $instance['title_commented'] : ''));
        $title_tabs = array_filter($title_tabs);
        $title_length = isset($instance['title_length']) ? (int) $instance['title_length'] : 0;
        $number = isset($instance['number']) ? (int) $instance['number'] : '';
        $show_info = isset($instance['show_info']) ? $instance['show_info'] : 0;
        $show_rating = isset($instance['show_rating']) ? (int) $instance['show_rating'] : 0;
        $show_cats = isset($instance['show_cats']) ? (int) $instance['show_cats'] : 0;
        $category = isset($instance['category']) ? (int) $instance['category'] : 0;
        $post_thumb = isset($instance['post_thumb']) ? $instance['post_thumb'] : 'hide';
        $output = $tabs = '';
        $titles_str = implode('', $title_tabs);
        for ($i = 0; $i < 3; $i++) {
            if (!empty($title_tabs[$i]) || $i == 0 && strlen($titles_str) == 0) {
                $args = array('post_type' => 'post', 'post_status' => current_user_can('read_private_pages') && current_user_can('read_private_posts') ? array('publish', 'private') : 'publish', 'post_password' => '', 'posts_per_page' => $number, 'ignore_sticky_posts' => 1, 'order' => 'DESC');
                if ($i == 0) {
                    // Latest
                    $args['orderby'] = 'date';
                    $args['order'] = 'DESC';
                } else {
                    if ($i == 1) {
                        // Most popular
                        $args['meta_key'] = 'post_views_count';
                        $args['orderby'] = 'meta_value_num';
                    } else {
                        // Most commented
                        $args['orderby'] = 'comment_count';
                    }
                }
                if ($category > 0) {
                    $args['cat'] = $category;
                }
                $ex = get_theme_option('exclude_cats');
                if (!empty($ex)) {
                    $args['category__not_in'] = explode(',', $ex);
                }
                query_posts($args);
                /* Loop posts */
                if (have_posts()) {
                    $abc_range = range('a', 'z');
                    $abc_shuffle = str_shuffle(implode('', $abc_range));
                    $unikey = substr($abc_shuffle, 10);
                    if (count($title_tabs) > 1) {
                        $tabs .= '<li><a href="#widget_popular_' . $i . '_' . $unikey . '" class="theme_button"><span>' . $title_tabs[$i] . '</span></a></li>';
                    }
                    $output .= '
						<div class="tab_content style_' . $post_thumb . '" id="widget_popular_' . $i . '_' . $unikey . '">
					';
                    $post_number = 0;
                    while (have_posts()) {
                        the_post();
                        $post_number++;
                        $post_id = get_the_ID();
                        $post_date = getDateOrDifference(get_the_date('Y-m-d H:i:s'));
                        $post_title = $post->post_title;
                        $post_title = !empty($title_length) ? getShortString($post_title, $title_length, '...') : $post_title;
                        $post_link = get_permalink();
                        $post_custom = get_post_custom($post_id);
                        $post_comments = wp_count_comments($post_id);
                        $post_comments_link = '<a href="' . $post_link . '#comments"><i class="icon-comment-1"></i>' . $post_comments->approved . '</a>';
                        $post_votes = !empty($post_custom['post_votes_data']) ? $post_custom['post_votes_data'] : '';
                        $votes_data = !empty($post_votes[0]) ? unserialize($post_votes[0]) : '';
                        $crit_points = !empty($votes_data['criterias_points']) ? $votes_data['criterias_points'] : '';
                        $total_votes = !empty($votes_data['total_votes']) ? $votes_data['total_votes'] : '';
                        $votes_sum = !empty($crit_points) ? array_sum($crit_points) : '';
                        $votes_avg = !empty($votes_sum) && !empty($total_votes) ? $votes_sum / $total_votes : '';
                        $votes_avg = !empty($votes_avg) ? number_format($votes_avg, 0) : '';
                        $cust_opt = !empty($post_custom['post_custom_options'][0]) ? unserialize($post_custom['post_custom_options'][0]) : '';
                        $author_review = !empty($cust_opt['reviews_marks']) ? explode(',', $cust_opt['reviews_marks']) : '';
                        $author_avg = count($author_review) > 0 ? number_format(array_sum($author_review) / count($author_review), 0) : 0;
                        $post_format = get_post_format($post_id);
                        $post_format = $post_format == false ? 'standard' : $post_format;
                        $format_icon = getPostFormatIcon($post_format);
                        $avg_arr = array();
                        $avg_arr[] = !empty($author_avg) ? $author_avg : '';
                        $avg_arr[] = !empty($votes_avg) ? $votes_avg : '';
                        $avg_arr = array_filter($avg_arr);
                        $total_avg = count($avg_arr) > 0 ? array_sum($avg_arr) / count($avg_arr) : '';
                        $post_categories_links = '';
                        if ($show_cats) {
                            $post_categories_list = getCategoriesByPostId($post_id);
                            $ex_cats = explode(',', get_theme_option('exclude_cats'));
                            $cat_count = count($post_categories_list);
                            for ($c = 0; $c < $cat_count; $c++) {
                                if (in_array($post_categories_list[$c]['term_id'], $ex_cats)) {
                                    continue;
                                }
                                $post_categories_ids[] = $post_categories_list[$c]['term_id'];
                                $category_color = get_category_inherited_property($post_categories_list[$c]['term_id'], 'category_color');
                                if (empty($category_color)) {
                                    $category_color = get_theme_option('category_color');
                                }
                                $post_categories_links .= '<a class="cat_link" href="' . $post_categories_list[$c]['link'] . '"' . (!empty($category_color) ? ' style="background:' . $category_color . '"' : '') . '	>' . $post_categories_list[$c]['name'] . '</a>';
                                $post_categories_links .= count($post_categories_list) > 1 && $c + 1 == $cat_count ? '<br>' : '';
                            }
                        }
                        $review = '';
                        if (!empty($total_avg) && $show_rating) {
                            $review = get_review_rating('5stars', min($total_avg, 100), 100, '#f2c574');
                        }
                        $post_info = '';
                        $post_info .= !empty($show_info) ? '<div class="widget_popular_post_info">' . $post_categories_links . (!empty($review) ? $review : '') . '<span class="post_date"><i class="icon-clock-1"></i>' . $post_date . '</span>' . '<span class="divider">|</span>' . $post_comments_link . '</div>' : '';
                        $output .= '
							<div class="post_item' . ($post_number == 1 ? ' first' : '') . (!empty($post_thumb) ? ' thumb_style_' . $post_thumb : '') . '">
						';
                        $thumb_size = $post_thumb == 'large_thumb' ? 100 : 64;
                        if (in_array($post_thumb, array('large_thumb', 'default_thumb'))) {
                            $post_thumbnail = getResizedImageTag($post_id, $thumb_size, $thumb_size);
                            if ($post_thumbnail) {
                                $output .= '
										<div class="post_thumb image_wrapper">' . $post_thumbnail . '</div>
								';
                            }
                        } else {
                            if ($post_thumb == 'post_format') {
                                $output .= '<i class="' . $format_icon . ' format-icon"></i>';
                            }
                        }
                        $output .= '
										<div class="post_wrapper">
											<h5 class="post_title theme_title"><a href="' . $post_link . '">' . $post_title . '</a></h5>
						';
                        $output .= $post_info . '
								</div>
							</div>
						';
                        if ($post_number >= $number) {
                            break;
                        }
                    }
                    $output .= '
						</div>';
                }
            }
        }
        /* Restore main wp_query and current post data in the global var $post */
        wp_reset_query();
        wp_reset_postdata();
        if (!empty($output)) {
            wp_enqueue_script('jquery-ui-tabs', false, array('jquery', 'jquery-ui-core'), null, true);
            /* Before widget (defined by themes). */
            echo $before_widget;
            /* Display the widget title if one was input (before and after defined by themes). */
            if ($title) {
                echo $before_title . $title . $after_title;
            }
            echo '
				<div class="popular_and_commented_tabs' . ($show_info || $show_rating ? '' : ' flat_list') . '">
					' . (strlen($titles_str) != 0 && count($title_tabs) > 1 ? '<ul class="tabs">' . $tabs . '</ul>' : '') . '
					' . $output . '
					<script type="text/javascript">
						jQuery(document).ready(function() {
							jQuery(\'.popular_and_commented_tabs\').tabs();
						});
					</script>
				</div>
			';
            /* After widget (defined by themes). */
            echo $after_widget;
        }
    }