<?php
/**
 * Theme widgets
 *
 * @package Education_Soul
 */

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

if ( ! function_exists( 'education_soul_load_widgets' ) ) :

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

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

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

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

		// Quick Contact widget.
		register_widget( 'Education_Soul_Quick_Contact_Widget' );

		if ( class_exists( 'WooCommerce' ) ) {

			// Products Grid widget.
			register_widget( 'Education_Soul_Products_Grid_Widget' );
		}

	}

endif;

add_action( 'widgets_init', 'education_soul_load_widgets' );

if ( ! class_exists( 'Education_Soul_Social_Widget' ) ) :

	/**
	 * Social widget Class.
	 *
	 * @since 1.0.0
	 */
	class Education_Soul_Social_Widget extends Education_Soul_Widget_Base {

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

			$opts   = array(
				'classname'                   => 'education_soul_widget_social',
				'description'                 => __( 'Displays social icons.', 'education-soul-pro' ),
				'customize_selective_refresh' => true,
			);
			$fields = array(
				'title' => array(
					'label' => __( 'Title:', 'education-soul-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
				),
			);

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

			parent::__construct( 'education-soul-social', __( 'ES: Social', 'education-soul-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'] . esc_html( apply_filters( 'widget_title', $params['title'] ) ) . $args['after_title'];
			}

			if ( has_nav_menu( 'social' ) ) {
				wp_nav_menu(
					array(
						'theme_location' => 'social',
						'container'      => false,
						'depth'          => 1,
						'link_before'    => '<span class="screen-reader-text">',
						'link_after'     => '</span>',
						'item_spacing'   => 'discard',
					)
				);
			}

			echo $args['after_widget'];

		}
	}
endif;

