<?php

if ( ! class_exists( 'moral_magazine_About_Widget' ) ) :


	class Moral_Magazine_About_Widget extends WP_Widget {
		/**
         * Sets up the widgets name etc
         */
		public function __construct() {
			$mt_about_widgets = array(
				'classname'   => 'widget_about_us',
				'description' => esc_html__( 'About Us Image', 'moral-magazine' ),
			);
			parent::__construct( 'moral_magazine_About_Widget', esc_html__('MT: About','moral-magazine'), $mt_about_widgets );
		}

		public function widget( $args, $instance ) {
			extract( $args , EXTR_SKIP );

			$mtiw_title   			= ( ! empty( $instance['title'] ) ) ? ( $instance['title'] ) : __( 'About US', 'moral-magazine' );

            $mtiw_title   			= apply_filters( 'widget_title', $mtiw_title , $instance, $this->id_base );

            $mtiw_name   			= ( ! empty( $instance['name'] ) ) ? ( $instance['name'] ) : '';

            $mtiw_description   	= ( ! empty( $instance['description'] ) ) ? ( $instance['description'] ) : '';

			$mtiw_image_url     	= ! empty( $instance['tpiw_image_url'] ) ? $instance['tpiw_image_url'] : '';
	        $open_link  			= ! empty( $instance['open_link'] ) ? true : false;
			$target 				= ( empty( $open_link ) ) ? '' : 'target="_blank"';
			$number 				= isset( $instance['number'] ) ? absint( $instance['number'] ) : 3; 

			echo $args['before_widget'];
				if ( ! empty( $mtiw_title ) ) {
					echo "<div class='widget-header'>";
					echo $args['before_title'] . esc_html( $mtiw_title ) . $args['after_title'];
					echo "</div>";
				}
			?>

	            <ul class="about-us-post">
	            	<li>	
	            		<div class="entry-container">	
	            			<div class="featured-image">	
	            				<?php
					                if ( ! empty( $mtiw_image_url ) ) {
										$sizes = array();
										echo  '<img src="' . esc_url( $mtiw_image_url ) . '" alt="' . esc_attr( $mtiw_name  ) . '"  />';
							        } // End if : image is there.
						    	?>
	            			</div>

	            			<header class="entry-header">	
	            				<h2 class="entry-title"><?php echo esc_html( $mtiw_name ); ?></h2>
	            			</header>

				            <div class="entry-content">	
				            	<p><?php echo esc_html( $mtiw_description ); ?> </p>
				            </div> 

			                <div class="social-icons">
			                    <ul class="menu">
									<?php
									for ( $i=1; $i <= $number ; $i++ ) {
										$link = ( ! empty( $instance['link' . '-' . $i] ) ) ? $instance['link' . '-' . $i] : ''; 
										if ( ! empty( $link ) ) :
									?>
								        <li>
								        	<a href="<?php echo esc_url( $link ) . '" ' . esc_attr( $target ); ?>"><?php echo moral_magazine_return_social_icon( $link ); ?>
								        	</a>
								        </li>
									<?php endif;
									} ?>
					     		</ul>
			                </div><!-- .social-icons -->
	            		</div>	            		
	            	</li>		            
	            </ul><!-- .textwidget -->
        <?php
			echo $args['after_widget'];
		}

		/**
		 * Back-end widget form.
		 *
		 * @see WP_Widget::form()
		 *
		 * @param array $instance Previously saved values from database.
		 */
		public function form( $instance ) {
			// Defaults.
	        $instance = wp_parse_args( (array) $instance, array(
				'title'                	=>  '',
				'name'                	=>  '',
				'description'           =>  '',
				'tpiw_image_url'       	=>  '',
				'tpiw_link'            	=>  '',
				'number'        		=>  3,
				'open_link'       		=>  0,
	      	) );

			$mtiw_title                	= htmlspecialchars( $instance['title'] );
			$mtiw_name                	= htmlspecialchars( $instance['name'] );
			$mtiw_description           = htmlspecialchars( $instance['description'] );
			$mtiw_image_url             = isset( $instance['tpiw_image_url'] ) ? $instance['tpiw_image_url'] : '';
			$number 	= isset( $instance['number'] ) ? absint( $instance['number'] ) : 3;
			$open_link 	= isset( $instance['open_link'] ) ? $instance['open_link'] : false;
			?>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title :', 'moral-magazine' ); ?></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_attr( $mtiw_title ); ?>">
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'name' ) ); ?>"><?php esc_html_e( 'Name :', 'moral-magazine' ); ?></label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'name' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'name' ) ); ?>" type="text" value="<?php echo esc_attr( $mtiw_name ); ?>">
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'description' ) ); ?>"><?php esc_html_e( 'Description :', 'moral-magazine' ); ?></label>
				<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'description' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'description' ) ); ?>"  value="<?php echo esc_attr( $mtiw_description ); ?>"> <?php echo wp_kses_post( $mtiw_description ); ?> </textarea>
			</p>


			<!-- Place holder for image upload -->
			<div>
				<label for="<?php echo esc_attr( $this->get_field_id( 'tpiw_image_url' ) ); ?>"><?php esc_html_e( 'Image URL', 'moral-magazine' ); ?></label>:<br />
				<input type="url" class="img widefat" name="<?php echo esc_attr( $this->get_field_name( 'tpiw_image_url' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'tpiw_image_url' ) ); ?>" value="<?php echo esc_url( $mtiw_image_url ); ?>" />

	    	</div>

		   <p>
			   	<label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php esc_html_e( 'Number of links to show:', 'moral-magazine' ); ?></label>
			   	<input class="tiny-text" id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="number" step="1" min="1" max="6" value="<?php echo absint( $number ); ?>" size="3" />
		   </p>

		   <p>
		    	<label for="<?php echo esc_attr( $this->get_field_id( 'open_link' ) ); ?>"><?php esc_html_e( 'Open Social link in New Tab', 'moral-magazine' ); ?>:</label>
		      	<input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'open_link' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'open_link' ), 'moral-magazine' ); ?>"  <?php checked( $open_link, true ); ?> />
		   </p>

		   <?php for ( $i=1; $i <= $number; $i++ ) {
		   	$link = isset( $instance['link'. '-' . $i ] ) ? $instance['link' . '-' . $i ] : '';?>
			   <p>
				   	<label for="<?php echo esc_attr( $this->get_field_id( 'link' . '-' . $i ) ); ?>"><?php printf( esc_html__( 'Link %s :', 'moral-magazine' ), $i ); ?></label>
				   	<input type="url" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'link' . '-' . $i ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'link' . '-' . $i ) ); ?>" value="<?php echo esc_url( $link ); ?>"/>
		     	</p>
		   <?php }?>
		<?php
		}

		/**
		 * Sanitize widget form values as they are saved.
		 *
		 * @see WP_Widget::update()
		 *
		 * @param array $new_instance Values just sent to be saved.
		 * @param array $old_instance Previously saved values from database.
		 *
		 * @return array Updated safe values to be saved.
		 */
		public function update( $new_instance, $old_instance ) {
			$instance                         = $old_instance;

			$instance['title']               = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ): __( 'About US', 'moral-magazine' );
			$instance['name']                = ( ! empty( $new_instance['name'] ) ) ? sanitize_text_field( $new_instance['name'] ): '';
			$instance['description']         = ( ! empty( $new_instance['description'] ) ) ? wp_kses_post( $new_instance['description'] ): '';
			$instance['tpiw_image_url']      = esc_url_raw( $new_instance['tpiw_image_url'] );
			$instance['number'] 			= absint( $new_instance['number'] );
			$instance['open_link']       	= moral_magazine_sanitize_checkbox( $new_instance['open_link'] );
			for ( $i=1; $i <= $instance['number']; $i++ ) {
				$instance['link' . '-' . $i] = esc_url_raw( $new_instance['link' . '-' . $i] );
			}

			return $instance;
		}

	} // class tp_about_us_image_widget
endif;
