<?php
global $redux_demo;
?>
<aside class="col-sm-3 left-aside">
    <div class="sidebar-menu">
        <?php if($redux_demo['left_sidebar'] == 1) { ?>
            <div class="top-menu">
                <div class="inception-menu">
                    <a href="#" class="selector">Menu</a>
                    <div class="sub-navigation">
                        <div class="half <?php if($redux_demo['enable_bookmarks'] == 2) echo 'full';?>">
                            <a href="#" rel="sidebar-content-hot"><i class='icon-hot'></i> <?php _e('Hot Posts', 'Aruna');?></a>
                        </div>
                        <?php if($redux_demo['enable_bookmarks'] != 2) { ?>
                            <div class="half">
                                <a href="#" rel="sidebar-content-bookmarks"><i class='icon-bookmarks'></i> <?php _e('Bookmarks', 'Aruna');?></a>
                            </div>
                        <?php } ?>
                    </div>
                </div>
                <div class="section-title"><a href="#" rel="sidebar-content-category"><?php _e('Categories', 'Aruna');?></a></div>
            </div>
            <div class="sidebar-content sidebar-content-category">
                <?php
                //showing the categories and the latest 5 posts from each individual cat
                $categories = get_categories('hide_empty=1&orderby=count&order=DESC');
                foreach($categories as $cat) {
                    $id = $cat->term_id;
                    echo '
                    <div class="menu-group">
                        <h2><a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s", 'Aruna' ), $cat->name ) . '">' . $cat->name . '</a></h2>
                        <hr />
                        <ul>';
                                
                    $args = array(); //preparing for getting the latest 5 posts from this category
                    $args['post_type'] = 'post';
                    $args['posts_per_page'] = 5;
                    $args['category__in'] = $id;
                    //performance improvements
                    $args['no_found_rows'] = true;
                    $args['update_post_term_cache'] = true;
                    $args['update_post_meta_cache'] = true;
                    $query = new WP_Query($args);
                    $i = 1;
                    while($query->have_posts() ) : $query->the_post();
                        global $post;
                        if($i == 1) {
                            $type = get_post_format($post->ID);
                            $thumb = '';
                            if($type == 'image' || $type == 'standard') {
                                $thumb = get_post_meta($post->ID, '_standard_image', true);
                            }
                            elseif($type == 'gallery') {
                                $thumb = get_post_meta($post->ID, '_slider_image1', true);
                            }
                            elseif($type == 'quote') {
                                $quoteimg = get_post_meta($post->ID, '_quote_image', true);
                                if($quoteimg) {
                                    $thumb = $quoteimg;
                                }
                            }
                            if($thumb == '') {
                                $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
                            }
                            if($thumb == '' && $type == 'video') {
                                $thumb = get_template_directory_uri() . '/img/play_btn_sidebar.jpg';
                            }
                            $resized = aq_resize($thumb, 300, 225, true);
                            if($resized == '') {
                                $resized = $thumb;
                                //we can't resize the original image so we keep that one
                            }
                            ?>
                            <li class="noborder">
                                <div class="side-article">
                                    <?php if($resized != '') { ?>
                                        <figure>
                                            <a href="<?php the_permalink();?>">
                                                <img src="<?php echo $resized;?>" alt="<?php the_title();?>" />
                                            </a>
                                        </figure>
                                    <?php } ?>
                                    <h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
                                    <div class="counters-line">
                                        <div class="pull-left">
                                            <div class="date"><i class="icon-date"></i> <?php the_time("d/m");?> </div>
                                            <div class="comments"><i class="icon-comments"></i> <?php comments_popup_link('0', '1', '%'); ?></div>
                                        </div>
                                    </div>
                                </div>
                            </li>
                        <?php }
                        else {
                            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
                        }
                    $i++; endwhile; wp_reset_postdata();
                    echo '</ul>
                    </div>';
                }
                ?>
            </div>
            <div class="sidebar-content sidebar-content-hot">
                <?php
                //Hot posts
                $args = array();
                $args['post_type'] = 'post';
                $args['posts_per_page'] = 10;
                //performance improvements
                $args['no_found_rows'] = true;
                $args['update_post_term_cache'] = true;
                $args['update_post_meta_cache'] = true;
                $query = new WP_Query($args);
                while($query->have_posts() ) : $query->the_post();
                    global $post;
                    $type = get_post_format($post->ID);
                    $thumb = '';
                    if($type == 'image' || $type == 'standard') {
                        $thumb = get_post_meta($post->ID, '_standard_image', true);
                    }
                    elseif($type == 'gallery') {
                        $thumb = get_post_meta($post->ID, '_slider_image1', true);
                    }
                    elseif($type == 'quote') {
                        $quoteimg = get_post_meta($post->ID, '_quote_image', true);
                        if($quoteimg) {
                            $thumb = $quoteimg;
                        }
                    }
                    if($thumb == '') {
                        $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
                    }
                    $resized = aq_resize($thumb, 256);
                    if($resized == '') {
                        $resized = $thumb;
                        //we can't resize the original image so we keep that one
                    }
                    ?>
                    <div class="side-article">
                        <?php if($resized != '') { ?>
                            <figure>
                                <a href="<?php the_permalink();?>">
                                    <img src="<?php echo $resized;?>" alt="<?php the_title();?>" />
                                </a>
                            </figure>
                        <?php } ?>
                        <h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
                        <div class="counters-line">
                            <div class="pull-left">
                                <div class="date"><i class="icon-date"></i> <?php the_time("d/m");?> </div>
                                <div class="comments"><i class="icon-comments"></i> <?php comments_popup_link('0', '1', '%'); ?></div>
                            </div>
                        </div>
                    </div>
                <?php endwhile; wp_reset_postdata(); ?>
            </div>
            <div class="sidebar-content sidebar-content-bookmarks">
                <?php
                if(!is_user_logged_in() ) {
                    ?>
                    <div class="side-article">
                        <h2><?php _e('Please login if you want to see bookmarked posts!', 'Aruna');?></h2>
                    </div>
                <?php }
                else {
                    $current_user = wp_get_current_user();
                    $bookmarks = get_user_meta($current_user->ID, 'bookmarks', true);
                    if($bookmarks) { 
                        $bookmarks = unserialize($bookmarks);
                        //Bookmarks
                        $args = array();
                        $args['posts_per_page'] = 15;
                        $args['post__in'] = $bookmarks;
                        $args['post_type'] = 'post';
                        //performance improvements
                        $args['no_found_rows'] = true;
                        $args['update_post_term_cache'] = true;
                        $args['update_post_meta_cache'] = true;
                        $query = new WP_Query($args);
                        while($query->have_posts() ) : $query->the_post();
                            global $post;
                            $type = get_post_format($post->ID);
                            $thumb = '';
                            if($type == 'image' || $type == 'standard') {
                                $thumb = get_post_meta($post->ID, '_standard_image', true);
                            }
                            elseif($type == 'gallery') {
                                $thumb = get_post_meta($post->ID, '_slider_image1', true);
                            }
                            elseif($type == 'quote') {
                                $quoteimg = get_post_meta($post->ID, '_quote_image', true);
                                if($quoteimg) {
                                    $thumb = $quoteimg;
                                }
                            }
                            if($thumb == '') {
                                $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
                            }
                            $resized = aq_resize($thumb, 256);
                            if($resized == '') {
                                $resized = $thumb;
                                //we can't resize the original image so we keep that one
                            }
                            ?>
                            <div class="side-article">
                                <?php if($resized != '') { ?>
                                    <figure>
                                        <a href="<?php the_permalink();?>">
                                            <img src="<?php echo $resized;?>" alt="<?php the_title();?>" />
                                        </a>
                                    </figure>
                                <?php } ?>
                                <h2><a href="<?php the_permalink();?>"><?php the_title();?></a></h2>
                                <div class="counters-line">
                                    <div class="pull-left">
                                        <div class="date"><i class="icon-date"></i> <?php the_time("d/m");?> </div>
                                        <div class="user"><i class="icon-user"></i> <?php the_author_posts_link();?></div>
                                        <div class="comments"><i class="icon-comments"></i> <?php comments_popup_link('0', '1', '%'); ?></div>
                                    </div>
                                </div>
                            </div>
                        <?php endwhile; wp_reset_postdata(); 
                    } else { ?>
                        <div class="side-article">
                            <h2><?php _e("Bummer, no bookmarks posts!", 'Aruna');?></h2>
                        </div>
                    <?php }
                } ?>
            </div>
        <?php }
        else { 
            echo '<div class="sidebar-content left-widgets">';
            dynamic_sidebar('Left Sidebar');
            echo '</div>';
        } ?>
    </div>
</aside>