<?php
/**
 * Widget Name: AIO Texts
 *
 * @package All In One Custom Post
 * @since All In One Custom Post 1.0.0 
 */

if ( ! class_exists( 'vr_widget_text' ) ) {
	class vr_widget_text extends WP_Widget {

		// Widget Construct
		public function __construct() {
			parent::__construct(
				false,
				_x( 'AIO Text' , 'backend', PLUGIN_NAME),
				array(
					'description' => _x( 'Display text with shortcode support.', 'backend', PLUGIN_NAME),
					'classname'   => 'aio-widget-text',
				)
			);
		}

		// Widget Init
		public function widget( $args, $instance ) {

			$title = empty( $instance['title'] ) ? '' : $instance['title'];
			$content = empty( $instance['content'] ) ? '' : $instance['content'];
			$animation = empty( $instance['animation'] ) ? '' : $instance['animation'];
			$ani  = '';
			
			//if ( $title ) { echo $before_title . $title . $after_title; }
			if ( $animation != '' ) { $animation = 'data-animation="' . $animation . '"'; $ani = 'fx-start'; }

			echo $args['before_widget'];
			if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; }
			?>
		
				<article class="text clearfix <?php echo sanitize_html_class( $ani ); ?>" <?php echo $animation; ?>>
					<?php echo do_shortcode( $content ); ?>
				</article>
		
			<?php
			echo $args['after_widget'];
		}

		// Form Update
		public function update( $new_instance, $old_instance ) {
			$instance = array();

			$instance['title']		= wp_strip_all_tags( $new_instance['title'] );
			$instance['content']	= $new_instance['content'];
			$instance['animation'] 	= esc_attr( $new_instance['animation'] );

			return $instance;
		}

		// Widget Form
		public function form( $instance ) {

			$title = empty( $instance['title'] ) ? '' : $instance['title'];
			$content = empty( $instance['content'] ) ? '' : $instance['content'];
			$animation = empty( $instance['animation'] ) ? '' : $instance['animation'];
			
			?>

			<p>
				<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title :', PLUGIN_NAME ); ?></label>
				<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" style="width:100%;" type="text" />
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'content' ); ?>"><?php _e( 'Content :', PLUGIN_NAME ); ?></label>
				<textarea id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>" style="width:100%;" rows="6"><?php echo $content; ?></textarea>
				<small>Note: you can add various shortcode here.</small>
			</p>
			<p>
				<label for="<?php echo $this->get_field_id( 'animation' ); ?>"><?php _e( 'Animation :', PLUGIN_NAME ); ?></label>
				<select id="<?php echo $this->get_field_id( 'animation' ); ?>" name="<?php echo $this->get_field_name( 'animation' ); ?>" style="width:100%;">
					<option value="" <?php if ( $animation == '' ) { echo 'selected="selected"'; } ?>>None</option>
					<option value="fadeIn" <?php if ( $animation == 'fadeIn' ) { echo 'selected="selected"'; } ?>>fadeIn</option>
					<option value="fadeInUp" <?php if ( $animation == 'fadeInUp' ) { echo 'selected="selected"'; } ?>>fadeInUp</option>
					<option value="fadeInDown" <?php if ( $animation == 'fadeInDown' ) { echo 'selected="selected"'; } ?>>fadeInDown</option>
					<option value="fadeInLeft" <?php if ( $animation == 'fadeInLeft' ) { echo 'selected="selected"'; } ?>>fadeInLeft</option>
					<option value="fadeInRight" <?php if ( $animation == 'fadeInRight' ) { echo 'selected="selected"'; } ?>>fadeInRight</option>
					<option value="rotateFromLeft" <?php if ( $animation == 'rotateFromLeft' ) { echo 'selected="selected"'; } ?>>rotateFromLeft</option>
					<option value="rotateFromRight" <?php if ( $animation == 'rotateFromRight' ) { echo 'selected="selected"'; } ?>>rotateFromRight</option>
					<option value="flipInX" <?php if ( $animation == 'flipInX' ) { echo 'selected="selected"'; } ?>>flipInX</option>
					<option value="flipOutX" <?php if ( $animation == 'flipOutX' ) { echo 'selected="selected"'; } ?>>flipOutX</option>
					<option value="flipInY" <?php if ( $animation == 'flipInY' ) { echo 'selected="selected"'; } ?>>flipInY</option>
					<option value="flipOutY" <?php if ( $animation == 'flipOutY' ) { echo 'selected="selected"'; } ?>>flipOutY</option>
					<option value="bounceIn" <?php if ( $animation == 'bounceIn' ) { echo 'selected="selected"'; } ?>>zoomIn</option>
					<option value="bounceInUp" <?php if ( $animation == 'bounceInUp' ) { echo 'selected="selected"'; } ?>>bounceInUp</option>
					<option value="bounceInDown" <?php if ( $animation == 'bounceInDown' ) { echo 'selected="selected"'; } ?>>bounceInDown</option>
					<option value="bounceInLeft" <?php if ( $animation == 'bounceInLeft' ) { echo 'selected="selected"'; } ?>>bounceInLeft</option>
					<option value="bounceInRight" <?php if ( $animation == 'bounceInRight' ) { echo 'selected="selected"'; } ?>>bounceInRight</option>
				</select>
			</p>

			<?php
		}

	}
	add_action( 'widgets_init', create_function( '', 'register_widget( "vr_widget_text" );' ) );
}