<?php
/*
 * Recent Post Widget
 * Author & Copyright: themexshaper
 * URL: http://themeforest.net/user/themexshaper
 */
class confin_recent_posts extends WP_Widget {

  /**
   * Specifies the widget name, description, class name and instatiates it
   */
  public function __construct() {
    parent::__construct(
      '-recent-blog',
      CONFIN_THEME_NAME_PLUGIN . esc_html__( ': Recent Posts', 'confin' ),
      array(
        'classname'   => 'recent-post-widget',
        'description' => CONFIN_THEME_NAME_PLUGIN . esc_html__( ' widget that displays recent posts.', 'confin' )
      )
    );
  }

  /**
   * Generates the back-end layout for the widget
   */
  public function form( $instance ) {
    // Default Values
    $instance   = wp_parse_args( $instance, array(
      'title'    => esc_html__( 'Recent Posts', 'confin' ),
      'ptypes'   => 'post',
      'limit'    => '3',
      'date'     => true,
      'category' => '',
      'order' => '',
      'orderby' => '',
    ));

    // Title
    $title_value = esc_attr( $instance['title'] );
    $title_field = array(
      'id'    => $this->get_field_name('title'),
      'name'  => $this->get_field_name('title'),
      'type'  => 'text',
      'title' => esc_html__( 'Title :', 'confin' ),
      'wrap_class' => 'cs-widget-fields',
    );
    echo cs_add_element( $title_field, $title_value );

    // Post Type
    $ptypes_value = esc_attr( $instance['ptypes'] );
    $ptypes_field = array(
      'id'    => $this->get_field_name('ptypes'),
      'name'  => $this->get_field_name('ptypes'),
      'type' => 'select',
      'options' => 'post_types',
      'default_option' => esc_html__( 'Select Post Type', 'confin' ),
      'title' => esc_html__( 'Post Type :', 'confin' ),
    );
    echo cs_add_element( $ptypes_field, $ptypes_value );

    // Limit
    $limit_value = esc_attr( $instance['limit'] );
    $limit_field = array(
      'id'    => $this->get_field_name('limit'),
      'name'  => $this->get_field_name('limit'),
      'type'  => 'text',
      'title' => esc_html__( 'Limit :', 'confin' ),
      'help' => esc_html__( 'How many posts want to show?', 'confin' ),
    );
    echo cs_add_element( $limit_field, $limit_value );

    // Date
    $date_value = esc_attr( $instance['date'] );
    $date_field = array(
      'id'    => $this->get_field_name('date'),
      'name'  => $this->get_field_name('date'),
      'type'  => 'switcher',
      'on_text'  => esc_html__( 'Yes', 'confin' ),
      'off_text'  => esc_html__( 'No', 'confin' ),
      'title' => esc_html__( 'Display Date :', 'confin' ),
    );
    echo cs_add_element( $date_field, $date_value );

    // Category
    $category_value = esc_attr( $instance['category'] );
    $category_field = array(
      'id'    => $this->get_field_name('category'),
      'name'  => $this->get_field_name('category'),
      'type'  => 'text',
      'title' => esc_html__( 'Category :', 'confin' ),
      'help' => esc_html__( 'Enter category slugs with comma(,) for multiple items', 'confin' ),
    );
    echo cs_add_element( $category_field, $category_value );

    // Order
    $order_value = esc_attr( $instance['order'] );
    $order_field = array(
      'id'    => $this->get_field_name('order'),
      'name'  => $this->get_field_name('order'),
      'type' => 'select',
      'options'   => array(
        'ASC' => 'Ascending',
        'DESC' => 'Descending',
      ),
      'default_option' => esc_html__( 'Select Order', 'confin' ),
      'title' => esc_html__( 'Order :', 'confin' ),
    );
    echo cs_add_element( $order_field, $order_value );

    // Orderby
    $orderby_value = esc_attr( $instance['orderby'] );
    $orderby_field = array(
      'id'    => $this->get_field_name('orderby'),
      'name'  => $this->get_field_name('orderby'),
      'type' => 'select',
      'options'   => array(
        'none' => esc_html__('None', 'confin'),
        'ID' => esc_html__('ID', 'confin'),
        'author' => esc_html__('Author', 'confin'),
        'title' => esc_html__('Title', 'confin'),
        'name' => esc_html__('Name', 'confin'),
        'type' => esc_html__('Type', 'confin'),
        'date' => esc_html__('Date', 'confin'),
        'modified' => esc_html__('Modified', 'confin'),
        'rand' => esc_html__('Random', 'confin'),
      ),
      'default_option' => esc_html__( 'Select OrderBy', 'confin' ),
      'title' => esc_html__( 'OrderBy :', 'confin' ),
    );
    echo cs_add_element( $orderby_field, $orderby_value );

  }

