<?php
/**
 * Registers a widget to show a groupable featured content subsection.
 *
 * @package Organic Widgets Pro
 */

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

/**
 * Adds Organic_Widgets_Pro_Content_Widget widget.
 */
class Organic_Widgets_Pro_Content_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_featured_content', // Base ID.
			__( 'Organic Content', 'organic-widgets-pro' ), // Name.
			array(
				'description'                 => __( 'A widget for displaying an existing page or custom content.', 'organic-widgets-pro' ),
				'customize_selective_refresh' => false,
			)
		);

		$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,
			'content_heading_font'   => false,
			'content_heading_size'   => false,
			'content_heading_weight' => false,
			'body_font'              => true,
			'body_color'             => true,
			'text_alignment'         => true,
		);

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

		// Admin Scripts.
		add_action( 'admin_print_scripts', array( $this, 'admin_setup' ) );
		add_action( 'admin_footer', array( $this, 'render_control_template_scripts' ) );

		// Public scripts.
		add_action( 'customize_preview_init', 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 ) {

		$group    = $this->organic_widgets_pro_groupable_widget( $args );
		$group_id = $group['group_id'];
		if ( $group['first'] && ! $group['last'] ) {
			$first_last = 'organic-widgets-groupable-first';
		} elseif ( $group['last'] && ! $group['first'] ) {
			$first_last = 'organic-widgets-groupable-last';
		} else {
			$first_last = false;
		}

		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-featured-content-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'] ) || ! empty( $instance['body_text_color'] ) ) { ?>
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-text p,
				#<?php echo esc_attr( $this->id ); ?> .organic-widgets-card-content p {
					<?php if ( ! empty( $instance['body_font_family'] ) ) { ?>
					font-family: <?php echo esc_html( $instance['body_font_family'] ); ?>;
					<?php } ?>
					<?php if ( ! empty( $instance['body_text_color'] ) ) { ?>
					color: <?php echo esc_html( $instance['body_text_color'] ); ?>;
					<?php } ?>
				}
			<?php } ?>
		<?php echo '</style>'; ?>

		<?php

		if ( ! empty( $instance['page_id'] ) ) {

			$bg_color       = isset( $instance['bg_color'] ) ? $instance['bg_color'] : false;
			$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;
			$page_id        = $instance['page_id'];
			$page_excerpt   = $this->organic_widgets_pro_get_the_excerpt( $page_id );
			$page_title     = get_the_title( $page_id );

			// 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;
			}
			// Register Text Align Option.
			if ( isset( $instance['text_align'] ) ) {
				$text_align = $instance['text_align'];
			} else {
				$text_align = 'center';
			}

			$page_query = new WP_Query(array(
				'post_type'      => 'page',
				'page_id'        => $page_id,
				'posts_per_page' => 1,
			) );

			if ( $page_query->have_posts() ) : while ( $page_query->have_posts() ) : $page_query->the_post();

					?>

			<?php /** BEGIN .organic-widgets-section */ ?>
			<div <?php if ( ! empty( $instance['widget_custom_anchor'] ) ) { ?>id="<?php echo esc_html( $instance['widget_custom_anchor'] ); ?>"<?php } ?>
			class="organic-widgets-section organic-widgets-featured-content-section
			<?php if ( $first_last ) { echo esc_attr( $first_last ); } ?>
			<?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 if ( $group_id ) { echo 'data-group-id="' . esc_html( $group_id ) . '"'; } ?>>

				<?php /** BEGIN .organic-widgets-content */ ?>
				<div class="organic-widgets-content <?php if ( $text_align ) echo 'organic-widgets-text-'?><?php echo $text_align; ?>">

					<?php if ( has_post_thumbnail() ) {  ?>
						<div class="organic-widgets-img"><?php echo get_the_post_thumbnail( $page_id, 'organic-widgets-featured-medium' ); ?></div>
					<?php } ?>

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

						<?php the_content( esc_html__( 'Read More', 'organic-widgets-pro' ) ); ?>
						<?php edit_post_link( esc_html__( '(Edit)', 'organic-widgets-pro' ), '<p>', '</p>' ); ?>

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

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

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

			<?php

			endwhile;
			endif;
			wp_reset_postdata();

		} else {

			$attr              = array();
			$attr              = apply_filters( 'image_widget_image_attributes', $attr, $instance );
			$bg_color          = isset( $instance['bg_color'] ) ? $instance['bg_color'] : false;
			$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;
			$featured_image_id = isset( $instance['featured_image_id'] ) ? $instance['featured_image_id'] : false;
			$featured_image    = ( isset( $instance['featured_image'] ) && '' !== $instance['featured_image'] ) ? $instance['featured_image'] : false;
			$link_url          = ( isset( $instance['link_url'] ) ) ? $instance['link_url'] : false;
			$link_title        = ( isset( $instance['link_title'] ) ) ? $instance['link_title'] : false;

			// Register Button Link Target Option.
			if ( isset( $instance['link_target'] ) ) {
				$link_target = $instance['link_target'];
			} else {
				$link_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;
			}
			// Register Text Align Option.
			if ( isset( $instance['text_align'] ) ) {
				$text_align = $instance['text_align'];
			} else {
				$text_align = 'center';
			}
			?>

			<?php /** BEGIN .organic-widgets-section */ ?>
			<div <?php if ( ! empty( $instance['widget_custom_anchor'] ) ) { ?>id="<?php echo esc_html( $instance['widget_custom_anchor'] ); ?>"<?php } ?>
			class="organic-widgets-section organic-widgets-featured-content-section
			<?php if ( $first_last ) { echo esc_attr( $first_last ); } ?>
			<?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 if ( $group_id ) { echo 'data-group-id="' . esc_html( $group_id ) . '"'; } ?>>

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

					<?php /** BEGIN .organic-widgets-content */ ?>
					<div class="organic-widgets-content <?php if ( $text_align ) echo 'organic-widgets-text-'?><?php echo $text_align; ?>">

						<?php if ( $featured_image_id > 0 ) { ?>
							<div class="organic-widgets-img"><img src="<?php echo esc_url( $featured_image ); ?>" alt="<?php __( 'Featured Image', 'organic-widgets-pro' ) ?>" /></div>
						<?php } ?>

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

							<?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'] ) ) { ?>
								<?php echo apply_filters( 'the_content', $instance['text'] ); ?>
							<?php } ?>

							<?php if ( ! empty( $link_url ) ) { ?>
								<a class="button" href="<?php echo esc_url( $link_url ); ?>" <?php if ( '1' == $link_target ) { ?>target="_blank"<?php } ?>>
									<?php if ( ! empty( $link_title ) ) { ?>
										<?php echo esc_html( $link_title ); ?>
									<?php } else { ?>
										<?php esc_html_e( 'Read More', 'organic-widgets-pro' ); ?>
									<?php } ?>
								</a>
							<?php } ?>

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

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

				<?php } ?>

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

			<?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 ) {

		$instance = wp_parse_args(
			(array) $instance,
			array(
				'title' => '',
				'text'  => '',
			)
		);

		// Setup variables.
		$this->id_prefix = $this->get_field_id( '' );
		$bg_color        = ( isset( $instance['bg_color'] ) && '' !== $instance['bg_color'] ) ? $instance['bg_color'] : false;

		if ( isset( $instance['bg_image_id'] ) && '' !== $instance['bg_image_id'] ) {
			$bg_image_id = $instance['bg_image_id'];
		} else {
			$bg_image_id = 0;
		}
		if ( isset( $instance['bg_image'] ) && '' !== $instance['bg_image'] ) {
			$bg_image = $instance['bg_image'];
		} else {
			$bg_image = false;
		}
		if ( isset( $instance['featured_image_id'] ) ) {
			$featured_image_id = $instance['featured_image_id'];
		} else {
			$featured_image_id = 0;
		}
		if ( isset( $instance['featured_image_id'] ) && isset( $instance['featured_image'] ) ) {
			$featured_image = $instance['featured_image'];
		} else {
			$featured_image = false;
		}
		if ( isset( $instance['page_id'] ) ) {
			$page_id = $instance['page_id'];
		} else {
			$page_id = 0;
		}
		if ( isset( $instance['title'] ) ) {
			$title = $instance['title'];
		} else {
			$title = '';
		}
		if ( isset( $instance['text'] ) ) {
			$text = $instance['text'];
		} else {
			$text = '';
		}
		if ( isset( $instance['link_url'] ) ) {
			$link_url = $instance['link_url'];
		} else {
			$link_url = '';
		}
		if ( isset( $instance['link_title'] ) ) {
			$link_title = $instance['link_title'];
		} else {
			$link_title = '';
		}
		if ( isset( $instance['link_target'] ) ) {
			$link_target = $instance['link_target'];
		} else {
			$link_target = false;
		}

		?>

		<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">

		<p>
			<label for="<?php echo $this->get_field_id( 'featured_image' ); ?>"><?php _e( 'Featured Image:', 'organic-widgets-pro' ); ?></label>
			<div class="uploader">
				<input type="submit" class="button" name="<?php echo $this->get_field_name( 'featured_image_uploader_button' ); ?>" id="<?php echo $this->get_field_id( 'featured_image_uploader_button' ); ?>" value="<?php if ( $featured_image_id ) { _e( 'Change Image', 'organic-widgets-pro' ); } else { _e( 'Select Image', 'organic-widgets-pro' ); }?>" onclick="organicWidgetFeaturedImage.uploader( '<?php echo $this->id; ?>', '<?php echo $this->id_prefix; ?>' ); return false;" />
				<input type="submit" class="organic-widgets-remove-image-button button" name="<?php echo $this->get_field_name( 'featured_image_remover_button' ); ?>" id="<?php echo $this->get_field_id( 'featured_image_remover_button' ); ?>" value="<?php _e( 'Remove Image', 'organic-widgets-pro' ); ?>" onclick="organicWidgetFeaturedImage.remover( '<?php echo $this->id; ?>', '<?php echo $this->id_prefix; ?>', 'featured_image_remover_button' ); return false;" <?php if ( $featured_image_id < 1 ) { echo( 'style="display:none;"' ); } ?>/>
				<div class="organic-widgets-widget-image-preview" id="<?php echo $this->get_field_id( 'featured_image_preview' ); ?>">
					<?php echo $this->get_featured_image_html( $instance ); ?>
				</div>
				<input type="hidden" id="<?php echo $this->get_field_id( 'featured_image_id' ); ?>" name="<?php echo $this->get_field_name( 'featured_image_id' ); ?>" value="<?php echo absint( $featured_image_id ); ?>" />
				<input type="hidden" id="<?php echo $this->get_field_id( 'featured_image' ); ?>" name="<?php echo $this->get_field_name( 'featured_image' ); ?>" value="<?php echo $featured_image; ?>" />
			</div>
		</p>

		<div class="organic-widgets-option-set">
			<p class="organic-widgets-input-half">
				<label for="<?php echo $this->get_field_id( 'link_title' ); ?>"><?php _e( 'Button Text:', 'organic-widgets-pro' ); ?></label>
				<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'link_title' ); ?>" name="<?php echo $this->get_field_name( 'link_title' ); ?>" value="<?php echo esc_html( $link_title ); ?>" />
			</p>
			<p class="organic-widgets-input-half last">
				<label for="<?php echo $this->get_field_id( 'link_url' ); ?>"><?php _e('Button URL', 'organic-widgets-pro'); ?></label>
				<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'link_url' ); ?>" name="<?php echo $this->get_field_name( 'link_url' ); ?>" value="<?php echo esc_url( $link_url ); ?>" />
			</p>
		</div>
		<p>
			<input class="checkbox" type="checkbox" value="1" <?php checked( $link_target, '1' ); ?> id="<?php echo $this->get_field_id( 'link_target' ); ?>" name="<?php echo $this->get_field_name( 'link_target' ); ?>" />
			<label for="<?php echo $this->get_field_id( 'link_target' ); ?>"><?php esc_html_e( 'Open Link In New Window', 'organic-widgets-pro' ); ?></label>
		</p>

		<h3><?php _e( 'Or Choose Existing Page:', 'organic-widgets-pro' ) ?></h3>

		<p>
			<?php
			wp_dropdown_pages( array(
				'class'             => 'widefat',
				'selected'          => absint( $page_id ),
				'id'                => $this->get_field_id( 'page_id' ),
				'name'              => $this->get_field_name( 'page_id' ),
				'show_option_none'  => __( '— Select Existing Page —', 'organic-widgets-pro' ),
				'option_none_value' => '0',
			) );
			?>
		</p>

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

		<p class="organic-widgets-input">
			<label for="<?php echo $this->get_field_id( 'widget_custom_class' ); ?>"><?php _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 _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

	}

	/**
	 * Render the featured image html output.
	 *
	 * @since 1.0.0
	 *
	 * @param array $instance Widget instance.
	 * @return string image html
	 */
	protected function get_featured_image_html( $instance ) {

		if ( isset( $instance['featured_image_id'] ) ) {
			$image_id = $instance['featured_image_id'];
		} else {
			$image_id = 0;
		}

		// If there is an featured_image, use it to render the image. Eventually we should kill this and simply rely on featured_image_ids.
		if ( (int) $image_id > 0 ) {
			$size      = 'organic-widgets-featured-large';
			$img_array = wp_get_attachment_image( $image_id, 'full', false );
			if ( is_array( $img_array ) ) {
				$output = '<img src="' . $img_array[0] . '" />';
			} else {
				$output = $img_array;
			}
		} else {
			$output = '';
		}

		return $output;

	}

	/**
	 * Render form template scripts.
	 *
	 * @access public
	 */
	public function render_control_template_scripts() {

		?>
		<script type="text/html" id="tmpl-widget-organic_widgets_featured_content_section-control-fields">

			<# var elementIdPrefix = 'el' + String( Math.random() ).replace( /\D/g, '' ) + '_' #>

			<h3><?php _e('Add Custom Content:', 'organic-widgets-pro') ?></h3>

			<p>
				<label for="{{ elementIdPrefix }}title"><?php esc_html_e( 'Title:' ); ?></label>
				<input id="{{ elementIdPrefix }}title" type="text" class="widefat title">
			</p>
			<p>
				<label for="{{ elementIdPrefix }}text" class="screen-reader-text"><?php esc_html_e( 'Content:' ); ?></label>
				<textarea id="{{ elementIdPrefix }}text" class="widefat text" style="height: 200px" rows="16" cols="20"></textarea>
			</p>
		</script>
		<?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 = array();

		/*--- 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 ----*/

		/*--- Unique Options ----*/
		if ( ! isset( $old_instance['created'] ) ) {
			$instance['created'] = time();
		}
		if ( isset( $new_instance['featured_image_id'] ) ) {
			$instance['featured_image_id'] = wp_strip_all_tags( $new_instance['featured_image_id'] );
		}
		if ( isset( $new_instance['featured_image'] ) ) {
			$instance['featured_image'] = wp_strip_all_tags( $new_instance['featured_image'] );
		}
		if ( isset( $new_instance['link_target'] ) ) {
			$instance['link_target'] = true;
		} else {
			$instance['link_target'] = false;
		}
		if ( isset( $new_instance['page_id'] ) && $new_instance['page_id'] > 0 ) {
			$instance['page_id'] = wp_strip_all_tags( $new_instance['page_id'] );
		}
		$instance['link_url']   = wp_strip_all_tags( $new_instance['link_url'] );
		$instance['link_title'] = wp_strip_all_tags( $new_instance['link_title'] );
		/*--- END Unique 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 ----*/

		/*--- 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'] );
		}
		if ( isset( $new_instance['body_text_color'] ) ) {
			$instance['body_text_color'] = wp_strip_all_tags( $new_instance['body_text_color'] );
		}
		if ( isset( $new_instance['text_align'] ) ) {
			$instance['text_align'] = wp_strip_all_tags( $new_instance['text_align'] );
		}
		/*--- END Font Options ----*/

		/*--- Layout ----*/
		if ( isset( $new_instance['hide_content_bg'] ) ) {
			$instance['hide_content_bg'] = true;
		} else {
			$instance['hide_content_bg'] = false;
		}
		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['content_max_width'] ) ) {
			$instance['content_max_width'] = wp_strip_all_tags( $new_instance['content_max_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'] );
		}
		/*--- END Layout ----*/

		return $instance;

	}

	/**
	 * Get The Excerpt By Id
	 */
	private function organic_widgets_pro_get_the_excerpt( $post_id ) {
		global $post;
		$save_post = $post;
		$post      = get_post( $post_id );
		$output    = $post->post_content;
		$post      = $save_post;
		return $output;
	}

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

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

		if ( ! wp_script_is( 'organic-widgets-module-groupable-widgets' ) && is_customize_preview() ) {
			wp_enqueue_script( 'organic-widgets-module-groupable-widgets', ORGANIC_WIDGETS_PRO_ADMIN_JS_DIR . 'organic-widgets-module-groupable-widgets.js', array( 'jquery' ), '1.0', true );
			wp_localize_script( 'organic-widgets-module-groupable-widgets', 'GroupableWidgets', array(
				'active_pane' => false,
				'widgets'     => array(),
			) );
		}

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

		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', 'FeaturedContentWidget', array(
			'frame_title'  => __( 'Select an Image', 'organic-widgets-pro' ),
			'button_title' => __( 'Insert Into Widget', 'organic-widgets-pro' ),
		) );

		wp_enqueue_script( 'organic-widgets-module-image-featured', ORGANIC_WIDGETS_PRO_ADMIN_JS_DIR . 'organic-widgets-module-image-featured.js', array( 'jquery', 'media-upload', 'media-views' ), '1.0', true );
		wp_localize_script( 'organic-widgets-module-image-featured', 'OrganicWidgetIMG', 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 );
		}

	}

} // class Organic_Widgets_Pro_Content_Widget
