<?php
/**
 * Registers a widget to display a gallery of videos.
 *
 * @package Organic Widgets Pro
 */

// Block direct requests.
if ( ! defined( 'ABSPATH' ) ) {
		die( '-1' );
}

/**
 * Adds Organic_Widgets_Pro_Video_Gallery_Widget widget.
 */
class Organic_Widgets_Pro_Video_Gallery_Widget extends Organic_Widgets_Pro_Custom_Widget {

	const CUSTOM_IMAGE_SIZE_SLUG = 'organic_widgets_image_upload';

	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'organic_widgets_video_gallery_section', // Base ID.
			__( 'Organic Video Gallery', 'organic-widgets-pro' ), // Name.
			array(
				'description'                 => __( 'A gallery of videos.', 'organic-widgets-pro' ),
				'customize_selective_refresh' => true,
			)
		);

		$this->id_prefix = $this->get_field_id( '' );

		// BG options.
		$this->bg_options = array(
			'color'     => true,
			'image'     => true,
			'bg_fixed'  => true,
			'bg_repeat' => true,
			'video'     => false,
		);

		// Font options.
		$this->font_options = array(
			'title_heading' => true,
			'title_font'    => true,
			'title_color'   => true,
			'title_weight'  => true,
			'title_spacing' => true,
			'title_size'    => true,
			'body_font'     => true,
		);

		// Layout options.
		$this->layout_options = array(
			'content_aligner' => true,
			'padding_top'     => true,
			'padding_bottom'  => true,
			'custom_height'   => true,
			'custom_width'    => true,
			'full_height'     => false,
		);

		// Admin Scripts.
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_setup' ) );

		// Public scripts.
		if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
			add_action( 'wp_enqueue_scripts', array( $this, 'public_scripts' ) );
		}

	}

	/**
	 * Front-end display of widget.
	 *
	 * @see WP_Widget::widget()
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	public function widget( $args, $instance ) {

		$num_columns   = ( isset( $instance['num_columns'] ) ) ? $instance['num_columns'] : 3;
		$column_gutter = ( isset( $instance['column_gutter'] ) ) ? $instance['column_gutter'] : 24;
		$bg_image_id   = isset( $instance['bg_image_id'] ) ? $instance['bg_image_id'] : false;
		$bg_image      = ( isset( $instance['bg_image'] ) && '' !== $instance['bg_image'] ) ? $instance['bg_image'] : false;
		$bg_color      = ( isset( $instance['bg_color'] ) && '' !== $instance['bg_color'] ) ? $instance['bg_color'] : false;
		$gallery_style = ( isset( $instance['gallery_style'] ) ) ? $instance['gallery_style'] : 'style-gallery-default';

		// Register Button One Link Target Option.
		if ( isset( $instance['button_one_target'] ) ) {
				$button_one_target = $instance['button_one_target'];
		} else {
				$button_one_target = false;
		}
		// Register Button Two Link Target Option.
		if ( isset( $instance['button_two_target'] ) ) {
				$button_two_target = $instance['button_two_target'];
		} else {
				$button_two_target = false;
		}
		// Register BG Fixed Option.
		if ( isset( $instance['bg_image_fixed'] ) ) {
				$bg_image_fixed = $instance['bg_image_fixed'];
		} else {
				$bg_image_fixed = false;
		}
		// Register BG Repeat Option.
		if ( isset( $instance['bg_image_repeat'] ) ) {
				$bg_image_repeat = $instance['bg_image_repeat'];
		} else {
				$bg_image_repeat = false;
		}

		$repeatable_array = ( isset( $instance['repeatable_array'] ) ) ? json_decode( $instance['repeatable_array'], true ) : array();

		echo $args['before_widget'];

		?>

		<?php echo '<style media="screen">'; ?>
			<?php if ( ! empty( $instance['widget_custom_width'] ) ) { ?>
				#<?php echo esc_attr( $this->id ); ?> {
					width: <?php echo absint( $instance['widget_custom_width'] ); ?>%;
				}
			<?php } ?>
			<?php if ( ! empty( $instance['content_max_width'] ) ) { ?>
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-content {
					max-width: <?php echo absint( $instance['content_max_width'] ); ?>px;
				}
			<?php } ?>
			<?php if ( ! empty( $instance['widget_custom_height'] ) || '' !== $instance['widget_padding_top'] || '' !== $instance['widget_padding_bottom'] ) { ?>
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-video-gallery-section {
					<?php if ( ! empty( $instance['widget_custom_height'] ) ) { ?>
					min-height: <?php echo absint( $instance['widget_custom_height'] ); ?>px;
					<?php } ?>
					<?php if ( '' !== $instance['widget_padding_top'] ) { ?>
					padding-top: <?php echo absint( $instance['widget_padding_top'] ); ?>px;
					<?php } ?>
					<?php if ( '' !== $instance['widget_padding_bottom'] ) { ?>
					padding-bottom: <?php echo absint( $instance['widget_padding_bottom'] ); ?>px;
					<?php } ?>
				}
			<?php } ?>
			<?php if ( ! empty( $instance['title_font_family'] ) || ! empty( $instance['title_font_weight'] ) || ! empty( $instance['title_font_size'] ) || ! empty( $instance['title_letter_spacing'] ) || ! empty( $instance['title_text_color'] ) ) { ?>
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-title {
					<?php if ( ! empty( $instance['title_font_family'] ) ) { ?>
					font-family: <?php echo esc_html( $instance['title_font_family'] ); ?>;
					<?php } ?>
					<?php if ( ! empty( $instance['title_font_weight'] ) ) { ?>
					font-weight: <?php echo esc_html( $instance['title_font_weight'] ); ?>;
					<?php } ?>
					<?php if ( ! empty( $instance['title_font_size'] ) ) { ?>
					font-size: <?php echo esc_html( $instance['title_font_size'] ); ?>px;
					<?php } ?>
					<?php if ( ! empty( $instance['title_letter_spacing'] ) ) { ?>
					letter-spacing: <?php echo esc_html( $instance['title_letter_spacing'] ); ?>px;
					<?php } ?>
					<?php if ( ! empty( $instance['title_text_color'] ) ) { ?>
					color: <?php echo esc_html( $instance['title_text_color'] ); ?>;
					<?php } ?>
				}
			<?php } ?>
			<?php if ( ! empty( $instance['body_font_family'] ) ) { ?>
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-text p {
					font-family: <?php echo esc_html( $instance['body_font_family'] ); ?>;
				}
			<?php } ?>
			<?php if ( ! empty( $instance['column_gutter'] ) ) { ?>
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-grid-spacer {
					width: <?php echo esc_html( $instance['column_gutter'] ); ?>px;
				}
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-gallery-item {
					margin-bottom: <?php echo esc_html( $instance['column_gutter'] ); ?>px;
				}
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-gallery-item.organic-widgets-half {
					width: calc(50% - ( <?php echo esc_html( $instance['column_gutter'] ); ?>px * 1 / 2 ));
				}
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-gallery-item.organic-widgets-third {
					width: calc(33.33% - ( <?php echo esc_html( $instance['column_gutter'] ); ?>px * 2 / 3 ));
				}
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-gallery-item.organic-widgets-fourth {
					width: calc(25% - ( <?php echo esc_html( $instance['column_gutter'] ); ?>px * 3 / 4 ));
				}
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-gallery-item.organic-widgets-fifth {
					width: calc(20% - ( <?php echo esc_html( $instance['column_gutter'] ); ?>px * 4 / 5 ));
				}
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-gallery-item.organic-widgets-sixth {
					width: calc(16.66% - ( <?php echo esc_html( $instance['column_gutter'] ); ?>px * 5 / 6 ));
				}
			<?php } ?>
		<?php echo '</style>'; ?>

		<div <?php if ( ! empty( $instance['widget_custom_anchor'] ) ) { ?>id="<?php echo esc_html( $instance['widget_custom_anchor'] ); ?>"<?php } ?>
		class="organic-widgets-section organic-widgets-video-gallery-section
		<?php echo esc_html( $gallery_style ); ?>
		<?php if ( $bg_image_fixed ) echo 'organic-widgets-fixed-bg-img'; ?>
		<?php if ( $bg_image_repeat ) echo 'organic-widgets-repeat-bg-img'; ?>
		<?php if ( ! empty( $instance['widget_custom_class'] ) ) echo esc_html( $instance['widget_custom_class'] ); ?>"
		<?php if ( 0 < $bg_image_id ) { ?>
			style="background-image:url(<?php echo esc_url( $bg_image ); ?>);"
		<?php } if ( $bg_color ) { ?>
			style="background-color:<?php echo esc_attr( $bg_color ); ?>;"
		<?php } ?>>

			<?php /** BEGIN .organic-widgets-content */ ?>
			<div class="organic-widgets-content">

				<?php /** BEGIN .organic-widgets-aligner */ ?>
				<div class="organic-widgets-aligner <?php if ( ! empty( $instance['alignment'] ) ) { echo 'organic-widgets-aligner-' . esc_attr( $instance['alignment'] ); } else { echo 'organic-widgets-aligner-middle-center'; } ?>">

					<?php if ( ! empty( $instance['title'] ) || ! empty( $instance['text'] ) || ! empty( $instance['button_one_url'] ) ) { ?>

					<?php /** BEGIN .organic-widgets-video-gallery-information */ ?>
					<div class="organic-widgets-video-gallery-information organic-widgets-information">

					<?php if ( ! empty( $instance['title'] ) ) { ?>
						<<?php if ( ! empty( $instance['title_text_heading'] ) ) { echo $instance['title_text_heading']; } else { ?>h2<?php } ?> class="organic-widgets-title"><?php echo apply_filters( 'organic_widget_title', $instance['title'] ); ?></<?php if ( ! empty( $instance['title_text_heading'] ) ) { echo $instance['title_text_heading']; } else { ?>h2<?php } ?>>
					<?php } ?>

						<?php if ( ! empty( $instance['text'] ) || ! empty( $instance['button_one_url'] ) || ! empty( $instance['button_two_url'] ) ) { ?>

							<div class="organic-widgets-text">

							<?php if ( ! empty( $instance['text'] ) ) { ?>
								<?php echo apply_filters( 'the_content', $instance['text'] ); ?>
							<?php } ?>

							<?php if ( ! empty( $instance['button_one_url'] ) || ! empty( $instance['button_two_url'] ) ) { ?>
							<div class="organic-widgets-button-holder">
								<?php if ( ! empty( $instance['button_one_url'] ) ) { ?>
									<a class="organic-widgets-button button" href="<?php echo esc_url( $instance['button_one_url'] ); ?>" <?php if ( '1' == $button_one_target ) { ?>target="_blank"<?php } ?>><?php if ( ! empty( $instance['button_one_text'] ) ) { echo esc_html_e( $instance['button_one_text'], 'organic-widgets-pro' ); } else { esc_html_e( 'See More', 'organic-widgets-pro' ); } ?></a>
								<?php } ?>
								<?php if ( ! empty( $instance['button_two_url'] ) ) { ?>
									<a class="organic-widgets-button button alt" href="<?php echo esc_url( $instance['button_two_url'] ); ?>" <?php if ( '1' == $button_two_target ) { ?>target="_blank"<?php } ?>><?php if ( ! empty( $instance['button_two_text'] ) ) { echo esc_html_e( $instance['button_two_text'], 'organic-widgets-pro' ); } else { esc_html_e( 'See More', 'organic-widgets-pro' ); } ?></a>
								<?php } ?>
							</div>
							<?php } ?>

							</div>

						<?php } ?>

					<?php /** END .organic-widgets-video-gallery-information */ ?>
					</div>

					<?php } ?>

					<?php if ( ! empty( $repeatable_array ) ) { ?>

					<?php /** BEGIN .organic-widgets-gallery */ ?>
					<div class="organic-widgets-gallery organic-widgets-row organic-widgets-masonry-container <?php echo esc_html( $gallery_style ); ?>">

						<div class="organic-widgets-grid-spacer"></div>

						<?php

						if ( is_array( $repeatable_array ) && count( $repeatable_array ) ) {
							$incrementer = 0;
							usort( $repeatable_array, array( $this, 'sort_by_order' ) );
							foreach ( $repeatable_array as $key => $repeatable ) {

								$incrementer++;

								global $wp_embed;

								?>

								<?php /** BEGIN .organic-widgets-gallery-item */ ?>
								<div class="organic-widgets-gallery-item organic-widgets-masonry-wrapper organic-widgets-column organic-widgets-<?php echo esc_html( $this->column_string( $num_columns ) ); ?><?php if ( 'style-gallery-border' === $gallery_style ) { ?> organic-widgets-card<?php } ?>">

									<div class="video-embed">
										<?php echo $wp_embed->run_shortcode( '[embed]' . esc_url( $repeatable['video_url'] ) . '[/embed]' ); ?>
									</div>

									<?php if ( array_key_exists( 'video_caption', $repeatable ) ) { ?>
										<p class="video-caption"><?php echo esc_html( $repeatable['video_caption'] ); ?></p>
									<?php } ?>

								<?php /** END .organic-widgets-gallery-item */ ?>
								</div>

							<?php } ?>

						<?php } ?>

					<?php /** END .organic-widgets-gallery */ ?>
					</div>

					<?php } ?>

				<?php /** END .organic-widgets-aligner */ ?>
				</div>

			<?php /** END .organic-widgets-content */ ?>
			</div>

		<?php /** END .organic-widgets-section */ ?>
		</div>

		<?php

		if ( is_active_widget( false, false, $this->id_base, true ) ) {
			wp_enqueue_script( 'yt-player-api', 'https://www.youtube.com/player_api', array(), '1.0', true );
		}

		echo $args['after_widget'];

	}

	/**
	 * Back-end widget form.
	 *
	 * @see WP_Widget::form()
	 *
	 * @param array $instance Previously saved values from database.
	 */
	public function form( $instance ) {

		// Setup Variables.
		$this->id_prefix = $this->get_field_id( '' );

		if ( isset( $instance['gallery_style'] ) ) {
			$gallery_style = $instance['gallery_style'];
		} else {
			$gallery_style = 'style-gallery-default';
		}
		if ( isset( $instance['num_columns'] ) ) {
			$num_columns = $instance['num_columns'];
		} else {
			$num_columns = 3;
		}
		if ( isset( $instance['column_gutter'] ) ) {
			$column_gutter = $instance['column_gutter'];
		} else {
			$column_gutter = 24;
		}
		if ( isset( $instance['button_one_target'] ) ) {
			$button_one_target = $instance['button_one_target'];
		} else {
			$button_one_target = false;
		}
		if ( isset( $instance['button_two_target'] ) ) {
			$button_two_target = $instance['button_two_target'];
		} else {
			$button_two_target = false;
		}
		if ( isset( $instance['repeatable_array'] ) ) {
			$repeatable_array = json_decode( $instance['repeatable_array'], true );
		} else {
			$repeatable_array = array();
		}

		?>

		<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" class="title" type="hidden" value="<?php echo esc_attr( $instance['title'] ); ?>">
		<input id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" class="text organic-widgets-wysiwyg-anchor" type="hidden" value="<?php echo esc_attr( $instance['text'] ); ?>">
		<input id="<?php echo $this->get_field_id( 'filter' ); ?>" name="<?php echo $this->get_field_name( 'filter' ); ?>" class="filter" type="hidden" value="on">
		<input id="<?php echo $this->get_field_id( 'visual' ); ?>" name="<?php echo $this->get_field_name( 'visual' ); ?>" class="visual" type="hidden" value="on">

		<div class="organic-widgets-option-set">
			<p class="organic-widgets-input-half">
				<label for="<?php echo $this->get_field_id( 'button_one_text' ); ?>"><?php esc_html_e( 'Featured Link Text:', 'organic-widgets-pro' ); ?></label>
				<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'button_one_text' ); ?>" name="<?php echo $this->get_field_name( 'button_one_text' ); ?>" value="<?php if ( ! empty( $instance['button_one_text'] ) ) echo $instance['button_one_text']; ?>" />
			</p>
			<p class="organic-widgets-input-half last">
				<label for="<?php echo $this->get_field_id( 'button_one_url' ); ?>"><?php esc_html_e( 'Featured Link URL:', 'organic-widgets-pro' ); ?></label>
				<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'button_one_url' ); ?>" name="<?php echo $this->get_field_name( 'button_one_url' ); ?>" value="<?php if ( ! empty( $instance['button_one_url'] ) ) echo $instance['button_one_url']; ?>" />
			</p>
		</div>
		<p>
			<input class="checkbox" type="checkbox" value="1" <?php checked( $button_one_target, '1' ); ?> id="<?php echo $this->get_field_id( 'button_one_target' ); ?>" name="<?php echo $this->get_field_name( 'button_one_target' ); ?>" />
			<label for="<?php echo $this->get_field_id( 'button_one_target' ); ?>"><?php esc_html_e( 'Open Link In New Window', 'organic-widgets-pro' ); ?></label>
		</p>

		<div class="organic-widgets-option-set">
			<p class="organic-widgets-input-half">
				<label for="<?php echo $this->get_field_id( 'button_two_text' ); ?>"><?php esc_html_e( 'Alternate Link Text:', 'organic-widgets-pro' ); ?></label>
				<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'button_two_text' ); ?>" name="<?php echo $this->get_field_name( 'button_two_text' ); ?>" value="<?php if ( ! empty( $instance['button_two_text'] ) ) echo $instance['button_two_text']; ?>" />
			</p>
			<p class="organic-widgets-input-half last">
				<label for="<?php echo $this->get_field_id( 'button_two_url' ); ?>"><?php esc_html_e( 'Alternate Link URL:', 'organic-widgets-pro' ); ?></label>
				<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'button_two_url' ); ?>" name="<?php echo $this->get_field_name( 'button_two_url' ); ?>" value="<?php if ( ! empty( $instance['button_two_url'] ) ) echo $instance['button_two_url']; ?>" />
			</p>
		</div>
		<p>
			<input class="checkbox" type="checkbox" value="1" <?php checked( $button_two_target, '1' ); ?> id="<?php echo $this->get_field_id( 'button_two_target' ); ?>" name="<?php echo $this->get_field_name( 'button_two_target' ); ?>" />
			<label for="<?php echo $this->get_field_id( 'button_two_target' ); ?>"><?php esc_html_e( 'Open Link In New Window', 'organic-widgets-pro' ); ?></label>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'gallery_style' ); ?>"><?php esc_html_e( 'Gallery Style:', 'organic-widgets-pro' ); ?>
				<select id="<?php echo $this->get_field_id( 'gallery_style' ); ?>" name="<?php echo $this->get_field_name( 'gallery_style' ); ?>">
					<option value="style-gallery-default" <?php echo ( 'style-gallery-default' === $gallery_style ) ? 'selected' : ''; ?>><?php esc_html_e( 'Default', 'organic-widgets-pro' ); ?></option>
					<option value="style-gallery-round" <?php echo ( 'style-gallery-round' === $gallery_style ) ? 'selected' : ''; ?>><?php esc_html_e( 'Round', 'organic-widgets-pro' ); ?></option>
					<option value="style-gallery-border" <?php echo ( 'style-gallery-border' === $gallery_style ) ? 'selected' : ''; ?>><?php esc_html_e( 'Border', 'organic-widgets-pro' ); ?></option>
					<option value="style-gallery-shadow" <?php echo ( 'style-gallery-shadow' === $gallery_style ) ? 'selected' : ''; ?>><?php esc_html_e( 'Shadow', 'organic-widgets-pro' ); ?></option>
				</select>
			</label>
		</p>

		<div class="organic-widgets-option-set">
			<p class="organic-widgets-input-half">
				<label for="<?php echo $this->get_field_id( 'num_columns' ); ?>"><?php esc_html_e( 'Gallery Columns:', 'organic-widgets-pro' ) ?></label>
				<select id="<?php echo $this->get_field_id( 'num_columns' ); ?>" name="<?php echo $this->get_field_name( 'num_columns' ); ?>" class="widefat" style="width:100%;">
					<option <?php selected( $num_columns, '1' ); ?> value="1">1</option>
					<option <?php selected( $num_columns, '2' ); ?> value="2">2</option>
					<option <?php selected( $num_columns, '3' ); ?> value="3">3</option>
					<option <?php selected( $num_columns, '4' ); ?> value="4">4</option>
					<option <?php selected( $num_columns, '5' ); ?> value="5">5</option>
					<option <?php selected( $num_columns, '6' ); ?> value="6">6</option>
				</select>
			</p>
			<p class="organic-widgets-input-half unit last">
				<label for="<?php echo $this->get_field_id( 'column_gutter' ); ?>"><?php esc_html_e( 'Gutter Width:', 'organic-widgets-pro' ); ?></label>
				<input class="widefat maximum" type="number" min="0" max="100" id="<?php echo $this->get_field_id( 'column_gutter' ); ?>" name="<?php echo $this->get_field_name( 'column_gutter' ); ?>" value="<?php if ( ! empty( $instance['column_gutter'] ) ) { echo absint( $instance['column_gutter'] ); } else { echo '24'; } ?>" />
				<span><?php esc_html_e( 'px', 'organic-widgets-pro' ); ?></span>
			</p>
		</div>

		<br />

		<?php $this->section_font_input_markup( $instance, $this->font_options ); ?>

		<?php $this->section_layout_input_markup( $instance, $this->layout_options ); ?>

		<?php $this->section_background_input_markup( $instance, $this->bg_options ); ?>

		<div class="organic-widgets-repeatable-form-item-widget-admin">

			<?php $this->repeatable_form_item_inputs_markup( $repeatable_array, '' ); ?>

		</div>

		<p class="organic-widgets-input">
			<label for="<?php echo $this->get_field_id( 'widget_custom_class' ); ?>"><?php esc_html_e( 'Custom Widget Class', 'organic-widgets-pro' ); ?></label>
			<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'widget_custom_class' ); ?>" name="<?php echo $this->get_field_name( 'widget_custom_class' ); ?>" value="<?php if ( ! empty( $instance['widget_custom_class'] ) ) echo $instance['widget_custom_class']; ?>" />
		</p>

		<p class="organic-widgets-input">
			<label for="<?php echo $this->get_field_id( 'widget_custom_anchor' ); ?>"><?php esc_html_e( 'Custom Widget Anchor', 'organic-widgets-pro' ); ?></label>
			<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'widget_custom_anchor' ); ?>" name="<?php echo $this->get_field_name( 'widget_custom_anchor' ); ?>" value="<?php if ( ! empty( $instance['widget_custom_anchor'] ) ) echo $instance['widget_custom_anchor']; ?>" />
		</p>

		<?php

	}

	/**
	 * Ouput HTML for a Repeatable Form Item
	 *
	 * @access protected
	 */
	protected function echo_repeatable_form_item( $id = 'template', $order = 'template', $repeatable = false, $instance = false ) {

		if ( 'template' === $id || 'template' === $order ) {
			$template = true;
			$id       = '__x__';
			$order    = '';
		} else {
			$template = false;
			$id       = (int) $id;
			$order    = (int) $order;
		}

		?>

		<div class="<?php if ( $template ) { echo 'organic-widgets-repeatable-form-item-template'; } else { echo 'organic-widgets-repeatable-form-item'; } ?>" data-feature-id="<?php echo $id; ?>" data-order="<?php echo $order; ?>">

			<div class="organic-widgets-repeatable-form-item-title-bar">
				<?php esc_html_e( 'Video Information', 'organic-widgets-pro' ); ?>
				<span class="organic-widgets-repeatable-item-number">
					<?php
					if ( is_numeric( $order ) ) {
						echo $order + 1;
					}
					?>
				</span>
			</div>

			<div class="organic-widgets-repeatable-form-item-fields-wrapper organic-widgets-video-fields-wrapper">

				<div class="organic-widgets-video-fields">

					<p>
						<label><?php esc_html_e( 'Video URL:', 'organic-widgets-pro' ) ?></label>
						<input class="widefat organic-widgets-video-url-input organic-widgets-repeatable-form-item-input" data-input-name="video_url" data-activator="true" type="text" value="<?php if ( $repeatable && array_key_exists( 'video_url', $repeatable ) ) echo esc_url( $repeatable['video_url'] ); ?>" data-feature-id="<?php echo $id; ?>"/>
					</p>

					<p>
						<label><?php esc_html_e( 'Video Caption:', 'organic-widgets-pro' ) ?></label>
						<input class="widefat organic-widgets-video-caption-input organic-widgets-repeatable-form-item-input" data-input-name="video_caption" data-activator="true" type="text" value="<?php if ( $repeatable && array_key_exists( 'video_caption', $repeatable ) ) echo esc_html( $repeatable['video_caption'] ); ?>" data-feature-id="<?php echo $id; ?>"/>
					</p>

				</div>

				<br />

				<div class="organic-widgets-clear"></div>

				<?php $this->echo_repeatable_form_item_actions(); ?>

			</div>

		</div>

		<?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;

		/*--- Text/Title ----*/
		if ( ! isset( $newinstance['filter'] ) ) {
			$instance['filter'] = false;
		}
		if ( ! isset( $newinstance['visual'] ) ) {
			$instance['visual'] = null;
		}
		// Upgrade 4.8.0 format.
		if ( isset( $old_instance['filter'] ) && 'content' === $old_instance['filter'] ) {
			$instance['visual'] = true;
		}
		if ( 'content' === $new_instance['filter'] ) {
			$instance['visual'] = true;
		}
		if ( isset( $new_instance['visual'] ) ) {
			$instance['visual'] = ! empty( $new_instance['visual'] );
		}
		// Filter is always true in visual mode.
		if ( ! empty( $instance['visual'] ) ) {
			$instance['filter'] = true;
		}
		if ( current_user_can( 'unfiltered_html' ) ) {
			$instance['title'] = $new_instance['title'];
			$instance['text']  = $new_instance['text'];
		} else {
			$instance['title'] = wp_kses_post( $new_instance['title'] );
			$instance['text']  = wp_kses_post( $new_instance['text'] );
		}
		/*--- END Text/Title ----*/

		if ( ! isset( $old_instance['created'] ) ) {
			$instance['created'] = time();
		}
		if ( isset( $new_instance['button_one_text'] ) ) {
			$instance['button_one_text'] = wp_strip_all_tags( $new_instance['button_one_text'] );
		}
		if ( isset( $new_instance['button_one_url'] ) ) {
			$instance['button_one_url'] = wp_strip_all_tags( $new_instance['button_one_url'] );
		}
		if ( isset( $new_instance['button_two_text'] ) ) {
			$instance['button_two_text'] = wp_strip_all_tags( $new_instance['button_two_text'] );
		}
		if ( isset( $new_instance['button_two_url'] ) ) {
			$instance['button_two_url'] = wp_strip_all_tags( $new_instance['button_two_url'] );
		}
		if ( isset( $new_instance['button_one_target'] ) ) {
			$instance['button_one_target'] = true;
		} else {
			$instance['button_one_target'] = false;
		}
		if ( isset( $new_instance['button_two_target'] ) ) {
			$instance['button_two_target'] = true;
		} else {
			$instance['button_two_target'] = false;
		}
		if ( isset( $new_instance['gallery_style'] ) ) {
			$instance['gallery_style'] = wp_strip_all_tags( $new_instance['gallery_style'] );
		}
		if ( isset( $new_instance['num_columns'] ) ) {
			$instance['num_columns'] = wp_strip_all_tags( $new_instance['num_columns'] );
		}
		if ( isset( $new_instance['column_gutter'] ) ) {
			$instance['column_gutter'] = wp_strip_all_tags( $new_instance['column_gutter'] );
		}
		if ( isset( $new_instance['repeatable_array'] ) ) {
			$instance['repeatable_array'] = $new_instance['repeatable_array'];
		}

		/*--- Font Options ----*/
		if ( isset( $new_instance['title_text_heading'] ) ) {
			$instance['title_text_heading'] = wp_strip_all_tags( $new_instance['title_text_heading'] );
		}
		if ( isset( $new_instance['title_text_color'] ) ) {
			$instance['title_text_color'] = wp_strip_all_tags( $new_instance['title_text_color'] );
		}
		if ( isset( $new_instance['title_font_weight'] ) ) {
			$instance['title_font_weight'] = wp_strip_all_tags( $new_instance['title_font_weight'] );
		}
		if ( isset( $new_instance['title_font_size'] ) ) {
			$instance['title_font_size'] = wp_strip_all_tags( $new_instance['title_font_size'] );
		}
		if ( isset( $new_instance['title_letter_spacing'] ) ) {
			$instance['title_letter_spacing'] = wp_strip_all_tags( $new_instance['title_letter_spacing'] );
		}
		if ( isset( $new_instance['title_font_family'] ) ) {
			$instance['title_font_family'] = wp_strip_all_tags( $new_instance['title_font_family'] );
		}
		if ( isset( $new_instance['body_font_family'] ) ) {
			$instance['body_font_family'] = wp_strip_all_tags( $new_instance['body_font_family'] );
		}
		/*--- END Font Options ----*/

		/*--- Background ----*/
		if ( isset( $new_instance['bg_image_id'] ) ) {
			$instance['bg_image_id'] = wp_strip_all_tags( $new_instance['bg_image_id'] );
		}
		if ( isset( $new_instance['bg_image'] ) ) {
			$instance['bg_image'] = wp_strip_all_tags( $new_instance['bg_image'] );
		}
		if ( isset( $new_instance['bg_image_fixed'] ) ) {
			$instance['bg_image_fixed'] = true;
		} else {
			$instance['bg_image_fixed'] = false;
		}
		if ( isset( $new_instance['bg_image_repeat'] ) ) {
			$instance['bg_image_repeat'] = true;
		} else {
			$instance['bg_image_repeat'] = false;
		}
		if ( isset( $new_instance['bg_color'] ) && $this->check_hex_color( $new_instance['bg_color'] ) ) {
			$instance['bg_color'] = wp_strip_all_tags( $new_instance['bg_color'] );
		} else {
			$instance['bg_color'] = false;
		}
		/*--- END Background ----*/

		/*--- Layout ----*/
		if ( isset( $new_instance['alignment'] ) ) {
			$instance['alignment'] = wp_strip_all_tags( $new_instance['alignment'] );
		}
		if ( isset( $new_instance['widget_padding_top'] ) ) {
			$instance['widget_padding_top'] = wp_strip_all_tags( $new_instance['widget_padding_top'] );
		}
		if ( isset( $new_instance['widget_padding_bottom'] ) ) {
			$instance['widget_padding_bottom'] = wp_strip_all_tags( $new_instance['widget_padding_bottom'] );
		}
		if ( isset( $new_instance['widget_custom_height'] ) ) {
			$instance['widget_custom_height'] = wp_strip_all_tags( $new_instance['widget_custom_height'] );
		}
		if ( isset( $new_instance['widget_custom_width'] ) ) {
			$instance['widget_custom_width'] = wp_strip_all_tags( $new_instance['widget_custom_width'] );
		}
		if ( isset( $new_instance['widget_custom_class'] ) ) {
			$instance['widget_custom_class'] = wp_strip_all_tags( $new_instance['widget_custom_class'] );
		}
		if ( isset( $new_instance['widget_custom_anchor'] ) ) {
			$instance['widget_custom_anchor'] = wp_strip_all_tags( $new_instance['widget_custom_anchor'] );
		}
		if ( isset( $new_instance['content_max_width'] ) ) {
			$instance['content_max_width'] = wp_strip_all_tags( $new_instance['content_max_width'] );
		}
		/*--- END Layout ----*/

		return $instance;
	}

	/**
	 * Enqueue all the javascript.
	 */
	public function admin_setup() {

		// Text Editor.
		wp_enqueue_editor();
		wp_enqueue_script( 'organic-widgets-video-gallery-widgets-text-title', plugin_dir_url( __FILE__ ) . 'js/video-gallery-widgets.js', array( 'jquery', 'wp-embed', 'underscore' ), '1.0', true );
		wp_localize_script( 'organic-widgets-video-gallery-widgets-text-title', 'OrganicVideoGalleryWidget', array(
			'id_base' => $this->id_base,
		) );
		wp_add_inline_script( 'organic-widgets-video-gallery-widgets-text-title', 'wp.organicVideoGalleryWidget.init();', 'after' );

		// Content Aligner.
		if ( ! wp_script_is( 'organic-widgets-module-content-aligner' ) && is_customize_preview() ) {
			wp_enqueue_script( 'organic-widgets-module-content-aligner', ORGANIC_WIDGETS_PRO_ADMIN_JS_DIR . 'organic-widgets-module-content-aligner.js', array( 'jquery' ), '1.0', true );
		}

		wp_enqueue_media();

		wp_enqueue_style( 'wp-color-picker' );
		wp_enqueue_script( 'wp-color-picker' );
		wp_enqueue_script( 'organic-widgets-module-color-picker', ORGANIC_WIDGETS_PRO_ADMIN_JS_DIR . 'organic-widgets-module-color-picker.js', array( 'jquery', 'wp-color-picker' ), '1.0', true );

		// Repeatable Form Items.
		if ( ! wp_script_is( 'organic-widgets-module-repeatable-form-item-js' ) && is_customize_preview() ) {
			wp_enqueue_script( 'organic-widgets-module-repeatable-form-item-js', ORGANIC_WIDGETS_PRO_ADMIN_JS_DIR . 'organic-widgets-module-repeatable-form-items.js', array( 'jquery' ), '1.0', true );
		}

		if ( ! wp_script_is( 'organic-widgets-module-image-background' ) ) {
			wp_enqueue_script( 'organic-widgets-module-image-background', ORGANIC_WIDGETS_PRO_ADMIN_JS_DIR . 'organic-widgets-module-image-background.js', array( 'jquery', 'media-upload', 'media-views', 'wp-color-picker' ), '1.0', true );
			wp_localize_script( 'organic-widgets-module-image-background', 'OrganicWidgetBG', array(
				'frame_title'  => __( 'Select an Image', 'organic-widgets-pro' ),
				'button_title' => __( 'Insert Into Widget', 'organic-widgets-pro' ),
			) );
		}

	}

	/**
	 * Enqueue public javascript.
	 */
	public function public_scripts() {

		if ( ! wp_script_is( 'organic-widgets-backgroundimagebrightness-js' ) ) {
			wp_enqueue_script( 'organic-widgets-backgroundimagebrightness-js', ORGANIC_WIDGETS_PRO_BASE_DIR . 'public/js/jquery.backgroundbrightness.js', array( 'jquery' ), '1.0', true );
		}

		wp_register_script( 'jquery-fitvids', ORGANIC_WIDGETS_PRO_BASE_DIR . 'public/js/jquery.fitvids.js', array( 'jquery' ), '1.0', true );
		wp_enqueue_script( 'organic-widgets-fitvids', ORGANIC_WIDGETS_PRO_BASE_DIR . 'public/js/fitvids-setup.js', array( 'jquery', 'jquery-fitvids' ), '1.0', true );
		wp_enqueue_script( 'organic-widgets-masonry', ORGANIC_WIDGETS_PRO_BASE_DIR . 'public/js/masonry-setup.js', array( 'jquery', 'masonry' ), '1.0', true );

	}

} // class Organic_Widgets_Pro_Video_Gallery_Widget