  /**
   * Processes the widget's values
   */
  public function update( $new_instance, $old_instance ) {
    $instance = $old_instance;

    // Update values
    $instance['title']        = strip_tags( stripslashes( $new_instance['title'] ) );
    $instance['ptypes']       = strip_tags( stripslashes( $new_instance['ptypes'] ) );
    $instance['limit']        = strip_tags( stripslashes( $new_instance['limit'] ) );
    $instance['date']         = strip_tags( stripslashes( $new_instance['date'] ) );
    $instance['category']     = strip_tags( stripslashes( $new_instance['category'] ) );
    $instance['order']        = strip_tags( stripslashes( $new_instance['order'] ) );
    $instance['orderby']      = strip_tags( stripslashes( $new_instance['orderby'] ) );

    return $instance;
  }

  /**
   * Output the contents of the widget
   */
  public function widget( $args, $instance ) {
    // Extract the arguments
    extract( $args );

    $title          = apply_filters( 'widget_title', $instance['title'] );
    $ptypes         = $instance['ptypes'];
    $limit          = $instance['limit'];
    $display_date   = $instance['date'];
    $category       = $instance['category'];
    $order          = $instance['order'];
    $orderby        = $instance['orderby'];

    $args = array(
      // other query params here,
      'post_type' => esc_attr($ptypes),
      'posts_per_page' => (int)$limit,
      'orderby' => esc_attr($orderby),
      'order' => esc_attr($order),
      'category_name' => esc_attr($category),
      'ignore_sticky_posts' => 1,
     );

     $confin_rpw = new WP_Query( $args );
     global $post;

    // Display the markup before the widget
    echo $before_widget;

    if ( $title ) {
      echo $before_title . $title . $after_title;
    }
    echo '<div class="popular-post-widget"><ul>';
    if ($confin_rpw->have_posts()) : while ($confin_rpw->have_posts()) : $confin_rpw->the_post();
      $post_options = get_post_meta( get_the_ID(), 'post_type_metabox', true );
      $grid_image = isset( $post_options['grid_image'] ) ? $post_options['grid_image'] : '';

      $image_url = wp_get_attachment_url( $grid_image );
      $image_alt = get_post_meta( $grid_image , '_wp_attachment_image_alt', true);
      $title_standard = get_the_title();
      $title_standard = explode(' ', $title_standard);
      $sliced_conent = array_slice( $title_standard, 0, 7 );
      $title_standard = implode( ' ', $sliced_conent);
      $confin_large_image =  wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'fullsize', false, '' );
      $confin_large_image = $confin_large_image[0] ;

      if(class_exists('Aq_Resize')) {
        $post_img = aq_resize( $confin_large_image, '80', '80', true );
      } else {
         $post_img = $confin_large_image;
      }
      if (!empty( $post_img )) {
        $post_img = $post_img;
      } else {
        $post_img = CONFIN_PLUGIN_IMGS.'/80X80.jpg';
      } ?>
    <li>
      <div class="post-image">
        <a href="<?php echo esc_url( get_permalink() ); ?>"><img src="<?php echo esc_url(  $post_img ); ?>" alt="<?php echo esc_attr( $image_alt ); ?>"></a>
      </div>
      <div class="post-info">
        <a href="<?php echo esc_url( get_permalink() ); ?>"><span class="post-title"><?php echo esc_html( $title_standard ); ?></span></a>
        <?php if ($display_date === '1') { ?>
          <p class="date"><i class="ti-time"></i>
            <?php  echo get_the_date(); ?>
          </p>
        <?php } ?>
      </div>
    </li>
  <?php
    endwhile; endif;
    echo '</ul></div>';
    wp_reset_postdata();
    // Display the markup after the widget
    echo $after_widget;
  }
}

// Register the widget using an annonymous function
add_action( 'widgets_init', function() { register_widget( "confin_recent_posts" ); } );