<?php
/**
 * Widget: Gallery
 *
 * @package WordPress
 * @subpackage Charity
 * @since Charity 1.0
 */
?>
<?php


add_action('widgets_init', function(){return register_widget("wpl_sidebar_gallery_widget");});
class wpl_sidebar_gallery_widget extends WP_Widget {


	/*-----------------------------------------------------------------------------------*/
	/*	Widget actual processes
	/*-----------------------------------------------------------------------------------*/

	public function __construct() {
		parent::__construct(
	 		'wpl_sidebar_gallery_widget',
			__( 'WPL Gallery (Sidebar)', 'charity-wpl' ),
			array( 'description' => __( 'A widget to displaying Gallery', 'charity-wpl' ), )
		);
	}

	/*-----------------------------------------------------------------------------------*/
	/*	Outputs the options form on admin
	/*-----------------------------------------------------------------------------------*/

	public function form( $instance ) {
		if ( $instance ) {
			$title = esc_attr( $instance[ 'title' ] );
			$categories = esc_attr( $instance[ 'categories' ] );
			$display_type = esc_attr( $instance[ 'display_type' ] );
			$nr_posts = esc_attr( $instance[ 'nr_posts' ] );
		}
		else {
			$title = __( ' Gallery', 'charity-wpl' );
			$categories = __( 'All', 'charity-wpl' );
			$display_type = 'rand';
			$nr_posts =  6;
		}

		 ?>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"> <?php _e('Title:', 'charity-wpl'); ?> </label>
				<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_html( $title ); ?>" />
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('categories') ); ?>">
					<?php _e('Category:', 'charity-wpl'); ?>
					<br />
				</label>

				<?php wp_dropdown_categories(
					array(
						'name'	=> $this->get_field_name("categories"),
						'show_option_all'    => __('All', 'charity-wpl'),
						'show_count'	=> 1,
						'selected' => $categories,
						'taxonomy'  => 'wpl_gallery_category'
					)
				); ?>
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('display_type') ); ?>"><?php _e('Order by:', 'charity-wpl'); ?> <br /> </label>
				<select id="<?php echo esc_attr( $this->get_field_id('display_type') ); ?>" name="<?php echo esc_attr( $this->get_field_name('display_type') ); ?>">
					<option value="rand" <?php selected( 'rand', $display_type ); ?>><?php _e('Random', 'charity-wpl'); ?></option>
					<option value="date" <?php selected( 'date', $display_type ); ?>><?php _e('Latest', 'charity-wpl'); ?></option>
				</select>
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('nr_posts') ); ?>"> <?php _e('Number of Gallery:', 'charity-wpl'); ?> </label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('nr_posts') ); ?>" name="<?php echo esc_attr( $this->get_field_name('nr_posts') ); ?>" type="text" value="<?php echo esc_attr( $nr_posts ); ?>" />
				<p style="font-size: 10px; color: #999; margin: -10px 0 0 0px; padding: 0px;"> <?php _e('Number of Gallery you want to display', 'charity-wpl'); ?></p>
			</p>

			<br />
			<p style="font-size: 10px; color: #999; margin: -10px 0 0 0px; padding: 0px;">
				<?php printf( __( 'The ID of this widget is: %1$s %3$s %2$s', 'charity-wpl' ), '<strong>', '</strong>',  '#'.esc_attr( $this->id ) ); ?>
			</p>
			<br />
		<?php
	}


	/*-----------------------------------------------------------------------------------*/
	/*	Processes widget options to be saved
	/*-----------------------------------------------------------------------------------*/

	public function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = sanitize_text_field($new_instance['title']);
		$instance['categories'] = sanitize_text_field($new_instance['categories']);
		$instance['nr_posts'] = sanitize_text_field($new_instance['nr_posts']);
		$instance['display_type'] = sanitize_text_field($new_instance['display_type']);
		return $instance;
	}


	/*-----------------------------------------------------------------------------------*/
	/*	Outputs the content of the widget
	/*-----------------------------------------------------------------------------------*/

	public function widget( $args, $instance ) {
		global $post;
		extract( $args );
		$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance );
		$categories = isset( $instance['categories'] ) ? esc_attr( $instance['categories'] ) : '';
		$nr_posts = isset( $instance['nr_posts'] ) ? esc_attr( $instance['nr_posts'] ) : '';
		$display_type = isset( $instance['display_type'] ) ? esc_attr( $instance['display_type'] ) : '';

		// WP_Query arguments
		$args = array(
			'post_type'              => 'post_gallery',
			'post_status'            => 'publish',
			'pagination'             => false,
			'posts_per_page'         => $nr_posts,
			'orderby'                => $display_type
			 );

		if ( $categories ) {
			$args['tax_query'] = array(
					array(
						'taxonomy' => 'wpl_gallery_category',
						'field' => 'id',
						'terms' => $categories
					)
				);
		}

		// The Query
		$the_query = null;
		$the_query = new WP_Query( $args ); ?>

			<aside class="widget widget_gallery">
				<h3 class="widget-title"><?php echo esc_html( $title ); ?></h3><!-- /.widget-title -->
				<div class="grid-x grid-margin-x">
					<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();  ?>
						<div class="cell small-4">
							<div class="image-item">
								<?php
									if ( has_post_thumbnail() ) {
										the_post_thumbnail('thumbnail');
								}?>
								<a href="<?php the_permalink(); ?>">
									<i class="fas fa-search"></i>
								</a>
							</div>
						</div>
					<?php endwhile; ?>
					<?php else: ?>
						<div class="cell small-12">
							<?php _e('Sorry, no posts matched your criteria.', 'charity-wpl'); ?>
						</div>
					<?php endif; ?>
					<?php wp_reset_postdata(); ?>
				</div><!-- /.grid-x grid-margin-x -->
			</aside><!--   <?php echo esc_attr($this->id); ?> -->
		<?php
	}
}
?>
