<?php

/**
 * oneline Recent Post Widget
 * This widget shows latest place post
 */
class onelinePost extends WP_Widget {

    /** constructor */
    function __construct() {
        $widget_ops = array(
            'classname' => 'oneline_recent_posts_widget',
            'description' => oneline_RECENT_DESC
        );
        parent::__construct('advanced-recent-posts', RECENT_oneline_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 = '[...]';
        $linkmore .= ' <a href="' . get_permalink() . '" class="more-link">' . $excerpt_readmore . '</a>';
        $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="widget-title sidebar-heading"><?php echo $instance["title"]; ?></h4>
            <div class="sidebar-tip"></div>
            <?php
            global $post;
            while ($adv_recent_posts->have_posts()) {
                $adv_recent_posts->the_post();
                $img_source = '';
                    $permalink = get_permalink();
                    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
                    if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) {
                            $img_source =wp_get_attachment_url(get_post_thumbnail_id($post->ID, 'thumbnail'));                                
                           $img_path=th_image_resize($img_source, 64, 64);
                           if (!empty($img_path['url'])) {

                            $img_source = $img_path['url'];
                           }
                        }
                        elseif (isset($matches [1] [0])) {
                            $img_source = $matches [1] [0];
                            $img_path=th_image_resize($img_source, 64, 64);
                           if (!empty($img_path['url'])) {

                            $img_source = $img_path['url'];
                           }
                        }

                        if($img_source=='')
                            continue;
                ?>      
                <!--Start Review Element-->
                <div class="oneline-comment">
                    <img  width="38" src="<?php echo $img_source; ?>" /> 
              
                    <div class="oneline-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="recent-oneline-meta">
                             <?php if (isset($instance['date'])) : ?>
                            <li class="post-date"><?php the_time("j M Y"); ?></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() . $linkmore; ?> </p>
                            <?php endif; ?>
                            </div>
                        <?php endif; ?>               
                          
                    </div>
                </div>
                <div class="recent-listing-partition"></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_oneline_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 echo RECENT_TITLE; ?></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 echo RECENT_SORT_BY; ?>:
                <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 echo RECENT_SORT_DATE; ?></option>
                    <option value="title"<?php selected($instance["sort_by"], "title"); ?>><?php echo RECENT_SORT_TITLE; ?></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 echo RECENT_SORT_RANDOM; ?></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 checked((bool) $instance["excerpt"], true); ?> />
        <?php echo RECENT_EXCERPT_POST; ?>
            </label>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id("excerpt_length"); ?>">
                <?php echo RECENT_EXCERPT_LENGTH; ?>
            </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 echo RECENT_IN_POST_DATE; ?>
            </label>
        </p>
        <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php echo RECENT_NO_P_SHOW; ?></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 oneline_recent_post() {
    register_widget('onelinePost');
}

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