<?php
/**
 * Theme widgets.
 *
 * @package Blogism
 */

// Load widget base.
require_once get_template_directory() . '/lib/widget-base/class-widget-base.php';

if ( ! function_exists( 'blogism_load_widgets' ) ) :

	/**
	 * Load widgets.
	 *
	 * @since 1.0.0
	 */
	function blogism_load_widgets() {

		// Social widget.
		register_widget( 'Blogism_Social_Widget' );

		// Image widget.
		register_widget( 'Blogism_Image_Widget' );

		// Featured Page widget.
		register_widget( 'Blogism_Featured_Page_Widget' );

		// Latest News widget.
		register_widget( 'Blogism_Latest_News_Widget' );

		// Call To Action widget.
		register_widget( 'Blogism_Call_To_Action_Widget' );

		// Recent Posts widget.
		register_widget( 'Blogism_Recent_Posts_Widget' );

	}

endif;

add_action( 'widgets_init', 'blogism_load_widgets' );

if ( ! class_exists( 'Blogism_Social_Widget' ) ) :

	/**
	 * Social widget Class.
	 *
	 * @since 1.0.0
	 */
	class Blogism_Social_Widget extends Blogism_Widget_Base {

		/**
		 * Sets up a new widget instance.
		 *
		 * @since 1.0.0
		 */
		function __construct() {

			$opts = array(
				'classname'                   => 'blogism_widget_social',
				'description'                 => esc_html__( 'Displays social icons.', 'blogism-pro' ),
				'customize_selective_refresh' => true,
				);
			$fields = array(
				'title' => array(
					'label' => esc_html__( 'Title:', 'blogism-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				'subtitle' => array(
					'label' => esc_html__( 'Subtitle:', 'blogism-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				);

			if ( false === has_nav_menu( 'social' ) ) {
				$fields['message'] = array(
					'label' => esc_html__( 'Social menu is not set. Please create menu and assign it to Social Menu.', 'blogism-pro' ),
					'type'  => 'message',
					'class' => 'widefat',
					);
			}

			parent::__construct( 'blogism-social', esc_html__( 'Blogism: Social', 'blogism-pro' ), $opts, array(), $fields );

		}

		/**
		 * Outputs the content for the current widget instance.
		 *
		 * @since 1.0.0
		 *
		 * @param array $args     Display arguments.
		 * @param array $instance Settings for the current widget instance.
		 */
		function widget( $args, $instance ) {

			$params = $this->get_params( $instance );

			echo $args['before_widget'];

			if ( ! empty( $params['title'] ) ) {
				echo $args['before_title'] . $params['title'] . $args['after_title'];
			}

			if ( ! empty( $params['subtitle'] ) ) {
				echo '<p class="widget-subtitle">' . esc_html( $params['subtitle'] ) . '</p>';
			}

			if ( has_nav_menu( 'social' ) ) {
				echo '<nav class="social-navigation" role="navigation" aria-label="' . esc_attr__( 'Social Menu', 'blogism-pro' ) . '">';
				wp_nav_menu( array(
					'theme_location' => 'social',
					'container'      => false,
					'depth'          => 1,
					'link_before'    => '<span class="screen-reader-text">',
					'link_after'     => '</span>',
				) );
				echo '</nav><!-- .social-navigation -->';
			}

			echo $args['after_widget'];

		}
	}
endif;

if ( ! class_exists( 'Blogism_Image_Widget' ) ) :

	/**
	 * Image widget Class.
	 *
	 * @since 1.0.0
	 */
	class Blogism_Image_Widget extends Blogism_Widget_Base {

		/**
		 * Sets up a new widget instance.
		 *
		 * @since 1.0.0
		 */
		function __construct() {

			$opts = array(
				'classname'                   => 'blogism_widget_image',
				'description'                 => esc_html__( 'Displays a image with link.', 'blogism-pro' ),
				'customize_selective_refresh' => true,
				);
			$fields = array(
				'title' => array(
					'label' => esc_html__( 'Title:', 'blogism-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				'image_url' => array(
					'label' => esc_html__( 'Image:', 'blogism-pro' ),
					'type'  => 'image',
					),
				'link_url' => array(
					'label' => esc_html__( 'URL:', 'blogism-pro' ),
					'type'  => 'url',
					'class' => 'widefat',
					),
				);

			parent::__construct( 'blogism-image', esc_html__( 'Blogism: Image', 'blogism-pro' ), $opts, array(), $fields );

		}

		/**
		 * Outputs the content for the current widget instance.
		 *
		 * @since 1.0.0
		 *
		 * @param array $args     Display arguments.
		 * @param array $instance Settings for the current widget instance.
		 */
		function widget( $args, $instance ) {

			$params = $this->get_params( $instance );

			echo $args['before_widget'];

			if ( ! empty( $params['title'] ) ) {
				echo $args['before_title'] . $params['title'] . $args['after_title'];
			}

			if ( ! empty( $params['image_url'] ) ) {
				$link_open  = '';
				$link_close = '';
				if ( ! empty( $params['link_url'] ) ) {
					$link_open  = '<a href="' . esc_url( $params['link_url'] ) . '">';
					$link_close = '</a>';
				}
				echo $link_open;
				echo '<img src="' . esc_url( $params['image_url'] ) . '" />';
				echo $link_close;
			}

			echo $args['after_widget'];

		}
	}
endif;

if ( ! class_exists( 'Blogism_Featured_Page_Widget' ) ) :

	/**
	 * Featured page widget Class.
	 *
	 * @since 1.0.0
	 */
	class Blogism_Featured_Page_Widget extends Blogism_Widget_Base {

		/**
		 * Sets up a new widget instance.
		 *
		 * @since 1.0.0
		 */
		function __construct() {

			$opts = array(
				'classname'                   => 'blogism_widget_featured_page',
				'description'                 => esc_html__( 'Displays single featured Page or Post.', 'blogism-pro' ),
				'customize_selective_refresh' => true,
				);
			$fields = array(
				'title' => array(
					'label' => esc_html__( 'Title:', 'blogism-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				'subtitle' => array(
					'label' => esc_html__( 'Subtitle:', 'blogism-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				'use_page_title' => array(
					'label'   => esc_html__( 'Use Page/Post Title as Widget Title', 'blogism-pro' ),
					'type'    => 'checkbox',
					'default' => true,
					),
				'featured_page' => array(
					'label'            => esc_html__( 'Select Page:', 'blogism-pro' ),
					'type'             => 'dropdown-pages',
					'show_option_none' => esc_html__( '&mdash; Select &mdash;', 'blogism-pro' ),
					),
				'id_message' => array(
					'label'            => '<strong>' . _x( 'OR', 'featured page widget', 'blogism-pro' ) . '</strong>',
					'type'             => 'message',
					),
				'featured_post' => array(
					'label'             => esc_html__( 'Post ID:', 'blogism-pro' ),
					'placeholder'       => esc_html__( 'Eg: 1234', 'blogism-pro' ),
					'type'              => 'text',
					'sanitize_callback' => 'blogism_widget_sanitize_post_id',
					),
				'content_type' => array(
					'label'   => esc_html__( 'Show Content:', 'blogism-pro' ),
					'type'    => 'select',
					'default' => 'full',
					'options' => array(
						'excerpt' => esc_html__( 'Excerpt', 'blogism-pro' ),
						'full'    => esc_html__( 'Full', 'blogism-pro' ),
						),
					),
				'excerpt_length' => array(
					'label'       => esc_html__( 'Excerpt Length:', 'blogism-pro' ),
					'description' => esc_html__( 'Applies when Excerpt is selected in Content option.', 'blogism-pro' ),
					'type'        => 'number',
					'css'         => 'max-width:60px;',
					'default'     => 40,
					'min'         => 1,
					'max'         => 400,
					),
				'featured_image' => array(
					'label'   => esc_html__( 'Featured Image:', 'blogism-pro' ),
					'type'    => 'select',
					'options' => blogism_get_image_sizes_options(),
					),
				'featured_image_alignment' => array(
					'label'   => esc_html__( 'Image Alignment:', 'blogism-pro' ),
					'type'    => 'select',
					'default' => 'center',
					'options' => blogism_get_image_alignment_options(),
					),
				);

			parent::__construct( 'blogism-featured-page', esc_html__( 'Blogism: Featured Page', 'blogism-pro' ), $opts, array(), $fields );

		}

		/**
		 * Outputs the content for the current widget instance.
		 *
		 * @since 1.0.0
		 *
		 * @param array $args     Display arguments.
		 * @param array $instance Settings for the current widget instance.
		 */
		function widget( $args, $instance ) {

			$params = $this->get_params( $instance );

			echo $args['before_widget'];

			$our_id = '';

			if ( absint( $params['featured_post'] ) > 0 ) {
				$our_id = absint( $params['featured_post'] );
			}

			if ( absint( $params['featured_page'] ) > 0 ) {
				$our_id = absint( $params['featured_page'] );
			}

			if ( absint( $our_id ) > 0 ) {

				$qargs = array(
					'p'                   => absint( $our_id ),
					'post_type'           => array( 'page', 'post' ),
					'no_found_rows'       => true,
					'ignore_sticky_posts' => true,
					);

				$the_query = new WP_Query( $qargs );

				if ( $the_query->have_posts() ) {
					while ( $the_query->have_posts() ) {
						$the_query->the_post();

						if ( true === $params['use_page_title'] ) {
							$params['title'] = get_the_title();
						}

						if ( ! empty( $params['title'] ) ) {
							echo $args['before_title'] . $params['title'] . $args['after_title'];
						}

						if ( ! empty( $params['subtitle'] ) ) {
							echo '<p class="widget-subtitle">' . esc_html( $params['subtitle'] ) . '</p>';
						}

						echo '<div class="featured-page-widget entry-content">';

						if ( 'disable' !== $params['featured_image'] && has_post_thumbnail() ) {
							the_post_thumbnail( esc_attr( $params['featured_image'] ), array( 'class' => 'align' . esc_attr( $params['featured_image_alignment'] ) ) );
						}

						if ( 'excerpt' === $params['content_type'] ) {
							$excerpt = blogism_the_excerpt( absint( $params['excerpt_length'] ) );
							echo wp_kses_post( wpautop( $excerpt ) );
						} else {
							the_content( sprintf(
								/* translators: %s: Name of current post. */
								wp_kses( __( 'Continue reading %s <span class="meta-nav">&rarr;</span>', 'blogism-pro' ), array( 'span' => array( 'class' => array() ) ) ),
								the_title( '<span class="screen-reader-text">"', '"</span>', false )
							) );
						}


						echo '</div><!-- .featured-page-widget -->';

					} // End while.

					// Reset.
					wp_reset_postdata();

				} // End if.

			}

			echo $args['after_widget'];

		}
	}
endif;

if ( ! class_exists( 'Blogism_Latest_News_Widget' ) ) :

	/**
	 * Latest news widget Class.
	 *
	 * @since 1.0.0
	 */
	class Blogism_Latest_News_Widget extends Blogism_Widget_Base {

		/**
		 * Sets up a new widget instance.
		 *
		 * @since 1.0.0
		 */
		function __construct() {
			$opts = array(
				'classname'                   => 'blogism_widget_latest_news',
				'description'                 => esc_html__( 'Displays latest posts in grid.', 'blogism-pro' ),
				'customize_selective_refresh' => true,
				);
			$fields = array(
				'title' => array(
					'label' => esc_html__( 'Title:', 'blogism-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				'subtitle' => array(
					'label' => esc_html__( 'Subtitle:', 'blogism-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				'post_category' => array(
					'label'           => esc_html__( 'Select Category:', 'blogism-pro' ),
					'type'            => 'dropdown-taxonomies',
					'show_option_all' => esc_html__( 'All Categories', 'blogism-pro' ),
					),
				'post_layout' => array(
					'label'    => __( 'Post Layout:', 'blogism-pro' ),
					'type'     => 'select',
					'default'  => 1,
					'adjacent' => true,
					'options'  => blogism_get_numbers_dropdown_options( 1, 2, __( 'Layout', 'blogism-pro' ) . ' ' ),
					),
				'post_number' => array(
					'label'   => esc_html__( 'Number of Posts:', 'blogism-pro' ),
					'type'    => 'number',
					'default' => 3,
					'css'     => 'max-width:60px;',
					'min'     => 1,
					'max'     => 100,
					),
				'post_column' => array(
					'label'   => esc_html__( 'Number of Columns:', 'blogism-pro' ),
					'type'    => 'select',
					'default' => 3,
					'options' => blogism_get_numbers_dropdown_options( 1, 4 ),
					),
				'featured_image' => array(
					'label'   => esc_html__( 'Featured Image:', 'blogism-pro' ),
					'type'    => 'select',
					'default' => 'medium',
					'options' => blogism_get_image_sizes_options(),
					),
				'excerpt_length' => array(
					'label'       => esc_html__( 'Excerpt Length:', 'blogism-pro' ),
					'description' => esc_html__( 'in words', 'blogism-pro' ),
					'type'        => 'number',
					'css'         => 'max-width:60px;',
					'default'     => 15,
					'min'         => 1,
					'max'         => 400,
					'adjacent'    => true,
					),
				'more_text' => array(
					'label'   => esc_html__( 'More Text:', 'blogism-pro' ),
					'type'    => 'text',
					'default' => esc_html__( 'Read more', 'blogism-pro' ),
					),
				'disable_date' => array(
					'label'   => esc_html__( 'Disable Date', 'blogism-pro' ),
					'type'    => 'checkbox',
					'default' => false,
					),
				'disable_excerpt' => array(
					'label'   => esc_html__( 'Disable Excerpt', 'blogism-pro' ),
					'type'    => 'checkbox',
					'default' => false,
					),
				'disable_more_text' => array(
					'label'   => esc_html__( 'Disable More Text', 'blogism-pro' ),
					'type'    => 'checkbox',
					'default' => false,
					),
				);

			parent::__construct( 'blogism-latest-news', esc_html__( 'Blogism: Latest News', 'blogism-pro' ), $opts, array(), $fields );
		}

		/**
		 * Outputs the content for the current widget instance.
		 *
		 * @since 1.0.0
		 *
		 * @param array $args     Display arguments.
		 * @param array $instance Settings for the current widget instance.
		 */
		function widget( $args, $instance ) {

			$params = $this->get_params( $instance );

			echo $args['before_widget'];

			if ( ! empty( $params['title'] ) ) {
				echo $args['before_title'] . $params['title'] . $args['after_title'];
			}
			if ( ! empty( $params['subtitle'] ) ) {
				echo '<p class="widget-subtitle">' . esc_html( $params['subtitle'] ) . '</p>';
			}

			$qargs = array(
				'posts_per_page' => esc_attr( $params['post_number'] ),
				'no_found_rows'  => true,
				);
			if ( absint( $params['post_category'] ) > 0  ) {
				$qargs['cat'] = absint( $params['post_category'] );
			}
			$all_posts = get_posts( $qargs );
			?>
			<?php if ( ! empty( $all_posts ) ) : ?>

				<?php global $post; ?>

				<div class="latest-news-widget latest-news-layout-<?php echo esc_attr( $params['post_layout'] ); ?> latest-news-col-<?php echo esc_attr( $params['post_column'] ); ?>">

					<div class="inner-wrapper">

						<?php foreach ( $all_posts as $key => $post ) : ?>
							<?php setup_postdata( $post ); ?>

							<div class="latest-news-item">
							<div class="latest-news-item-wrapper">
									<?php if ( 'disable' !== $params['featured_image'] && has_post_thumbnail() ) : ?>
										<div class="latest-news-thumb">
											<a href="<?php the_permalink(); ?>">
												<?php
												$img_attributes = array( 'class' => 'aligncenter' );
												the_post_thumbnail( esc_attr( $params['featured_image'] ), $img_attributes );
												?>
											</a>
										</div><!-- .latest-news-thumb -->
									<?php endif; ?>
									<div class="latest-news-text-wrap">

										<div class="latest-news-text-content">
											<h3 class="latest-news-title">
												<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
											</h3><!-- .latest-news-title -->

											<?php if ( false === $params['disable_excerpt'] ) :  ?>
												<div class="latest-news-summary">
												<?php
												$summary = blogism_the_excerpt( esc_attr( $params['excerpt_length'] ), $post );
												echo wp_kses_post( wpautop( $summary ) );
												?>
												</div><!-- .latest-news-summary -->
											<?php endif; ?>
										</div><!-- .latest-news-text-content -->

										<?php if ( false === $params['disable_date'] || false === $params['disable_more_text'] ) : ?>
											<div class="latest-news-meta">

											<?php if ( false === $params['disable_date'] ) : ?>
												<span class="latest-news-date"><?php the_time( 'j M Y' ); ?></span>
											<?php endif; ?>
											<?php if ( false === $params['disable_more_text'] ) : ?>
												<a href="<?php the_permalink(); ?>" class="news-read-more"><i class="fa fa-share" aria-hidden="true"></i><?php echo esc_html( $params['more_text'] ); ?><span class="screen-reader-text">"<?php the_title(); ?>"</span>
												</a>
											<?php endif; ?>

											</div><!-- .latest-news-meta -->
										<?php endif; ?>

									</div><!-- .latest-news-text-wrap -->
								</div><!--.latest-news-item-wrapper -->
							</div><!-- .latest-news-item -->

						<?php endforeach; ?>

					</div><!-- .row -->

				</div><!-- .latest-news-widget -->

				<?php wp_reset_postdata(); ?>

			<?php endif; ?>

			<?php echo $args['after_widget'];

		}
	}
endif;


if ( ! class_exists( 'Blogism_Call_To_Action_Widget' ) ) :

	/**
	 * Call to action widget Class.
	 *
	 * @since 1.0.0
	 */
	class Blogism_Call_To_Action_Widget extends Blogism_Widget_Base {

		/**
		 * Sets up a new widget instance.
		 *
		 * @since 1.0.0
		 */
		function __construct() {

			$opts = array(
				'classname'                   => 'blogism_widget_call_to_action',
				'description'                 => esc_html__( 'Call To Action Widget.', 'blogism-pro' ),
				'customize_selective_refresh' => true,
				);
			$fields = array(
				'title' => array(
					'label' => esc_html__( 'Title:', 'blogism-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				'description' => array(
					'label' => esc_html__( 'Description:', 'blogism-pro' ),
					'type'  => 'textarea',
					'class' => 'widefat',
					),
				'primary_button_text' => array(
					'label'   => esc_html__( 'Primary Button Text:', 'blogism-pro' ),
					'default' => esc_html__( 'Learn more', 'blogism-pro' ),
					'type'    => 'text',
					'class'   => 'widefat',
					),
				'primary_button_url' => array(
					'label' => esc_html__( 'Primary Button URL:', 'blogism-pro' ),
					'type'  => 'url',
					'class' => 'widefat',
					),
				'secondary_button_text' => array(
					'label'   => esc_html__( 'Secondary Button Text:', 'blogism-pro' ),
					'default' => '',
					'type'    => 'text',
					'class'   => 'widefat',
					),
				'secondary_button_url' => array(
					'label' => esc_html__( 'Secondary Button URL:', 'blogism-pro' ),
					'type'  => 'url',
					'class' => 'widefat',
					),
				'image_url' => array(
					'label' => esc_html__( 'Background Image:', 'blogism-pro' ),
					'type'  => 'image',
					),
				);

			parent::__construct( 'blogism-call-to-action', esc_html__( 'Blogism: Call To Action', 'blogism-pro' ), $opts, array(), $fields );

		}

		/**
		 * Outputs the content for the current widget instance.
		 *
		 * @since 1.0.0
		 *
		 * @param array $args     Display arguments.
		 * @param array $instance Settings for the current widget instance.
		 */
		function widget( $args, $instance ) {

			$params = $this->get_params( $instance );

			if ( ! empty( $params['image_url'] ) ) {
				echo '<div class="background-wrapper" style="background-image:url(' . esc_url( $params['image_url'] ) . ');background-position: center center;">';
			}

			echo $args['before_widget'];

			echo '<div class="cta-wrapper">';
			?>
			<div class="call-to-action-content">
				<?php
				if ( ! empty( $params['title'] ) ) {
					echo $args['before_title'] . $params['title'] . $args['after_title'];
				}
				?>
				<?php if ( ! empty( $params['description'] ) ) : ?>
					<div class="call-to-action-description">
						<?php echo wp_kses_post( wpautop( $params['description'] ) ); ?>
					</div><!-- .call-to-action-description -->
				<?php endif; ?>
				<?php if ( ! empty( $params['primary_button_text'] ) || ! empty( $params['secondary_button_text'] ) ) : ?>
					<div class="call-to-action-buttons">
						<?php if ( ! empty( $params['primary_button_text'] ) ) : ?>
							<a href="<?php echo esc_url( $params['primary_button_url'] ); ?>" class="custom-button btn-call-to-action btn-call-to-primary"><?php echo esc_html( $params['primary_button_text'] ); ?></a>
						<?php endif; ?>
						<?php if ( ! empty( $params['secondary_button_text'] ) ) : ?>
							<a href="<?php echo esc_url( $params['secondary_button_url'] ); ?>" class="custom-button btn-call-to-action btn-call-to-secondary"><?php echo esc_html( $params['secondary_button_text'] ); ?></a>
						<?php endif; ?>
					</div><!-- .call-to-action-buttons -->
				<?php endif; ?>
			</div><!-- .call-to-action-content -->
			<?php
			echo '</div><!-- .cta-wrapper -->';

			echo $args['after_widget'];

			if ( ! empty( $params['image_url'] ) ) {
				echo '</div><!-- .background-wrapper -->';
			}

		}
	}
endif;

if ( ! class_exists( 'Blogism_Recent_Posts_Widget' ) ) :

	/**
	 * Recent posts widget Class.
	 *
	 * @since 1.0.0
	 */
	class Blogism_Recent_Posts_Widget extends Blogism_Widget_Base {

		/**
		 * Sets up a new widget instance.
		 *
		 * @since 1.0.0
		 */
		function __construct() {

			$opts = array(
				'classname'                   => 'blogism_widget_recent_posts',
				'description'                 => esc_html__( 'Displays recent posts.', 'blogism-pro' ),
				'customize_selective_refresh' => true,
				);
			$fields = array(
				'title' => array(
					'label' => esc_html__( 'Title:', 'blogism-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				'subtitle' => array(
					'label' => esc_html__( 'Subtitle:', 'blogism-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				'post_category' => array(
					'label'           => esc_html__( 'Select Category:', 'blogism-pro' ),
					'type'            => 'dropdown-taxonomies',
					'show_option_all' => esc_html__( 'All Categories', 'blogism-pro' ),
					),
				'post_number' => array(
					'label'   => esc_html__( 'Number of Posts:', 'blogism-pro' ),
					'type'    => 'number',
					'default' => 4,
					'css'     => 'max-width:60px;',
					'min'     => 1,
					'max'     => 100,
					),
				'featured_image' => array(
					'label'   => esc_html__( 'Featured Image:', 'blogism-pro' ),
					'type'    => 'select',
					'default' => 'thumbnail',
					'options' => blogism_get_image_sizes_options( true, array( 'disable', 'thumbnail' ), false ),
					),
				'image_width' => array(
					'label'       => esc_html__( 'Image Width:', 'blogism-pro' ),
					'type'        => 'number',
					'description' => esc_html__( 'px', 'blogism-pro' ),
					'css'         => 'max-width:60px;',
					'adjacent'    => true,
					'default'     => 90,
					'min'         => 1,
					'max'         => 150,
					),
				'disable_date' => array(
					'label'   => esc_html__( 'Disable Date', 'blogism-pro' ),
					'type'    => 'checkbox',
					'default' => false,
					),
				);

			parent::__construct( 'blogism-recent-posts', esc_html__( 'Blogism: Recent Posts', 'blogism-pro' ), $opts, array(), $fields );

		}

		/**
		 * Outputs the content for the current widget instance.
		 *
		 * @since 1.0.0
		 *
		 * @param array $args     Display arguments.
		 * @param array $instance Settings for the current widget instance.
		 */
		function widget( $args, $instance ) {

			$params = $this->get_params( $instance );

			echo $args['before_widget'];

			if ( ! empty( $params['title'] ) ) {
				echo $args['before_title'] . $params['title'] . $args['after_title'];
			}

			if ( ! empty( $params['subtitle'] ) ) {
				echo '<p class="widget-subtitle">' . esc_html( $params['subtitle'] ) . '</p>';
			}

			$qargs = array(
				'posts_per_page' => esc_attr( $params['post_number'] ),
				'no_found_rows'  => true,
				);
			if ( absint( $params['post_category'] ) > 0  ) {
				$qargs['cat'] = $params['post_category'];
			}
			$all_posts = get_posts( $qargs );

			?>
			<?php if ( ! empty( $all_posts ) ) :  ?>

				<?php global $post; ?>

				<div class="recent-posts-wrapper">

					<?php foreach ( $all_posts as $key => $post ) :  ?>
						<?php setup_postdata( $post ); ?>

						<div class="recent-posts-item">

							<?php if ( 'disable' !== $params['featured_image'] && has_post_thumbnail() ) :  ?>
								<div class="recent-posts-thumb">
									<a href="<?php the_permalink(); ?>">
										<?php
										$img_attributes = array(
											'class' => 'alignleft',
											'style' => 'max-width:' . esc_attr( $params['image_width'] ). 'px;',
											);
										the_post_thumbnail( esc_attr( $params['featured_image'] ), $img_attributes );
										?>
									</a>
								</div><!-- .recent-posts-thumb -->
							<?php endif ?>
							<div class="recent-posts-text-wrap">
								<h3 class="recent-posts-title">
									<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
								</h3><!-- .recent-posts-title -->

								<?php if ( false === $params['disable_date'] ) :  ?>
									<div class="recent-posts-meta">

										<?php if ( false === $params['disable_date'] ) :  ?>
											<span class="recent-posts-date"><?php the_time( get_option( 'date_format' ) ); ?></span><!-- .recent-posts-date -->
										<?php endif; ?>

									</div><!-- .recent-posts-meta -->
								<?php endif; ?>

							</div><!-- .recent-posts-text-wrap -->

						</div><!-- .recent-posts-item -->

					<?php endforeach; ?>

				</div><!-- .recent-posts-wrapper -->

				<?php wp_reset_postdata(); // Reset. ?>

			<?php endif; ?>

			<?php
			echo $args['after_widget'];

		}
	}
endif;