if ( ! class_exists( 'Education_Soul_Featured_Page_Widget' ) ) :

	/**
	 * Featured page widget Class.
	 *
	 * @since 1.0.0
	 */
	class Education_Soul_Featured_Page_Widget extends Education_Soul_Widget_Base {

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

			$opts = array(
				'classname'                   => 'education_soul_widget_featured_page',
				'description'                 => __( 'Displays single featured Page or Post.', 'education-soul-pro' ),
				'customize_selective_refresh' => true,
			);

			$fields = array(
				'title'                    => array(
					'label' => esc_html__( 'Title:', 'education-soul-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
				),
				'featured_page'            => array(
					'label'            => esc_html__( 'Select Page:', 'education-soul-pro' ),
					'type'             => 'dropdown-pages',
					'show_option_none' => esc_html__( '&mdash; Select &mdash;', 'education-soul-pro' ),
				),
				'content_type'             => array(
					'label'   => esc_html__( 'Show Content:', 'education-soul-pro' ),
					'type'    => 'select',
					'default' => 'full',
					'options' => array(
						'excerpt' => esc_html__( 'Excerpt', 'education-soul-pro' ),
						'full'    => esc_html__( 'Full', 'education-soul-pro' ),
					),
				),
				'excerpt_length'           => array(
					'label'       => esc_html__( 'Excerpt Length:', 'education-soul-pro' ),
					'description' => esc_html__( 'Applies when Excerpt is selected in Show Content option.', 'education-soul-pro' ),
					'type'        => 'number',
					'css'         => 'max-width:60px;',
					'default'     => 40,
					'min'         => 1,
					'max'         => 400,
				),
				'featured_image'           => array(
					'label'   => esc_html__( 'Featured Image:', 'education-soul-pro' ),
					'type'    => 'select',
					'options' => education_soul_get_image_sizes_options(),
				),
				'featured_image_alignment' => array(
					'label'   => esc_html__( 'Image Alignment:', 'education-soul-pro' ),
					'type'    => 'select',
					'default' => 'center',
					'options' => education_soul_get_image_alignment_options(),
				),
			);

			parent::__construct( 'education-soul-featured-page', __( 'ES: Featured Page', 'education-soul-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'] . esc_html( $params['title'] ) . $args['after_title'];
			}
			?>

			<?php if ( absint( $params['featured_page'] ) > 0 ) : ?>

				<?php
				$qargs = array(
					'p'              => absint( $params['featured_page'] ),
					'posts_per_page' => 1,
					'no_found_rows'  => true,
					'post_type'      => 'page',
				);

				$the_query = new WP_Query( $qargs );
				?>

				<?php if ( $the_query->have_posts() ) : ?>

					<?php
					while ( $the_query->have_posts() ) :
						$the_query->the_post();
						?>

						<div class="featured-page-widget entry-content">
							<?php if ( 'disable' !== $params['featured_image'] && has_post_thumbnail() ) : ?>
								<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( esc_attr( $params['featured_image'] ), array( 'class' => 'align' . esc_attr( $params['featured_image_alignment'] ) ) ); ?></a>
							<?php endif; ?>
							<?php if ( 'excerpt' === $params['content_type'] ) : ?>
								<?php
								$excerpt = education_soul_the_excerpt( absint( $params['excerpt_length'] ) );
								echo wp_kses_post( wpautop( $excerpt ) );
								?>
							<?php else : ?>
								<?php the_content(); ?>
							<?php endif; ?>
						</div><!-- .featured-page-widget -->

					<?php endwhile; ?>

					<?php wp_reset_postdata(); ?>

				<?php endif; // End if have_posts(). ?>

			<?php endif; ?>

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

if ( ! class_exists( 'Education_Soul_Quick_Contact_Widget' ) ) :

	/**
	 * Quick contact widget Class.
	 *
	 * @since 1.0.0
	 */
	class Education_Soul_Quick_Contact_Widget extends Education_Soul_Widget_Base {

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

			$opts = array(
				'classname'                   => 'education_soul_widget_quick_contact',
				'description'                 => __( 'Displays quick contact', 'education-soul-pro' ),
				'customize_selective_refresh' => true,
				);
			$fields = array(
				'title' => array(
					'label' => __( 'Title:', 'education-soul-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				'contact_address' => array(
					'label' => __( 'Address:', 'education-soul-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				'contact_email' => array(
					'label' => __( 'Email:', 'education-soul-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				'contact_phone' => array(
					'label' => __( 'Phone:', 'education-soul-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				'contact_fax' => array(
					'label' => __( 'Fax:', 'education-soul-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
					),
				);

			parent::__construct( 'education-soul-quick-contact', __( 'ES: Quick Contact', 'education-soul-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'] . esc_html( $params['title'] ) . $args['after_title'];
			}
			?>
			<div class="contact-main-wrapper">
				<div class="contact-main-details">
					<?php if ( ! empty( $params['contact_phone'] ) ) : ?>
						<div class="contact-info-wrapper info-phone">
							<strong class="contact-type"><?php esc_html_e( 'Phone:', 'education-soul-pro' ); ?></strong>
							<span class="contact-detail"><a href="<?php echo esc_url( 'tel:' . preg_replace( '/\D+/', '', $params['contact_phone'] ) ); ?>"><?php echo esc_html( $params['contact_phone'] ); ?></a>	</span>
						</div>
					<?php endif; ?>

					<?php if ( ! empty( $params['contact_fax'] ) ) : ?>
						<div class="contact-info-wrapper info-fax">
							<strong class="contact-type"><?php esc_html_e( 'Fax:', 'education-soul-pro' ); ?></strong>
							<span class="contact-detail"><?php echo esc_html( $params['contact_fax'] ); ?></span>
						</div>
					<?php endif; ?>

					<?php if ( ! empty( $params['contact_address'] ) ) : ?>
						<div class="contact-info-wrapper info-address">
							<strong class="contact-type"><?php esc_html_e( 'Address:', 'education-soul-pro' ); ?></strong>
							<span class="contact-detail"><?php echo esc_html( $params['contact_address'] ); ?></span>
						</div>
					<?php endif; ?>
					<?php if ( ! empty( $params['contact_email'] ) ) : ?>
						<div class="contact-info-wrapper info-contact">
							<strong class="contact-type"><?php esc_html_e( 'Email:', 'education-soul-pro' ); ?></strong>
							<span class="contact-detail"><a href="<?php echo esc_url( 'mailto:' . $params['contact_email'] ); ?>"><?php echo esc_html( antispambot( $params['contact_email'] ) ); ?></a> </span>
						</div>
					<?php endif; ?>
				</div><!-- .contact-main-details -->
			</div><!-- .contact-main-wrapper -->
			<?php

			echo $args['after_widget'];

		}
	}
endif;

if ( ! class_exists( 'Education_Soul_Recent_Posts_Widget' ) ) :

	/**
	 * Recent posts widget Class.
	 *
	 * @since 1.0.0
	 */
	class Education_Soul_Recent_Posts_Widget extends Education_Soul_Widget_Base {

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

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

			parent::__construct( 'education-soul-recent-posts', __( 'ES: Recent Posts', 'education-soul-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'];
			}

			$qargs = array(
				'posts_per_page'      => absint( $params['post_number'] ),
				'no_found_rows'       => true,
				'ignore_sticky_posts' => true,
			);

			if ( absint( $params['post_category'] ) > 0 ) {
				$qargs['cat'] = $params['post_category'];
			}

			$the_query = new WP_Query( $qargs );
			?>

			<?php if ( $the_query->have_posts() ) : ?>

				<?php global $post; ?>

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

					<?php
					while ( $the_query->have_posts() ) :
						$the_query->the_post();
						?>

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

							<?php if ( 'disable' !== $params['featured_image'] ) : ?>
									<div class="recent-posts-thumb">
										<a href="<?php the_permalink(); ?>">
											<?php if ( has_post_thumbnail() ) : ?>
												<?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 );
												?>
											<?php else : ?>
												<img src="<?php echo esc_url( get_template_directory_uri() . '/images/no-image-square.png' ); ?>" alt="<?php the_title_attribute(); ?>" class="alignleft" style="max-width:<?php echo esc_attr( absint( $params['image_width'] ) ); ?>px;" />
											<?php endif; ?>
										</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>
										<?php endif; ?>
									</div><!-- .recent-posts-meta -->
								<?php endif; ?>

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

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

					<?php endwhile; ?>

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

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

			<?php endif; ?>

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

		}
	}
endif;

if ( ! class_exists( 'Education_Soul_Products_Grid_Widget' ) ) :

	/**
	 * Products Grid Class.
	 *
	 * @since 1.0.0
	 */
	class Education_Soul_Products_Grid_Widget extends Education_Soul_Widget_Base {

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

			$opts   = array(
				'classname'                   => 'education_soul_widget_products_grid',
				'description'                 => __( 'Displays products in grid.', 'education-soul-pro' ),
				'customize_selective_refresh' => true,
			);
			$fields = array(
				'title'            => array(
					'label' => __( 'Title:', 'education-soul-pro' ),
					'type'  => 'text',
					'class' => 'widefat',
				),
				'product_category' => array(
					'label'           => __( 'Select Product Category:', 'education-soul-pro' ),
					'type'            => 'dropdown-taxonomies',
					'taxonomy'        => 'product_cat',
					'show_option_all' => __( 'All Product Categories', 'education-soul-pro' ),
				),
				'post_number'      => array(
					'label'   => __( 'Number of Products:', 'education-soul-pro' ),
					'type'    => 'number',
					'default' => 6,
					'css'     => 'max-width:60px;',
					'min'     => 1,
					'max'     => 100,
				),
				'post_column'      => array(
					'label'   => __( 'Number of Columns:', 'education-soul-pro' ),
					'type'    => 'select',
					'default' => 4,
					'options' => education_soul_get_numbers_dropdown_options( 3, 4 ),
				),
			);

			parent::__construct( 'education-soul-products-grid', __( 'ES: Products Grid', 'education-soul-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'];
			}

			// Render now.
			$this->render_products( $params );

			echo $args['after_widget'];

		}

		/**
		 * Render products.
		 *
		 * @since 1.0.0
		 *
		 * @param array $params Parameters.
		 * @return void
		 */
		function render_products( $params ) {

			$query_args = array(
				'post_type'           => 'product',
				'post_status'         => 'publish',
				'ignore_sticky_posts' => true,
				'posts_per_page'      => absint( $params['post_number'] ),
				'no_found_rows'       => true,
			);
			if ( absint( $params['product_category'] ) > 0 ) {
				$query_args['tax_query'] = array(
					array(
						'taxonomy' => 'product_cat',
						'field'    => 'term_id',
						'terms'    => absint( $params['product_category'] ),
					),
				);
			}

			global $woocommerce_loop;
			$products = new WP_Query( $query_args );

			if ( $products->have_posts() ) {
				?>
				<div class="education-soul-woocommerce education-soul-woocommerce-product-grid-<?php echo absint( $params['post_column'] ); ?>">

					<ul class="products">

						<?php
						while ( $products->have_posts() ) :
							$products->the_post();
							?>

							<?php wc_get_template_part( 'content', 'product' ); ?>

						<?php endwhile; // End of the loop. ?>

					</ul><!-- .products -->

				</div><!-- .woocommerce -->
				<?php
			}
			woocommerce_reset_loop();
			wp_reset_postdata();

		}
	}
endif;
