<?php

class MW_section_counter extends WP_Widget {

	function __construct(){
		parent::__construct( 
			'oginer_hompage_counter', 
			esc_html__('Oginer: Counter', 'oginer' ), 
			array(
				'classname' => 'mw_widget_homepage_counter',
				'description' => esc_html__( 'Display Counter.', 'oginer')
			)
		);
	}

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

		echo $args['before_widget'];
		
		$counter_icon = $instance[ 'oginer_counter_icon' ];
		$counter_number = $instance[ 'oginer_counter_number' ];
		$counter_title = $instance[ 'oginer_counter_title' ];
		
			if ( $counter_number > 0 ) {
			?>
				<div class="col-md-6 col-lg-3">
					<div class="counter-item">
						<div><i class="fa <?php echo esc_attr( $counter_icon ); ?>"></i></div>
						<h2><span class="counter-number"><?php echo esc_html( $counter_number ); ?></span></h2>
						<p><?php echo esc_html( $counter_title ); ?></p>
					</div>
				</div>
			<?php
			}
	
		echo $args['after_widget'];
	}
	
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		
		$instance['oginer_counter_icon'] = sanitize_text_field( $new_instance['oginer_counter_icon'] );
		$instance['oginer_counter_title'] = sanitize_text_field( $new_instance['oginer_counter_title'] );
		$instance['oginer_counter_number'] = absint( $new_instance['oginer_counter_number'] );
		
		return $instance;			
	}
	
	function form( $instance ) {
		?>
	
		<p>
			<label for="<?php echo $this->get_field_id( 'oginer_counter_icon' ); ?>"><?php _e( 'Counter Icon', 'oginer');?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'oginer_counter_icon' ); ?>" name="<?php echo $this->get_field_name( 'oginer_counter_icon' ); ?>" type="text" value="<?php echo $instance['oginer_counter_icon' ]; ?>" />
			<small><?php echo sprintf( __( 'Use font awesome icon: Eg: %1$s. %2$s See more here %3$s', 'oginer' ), 'fa-desktop','<a href="'.esc_url( 'https://fontawesome.com/v4.7.0/icons/' ).'" target="_blank">','</a>' ); ?></small>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'oginer_counter_title' ); ?>"><?php _e( 'Counter Title', 'oginer' );?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'oginer_counter_title' ); ?>" name="<?php echo $this->get_field_name( 'oginer_counter_title' ); ?>" type="text" value="<?php echo $instance[ 'oginer_counter_title' ]; ?>" />		
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'oginer_counter_number' ); ?>"><?php _e( 'Counter Number', 'oginer' );?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'oginer_counter_number' ); ?>" name="<?php echo $this->get_field_name( 'oginer_counter_number' ); ?>" type="text" value="<?php echo $instance[ 'oginer_counter_number' ]; ?>" />		
		</p>

		<?php
	}
}

function mw_section_counter(){
	register_widget( 'MW_section_counter' );
}
add_action( 'widgets_init', 'mw_section_counter' );