<?php
/**
 * Registers a widget to display a portfolio section.
 *
 * @package Organic Widgets Pro
 */

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


/**
 * Adds Organic_Widgets_Pro_Portfolio_Widget widget.
 */
class Organic_Widgets_Pro_Portfolio_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_portfolio_section', // Base ID.
			__( 'Organic Portfolio', 'organic-widgets-pro' ), // Name.
			array(
				'description'                 => __( 'A section displaying portfolio posts.', 'organic-widgets-pro' ),
				'customize_selective_refresh' => false, // No selective refresh to reload masonry on changes.
			)
		);

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

		// BG options.
		$this->bg_options = array(
			'color'     => true,
			'image'     => true,
			'bg_fixed'  => false,
			'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' => false,
			'padding_top'     => true,
			'padding_bottom'  => true,
			'custom_height'   => true,
			'custom_width'    => true,
			'content_width'   => true,
			'full_height'     => false,
		);

		// Display options.
		$this->display_options = array(
			'hide_title'   => true,
			'hide_meta'    => false,
			'hide_content' => false,
			'hide_bg'      => 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 ) {

		$attr           = array();
		$attr           = apply_filters( 'image_widget_image_attributes', $attr, $instance );
		$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;
		$portfolio_type = ( isset( $instance['portfolio_type'] ) ) ? $instance['portfolio_type'] : 'wp-category';
		$category       = ( isset( $instance['category'] ) ) ? $instance['category'] : 0;
		$num_columns    = ( isset( $instance['num_columns'] ) ) ? $instance['num_columns'] : 3;
		$max_posts      = ( isset( $instance['max_posts'] ) ) ? $instance['max_posts'] : 12;
		$column_gutter  = ( isset( $instance['column_gutter'] ) ) ? $instance['column_gutter'] : 24;
		$card_style     = ( isset( $instance['card_style'] ) ) ? $instance['card_style'] : 'style-squared';

		if ( isset( $instance['filter_menu'] ) ) {
			$filter_menu = $instance['filter_menu'];
		} else {
			$filter_menu = false;
		}
		if ( isset( $instance['bg_image_repeat'] ) ) {
			$bg_image_repeat = $instance['bg_image_repeat'];
		} else {
			$bg_image_repeat = false;
		}
		if ( isset( $instance['hide_post_title'] ) ) {
			$hide_post_title = $instance['hide_post_title'];
		} else {
			$hide_post_title = 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-portfolio-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 intval( $instance['widget_padding_top'] ); ?>px;
					<?php } ?>
					<?php if ( '' !== $instance['widget_padding_bottom'] ) { ?>
					padding-bottom: <?php echo intval( $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 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-portfolio-item {
				margin-bottom: <?php echo esc_html( $instance['column_gutter'] ); ?>px;
			}
			#<?php echo esc_attr( $this->id ); ?> .organic-widgets-portfolio-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-portfolio-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-portfolio-item.organic-widgets-fourth {
				width: calc(25% - ( <?php echo esc_html( $instance['column_gutter'] ); ?>px * 3 / 4 ));
			}
		<?php echo '</style>'; ?>

		<?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-portfolio-section
		<?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 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'] ) ) { ?>
				<div class="organic-widgets-text"><?php echo apply_filters( 'the_content', $instance['text'] ); ?></div>
			<?php } ?>

			<?php if ( class_exists( 'Jetpack' ) && post_type_exists( 'jetpack-portfolio' ) && 'jp-post-type' === $portfolio_type ) { $jetpack_project_query = new WP_Query( array( 'post_type' => 'jetpack-portfolio' ) ); } ?>

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

			<div class="row filter-button-group">
				<?php
				if ( class_exists( 'Jetpack' ) && post_type_exists( 'jetpack-portfolio' ) && 'jp-post-type' === $portfolio_type && $jetpack_project_query->have_posts() ) {
					$terms2 = get_terms( 'jetpack-portfolio-type' ); // This will go get all the categories.
				} else {
					$terms2 = get_categories( array( 'child_of' => $category ) );
				}
				$count = count( $terms2 ); // This counts the number of categories.
				echo '<button class="button" data-filter="*">Show All</button>';
				if ( $count > 0 ) {
					foreach ( $terms2 as $term ) {
						$termname = strtolower( $term->name );
						$termname = str_replace( ' ', '-', $termname );
						echo '<button class="button" data-filter=".' . $termname . '">' . $term->name . '</button>';
					}
				} // In the above foreach loop, the code will return all the values stored in $terms2 array.
				?>
			</div>

			<?php } ?>

			<?php

			if ( class_exists( 'Jetpack' ) && post_type_exists( 'jetpack-portfolio' ) && 'jp-post-type' === $portfolio_type && $jetpack_project_query->have_posts() ) {

				$post_type = 'jetpack-portfolio';
				$tax_query = array();

			} else {

				$post_type = 'post';
				$tax_query = array(
					array(
						'taxonomy' => 'category',
						'field'    => 'id',
						'terms'    => $category,
					),
				);

			}

			$wp_query = new WP_Query( array(
				'posts_per_page'   => $max_posts,
				'post_type'        => $post_type,
				'suppress_filters' => 0,
				'tax_query'        => $tax_query,
			) );

			if ( $wp_query->have_posts() ) : ?>

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

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

					<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

						<?php global $post; ?>

						<?php
						// Get the category for each unique post using the post ID.
						if ( class_exists( 'Jetpack' ) && post_type_exists( 'jetpack-portfolio' ) && 'jp-post-type' === $portfolio_type && $jetpack_project_query->have_posts() ) {
							$terms = get_the_terms( $post->ID, 'jetpack-portfolio-type' );
						} else {
							$terms = get_the_category( $post->ID, array( 'child_of' => $category ) );
						}
						if ( $terms && ! is_wp_error( $terms ) ) :
							$links = array();
							foreach ( $terms as $term ) {
								$links[] = $term->name;
							}
							$tax_links = join( ' ', str_replace( ' ', '-', $links ) );
							$tax       = strtolower( $tax_links );
						else :
								$tax = '';
						endif;
						?>

					<?php if ( has_post_thumbnail( get_the_ID() ) ) { ?>

					<?php /** BEGIN .organic-widgets-masonry-wrapper */ ?>
					<div class="organic-widgets-masonry-wrapper organic-widgets-column organic-widgets-portfolio-item <?php echo esc_html( $tax ); ?> organic-widgets-<?php echo $this->column_string( $num_columns ); ?>">

						<article>
							<div class="organic-widgets-portfolio-img">
								<a class="organic-widgets-portfolio-link" href="<?php the_permalink(); ?>">
									<?php if ( ! $hide_post_title ) { ?>
										<div class="organic-widgets-img-text"><h6><?php the_title(); ?></h6></div>
									<?php } ?>
									<div class="organic-widgets-featured-img">
										<?php if ( '1' === $num_columns ) { ?>
											<?php the_post_thumbnail( 'full' ); ?>
										<?php } else { ?>
											<?php the_post_thumbnail( 'large' ); ?>
										<?php } ?>
									</div>
								</a>
							</div>
						</article>

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

					<?php } ?>

					<?php endwhile; ?>

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

			<?php endif; ?>
			<?php wp_reset_postdata(); ?>

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

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

		if ( isset( $instance['portfolio_type'] ) ) {
			$portfolio_type = $instance['portfolio_type'];
		} else {
			$portfolio_type = 'wp-category';
		}
		if ( isset( $instance['category'] ) ) {
			$category = $instance['category'];
		} else {
			$category = false;
		}
		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['max_posts'] ) ) {
			$max_posts = $instance['max_posts'];
		} else {
			$max_posts = 12;
		}
		if ( isset( $instance['filter_menu'] ) ) {
			$filter_menu = $instance['filter_menu'];
		} else {
			$filter_menu = false;
		}
		if ( isset( $instance['card_style'] ) ) {
			$card_style = $instance['card_style'];
		} else {
			$card_style = 'style-squared';
		}

		?>

		<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>
			<input class="checkbox" type="checkbox" value="1" <?php checked( $filter_menu, '1' ); ?> id="<?php echo $this->get_field_id( 'filter_menu' ); ?>" name="<?php echo $this->get_field_name( 'filter_menu' ); ?>" />
			<label for="<?php echo $this->get_field_id( 'filter_menu' ); ?>"><?php _e( 'Filterable Portfolio Menu', 'organic-widgets-pro' ); ?></label>
		</p>

		<?php if ( class_exists( 'Jetpack' ) && post_type_exists( 'jetpack-portfolio' ) ) { ?>
		<p>
			<label for="<?php echo $this->get_field_id( 'portfolio_type' ); ?>"><?php esc_html_e( 'Portfolio Post Type:', 'organic-widgets-pro' ); ?>
				<select id="<?php echo $this->get_field_id( 'portfolio_type' ); ?>" name="<?php echo $this->get_field_name( 'portfolio_type' ); ?>">
					<option value="wp-category" <?php echo ( 'wp-category' === $portfolio_type ) ? 'selected' : ''; ?>><?php esc_html_e( 'WordPress Post Category', 'organic-widgets-pro' ); ?></option>
					<option value="jp-post-type" <?php echo ( 'jp-post-type' === $portfolio_type ) ? 'selected' : ''; ?>><?php esc_html_e( 'Jetpack Portfolio Post Type', 'organic-widgets-pro' ); ?></option>
				</select>
			</label>
		</p>
		<?php } ?>

		<p>
			<label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Portfolio Category:', 'organic-widgets-pro' ) ?></label>
			<?php
			wp_dropdown_categories( array(
				'selected' => $category,
				'id'       => $this->get_field_id( 'category' ),
				'name'     => $this->get_field_name( 'category' ),
			));
			?>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'max_posts' ); ?>"><?php _e( 'Max Number of Posts:', 'organic-widgets-pro' ) ?></label>
			<input type="number" min="1" max="16" value="<?php echo absint( $max_posts ); ?>" id="<?php echo $this->get_field_id('max_posts'); ?>" name="<?php echo $this->get_field_name( 'max_posts' ); ?>" class="widefat" style="width:100%;"/>
		</p>

		<div class="organic-widgets-option-set">
			<p class="organic-widgets-input-half">
				<label for="<?php echo $this->get_field_id( 'num_columns' ); ?>"><?php _e( '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>
				</select>
			</p>
			<p class="organic-widgets-input-half unit last">
				<label for="<?php echo $this->get_field_id( 'column_gutter' ); ?>"><?php _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 _e( 'px', 'organic-widgets-pro' ); ?></span>
			</p>
		</div>

		<script>
		(function($) {
			$("input[type='number'].maximum").keydown(function () {
				// Save old value.
				if (!$(this).val() || (parseInt($(this).val()) <= 100 && parseInt($(this).val()) >= 0))
				$(this).data("old", $(this).val());
			});
			$("input[type='number'].maximum").keyup(function () {
				// Check correct, else revert back to old value.
				if (!$(this).val() || (parseInt($(this).val()) <= 100 && parseInt($(this).val()) >= 0))
				;
				else
				$(this).val($(this).data("old"));
			});
		})(jQuery);
		</script>

		<p>
			<label for="<?php echo $this->get_field_id( 'card_style' ); ?>"><?php _e( 'Portfolio Image Style:', 'organic-widgets-pro' ); ?>
				<select id="<?php echo $this->get_field_id( 'card_style' ); ?>" name="<?php echo $this->get_field_name( 'card_style' ); ?>">
					<option value="style-squared" <?php echo ( 'style-squared' === $card_style ) ? 'selected' : ''; ?>><?php _e( 'Square', 'organic-widgets-pro' ); ?></option>
					<option value="style-rounded" <?php echo ( 'style-rounded' === $card_style ) ? 'selected' : ''; ?>><?php _e( 'Round', 'organic-widgets-pro' ); ?></option>
					<option value="style-bordered" <?php echo ( 'style-bordered' === $card_style ) ? 'selected' : ''; ?>><?php _e( 'Border', 'organic-widgets-pro' ); ?></option>
					<option value="style-shadowed" <?php echo ( 'style-shadowed' === $card_style ) ? 'selected' : ''; ?>><?php _e( 'Shadow', 'organic-widgets-pro' ); ?></option>
				</select>
			</label>
		</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_display_input_markup( $instance, $this->display_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

	}

	/**
	 * 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['filter_menu'] ) && ! empty( $new_instance['filter_menu'] ) ) {
			$instance['filter_menu'] = true;
		} else {
			$instance['filter_menu'] = false;
		}
		if ( isset( $new_instance['portfolio_type'] ) ) {
			$instance['portfolio_type'] = wp_strip_all_tags( $new_instance['portfolio_type'] );
		}
		if ( isset( $new_instance['category'] ) ) {
			$instance['category'] = wp_strip_all_tags( $new_instance['category'] );
		}
		if ( isset( $new_instance['num_columns'] ) ) {
			$instance['num_columns'] = wp_strip_all_tags( $new_instance['num_columns'] );
		}
		if ( isset( $new_instance['max_posts'] ) ) {
			$instance['max_posts'] = wp_strip_all_tags( $new_instance['max_posts'] );
		}
		if ( isset( $new_instance['column_gutter'] ) ) {
			$instance['column_gutter'] = wp_strip_all_tags( $new_instance['column_gutter'] );
		}
		if ( isset( $new_instance['card_style'] ) ) {
			$instance['card_style'] = wp_strip_all_tags( $new_instance['card_style'] );
		}

		/*--- 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_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;
		}
		if ( isset( $new_instance['bg_image_repeat'] ) ) {
			$instance['bg_image_repeat'] = true;
		} else {
			$instance['bg_image_repeat'] = 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'] );
		}
		/*--- END Font Options ----*/

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

		/*--- Display ----*/
		if ( isset( $new_instance['hide_post_title'] ) ) {
			$instance['hide_post_title'] = true;
		} else {
			$instance['hide_post_title'] = false;
		}
		/*--- END Display ----*/

		return $instance;
	}

	/**
	 * Enqueue admin javascript.
	 */
	public function admin_setup() {

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

		// Toggle Options.
		wp_enqueue_script( 'organic-widgets-toggle-options', ORGANIC_WIDGETS_PRO_ADMIN_JS_DIR . 'organic-widgets-toggle-options.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 );

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

		if ( ! wp_script_is( 'organic-widgets-isotope' ) ) {
			wp_enqueue_script( 'organic-widgets-isotope', ORGANIC_WIDGETS_PRO_BASE_DIR . 'public/js/isotope.pkgd.js', array( 'jquery', 'masonry' ), '3.0.6', true );
			wp_enqueue_script( 'organic-widgets-isotope-setup', ORGANIC_WIDGETS_PRO_BASE_DIR . 'public/js/isotope-setup.js', array( 'jquery', 'organic-widgets-isotope', 'masonry' ), '1.0', true );
		}

	}

} // class Organic_Widgets_Pro_Portfolio_Widget
