<?php
/*
// =============================== My Recent News ======================================*/
class novablog_RecentNewsWidget extends WP_Widget {

	function __construct() {

		$widget_ops = array(
			'classname' => 'novablog_RecentNewsWidget', 
			'description' => esc_html__('A widget that displays posts in slideshow', 'novablog') 
		);
        $control_ops = array('width' => 500, 'height' => 350);
		parent::__construct(
			'novablog_RecentNewsWidget',
			esc_html__('Hercules - Recent News Slideshow', 'novablog'),
			$widget_ops, $control_ops
		);

	}
/**
 * Displays custom posts widget on blog.
 */
function widget($args, $instance) {
	
	extract( $args );
	$title 	= apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
  if (isset($instance['excerpt_length']))
    $limit = apply_filters('widget_title', $instance['excerpt_length']);
  else
    $limit = 0;

	
  $valid_sort_orders = array('date', 'title', 'rand');
  if ( in_array($instance['sort_by'], $valid_sort_orders) ) {
    $sort_by = $instance['sort_by'];
    $sort_order = (bool) $instance['asc_sort_order'] ? 'ASC' : 'DESC';
  } else {
    // by default, display latest first
    $sort_by = 'date';
    $sort_order = 'DESC';
  }
	
	// Get array of post info.
	
	$arg = array(
		'showposts' => $instance["num"],
		'ignore_sticky_posts' => 1,
		'post_type' => 'post',
		'orderby' => $sort_by,
		'order' => $sort_order,
		'tax_query' => array(
		 'relation' => 'AND',
			array(
				'taxonomy' => 'post_format',
				
				'terms' => array('post-format-aside', 'post-format-link', 'post-format-quote'),
				'operator' => 'NOT IN'
			)
		)
	);
	    if ($instance['posts_category']) {
      $arg['category_name'] = $instance['posts_category'];
    }
  $cat_posts = new WP_Query($arg);
	
	echo wp_kses_post( $args['before_widget'] );
	
		 if ( $title ) {
			echo wp_kses_post( $args['before_title'] . $title . $args['after_title'] );
		} 

	// Posts list
    $random_ID          = uniqid();
	$rtl_slide = '';
	if (is_rtl()) {$rtl_slide = 'true';}
	 if (isset($instance['slide_width'])){
	$slideshow_width = $instance['slide_width'];
	}else{
	$slideshow_width = 280;
	}
	 if (isset($instance['slide_height'])){
	$slideshow_height = $instance['slide_height'];
	}else{
	$slideshow_height = 420;
	}
	echo '<div class="parallax-disabled carousel-wrap slideshow '.(empty($instance['container_class']) ? '' : $instance['container_class']).'">';
				echo '<div id="owl-carousel-' . esc_attr($random_ID) . '" class="owl-carousel-post owl-carousel" data-center="false" data-howmany="' .esc_attr($instance["num"]). '" data-margin="0" data-items="1" data-tablet="1" data-mobile="1"  data-auto-play="true" data-auto-play-timeout="9000" data-nav="false" data-rtl="'.esc_attr($rtl_slide).'" data-pagination="true">';
	
	$limittext = $limit;
	while ( $cat_posts->have_posts() )
	{
		$cat_posts->the_post();
	?>
   
   
<?php
 if(has_post_thumbnail()) {
$post_id = $cat_posts->post->ID;

$img_url = wp_get_attachment_url( get_post_thumbnail_id());
				
		$img_width = $slideshow_width ? $slideshow_width : 280;
		$img_height = $slideshow_height ? $slideshow_height : 420;

        $img = aq_resize( $img_url, $img_width, $img_height, true, true, true ); 

		  ?>
   
                   
   
   
          <div class="owl-slide cover post-list_h <?php if ( empty($instance['show_border'] )) { echo 'slide-with-border'; }else{echo 'slide-no-border';} ?>" style="background-image: url(<?php echo esc_url($img); ?>)">
            <div class="cover-wrapper" style="height:<?php echo (empty($slideshow_height) ? '600' : $slideshow_height); ?>px;" >
              <div class="cover-content">
                <?php echo esc_attr( $instance['show_category']) ? novablog_post_category($post_id,' ') : ''; ?>
                <?php if ( $instance['show_title'] ) : ?>
<h4><a class="post-title" href="<?php esc_url(the_permalink()); ?>" rel="bookmark" title="<?php esc_attr(the_title_attribute()); ?>"><?php esc_attr(the_title()); ?></a></h4>
<?php endif; ?>
<?php if ( $instance['date'] ) : 
novablog_post_meta(array('date'), true, 'meta-space-top');
endif; ?>
		        <?php if ( $instance['excerpt'] ) : ?>
		<div class="excerpt">
<?php $excerpt = get_the_content();
echo novablog_limit_text($excerpt,$limittext); ?>
		   </div>
        <?php endif; ?>
          <?php if ( $instance['more_link'] ) : ?>
        <a href="<?php esc_url(the_permalink()) ?>" class="<?php if($instance['more_link_class']!="") {echo esc_attr($instance['more_link_class']);}else{ ?>slideshow-btn<?php } ?>"><?php if($instance['more_link_text']==""){  esc_html_e('Read more', 'novablog'); }else{ ?><?php echo esc_attr($instance['more_link_text']); ?><?php } ?></a>
      <?php endif; ?>
              </div>
            </div>
            <a href="<?php the_permalink();?>" class="cover-link"></a>
          </div>
   
   
   
   <?php } ?>
   
   
   
   
	<?php } ?>
	<?php echo "</div></div>"; 
	wp_reset_postdata();
	?>
	
<?php 	
	echo wp_kses_post( $args['after_widget'] );
	
}

/**
 * Form processing.
 */
function update($new_instance, $old_instance) {
  $instance = $old_instance;
  $instance['asc_sort_order'] = strip_tags($new_instance['asc_sort_order']);
  $instance['excerpt_length'] = strip_tags($new_instance['excerpt_length']);
  $instance['sort_by'] = strip_tags($new_instance['sort_by']);
  $instance['num'] = strip_tags($new_instance['num']);
  $instance['slide_width'] = strip_tags($new_instance['slide_width']);
  $instance['slide_height'] = strip_tags($new_instance['slide_height']);
  $instance['posttype'] = strip_tags($new_instance['posttype']);
  $instance['title'] = strip_tags($new_instance['title']);
  $instance['container_class'] = strip_tags($new_instance['container_class']);
  $instance['posts_category'] = strip_tags($new_instance['posts_category']);
  $instance['date'] = strip_tags($new_instance['date']);
  $instance['show_category'] = strip_tags($new_instance['show_category']);
  $instance['show_title'] = strip_tags($new_instance['show_title']);
  $instance['show_border'] = strip_tags($new_instance['show_border']);
  $instance['excerpt'] = strip_tags($new_instance['excerpt']);
  $instance['more_link'] = strip_tags($new_instance['more_link']);
  $instance['more_link_class'] = strip_tags($new_instance['more_link_class']);
  $instance['more_link_text'] = strip_tags($new_instance['more_link_text']);
	return $instance;
}

/**
 * The configuration form.
 */
function form($instance) {
  /* Set up some default widget settings. */
  $defaults = array( 'title' => '', 'posttype' => '', 'num' => '', 'slide_width' => '280', 'slide_height' => '400', 'sort_by' => '', 'asc_sort_order' => '', 'show_category' => '', 'date' => '', 'container_class' => '', 'posts_category' => '', 'show_title' => true, 'show_border' => '', 'excerpt' => '', 'excerpt_length' => '', 'more_link' => '', 'more_link_text' => '', 'more_link_class' => '' );
  $instance = wp_parse_args( (array) $instance, $defaults );

  $sort_by = esc_attr($instance['sort_by']);
?>

  <p>
    <label for="<?php echo esc_attr($this->get_field_id("title")); ?>">
        <?php esc_html_e( 'Title', 'novablog' ); ?>:
        <input class="widefat" id="<?php echo esc_attr($this->get_field_id("title")); ?>" name="<?php echo esc_attr($this->get_field_name("title")); ?>" type="text" value="<?php echo esc_attr($instance["title"]); ?>" />
    </label>
  </p>
  <div style="width:230px; float:left; padding-right:20px; border-right:1px solid #c7c7c7;">

  <p>
      <label for="<?php echo esc_attr($this->get_field_id("num")); ?>">
          <?php esc_html_e('Number of posts to show', 'novablog'); ?>:
          <input style="text-align: center;" id="<?php echo esc_attr($this->get_field_id("num")); ?>" name="<?php echo esc_attr($this->get_field_name("num")); ?>" type="text" value="<?php echo absint($instance["num"]); ?>" size='4' />
      </label>
</p>
<p>
      <label for="<?php echo esc_attr($this->get_field_id("slide_width")); ?>">
          <?php esc_html_e('Image width', 'novablog'); ?>:
          <input style="text-align: center;" id="<?php echo esc_attr($this->get_field_id("slide_width")); ?>" name="<?php echo esc_attr($this->get_field_name("slide_width")); ?>" type="text" value="<?php echo absint($instance["slide_width"]); ?>" size='3' />
      </label>
</p>
  <p>
      <label for="<?php echo esc_attr($this->get_field_id("slide_height")); ?>">
          <?php esc_html_e('Image height', 'novablog'); ?>:
          <input style="text-align: center;" id="<?php echo esc_attr($this->get_field_id("slide_height")); ?>" name="<?php echo esc_attr($this->get_field_name("slide_height")); ?>" type="text" value="<?php echo absint($instance["slide_height"]); ?>" size='3' />
      </label>
</p>


<p>
  <label for="<?php echo esc_attr($this->get_field_id("sort_by")); ?>">
  <?php esc_html_e('Sort by', 'novablog'); ?>:
    <select id="<?php echo esc_attr($this->get_field_id("sort_by")); ?>" name="<?php echo esc_attr($this->get_field_name("sort_by")); ?>">
      <?php
        $options = array('date', 'title', 'rand');
            foreach ($options as $option) {
              echo '<option value="' . $option . '" id="' . $option . '"', $sort_by == $option ? ' selected="selected"' : '', '>', $option, '</option>';
            } ?>
    </select>
  </label>
</p>
  
<p>
  <label for="<?php echo esc_attr($this->get_field_id("asc_sort_order")); ?>">
    <input type="checkbox" class="checkbox" id="<?php echo esc_attr($this->get_field_id("asc_sort_order")); ?>" name="<?php echo esc_attr($this->get_field_name("asc_sort_order")); ?>" value="1" <?php checked( (bool) $instance["asc_sort_order"], true ); ?> />
          <?php esc_html_e( 'Reverse sort order (ascending)', 'novablog' ); ?>
  </label>
</p>
<p>
  <label for="<?php echo esc_attr($this->get_field_id("show_category")); ?>">
      <input type="checkbox" class="checkbox" id="<?php echo esc_attr($this->get_field_id("show_category")); ?>" name="<?php echo esc_attr($this->get_field_name("show_category")); ?>"<?php checked( (bool) $instance["show_category"], true ); ?> />
      <?php esc_html_e( 'Show category name', 'novablog' ); ?>
  </label>
</p>

<p>
  <label for="<?php echo esc_attr($this->get_field_id("date")); ?>">
      <input type="checkbox" class="checkbox" id="<?php echo esc_attr($this->get_field_id("date")); ?>" name="<?php echo esc_attr($this->get_field_name("date")); ?>"<?php checked( (bool) $instance["date"], true ); ?> />
      <?php esc_html_e( 'Show meta', 'novablog' ); ?>
  </label>
</p>

  <p>
      <label for="<?php echo esc_attr($this->get_field_id("show_title")); ?>">
          <input type="checkbox" class="checkbox" id="<?php echo esc_attr($this->get_field_id("show_title")); ?>" name="<?php echo esc_attr($this->get_field_name("show_title")); ?>"<?php checked( (bool) $instance["show_title"], true ); ?> />
          <?php esc_html_e( 'Show post title', 'novablog' ); ?>
      </label>
  </p>
  <p>
      <label for="<?php echo esc_attr($this->get_field_id("show_border")); ?>">
          <input type="checkbox" class="checkbox" id="<?php echo esc_attr($this->get_field_id("show_border")); ?>" name="<?php echo esc_attr($this->get_field_name("show_border")); ?>"<?php checked( (bool) $instance["show_border"], true ); ?> />
          <?php esc_html_e( 'Disable border ?', 'novablog' ); ?>
      </label>
  </p>
<p>
  <label for="<?php echo esc_attr($this->get_field_id("posts_category")); ?>">
    <?php esc_html_e( 'Category', 'novablog' ); ?>:
    <input class="widefat" id="<?php echo esc_attr($this->get_field_id("posts_category")); ?>" name="<?php echo esc_attr($this->get_field_name("posts_category")); ?>" type="text" value="<?php echo esc_attr($instance["posts_category"]); ?>" /> <span style="font-size:11px; color:#999;"><?php esc_html_e( '(category: enter category slug (NOT name))', 'novablog' ); ?></span>
  </label>
</p>


<p>
  <label for="<?php echo esc_attr($this->get_field_id("container_class")); ?>">
    <?php esc_html_e( 'Container class', 'novablog' ); ?>:
    <input class="widefat" id="<?php echo esc_attr($this->get_field_id("container_class")); ?>" name="<?php echo esc_attr($this->get_field_name("container_class")); ?>" type="text" value="<?php echo esc_attr($instance["container_class"]); ?>" /> <span style="font-size:11px; color:#999;"><?php esc_html_e( '(default: "featured_custom_posts")', 'novablog' ); ?></span>
  </label>
</p>
</div>
<div style="width:230px; float:left; padding-left:17px;">
  <fieldset style="border:1px solid #F1F1F1; padding:10px 10px 0; margin-bottom:1em;">
  <legend style="padding:0 5px;"><?php esc_html_e('Excerpt', 'novablog'); ?>:</legend>
  <p>
      <label for="<?php echo esc_attr($this->get_field_id("excerpt")); ?>">
          <input type="checkbox" class="checkbox" id="<?php echo esc_attr($this->get_field_id("excerpt")); ?>" name="<?php echo esc_attr($this->get_field_name("excerpt")); ?>"<?php checked( (bool) $instance["excerpt"], true ); ?> />
          <?php esc_html_e( 'Show post excerpt', 'novablog' ); ?>
      </label>
  </p>
  <p>
      <label for="<?php echo esc_attr($this->get_field_id("excerpt_length")); ?>">
          <?php esc_html_e( 'Excerpt length (words):', 'novablog' ); ?>
      </label>
      <input style="text-align: center;" type="text" id="<?php echo esc_attr($this->get_field_id("excerpt_length")); ?>" name="<?php echo esc_attr($this->get_field_name("excerpt_length")); ?>" value="<?php echo esc_attr($instance["excerpt_length"]); ?>" size="3" />
  </p>
  </fieldset>
  <fieldset style="border:1px solid #F1F1F1; padding:10px 10px 0; margin-bottom:1em;">
  <legend style="padding:0 5px;"><?php esc_html_e('More link', 'novablog'); ?>:</legend>
  <p>
      <label for="<?php echo esc_attr($this->get_field_id("more_link")); ?>">
          <input type="checkbox" class="checkbox" id="<?php echo esc_attr($this->get_field_id("more_link")); ?>" name="<?php echo esc_attr($this->get_field_name("more_link")); ?>"<?php checked( (bool) $instance["more_link"], true ); ?> />
          <?php esc_html_e( 'Show "More link"', 'novablog' ); ?>
      </label>
  </p>
  
  <p>
  <label for="<?php echo esc_attr($this->get_field_id("more_link_text")); ?>">
    <?php esc_html_e( 'Link text', 'novablog' ); ?>:
    <input class="widefat" id="<?php echo esc_attr($this->get_field_id("more_link_text")); ?>" name="<?php echo esc_attr($this->get_field_name("more_link_text")); ?>" type="text" value="<?php echo esc_attr($instance["more_link_text"]); ?>" /> <span style="font-size:11px; color:#999;"><?php esc_html_e( '(default: "Read more")', 'novablog' ); ?></span>
  </label>
  </p>
  <p>
  <label for="<?php echo esc_attr($this->get_field_id("more_link_class")); ?>">
    <?php esc_html_e( 'Link class', 'novablog' ); ?>:
    <input class="widefat" id="<?php echo esc_attr($this->get_field_id("more_link_class")); ?>" name="<?php echo esc_attr($this->get_field_name("more_link_class")); ?>" type="text" value="<?php echo esc_attr($instance["more_link_class"]); ?>" /> <span style="font-size:11px; color:#999;"><?php esc_html_e( '(default: "link")', 'novablog' ); ?></span>
  </label>
  </p>
  </fieldset>
</div>
<div style="clear:both;"></div>
		
<?php
}
}
?>