<?php

/**
 * BlogAngel Recent Post Widget
 * This widget shows latest place post
 */
class THRecentPost extends WP_Widget {

    /** constructor */
    function __construct() {
        $widget_ops = array(
            'classname' => 'th_recent_posts_widget',
            'description' => 'Blogangel Recent Post'
        );
        parent::__construct('advanced-recent-posts', 'Blogangel Recent Post', $widget_ops);
    }

    function widget($args, $instance) {
        extract($args);
        $linkmore = '';
        $title = apply_filters('widget_title', empty($instance['title']) ? 'Recent Post' : $instance['title'], $instance, $this->id_base);
        if (!$number = absint($instance['number'])){
            $number = 5;
        }
        if (!$excerpt_length = absint($instance['excerpt_length'])){
            $excerpt_length = 5;
        }
        if (!$show_type = $instance["show_type"]){
            $show_type = 'post';
        }
        $default_sort_orders = array('date', 'title', 'comment_count', 'rand');
        // by default, display latest first

        $sort_by = isset($instance['sort_by']) ? esc_attr($instance['sort_by']) : 'date';      
        $sort_order='DESC';
        //Excerpt more filter
        $new_excerpt_more = create_function('$more', 'return " ";');
        add_filter('excerpt_more', $new_excerpt_more);
        // Excerpt length filter
        $new_excerpt_length = create_function('$length', "return " . $excerpt_length . ";");
        if ($instance["excerpt_length"] > 0){
            add_filter('excerpt_length', $new_excerpt_length);
        }
        // post info array.
        $my_args = array(
            'showposts' => $number,
            'orderby' => $sort_by,
            'order' => $sort_order,
            'post_type' => 'post'
        );
        $adv_recent_posts = '';
        $excerpt_readmore = '[...]';     
        $adv_recent_posts = new WP_Query($my_args);
        echo $before_widget;
        // Widget title

        ?>
        <!--Start Review Thumb-->
        <div class="recent-widget">
        <div class="recent-post">
           <h4 class="widgettitle"><span><?php echo apply_filters('widget_title', $instance['title'] ); ?></span></h4 class="widgettitle">
            <div class="sidebar-tip"></div>
            <?php
            global $post;
            while ($adv_recent_posts->have_posts()) {
				  $adv_recent_posts->the_post();
				   $link_more = ' <a href="' . get_permalink() . '" class="more-link">' . $excerpt_readmore . '</a>';
              
             
                ?>      
                <!--Start Review Element-->
                <div class="th-widget-recent-post">
                      <?php
                      if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) { ?>
                      <a href="<?php get_permalink(); ?>"> <?php the_post_thumbnail('custom-recent-post-thumb'); ?></a>

                      <?php  } else { ?>
                      <a href=""><?php echo th_post_image(90, 70); ?></a>
                      <?php  }  ?>
                                  <div class="th-recent-post">
                        <h5 class="r_title"><a  href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>" class="post-title"><?php if (strlen($post->post_title) > 18) {
                        echo substr(the_title($before = '', $after = '', FALSE), 0, 18) . '...'; } else {
                                   the_title();
                            } ?></a></h5>
                           <ul class="th-recent-meta">
                             <?php if (isset($instance['date'])) : ?>
                            <li class="post-date"><?php the_time( get_option('date_format') ); ?></li>
                            <?php endif; ?>
                            </ul>

                            <?php if (isset($instance['excerpt']) && $instance['excerpt']) : ?>
                            <div class="post-entry">                   
                            <?php if (isset($instance['excerpt']) && $instance['excerpt']) : ?>                            
                                    <p><?php echo get_the_excerpt() . $link_more; ?> </p>
                            <?php endif; ?>
                            </div>
                        <?php endif; ?>               
                          
                    </div>
                </div>
                            <div class="clearfix"></div>
                <!--End Review Element-->  

            <?php
        }
        wp_reset_query();
        ?>
        </div>
         </div>
        <!--End Review Thumb-->
        <?php
        echo $after_widget;
        remove_filter('excerpt_length', $new_excerpt_length);
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['sort_by'] = esc_attr($new_instance['sort_by']);
        $instance['show_type'] = esc_attr($new_instance['show_type']);
        $instance['number'] = absint($new_instance['number']);
        $instance['date'] = esc_attr($new_instance['date']);
        $instance['comment_num'] = esc_attr($new_instance['comment_num']);
        $instance["excerpt_length"] = absint($new_instance["excerpt_length"]);
        $instance["excerpt"] = esc_attr($new_instance["excerpt"]);
        return $instance;
    }

    function form($instance) {
        $title = isset($instance['title']) ? esc_attr($instance['title']) : 'Recent Blog Post';
        $number = isset($instance['number']) ? absint($instance['number']) : 5;
        $show_type = isset($instance['show_type']) ? esc_attr($instance['show_type']) : 'post';
        $excerpt_length = isset($instance['excerpt_length']) ? absint($instance['excerpt_length']) : 5;
     
        ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title','blogangel'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
        <p style="display:none;">
            <label for="<?php echo $this->get_field_id("sort_by"); ?>">
        <?php _e('Post Sort','blogangel'); ?>:
                <select id="<?php echo $this->get_field_id("sort_by"); ?>" name="<?php echo $this->get_field_name("sort_by"); ?>">
                    <option value="date"<?php selected($instance["sort_by"], "date"); ?>><?php _e('Recent Sort Post','blogangel'); ?></option>
                    <option value="title"<?php selected($instance["sort_by"], "title"); ?>><?php _e('Title Sort Post','blogangel'); ?></option>
                    <!-- <option value="comment_count"<?php selected($instance["sort_by"], "comment_count"); ?>><php echo 'R_N_CMT'; ?></option> -->
                    <option value="rand"<?php selected($instance["sort_by"], "rand"); ?>><?php _e('Random Sort Post','blogangel'); ?></option>
                </select>
            </label>
        </p>
        <p>
             <label for="<?php echo $this->get_field_id("excerpt"); ?>">
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("excerpt"); ?>" name="<?php echo $this->get_field_name("excerpt"); ?>"<?php if(isset($instance["excerpt"])){ checked((bool) $instance["excerpt"], true); } ?> />
        <?php _e('Excerpt post','blogangel'); ?>
            </label>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id("excerpt_length"); ?>">
                <?php _e('Excerpt post length','blogangel'); ?>
            </label>
            <input style="text-align: center;" type="text" id="<?php echo $this->get_field_id("excerpt_length"); ?>" name="<?php echo $this->get_field_name("excerpt_length"); ?>" value="<?php echo $excerpt_length; ?>" size="3" />
        </p>      
        <p style="display:none;">
            <label for="<?php echo $this->get_field_id("date"); ?>">
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("date"); ?>" name="<?php echo $this->get_field_name("date"); ?>"<?php checked((bool) $instance["date"], true); ?> />
                <?php _e('Post Date','blogangel'); ?> 
            </label>
        </p>
        <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of Post Show','blogangel'); ?></label>
            <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>

        <p>
            
                <input type="hidden" id="<?php echo $this->get_field_id('show_type'); ?>" name="<?php echo $this->get_field_name('show_type'); ?>" value="listing"/>
             
        </p>
        <?php
    }

}

function th_recent_post() {
    register_widget('THRecentPost');
}

// register RecentPostsPlus widget
add_action('widgets_init', 'th_recent_post');
?>
