<?php
/***************************************************************************
 *
 * 	----------------------------------------------------------------------
 * 						DO NOT EDIT THIS FILE
 *	----------------------------------------------------------------------
 * 
 *  				     Copyright (C) Themify
 * 
 *	----------------------------------------------------------------------
 *
 ***************************************************************************/

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

///////////////////////////////////////////
// Feature Posts Class
///////////////////////////////////////////
class Themify_Social_Links extends WP_Widget {
	
	///////////////////////////////////////////
	// Feature Posts
	///////////////////////////////////////////
	function __construct() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'themify-social-links', 'description' => __('Social media links.', 'themify') );

		/* Widget control settings. */
		$control_ops = array( 'id_base' => 'themify-social-links' );

		/* Create the widget. */
		parent::__construct( 'themify-social-links', __('Themify - Social Links', 'themify'), $widget_ops, $control_ops );

	}

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

		/** This filter is documented in wp-includes/default-widgets.php */
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

		/* Before widget (defined by themes). */
		echo $args['before_widget'];

		if ( $title ) {
			echo $args['before_title'] . $title . $args['after_title'];
		}

		$data = themify_get_data();
		$pre = 'setting-link_';

		$field_ids = isset( $data[$pre.'field_ids'] ) ? json_decode( $data[$pre.'field_ids'] ) : '';
		
		if ( is_array( $field_ids ) || is_object( $field_ids ) ) {

			$show_link_name  = isset( $instance['show_link_name'] ) && $instance['show_link_name'] ? true : false;
			$open_new_window = isset( $instance['open_new_window'] ) && $instance['open_new_window'] ? true : false;
			$new_window_attr = $open_new_window ? 'target="_blank"' : '';
			$icon_type = themify_check( $pre . 'icon_type' )? themify_get( $pre . 'icon_type' ) : 'image-icon';

			// Icon Size
			$icon_size = isset($instance['icon_size']) && '' != $instance['icon_size']? $instance['icon_size'] : 'icon-medium';

			// Orientation
			$orientation = isset($instance['orientation']) && '' != $instance['orientation']? $instance['orientation'] : 'horizontal';

			echo '<ul class="social-links ' . esc_attr( $orientation ) . '">';

				foreach($field_ids as $key => $fid){

					$type_val = isset($data[$pre.'type_'.$fid])? $data[$pre.'type_'.$fid] : '';
					if ( $type_val != $icon_type ) {
						continue;
					}
					
					$title_name = $pre.'title_'.$fid;

					if ( function_exists( 'icl_t' ) ) {
						$title_val = icl_t('Themify', $title_name, $data[$title_name]);
					} else {
						$title_val = isset($data[$title_name])? $data[$title_name] : '';
					}

					$link_name = $pre.'link_'.$fid;
					$link_val = isset($data[$link_name])? trim( $data[$link_name] ) : '';
					if ( '' == $link_val ) {
						continue;
					}

					// Image Icon
					$img_name = $pre.'img_'.$fid;
					$img_val = ! isset( $data[$img_name] ) || '' == $data[$img_name] ? '' : '<img src="' . esc_url( $data[ $img_name ] ) . '" alt="'. esc_attr( $show_link_name ? '' : $title_val ) .'" />';

					// Font Icon
					$font_icon = '';
					if ( $font_icon_class = themify_get( $pre.'ficon_'.$fid ) ) {
						$fi_style = '';
						$font_icon_class = themify_get_icon( $font_icon_class );
						if ( $font_icon_color = themify_get_color( $pre.'ficolor_'.$fid ) ) {
							$fi_style .= 'color: ' . $font_icon_color . ';';
						}
						if ( $font_icon_bgcolor = themify_get_color( $pre.'fibgcolor_'.$fid ) ) {
							$fi_style .= 'background-color: ' . $font_icon_bgcolor . ';';
						}
						if ( '' != $fi_style ) {
							$fi_style = 'style="' . esc_attr( $fi_style ) . '"';
						}
						$font_icon = '<i class="' . esc_attr( $font_icon_class ) . '" ' . $fi_style . '></i>';
					}

					if('' != $link_val){
						echo sprintf('
							<li class="social-link-item %s %s %s">
								<a href="%s" %s>%s %s %s</a>
							</li>
							<!-- /themify-link-item -->',
							sanitize_title($title_val),
							esc_attr( $icon_type ),
							esc_attr( $icon_size ),
							esc_url( $link_val ),
							$new_window_attr,
							$font_icon,
							$img_val,
							$show_link_name? $title_val : ''
						);
					}
				}
			
			echo '</ul>';
		}

		/* After widget (defined by themes). */
		echo $args['after_widget'];
	}
	
	
	///////////////////////////////////////////
	// Update
	///////////////////////////////////////////
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags (if needed) and update the widget settings. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['show_link_name'] = $new_instance['show_link_name'];
		$instance['open_new_window'] = $new_instance['open_new_window'];
		$instance['icon_size'] = $new_instance['icon_size'];
		$instance['orientation'] = $new_instance['orientation'];

		return $instance;
	}
	
	///////////////////////////////////////////
	// Form
	///////////////////////////////////////////
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array(
			'title' => '',
			'show_link_name' => false,
			'open_new_window' => false,
			'icon_size' => 'icon-medium',
			'orientation' => 'horizontal',
		);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e('Title:', 'themify'); ?></label><br />
			<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" type="text" />
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_link_name'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_link_name' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_link_name' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_link_name' ) ); ?>"><?php _e('Show link name', 'themify'); ?></label>
		</p>

		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['open_new_window'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'open_new_window' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'open_new_window' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'open_new_window' ) ); ?>"><?php _e('Open link in new window', 'themify'); ?></label>
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'icon_size' ) ); ?>"><?php _e('Icon Size', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'icon_size' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'icon_size' ) ); ?>">
				<?php
				$sizes = array(
					'icon-small' => __( 'Small', 'themify' ),
					'icon-medium' => __( 'Medium', 'themify' ),
					'icon-large' => __( 'Large', 'themify' ),
				);
				foreach( $sizes as $size => $name ) {
					echo '<option value="' . esc_attr( $size ) . '"' . selected( isset( $instance['icon_size'] )? $instance['icon_size'] : 'icon-medium', $size, false ) . '>';
						echo esc_html( $name );
					echo '</option>';
				}
				?>
			</select>
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'orientation' ) ); ?>"><?php _e('Orientation', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'orientation' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'orientation' ) ); ?>">
				<?php
				$orientation_options = array(
					'vertical'   => __( 'Vertical', 'themify' ),
					'horizontal' => __( 'Horizontal', 'themify' ),
				);
				foreach( $orientation_options as $orientation => $name ) {
					echo '<option value="' . esc_attr( $orientation ) . '"' . selected( isset( $instance['orientation'] )? $instance['orientation'] : 'horizontal', $orientation, false ) . '>';
						echo esc_html( $name );
					echo '</option>';
				}
				?>
			</select>
		</p>
		
		<p>
			<?php echo wp_kses_post( sprintf( __( '<small>Manage links at <a href="%s">Themify > Settings > Social Links</a>.</small>', 'themify' ), esc_url( admin_url( 'admin.php?page=themify#setting-social_links' ) ) ) ); ?>
		</p>
		<?php
	}
}

///////////////////////////////////////////
// Feature Posts Class
///////////////////////////////////////////
class Themify_Feature_Posts extends WP_Widget {
	
	///////////////////////////////////////////
	// Feature Posts
	///////////////////////////////////////////
	function __construct() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'feature-posts', 'description' => __('A list of posts, optionally filter by category.', 'themify') );

		/* Widget control settings. */
		$control_ops = array( 'id_base' => 'themify-feature-posts' );

		/* Create the widget. */
		parent::__construct( 'themify-feature-posts', __('Themify - Feature Posts', 'themify'), $widget_ops, $control_ops );
	}
	
	///////////////////////////////////////////
	// Widget
	///////////////////////////////////////////
	function widget( $args, $instance ) {

		extract( $args );

		/* User-selected settings. */
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
		$category 		= isset( $instance['category'] ) ? $instance['category'] : 0;
		$show_count 	= isset( $instance['show_count'] ) ? $instance['show_count'] : 5;
		$show_date 		= isset( $instance['show_date'] ) ? true : false;
		$show_thumb 	= isset( $instance['show_thumb'] ) ? true : false;
		$display 		= isset( $instance['display'] )? $instance['display'] : false;
		$show_excerpt 	= isset( $instance['show_excerpt'] ) && $instance['show_excerpt'] ? true : false;
		$excerpt_length = isset( $instance['excerpt_length'] ) ? $instance['excerpt_length'] : 55;
		$show_title 	= isset( $instance['hide_title'] ) ? false : true;
		$orderby 		= isset( $instance['orderby'] ) ? $instance['orderby'] : 'date';
		$order 			= isset( $instance['order'] ) ? $instance['order'] : 'DESC';

		$query_opts = apply_filters( 'themify_query', array(
			'posts_per_page' => $show_count,
			'post_type' => 'post',
			'orderby' => $orderby,
			'order' => $order,
			'suppress_filters' => false,
		), $instance, $this->id_base );
		if ( $category ) $query_opts['cat'] = $category;
		
		$loop = get_posts($query_opts);
		
		if($loop) {
			
			/* Before widget (defined by themes). */
			echo $before_widget;
			
			/* Title of widget (before and after defined by themes). */
			if ( $title ) {
				echo $args['before_title'] . $title . $args['after_title'];
			}

			echo '<ul class="feature-posts-list">';

			// Save current post
			global $post;
			$saved_post = $post;

			foreach ($loop as $post) {
				setup_postdata($post);
				echo '<li>';

					$link = get_post_meta( $post->ID, 'external_link', true );
					if ( ! isset( $link ) || '' == $link ) {
						$link = get_permalink();
					}

					if ( $show_thumb ) {
						themify_image('f_image=true&ignore=true&w='.$instance['thumb_width'].'&h='.$instance['thumb_height'].'&before=<a'. themify_aria_hidden( $show_title, false ) . ' href="' . esc_url( $link ) . '">&after=</a>&class=post-img' );
					}

					if ( $show_title ) echo '<a href="' . esc_url( $link ) . '" class="feature-posts-title">' . get_the_title() . '</a> <br />';

					if ( $show_date ) echo '<small>' . get_the_date( apply_filters( 'themify_filter_widget_date', '' ) ) . '</small> <br />';

					if ( $show_excerpt || 'excerpt' == $display ) {
						$the_excerpt = get_the_excerpt();
						if($excerpt_length != '') {
							// cut to character limit
							$the_excerpt = substr( $the_excerpt, 0, $excerpt_length );
							// cut to last space
							$the_excerpt = substr( $the_excerpt, 0, strrpos( $the_excerpt, ' '));
						}
						echo '<span class="post-excerpt">' . wp_kses_post( $the_excerpt ) . '</span>';
					} elseif( 'content' == $display ) {
						echo '<div class="post-content">';
						the_content();
						echo '</div>';
					}

				echo '</li>';
			} //end for each

			// Restore current post
			wp_reset_postdata();
			setup_postdata( $saved_post );

			echo '</ul>';

			/* After widget (defined by themes). */
			echo $after_widget;
			
		}//end if $loop
		
	}
	
	///////////////////////////////////////////
	// Update
	///////////////////////////////////////////
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags (if needed) and update the widget settings. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['category'] = $new_instance['category'];
		$instance['show_count'] = $new_instance['show_count'];
		$instance['show_date'] = $new_instance['show_date'];
		$instance['show_thumb'] = $new_instance['show_thumb'];
		$instance['display'] = $new_instance['display'];
		$instance['hide_title'] = $new_instance['hide_title'];
		$instance['thumb_width'] = $new_instance['thumb_width'];
		$instance['thumb_height'] = $new_instance['thumb_height'];
		$instance['excerpt_length'] = $new_instance['excerpt_length'];
		$instance['orderby'] = $new_instance['orderby'];
		$instance['order'] = $new_instance['order'];

		return $instance;
	}
	
	///////////////////////////////////////////
	// Form
	///////////////////////////////////////////
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array(
			'title'          => __( 'Recent Posts', 'themify' ),
			'category'       => 0,
			'show_count'     => 5,
			'show_date'      => false,
			'show_thumb'     => false,
			'display'        => 'none',
			'hide_title'     => false,
			'thumb_width'    => 50,
			'thumb_height'   => 50,
			'excerpt_length' => 55,
			'orderby' => 'date',
			'order' => 'DESC',
		);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e('Title:', 'themify'); ?></label><br />
			<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" width="100%" />
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>"><?php _e('Category:', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'category' ) ); ?>">
				<option value="0" <?php if ( !$instance['category'] ) echo 'selected="selected"'; ?>><?php _e('All', 'themify'); ?></option>
				<?php
				$categories = get_categories(array('type' => 'post'));
				
				foreach( $categories as $cat ) {
					echo '<option value="' . esc_attr( $cat->cat_ID ) . '"';
					
					if ( $cat->cat_ID == $instance['category'] ) echo  ' selected="selected"';
					
					echo '>' . esc_html( $cat->cat_name . ' (' . $cat->category_count . ')' );
					
					echo '</option>';
				}
				?>
			</select>
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>"><?php _e( 'Order By', 'themify' ); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>">
				<?php
				$orderby_options = apply_filters( 'themify_posts_widget_orderby', array(
						'date'          => __( 'Date (default)', 'themify' ),
						'rand'          => __( 'Random', 'themify' ),
						'author'        => __( 'Author', 'themify' ),
						'title'         => __( 'Post Title', 'themify' ),
						'comment_count' => __( 'Comments Number', 'themify' ),
						'modified'      => __( 'Modified Date', 'themify' ),
						'name'          => __( 'Post Slug', 'themify' ),
						'ID'            => __( 'Post ID', 'themify' )
					)
				);
				foreach ( $orderby_options as $criteria => $name ) {
					echo '<option value="' . esc_attr( $criteria ) . '"' . selected( isset( $instance['orderby'] ) ? $instance['orderby'] : 'date', $criteria, false ) . '>';
					echo esc_html( $name );
					echo '</option>';
				}
				?>
			</select>
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>"><?php _e( 'Order', 'themify' ); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'order' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'order' ) ); ?>">
				<?php
				$order_options = array(
					'DESC'	=> __( 'Descending (default)', 'themify' ),
					'ASC'  => __( 'Ascending', 'themify' ),
				);
				foreach ( $order_options as $criteria => $name ) {
					echo '<option value="' . esc_attr( $criteria ) . '"' . selected( isset( $instance['order'] ) ? $instance['order'] : 'date', $criteria, false ) . '>';
					echo esc_html( $name );
					echo '</option>';
				}
				?>
			</select>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>"><?php _e('Show:', 'themify'); ?></label>
			<input id="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_count' ) ); ?>" value="<?php echo esc_attr( $instance['show_count'] ); ?>" size="2" /> <?php _e('posts', 'themify'); ?>
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['hide_title'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'hide_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hide_title' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'hide_title' ) ); ?>"><?php _e('Hide post title', 'themify'); ?></label>
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_date'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_date' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_date' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_date' ) ); ?>"><?php _e('Display post date', 'themify'); ?></label>
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_thumb'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_thumb' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_thumb' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_thumb' ) ); ?>"><?php _e('Display post thumbnail', 'themify'); ?></label>
		</p>
		
		<?php
		// only allow thumbnail dimensions if GD library supported
		if ( function_exists('imagecreatetruecolor') ) {
		?>
		<p>
		   <label for="<?php echo esc_attr( $this->get_field_id( 'thumb_width' ) ); ?>"><?php _e('Thumbnail size', 'themify'); ?></label> <input type="text" id="<?php echo esc_attr( $this->get_field_id( 'thumb_width' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'thumb_width' ) ); ?>" value="<?php echo esc_attr( $instance['thumb_width'] ); ?>" size="3" /> x <input type="text" id="<?php echo esc_attr( $this->get_field_id( 'thumb_height' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'thumb_height' ) ); ?>" value="<?php echo esc_attr( $instance['thumb_height'] ); ?>" size="3" />
		</p>
		<?php
		}
		?>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'display' ) ); ?>"><?php _e('Display:', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'display' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'display' ) ); ?>">
				<?php
				foreach( array(
					'none' => __('None', 'themify'),
					'content' => __('Content', 'themify'),
					'excerpt' => __('Excerpt', 'themify')
				) as $key => $title ) {
					echo '<option value="' . esc_attr( $key ) . '" '.selected($key, $instance['display'], false).' >' . esc_html( $title ) . '</option>';
				}
				?>
			</select>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'excerpt_length' ) ); ?>"><?php _e('Excerpt character limit:', 'themify'); ?></label>
			<input id="<?php echo esc_attr( $this->get_field_id( 'excerpt_length' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'excerpt_length' ) ); ?>" value="<?php echo esc_attr( $instance['excerpt_length'] ); ?>" size="5" /><br /><small><?php _e('(leave empty = full excerpt)', 'themify'); ?></small>
		</p>
		
		<?php
	}
}

///////////////////////////////////////////
// List Pages Class
///////////////////////////////////////////
class Themify_List_Pages extends WP_Widget {
	
	///////////////////////////////////////////
	// List Pages
	///////////////////////////////////////////
	function __construct() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'list-pages', 'description' => __('A list of pages', 'themify') );

		/* Widget control settings. */
		$control_ops = array( 'id_base' => 'themify-list-pages' );

		/* Create the widget. */
		parent::__construct( 'themify-list-pages', __('Themify - List Pages', 'themify'), $widget_ops, $control_ops );
	}
	
	///////////////////////////////////////////
	// Widget
	///////////////////////////////////////////
	function widget( $args, $instance ) {
		extract( $args );

		/* User-selected settings. */
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
		$parent = isset( $instance['parent'] ) ? $instance['parent'] : '';
		$depth = isset( $instance['depth'] ) ? $instance['depth'] : 0;
		$orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : false;
		$exclude = isset( $instance['exclude'] ) ? $instance['exclude'] : false;

		/* Before widget (defined by themes). */
		echo $before_widget;

		/* Title of widget (before and after defined by themes). */
		if ( $title ) {
			echo $args['before_title'] . $title . $args['after_title'];
		}
		
		echo '<ul class="pages-list">';
		
		wp_list_pages(array(
			'child_of'       => $parent,
			'depth'         => $depth,
			'sort_column'   => $orderby,
			'exclude'  => $exclude,
			'title_li' => ''
		));
		
		echo '</ul>';
		
		/* After widget (defined by themes). */
		echo $after_widget;
	}
	
	///////////////////////////////////////////
	// Update
	///////////////////////////////////////////
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags (if needed) and update the widget settings. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['parent'] = $new_instance['parent'];
		$instance['depth'] = $new_instance['depth'];
		$instance['orderby'] = $new_instance['orderby'];
		$instance['exclude'] = $new_instance['exclude'];

		return $instance;
	}
	
	///////////////////////////////////////////
	// Form
	///////////////////////////////////////////
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array( 
			'title' => __('Pages', 'themify'),
			'parent' => '',
			'depth' => 0,
			'orderby' => 'post_title',
			'exclude' => ''
			);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e('Title:', 'themify'); ?></label><br />
			<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" width="100%" />
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'parent' ) ); ?>"><?php _e('Parent:', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'parent' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'parent' ) ); ?>">
				<option value="0" <?php if ( 0 == $instance['parent'] ) echo 'selected="selected"'; ?>>All</option>
				<?php
				$pages = get_pages();
				
				foreach( $pages as $thepage ) {
					echo '<option value="' . esc_attr( $thepage->ID ) . '"';
					
					if ( $thepage->ID == $instance['parent'] ) echo ' selected="selected"';
					
					echo '>' . esc_html( $thepage->post_title );
					
					echo '</option>';
				}
				?>
			</select>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'depth' ) ); ?>"><?php _e('Depth:', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'depth' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'depth' ) ); ?>">
				<option value="0" <?php if ( 0 == $instance['depth'] ) echo 'selected="selected"'; ?>><?php _e('0 (default)', 'themify'); ?></option>
				<option value="1" <?php if ( 1 == $instance['depth'] ) echo 'selected="selected"'; ?>>1</option>
				<option value="2" <?php if ( 2 == $instance['depth'] ) echo 'selected="selected"'; ?>>2</option>
				<option value="3" <?php if ( 3 == $instance['depth'] ) echo 'selected="selected"'; ?>>3</option>
				<option value="4" <?php if ( 4 == $instance['depth'] ) echo 'selected="selected"'; ?>>4</option>
			</select>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>"><?php _e('Sort By:', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>">
				<option value="id" <?php if ( 'id' == $instance['orderby'] ) echo 'selected="selected"'; ?>>ID</option>
				<option value="menu_order" <?php if ( 'menu_order' == $instance['orderby'] ) echo 'selected="selected"'; ?>><?php _e('Menu Order', 'themify'); ?></option>
				<option value="post_title" <?php if ( 'post_title' == $instance['orderby'] ) echo 'selected="selected"'; ?>><?php _e('Post Title', 'themify'); ?></option>
				<option value="post_date" <?php if ( 'post_date' == $instance['orderby'] ) echo 'selected="selected"'; ?>><?php _e('Post Date', 'themify'); ?></option>
				<option value="post_name" <?php if ( 'post_name' == $instance['orderby'] ) echo 'selected="selected"'; ?>><?php _e('Post Name', 'themify'); ?></option>
			</select>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e('Exclude:', 'themify'); ?></label><br />
			<input id="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'exclude' ) ); ?>" value="<?php echo esc_attr( $instance['exclude'] ); ?>" /><br />
			<small><?php _e('Page IDs, separated by commas (eg. 5,8)', 'themify'); ?></small>
		</p>
		
		<?php
	}
}

///////////////////////////////////////////
// List Categories Class
///////////////////////////////////////////
class Themify_List_Categories extends WP_Widget {
	
	///////////////////////////////////////////
	// List Categories
	///////////////////////////////////////////
	function __construct() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'list-categories', 'description' => __('A list of categories', 'themify') );

		/* Widget control settings. */
		$control_ops = array( 'id_base' => 'themify-list-categories' );

		/* Create the widget. */
		parent::__construct( 'themify-list-categories', __('Themify - List Categories', 'themify'), $widget_ops, $control_ops );
	}
	
	///////////////////////////////////////////
	// Widget
	///////////////////////////////////////////
	function widget( $args, $instance ) {
		extract( $args );

		$themify_this_widget_id_pre = isset( $args['widget_id'] ) ? $args['widget_id'] : '';
		$themify_widget_id = $themify_this_widget_id_pre . '-cats';

		/* User-selected settings. */
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
		$parent = isset( $instance['parent'] ) ? $instance['parent'] : null;
		$depth = isset( $instance['depth'] ) ? $instance['depth'] : null;
		$orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : null;
		$exclude = isset( $instance['exclude'] ) ? $instance['exclude'] : null;
		$show_dropdown = isset( $instance['show_dropdown'] ) ? $instance['show_dropdown'] : false;
		$show_counts = isset( $instance['show_counts'] ) ? $instance['show_counts'] : false;
		$show_hierarchy = isset( $instance['show_hierarchy'] ) ? $instance['show_hierarchy'] : false;

		/* Before widget (defined by themes). */
		echo $before_widget;

		/* Title of widget (before and after defined by themes). */
		if ( $title ) {
			echo $args['before_title'] . $title . $args['after_title'];
		}

		$args = array(
				'orderby'       => $orderby,
				'show_count'    => $show_counts,
				'child_of'      => $parent,
				'exclude'       => $exclude,
				'hierarchical'  => $show_hierarchy,
				'depth'         => $depth,
				'title_li'      => '',
				'id'			=> $themify_widget_id
			);

		if ( $show_dropdown ) {
			$args['show_option_none'] = __('Select Category', 'themify');
			wp_dropdown_categories($args);
		?>
		
			<script type='text/javascript'>
			/* <![CDATA[ */
				function onCatChange() {
					var dropdown = document.getElementById('<?php echo esc_js( $themify_widget_id ); ?>'),
						catSelected = dropdown.options[dropdown.selectedIndex].value;
					console.log(catSelected);
					if ( catSelected > 0 ) {
						location.href = "<?php echo home_url(); ?>/?cat="+catSelected;
					}
				}
				document.getElementById('<?php echo esc_js( $themify_widget_id ); ?>').onchange = onCatChange;
			/* ]]> */
			</script>
		
		<?php
		}
		else {
			echo '<ul class="categories-list">';
			
			wp_list_categories($args);
			
			echo '</ul>';
		}

		/* After widget (defined by themes). */
		echo $after_widget;
	}
	
	///////////////////////////////////////////
	// Update
	///////////////////////////////////////////
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags (if needed) and update the widget settings. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['parent'] = $new_instance['parent'];
		$instance['depth'] = $new_instance['depth'];
		$instance['orderby'] = $new_instance['orderby'];
		$instance['exclude'] = $new_instance['exclude'];
		$instance['show_dropdown'] = $new_instance['show_dropdown'];
		$instance['show_counts'] = $new_instance['show_counts'];
		$instance['show_hierarchy'] = $new_instance['show_hierarchy'];

		return $instance;
	}
	
	///////////////////////////////////////////
	// Form
	///////////////////////////////////////////
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array( 'title' => __('Categories', 'themify'), 'parent' => 0, 'depth' => 0, 'orderby' => 'name', 'exclude' => '', 'show_dropdown' => false, 'show_counts' => false, 'show_hierarchy' => true );
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e('Title:', 'themify'); ?></label><br />
			<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" width="100%" />
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'parent' ) ); ?>"><?php _e('Parent:', 'themify'); ?></label>
			<?php
			wp_dropdown_categories( array(
				'show_option_all' => __('All', 'themify'),
				'orderby'         => 'name',
				'hierarchical'    => 1,
				'selected'        => $instance['parent'],
				'id'              => $this->get_field_id( 'parent' ),
				'name'            => $this->get_field_name( 'parent' ),
			));
			?>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'depth' ) ); ?>"><?php _e('Depth:', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'depth' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'depth' ) ); ?>">
				<option value="0" <?php if ( 0 == $instance['depth'] ) echo 'selected="selected"'; ?>><?php _e('0 (default)', 'themify'); ?></option>
				<option value="1" <?php if ( 1 == $instance['depth'] ) echo 'selected="selected"'; ?>>1</option>
				<option value="2" <?php if ( 2 == $instance['depth'] ) echo 'selected="selected"'; ?>>2</option>
				<option value="3" <?php if ( 3 == $instance['depth'] ) echo 'selected="selected"'; ?>>3</option>
				<option value="4" <?php if ( 4 == $instance['depth'] ) echo 'selected="selected"'; ?>>4</option>
			</select>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>"><?php _e('Orderby:', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>">
				<option value="id" <?php if ( 'id' == $instance['orderby'] ) echo 'selected="selected"'; ?>><?php _e( 'ID', 'themify' ); ?></option>
				<option value="name" <?php if ( 'name' == $instance['orderby'] ) echo 'selected="selected"'; ?>><?php _e( 'Name', 'themify' ); ?></option>
				<option value="slug" <?php if ( 'slug' == $instance['orderby'] ) echo 'selected="selected"'; ?>><?php _e( 'Slug', 'themify' ); ?></option>
				<option value="count" <?php if ( 'count' == $instance['orderby'] ) echo 'selected="selected"'; ?>><?php _e('Count', 'themify'); ?></option>
			</select>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e('Exclude:', 'themify'); ?></label><br />
			<input id="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'exclude' ) ); ?>" value="<?php echo esc_attr( $instance['exclude'] ); ?>" /><br />
			<small><?php _e('Category IDs, separated by commas (eg. 5,8)', 'themify'); ?></small>
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_dropdown'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_dropdown' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_dropdown' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_dropdown' ) ); ?>"><?php _e('Show as dropdown', 'themify'); ?></label>
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_counts'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_counts' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_counts' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_counts' ) ); ?>"><?php _e('Show post counts', 'themify'); ?></label>
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_hierarchy'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_hierarchy' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_hierarchy' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_hierarchy' ) ); ?>"><?php _e('Show hierarchy', 'themify'); ?></label>
		</p>
		
		<?php
	}
}

///////////////////////////////////////////
// Recent Comments Class
///////////////////////////////////////////
class Themify_Recent_Comments extends WP_Widget {
	
	///////////////////////////////////////////
	// Recent Comments
	///////////////////////////////////////////
	function __construct() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'recent-comments', 'description' => __('A list of recent comments from all posts', 'themify') );

		/* Widget control settings. */
		$control_ops = array( 'id_base' => 'themify-recent-comments' );

		/* Create the widget. */
		parent::__construct( 'themify-recent-comments', __('Themify - Recent Comments', 'themify'), $widget_ops, $control_ops );
	}
	
	///////////////////////////////////////////
	// Widget
	///////////////////////////////////////////
	function widget( $args, $instance ) {
		extract( $args );

		/* User-selected settings. */
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
		$show_count = isset( $instance['show_count'] ) ? $instance['show_count'] : 3;
		$show_avatar = isset( $instance['show_avatar'] ) ? $instance['show_avatar'] : false;
		$avatar_size = isset( $instance['avatar_size'] ) ? $instance['avatar_size'] : 32;
		$excerpt_length = isset( $instance['excerpt_length'] ) ? $instance['excerpt_length'] : 60;

		$comments = get_comments(array(
			'number' => $show_count,
			'status' => 'approve',
			'type' => 'comment'
		));
		if($comments){
		
			/* Before widget (defined by themes). */
			echo $before_widget;
	
			/* Title of widget (before and after defined by themes). */
			if ( $title ) {
				echo $args['before_title'] . $title . $args['after_title'];
			}
			echo '<ul class="recent-comments-list">';
			
			foreach($comments as $comment){
				$p = get_post($comment->comment_post_ID);
				if( ! empty( $p->post_password ) ) continue;
				$comm_title = get_the_title($comment->comment_post_ID);
				$comm_link = get_comment_link($comment->comment_ID);
				?>
			
				<li>
					<?php
						if ( $show_avatar ) {
							echo '<a href="' . esc_url( $comm_link ) . '">' . get_avatar($comment,$size=$avatar_size, '', sprintf( __( 'Avatar of %s', 'themify' ), $comment->comment_author ) ) . '</a>';
						}
						$comment_text = get_comment_excerpt( $comment->comment_ID );
						if(0 != $excerpt_length) {
							$last = substr( substr( $comment_text , 0, $excerpt_length), -1);
							if ( strlen(count_chars($comment_text, 3)) > $excerpt_length ) {
								$comment_text = substr( $comment_text , 0, $excerpt_length - 1) . preg_replace('/[^(\x00-\x7F)]*/','', $last);
							} else {
								$comment_text = substr( $comment_text , 0, $excerpt_length - 1);
							}
						}
					?>
					<a href="<?php echo esc_url( $comm_link );?>"><strong class="comment-author"><?php echo wp_kses_data( $comment->comment_author ); ?></strong>:</a> <?php echo wp_kses_data( $comment_text ); ?>&hellip;
				</li> 
			
				<?php 
			}
			
			echo '</ul>';

			/* After widget (defined by themes). */
			echo $after_widget;
		}//end if $comments
	}
	
	///////////////////////////////////////////
	// Update
	///////////////////////////////////////////
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags (if needed) and update the widget settings. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['show_count'] = $new_instance['show_count'];
		$instance['show_avatar'] = $new_instance['show_avatar'];
		$instance['avatar_size'] = $new_instance['avatar_size'];
		$instance['excerpt_length'] = $new_instance['excerpt_length'];

		return $instance;
	}
	
	///////////////////////////////////////////
	// Form
	///////////////////////////////////////////
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array(
			'title' => __('Recent Comments', 'themify'),
			'show_count' => 3, 
			'show_avatar' => false, 
			'avatar_size' => 32,
			'excerpt_length' => 60
			);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e('Title:', 'themify'); ?></label><br />
			<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" width="100%" />
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>"><?php _e('Show:', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_count' ) ); ?>">
				<?php
				for ( $i = 1; $i < 11; $i++ ) {
					echo '<option' . ( $i == $instance['show_count'] ? ' selected="selected"' : '' ) . '>' . $i . '</option>';
				}
				?>
			</select>
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_avatar'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_avatar' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_avatar' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'avatar' ) ); ?>"><?php _e('Show avatar', 'themify'); ?></label>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'avatar_size' ) ); ?>"><?php _e('Avatar size:', 'themify'); ?></label>
			<input id="<?php echo esc_attr( $this->get_field_id( 'avatar_size' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'avatar_size' ) ); ?>" value="<?php echo esc_attr( $instance['avatar_size'] ); ?>" size="4" /> px
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'excerpt_length' ) ); ?>"><?php _e('Comment excerpt:', 'themify'); ?></label>
			<input id="<?php echo esc_attr( $this->get_field_id( 'excerpt_length' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'excerpt_length' ) ); ?>" value="<?php echo esc_attr( $instance['excerpt_length'] ); ?>" size="4" /> <?php _e('characters', 'themify'); ?>
		</p>
		
		<?php
	}
}

///////////////////////////////////////////
// Banners & Links Class
///////////////////////////////////////////
class Themify_Links extends WP_Widget {
	
	///////////////////////////////////////////
	// Themify Links
	///////////////////////////////////////////
	function __construct() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'links', 'description' => __('A list of bookmarks', 'themify') );

		/* Widget control settings. */
		$control_ops = array( 'id_base' => 'themify-links' );

		/* Create the widget. */
		parent::__construct( 'themify-links', __('Themify - Banners &amp; Links', 'themify'), $widget_ops, $control_ops );
	}
	
	///////////////////////////////////////////
	// Widget
	///////////////////////////////////////////
	function widget( $args, $instance ) {
		extract( $args );

		/* User-selected settings. */
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
		$category = isset( $instance['category'] ) ? $instance['category'] : '';
		$orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : 'rand';
		$show_count = isset( $instance['show_count'] ) ? $instance['show_count'] : '';
		$show_thumb = isset( $instance['show_thumb'] ) ? $instance['show_thumb'] : false;
		$show_name = isset( $instance['show_name'] ) ? $instance['show_name'] : false;
		$show_desc = isset( $instance['show_desc'] ) ? $instance['show_desc'] : false;

		/* Before widget (defined by themes). */
		echo $before_widget;

		/* Title of widget (before and after defined by themes). */
		if ( $title ) {
			echo $args['before_title'] . $title . $args['after_title'];
		}
		
		echo '<ul class="links-list">';
		
		wp_list_bookmarks( array(
			'categorize'        => false,
			'title_li'          => false,
			'orderby'           => $orderby,
			'limit'             => $show_count,
			'category'          => $category,
			'show_images'       => $show_thumb,
			'show_name'         => $show_name,
			'show_description'  => $show_desc
		));
		
		echo '</ul>';

		/* After widget (defined by themes). */
		echo $after_widget;
	}
	
	///////////////////////////////////////////
	// Update
	///////////////////////////////////////////
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags (if needed) and update the widget settings. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['category'] = $new_instance['category'];
		$instance['orderby'] = $new_instance['orderby'];
		$instance['show_count'] = $new_instance['show_count'];
		$instance['show_thumb'] = $new_instance['show_thumb'];
		$instance['show_name'] = $new_instance['show_name'];
		$instance['show_desc'] = $new_instance['show_desc'];

		return $instance;
	}
	
	///////////////////////////////////////////
	// Form
	///////////////////////////////////////////
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array( 'title' => __('Blogroll', 'themify'), 'category' => '', 'orderby' => 'rand', 'show_count' => 10, 'show_thumb' => false, 'show_name' => false, 'show_desc' => false );
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e('Title:', 'themify'); ?></label><br />
			<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" width="100%" />
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>"><?php _e('Category:', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'category' ) ); ?>">
				<option value="" <?php if ( '' == $instance['category'] ) echo 'selected="selected"'; ?>>All</option>
				<?php
				$categories = get_categories(array('type' => 'link'));
				
				foreach( $categories as $cat ) {
					echo '<option value="' . esc_attr( $cat->cat_ID ) . '"';
					
					if ( $cat->cat_ID == $instance['category'] ) echo  ' selected="selected"';
					
					echo '>' . esc_html( $cat->cat_name . ' (' . $cat->category_count . ')' );
					
					echo '</option>';
				}
				?>
			</select>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>"><?php _e('Orderby:', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>">
				<option value="id" <?php if ( 'id' == $instance['orderby'] ) echo 'selected="selected"'; ?>><?php _e('ID', 'themify'); ?></option>
				<option value="name" <?php if ( 'name' == $instance['orderby'] ) echo 'selected="selected"'; ?>><?php _e('Name', 'themify'); ?></option>
				<option value="rating" <?php if ( 'rating' == $instance['orderby'] ) echo 'selected="selected"'; ?>><?php _e('Rating', 'themify'); ?></option>
				<option value="rand" <?php if ( 'rand' == $instance['orderby'] ) echo 'selected="selected"'; ?>><?php _e('Random', 'themify'); ?></option>
			</select>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>"><?php _e('Limit:', 'themify'); ?></label>
			<input id="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_count' ) ); ?>" value="<?php echo esc_attr( $instance['show_count'] ); ?>" size="2" />
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_thumb'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_thumb' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_thumb' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_thumb' ) ); ?>"><?php _e('Show link image', 'themify'); ?></label>
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_name'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_name' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_name' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_name' ) ); ?>"><?php _e('Show link name', 'themify'); ?></label>
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_desc'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_desc' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_desc' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_desc' ) ); ?>"><?php _e('Show link description', 'themify'); ?></label>
		</p>
		
		<?php
	}
}

///////////////////////////////////////////
// Twitter Class
///////////////////////////////////////////
class Themify_Twitter extends WP_Widget {
	
	///////////////////////////////////////////
	// Twitter
	///////////////////////////////////////////
	function __construct() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'twitter', 'description' => __('A list of latest tweets', 'themify') );

		/* Widget control settings. */
		$control_ops = array( 'id_base' => 'themify-twitter' );

		/* Create the widget. */
		parent::__construct( 'themify-twitter', __('Themify - Twitter', 'themify'), $widget_ops, $control_ops );
	}
	
	///////////////////////////////////////////
	// Widget
	///////////////////////////////////////////
	function widget( $args, $instance ) {
		extract( $args );

		/* User-selected settings. */
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
		$username = isset( $instance['username'] ) ? $instance['username'] : '';
		$type = isset( $instance['type'] ) ? $instance['type'] : '';
		$timeline_height = isset( $instance['timeline_height'] ) ? $instance['timeline_height'] : '';
		$timeline_width = isset( $instance['timeline_width'] ) ? $instance['timeline_width'] : '';
		$show_count = isset( $instance['show_count'] ) ? $instance['show_count'] : 5;
		$embed_code = isset( $instance['grid_embed_code'] ) ? $instance['grid_embed_code'] : '';
		$hide_timestamp = isset( $instance['hide_timestamp'] ) ? 'false' : 'true';
		$hide_footer = isset( $instance['hide_footer'] ) ? true : false;
		$show_follow = isset( $instance['show_follow'] ) ? ''.$instance['show_follow'] : 'false';
		$follow_text = isset( $instance['follow_text'] ) ? $instance['follow_text'] : '';
		$include_retweets = isset( $instance['include_retweets'] ) ? 'true' : 'false';
		$exclude_replies = isset( $instance['exclude_replies'] ) ? 'true' : 'false';
		$widget_id = $this->id;

		/* Before widget (defined by themes). */
		echo $before_widget;

		/* Title of widget (before and after defined by themes). */
		if ( $title ) {
			echo $args['before_title'] . $title . $args['after_title'];
		}

		/* remove twitter.com from Twitter ID */
		$username = preg_replace( '/^(https?:\/\/)?twitter.com\//', '', $username );

		/* ensure themify_shortcode_twitter is available */
		if( ! function_exists( 'themify_shortcode_twitter' ) ) {
			require_once THEMIFY_DIR . '/themify-shortcodes.php';
		}
		
		if(isset($_POST['action']) && $_POST['action']==='tb_load_module_partial'){
			global $themify_twitter_instance;
			$themify_twitter_instance++;
		}

		echo themify_shortcode_twitter(array(
				'username'         => $username,
				'type'             => $type,
				'timeline_height'  => $timeline_height,
				'timeline_width'   => $timeline_width,	
				'show_count'       => $show_count,
				'show_timestamp'   => $hide_timestamp,
				'hide_footer'      => $hide_footer,
				'show_follow'      => $show_follow,
				'follow_text'      => $follow_text,
				'embed_code'       => $embed_code,
				'include_retweets' => $include_retweets,
				'exclude_replies'  => $exclude_replies,
				'is_widget'        => 'true',
				'widget_id'        => $widget_id
		));

		/* After widget (defined by themes). */
		echo $after_widget;
	}
	
	///////////////////////////////////////////
	// Update
	///////////////////////////////////////////
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags (if needed) and update the widget settings. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['username'] = $new_instance['username'];
		$instance['type'] = $new_instance['type'];
		$instance['timeline_height'] = $new_instance['timeline_height'];
		$instance['timeline_width'] = $new_instance['timeline_width'];
		$instance['show_count'] = $new_instance['show_count'];
		$instance['hide_timestamp'] = $new_instance['hide_timestamp'];
		$instance['grid_embed_code'] = $new_instance['grid_embed_code'];
		$instance['hide_footer'] = $new_instance['hide_footer'];
		$instance['show_follow'] = $new_instance['show_follow'];
		$instance['follow_text'] = $new_instance['follow_text'];
		$instance['include_retweets'] = $new_instance['include_retweets'];
		$instance['exclude_replies'] = $new_instance['exclude_replies'];

		// delete transient
		delete_transient( $this->id . '_themify_tweets' );

		return $instance;
	}
	
	///////////////////////////////////////////
	// Form
	///////////////////////////////////////////
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array(
				'title' => __('Latest Tweets', 'themify'),
				'username'         => '',
				'type'             => '',
				'timeline_width'   => 300,
				'timeline_height'  => 400,
				'show_count'       => 5,
				'grid_embed_code'  => '',
				'hide_timestamp'   => false,
				'hide_footer'      => false,
				'show_follow'      => true,
				'follow_text'      => __('&rarr; Follow me', 'themify'),
				'include_retweets' => false,
				'exclude_replies'  => true
		);
		$instance = wp_parse_args( (array) $instance, $defaults ); 
		
		$twitter_default = false;
		if( !$instance['type'] ) {
			$twitter_default = true;
		} ?>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e('Title:', 'themify'); ?></label><br />
			<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" type="text" />
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'type' ) ); ?>"><?php _e('Type:', 'themify'); ?></label>
			<select class="toggle-display" data-toggle-display="twitter-display-toggle" id="<?php echo esc_attr( $this->get_field_id( 'type' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'type' ) ); ?>">
				<option value="0" data-display="twitter-default" <?php if ( !$instance['type'] ) echo 'selected="selected"'; ?>><?php _e('Default', 'themify'); ?></option>
				<option value="type-timeline" data-display="twitter-timeline" <?php if ( $instance['type'] == 'type-timeline' ) echo 'selected="selected"'; ?>><?php _e('Timeline', 'themify'); ?></option>
				<option value="type-grid" data-display="twitter-grid" <?php if ( $instance['type'] == 'type-grid' ) echo 'selected="selected"'; ?>><?php _e('Grid', 'themify'); ?></option>
			</select>
		</p>

		<div class="twitter-username" style="display: <?php echo $instance['type'] == 'type-grid' ? 'none' : 'block' ?>">
			<p>
				<?php echo sprintf(__('<small>Twitter access token is required at <a href="%s">Themify > Settings > Twitter</a>.</small>', 'themify'), admin_url('admin.php?page=themify#setting-twitter_settings')); ?>
			</p>
			
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'username' ) ); ?>"><?php _e('Twitter ID:', 'themify'); ?></label>
				<input id="<?php echo esc_attr( $this->get_field_id( 'username' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'username' ) ); ?>" value="<?php echo esc_attr( $instance['username'] ); ?>" type="text"/>
			</p>
		</div>
		
		<div class="twitter-display-toggle twitter-default" data-display="twitter-default" style="display: <?php echo $twitter_default ? 'block' : 'none' ?>">
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>"><?php _e('Show:', 'themify'); ?></label>
				<input id="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_count' ) ); ?>" value="<?php echo esc_attr( $instance['show_count'] ); ?>" size="3" type="text" /> <?php _e('tweets', 'themify'); ?>
			</p>
			
			<p>
				<input class="checkbox" type="checkbox" <?php checked( $instance['hide_timestamp'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'hide_timestamp' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hide_timestamp' ) ); ?>" />
				<label for="<?php echo esc_attr( $this->get_field_id( 'hide_timestamp' ) ); ?>"><?php _e('Hide timestamp', 'themify'); ?></label>
			</p>
			
			<p>
				<input class="checkbox" type="checkbox" <?php checked( $instance['show_follow'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_follow' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_follow' ) ); ?>" />
				<label for="<?php echo esc_attr( $this->get_field_id( 'show_follow' ) ); ?>"><?php _e('Display follow me button', 'themify'); ?></label>
			</p>
			
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'follow_text' ) ); ?>"><?php _e('Follow me text:', 'themify'); ?></label>
				<input id="<?php echo esc_attr( $this->get_field_id( 'follow_text' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'follow_text' ) ); ?>" value="<?php echo esc_attr( $instance['follow_text'] ); ?>" type="text" />
			</p>
			
			<p>
				<input class="checkbox" type="checkbox" <?php checked( $instance['include_retweets'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'include_retweets' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'include_retweets' ) ); ?>" />
				<label for="<?php echo esc_attr( $this->get_field_id( 'include_retweets' ) ); ?>"><?php _e('Include retweets', 'themify'); ?></label>
			</p>
			
			<p>
				<input class="checkbox" type="checkbox" <?php checked( $instance['exclude_replies'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'exclude_replies' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'exclude_replies' ) ); ?>" />
				<label for="<?php echo esc_attr( $this->get_field_id( 'exclude_replies' ) ); ?>"><?php _e('Exclude replies', 'themify'); ?></label>
			</p>
		</div>
		
		<div class="twitter-display-toggle twitter-timeline" data-display="twitter-timeline" style="display: <?php echo $instance['type'] == 'type-timeline' ? 'block' : 'none' ?>">
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'timeline_height' ) ); ?>"><?php _e('Embed Height:', 'themify'); ?></label>
				<input id="<?php echo esc_attr( $this->get_field_id( 'timeline_height' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'timeline_height' ) ); ?>" size=4 value="<?php echo esc_attr( $instance['timeline_height'] ); ?>" type="text"/>
			</p>
			
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'timeline_width' ) ); ?>"><?php _e('Embed Width:', 'themify'); ?></label>
				<input id="<?php echo esc_attr( $this->get_field_id( 'timeline_width' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'timeline_width' ) ); ?>" size=4 value="<?php echo esc_attr( $instance['timeline_width'] ); ?>" type="text"/>
			</p>
			
			<p>
				<input class="checkbox" type="checkbox" <?php checked( $instance['hide_footer'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'hide_footer' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hide_footer' ) ); ?>" />
				<label for="<?php echo esc_attr( $this->get_field_id( 'hide_footer' ) ); ?>"><?php _e('Hide footer', 'themify'); ?></label>
			</p>
			
			<p>
				<input class="checkbox" type="checkbox" <?php checked( $instance['exclude_replies'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'exclude_replies' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'exclude_replies' ) ); ?>" />
				<label for="<?php echo esc_attr( $this->get_field_id( 'exclude_replies' ) ); ?>"><?php _e('Exclude replies', 'themify'); ?></label>
			</p>
		</div>
		
		<div class="twitter-display-toggle twitter-grid" data-display="twitter-grid" style="display: <?php echo $instance['type'] == 'type-grid' ? 'block' : 'none' ?>">
			<p>
				<label style="display:block;" for="<?php echo esc_attr( $this->get_field_id( 'grid_embed_code' ) ); ?>"><?php _e('Embed Code', 'themify'); ?></label>
				<textarea style="width: 100%; height: 100px;" id="<?php echo esc_attr( $this->get_field_id( 'grid_embed_code' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'grid_embed_code' ) ); ?>"><?php echo esc_attr( $instance['grid_embed_code'] ); ?></textarea>
			</p>
			
			<p>
				<?php echo sprintf(__('<small>To create a grid layout, you\'ll need to first <a target="_blank" href="%s">create a collection</a></small>', 'themify'), 'https://dev.twitter.com/web/embedded-timelines/collection'); ?>
			</p>
		</div>
		
		<script type="text/javascript">
		( function( $ ) {
			var toggleDisplay = $('.toggle-display');
			if( toggleDisplay.length ) {
				toggleDisplay.each( function() {
					var containerClass = $(this).attr( 'data-toggle-display' ),
						context = $( this ).closest( '.widget-content' );

					$(this).change( function() {
						var show = $(this).find( 'option:selected' ).attr( 'data-display' ),
							userFn = show == 'twitter-grid' ? $.fn.hide : $.fn.show;

						$( '.' + containerClass, context ).hide();
						$( '.' + containerClass + '.' + show, context ).show();

						userFn.call( $( '.twitter-username', context ) );
					} );
				} );
			}
		} )( jQuery );
		</script>
		<?php
	}
}

///////////////////////////////////////////
// Flickr Class
///////////////////////////////////////////
class Themify_Flickr extends WP_Widget {
	
	///////////////////////////////////////////
	// Flickr
	///////////////////////////////////////////
	function __construct() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'flickr', 'description' => __('A reel of latest photos from Flickr', 'themify') );

		/* Widget control settings. */
		$control_ops = array( 'id_base' => 'themify-flickr' );

		/* Create the widget. */
		parent::__construct( 'themify-flickr', __('Themify - Flickr', 'themify'), $widget_ops, $control_ops );
	}
	
	///////////////////////////////////////////
	// Widget
	///////////////////////////////////////////
	function widget( $args, $instance ) {
		extract( $args );

		/* User-selected settings. */
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
		$username = isset( $instance['username'] ) ? $instance['username'] : '';
		$show_count = isset( $instance['show_count'] ) ? $instance['show_count'] : '10';
		$show_link = isset( $instance['show_link'] ) ? $instance['show_link'] : false;

		/* Before widget (defined by themes). */
		echo $before_widget;

		/* Title of widget (before and after defined by themes). */
		if ( $title ) {
			echo $args['before_title'] . $title . $args['after_title'];
		}	
		
		echo '<div id="flickr_badge_wrapper" class="clearfix">
				<script type="text/javascript" src="' . esc_url( themify_https_esc( 'http://www.flickr.com/badge_code_v2.gne' ) . '?count=' . $show_count . '.&amp;display=latest&amp;size=s&amp;layout=x&amp;source=user&amp;user=' . $username ) . '"></script>
			</div>';
		if( $show_link )
			echo '<a href="' . esc_url( 'http://www.flickr.com/photos/' . $username . '/' ) . '">' . __( 'View my Flickr photostream', 'themify' ) . '</a>';

		/* After widget (defined by themes). */
		echo $after_widget;
	}
	
	///////////////////////////////////////////
	// Update
	///////////////////////////////////////////
	function update( $new_instance, $old_instance ) {
		
		$instance = $old_instance;

		/* Strip tags (if needed) and update the widget settings. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['username'] = $new_instance['username'];
		$instance['show_count'] = $new_instance['show_count'];
		$instance['show_link'] = $new_instance['show_link'];
		return $instance;
	}
	
	///////////////////////////////////////////
	// Form
	///////////////////////////////////////////
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array(
			'title' => __('Recent Photos', 'themify'),
			'username' => '',
			'show_count' => 10,
			'show_link' => false,
		);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e('Title:', 'themify'); ?></label><br />
			<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" width="100%" />
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'username' ) ); ?>"><?php _e('Flickr ID:', 'themify'); ?></label>
			<input id="<?php echo esc_attr( $this->get_field_id( 'username' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'username' ) ); ?>" value="<?php echo esc_attr( $instance['username'] ); ?>" /><br />
			<small><?php printf( __( '* Find your Flickr ID: <a href="%s" target="_blank">idGettr</a>', 'themify' ), 'http://www.idgettr.com' ); ?></small>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>"><?php _e('Show:', 'themify'); ?></label>
			<input id="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_count' ) ); ?>" value="<?php echo esc_attr( $instance['show_count'] ); ?>" size="2" /> <?php _e('photos', 'themify'); ?>
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_link'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_link' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_link' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_link' ) ); ?>"><?php _e('Show link to account', 'themify'); ?></label>
		</p>

		<?php
	}
}

/**
 * @package themify
 * @subpackage shortcodes
 * @access public
 * @since 1.1.2
 */
class Themify_Most_Commented extends WP_Widget{
	
	function __construct(){
		$widget_ops = array( 'classname' => 'themify-most-commented', 'description' => __('A list with the most commented posts.', 'themify') );
		$control_ops = array( 'id_base' => 'themify-most-commented' );
		parent::__construct( 'themify-most-commented', __('Themify - Most Commented', 'themify'), $widget_ops, $control_ops );
	}
	
	function widget( $args, $instance ) {

		extract( $args );

		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
		$show_count = isset( $instance['show_count'] ) ? $instance['show_count'] : 10;
		$show_excerpt = isset( $instance['show_excerpt'] ) ? $instance['show_excerpt'] : false;
		$show_thumb = isset( $instance['show_thumb'] ) ? $instance['show_thumb'] : false;
		$thumb_width = isset( $instance['thumb_width'] ) ? $instance['thumb_width'] : 50;
		$thumb_height = isset( $instance['thumb_height'] ) ? $instance['thumb_height'] : 50;
		$excerpt_length = isset( $instance['excerpt_length'] ) ? $instance['excerpt_length'] : 55;
		$hide_title = isset( $instance['hide_title'] ) ? $instance['hide_title'] : false;
		$show_comment_count = isset( $instance['show_comment_count'] ) ? $instance['show_comment_count'] : false;

		$loop = get_posts( array(
			'numberposts' => $show_count,
			'orderby' => 'comment_count',
			'post_type' => 'post',
			'order' => 'DESC',
			'suppress_filters' => false,
		) );

		if ( $loop ) {
			
			/* Before widget (defined by themes). */
			echo $before_widget;
			
			/* Title of widget (before and after defined by themes). */
			if ( $title ) {
				echo $args['before_title'] . $title . $args['after_title'];
			}	
		
			echo '<ul class="feature-posts-list">';
			global $post;
			foreach ( $loop as $post ) {
				setup_postdata( $post );
				
				echo '<li>';
				
				if ( $show_thumb ) {
					echo themify_get_image( 'ignore=true&w=' . $instance['thumb_width'] . '&h=' . $instance['thumb_height'] . '&before=<a href="' . esc_url( get_permalink() ) . '">&after=</a>&class=post-img' );
				}

				if( !$hide_title ){
					echo '<a href="' . esc_url( get_permalink() ) . '" class="feature-posts-title">' . get_the_title() . '</a>';
				}
				
				if ( $show_comment_count ){
					$comment_string = (get_comments_number() > 1)? __('comments', 'themify') : __('comment', 'themify');
					echo '<br/><small>' . get_comments_number() . ' ' . $comment_string . '</small> <br />';
				}
				if ( $show_excerpt ) {
					$the_excerpt = get_the_excerpt();

					if ( $excerpt_length != '' ) {
						// cut to character limit
						$the_excerpt = substr( $the_excerpt, 0, $excerpt_length );

						// cut to last space
						$the_excerpt = substr( $the_excerpt, 0, strrpos( $the_excerpt, ' ' ) );
					}
					
					echo '<span class="post-excerpt">' . wp_kses_post( $the_excerpt ) . '</span>';
				}
					
				echo '</li>';
				wp_reset_postdata();
			}
			echo '</ul>';

			echo $after_widget;
		}
		
	}
	
	function update( $new_instance, $old_instance ){
		$instance = $old_instance;
		
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['show_count'] = $new_instance['show_count'];
		$instance['show_thumb'] = $new_instance['show_thumb'];
		$instance['thumb_width'] = $new_instance['thumb_width'];
		$instance['thumb_height'] = $new_instance['thumb_height'];
		$instance['show_excerpt'] = $new_instance['show_excerpt'];
		$instance['excerpt_length'] = $new_instance['excerpt_length'];
		$instance['hide_title'] = $new_instance['hide_title'];
		$instance['show_comment_count'] = $new_instance['show_comment_count'];
		
		return $instance;
	}
	
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array(
			'title' => __('Most Commented Posts', 'themify'),
			'show_count' => 5,
			'show_excerpt'	=> false,
			'show_thumb' => false,
			'thumb_width' => 50,
			'thumb_height' => 50,
			'excerpt_length' => 55,
			'hide_title' => false,
			'show_comment_count' => false
		);
		
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e('Title:', 'themify'); ?></label><br />
			<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" type="text" class="widefat" />
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['hide_title'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'hide_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hide_title' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'hide_title' ) ); ?>"><?php _e('Hide post title', 'themify'); ?></label>
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_comment_count'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_comment_count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_comment_count' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_comment_count' ) ); ?>"><?php _e('Display comment count', 'themify'); ?></label>
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>"><?php _e('Number of posts:', 'themify'); ?></label>
			<input id="<?php echo esc_attr( $this->get_field_id( 'show_count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_count' ) ); ?>" value="<?php echo esc_attr( $instance['show_count'] ); ?>" size="2" type="text" />
		</p>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_thumb'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_thumb' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_thumb' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_thumb' ) ); ?>"><?php _e('Display post thumbnail', 'themify'); ?></label>
		</p>
		
		<?php
		// only allow thumbnail dimensions if GD library supported
		if ( function_exists('imagecreatetruecolor') ) {
		?>
		<p>
		   <label for="<?php echo esc_attr( $this->get_field_id( 'thumb_width' ) ); ?>"><?php _e('Thumbnail size', 'themify'); ?></label> <input type="text" id="<?php echo esc_attr( $this->get_field_id( 'thumb_width' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'thumb_width' ) ); ?>" value="<?php echo esc_attr( $instance['thumb_width'] ); ?>" size="3" /> x <input type="text" id="<?php echo esc_attr( $this->get_field_id( 'thumb_height' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'thumb_height' ) ); ?>" value="<?php echo esc_attr( $instance['thumb_height'] ); ?>" size="3" />
		</p>
		<?php
		}
		?>
		
		<p>
			<input class="checkbox" type="checkbox" <?php checked( $instance['show_excerpt'], 'on' ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_excerpt' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_excerpt' ) ); ?>" />
			<label for="<?php echo esc_attr( $this->get_field_id( 'show_excerpt' ) ); ?>"><?php _e('Display post excerpt', 'themify'); ?></label>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'excerpt_length' ) ); ?>"><?php _e('Excerpt character limit:', 'themify'); ?></label>
			<input id="<?php echo esc_attr( $this->get_field_id( 'excerpt_length' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'excerpt_length' ) ); ?>" value="<?php echo esc_attr( $instance['excerpt_length'] ); ?>" size="1" type="text" /><br/><small><?php _e('(leave empty = full excerpt)', 'themify'); ?></small>
		</p>

		<?php
	}
}

///////////////////////////////////////////
// Google Maps Class
///////////////////////////////////////////
class Themify_Google_Maps extends WP_Widget {
	
	///////////////////////////////////////////
	// Google Maps
	///////////////////////////////////////////
	function __construct() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'google-maps', 'description' => __('A map to place your location', 'themify') );

		/* Widget control settings. */
		$control_ops = array( 'id_base' => 'themify-google-maps' );

		/* Create the widget. */
		parent::__construct( 'themify-google-maps', __('Themify - Google Maps', 'themify'), $widget_ops, $control_ops );
	}
	
	///////////////////////////////////////////
	// Widget
	///////////////////////////////////////////
	function widget( $args, $instance ) {
		extract( $args );

		/* User-selected settings. */
		$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

		/* Before widget (defined by themes). */
		echo $before_widget;

		/* Title of widget (before and after defined by themes). */
		if ( $title ) {
			echo $args['before_title'] . $title . $args['after_title'];
		}	
		
		echo '<div id="themify_google_map_wrapper" class="clearfix">';

		if ($instance['map_display_type'] == 'static') : ?>
            <?php
            $args = '';
            if (!empty($instance['address_map'])) {
                $args .= 'center=' . $instance['address_map'];
            } elseif (!empty($instance['latlong_map'])) {
                $args .= 'center=' . $instance['latlong_map'];
            }
            $args .= '&zoom=' . $instance['zoom_map'];
            $args .= '&maptype=' . strtolower($instance['type_map']);
            $args .= '&size=' . ( isset( $instance['width'] ) ? filter_var( $instance['width'], FILTER_SANITIZE_NUMBER_INT ) : '500' ) . 'x' . ( isset( $instance['height'] ) ? $instance['height'] : '300' );
            ?>
            <img style="<?php echo esc_attr($instance['style']); ?>" src="//maps.googleapis.com/maps/api/staticmap?<?php echo $args; ?>" />

        <?php else : ?>
            <?php
			if ( isset( $instance['width'] ) ) {
				// use % or pixels
				if( ! preg_match( '/%$/', $instance['width'] ) ) {
					$instance['width'] .= 'px';
				}
			} else {
				$instance['width'] = '100%';
			}
            $style = 'width:' . $instance['width'] . ';';
            $style .= 'height:' . ( isset( $instance['height'] ) ? $instance['height'] : '300' ) . 'px;';

            if (!empty($instance['address_map']) || !empty($instance['latlong_map'])) {
                $geo_address = !empty($instance['address_map']) ? $instance['address_map'] : $instance['latlong_map'];
                ?>
                <?php
                $data['address'] = $geo_address;
                $data['zoom'] = $instance['zoom_map'];
                $data['type'] = $instance['type_map'];
                $data['scroll'] = $instance['scrollwheel_map'] == 'enable';
                $data['drag'] = 'enable' == $instance['draggable_map'];
                ?>
                <div data-map="<?php echo esc_attr( base64_encode( json_encode( $data ) ) ); ?>" class="themify_map map-container"  style="<?php echo esc_attr($style); ?>"  data-info-window="<?php echo esc_attr($instance['info_window_map']); ?>" data-reverse-geocoding="<?php echo ( empty($instance['address_map']) && !empty($instance['latlong_map']) ) ? true : false; ?>"></div>
            <?php } ?>
        <?php
        endif;
		echo '</div>';

		/* After widget (defined by themes). */
		echo $after_widget;
	}
	
	///////////////////////////////////////////
	// Update
	///////////////////////////////////////////
	function update( $new_instance, $old_instance ) {
		
		$instance = $old_instance;

		/* Strip tags (if needed) and update the widget settings. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['map_display_type'] = strip_tags( $new_instance['map_display_type'] );
		$instance['address_map'] = strip_tags( $new_instance['address_map'] );
		$instance['width'] = strip_tags( $new_instance['width'] );
		$instance['height'] = strip_tags( $new_instance['height'] );

		// Cleans the lat/lon string
		preg_match_all("/(?<lat>[-+]?([0-9]+\.[0-9]+)).*(?<long>[-+]?([0-9]+\.[0-9]+))/", $new_instance['latlong_map'], $matches);
		$instance['latlong_map'] = !empty($matches[0]) ? str_replace(' ', '', $matches[0][0]): '';

		$instance['zoom_map'] = strip_tags( $new_instance['zoom_map'] );
		$instance['type_map'] = strip_tags( $new_instance['type_map'] );
		$instance['scrollwheel_map'] = strip_tags( $new_instance['scrollwheel_map'] );
		$instance['draggable_map'] = strip_tags( $new_instance['draggable_map'] );
		$instance['draggable_disable_mobile_map'] = strip_tags( $new_instance['draggable_disable_mobile_map'] );
		$instance['info_window_map'] = strip_tags( $new_instance['info_window_map'] );
		return $instance;
	}
	
	///////////////////////////////////////////
	// Form
	///////////////////////////////////////////
	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array(
			'title' => 'Themify Google Map',
			'map_display_type' => 'dynamic',
			'address_map' => '',
			'latlong_map' => '43.6453137,-79.1831939',
			'width' => '100%',
			'height' => '300',
			'zoom_map' => 8,
			'type_map' => 'ROADMAP',
			'scrollwheel_map' => 'disable',
			'draggable_map' => 'enable',
			'draggable_disable_mobile_map' => 'yes',
			'info_window_map' => ''
		);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e('Title', 'themify'); ?></label><br>
			<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" />
		</p>

		<p>
			<label><?php _e('Type:', 'themify'); ?></label><br>
			<input type="radio" name="<?php echo esc_attr( $this->get_field_name( 'map_display_type' ) ); ?>" value="dynamic" <?php checked( $instance['map_display_type'], 'dynamic' ); ?>> <?php _e('Dynamic', 'themify'); ?><br>
  			<input type="radio" name="<?php echo esc_attr( $this->get_field_name( 'map_display_type' ) ); ?>" value="static" <?php checked( $instance['map_display_type'], 'static' ); ?>> <?php _e('Static', 'themify'); ?>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'address_map' ) ); ?>"><?php _e('Address:', 'themify'); ?></label><br>
			<textarea id="<?php echo esc_attr( $this->get_field_id( 'address_map' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'address_map' ) ); ?>" class="widefat"><?php echo esc_attr( $instance['address_map'] ); ?></textarea>
		</p>
		
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'latlong_map' ) ); ?>"><?php _e('Lat/Lon', 'themify'); ?></label><br>
			<input id="<?php echo esc_attr( $this->get_field_id( 'latlong_map' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'latlong_map' ) ); ?>" value="<?php echo esc_attr( $instance['latlong_map'] ); ?>" class="widefat" /><br/><small><?php _e('Use lat/lon instead of address (Leave address field empty to use this). Example: 43.6453137,-79.1831939', 'themify'); ?></small>
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'width' ) ); ?>"><?php _e('Width', 'themify'); ?></label>
			<input type="text" id="<?php echo esc_attr( $this->get_field_id( 'width' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'width' ) ); ?>" value="<?php echo esc_attr( $instance['width'] ); ?>"/>
			<label for="<?php echo esc_attr( $this->get_field_id( 'height' ) ); ?>"><?php _e('Height', 'themify'); ?></label>
			<input type="text" id="<?php echo esc_attr( $this->get_field_id( 'height' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'height' ) ); ?>" value="<?php echo esc_attr( $instance['height'] ); ?>"/>
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'zoom_map' ) ); ?>"><?php _e('Zoom:', 'themify'); ?></label>
			<input type="number" min="1" max="16" id="<?php echo esc_attr( $this->get_field_id( 'zoom_map' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'zoom_map' ) ); ?>" value="<?php echo esc_attr( $instance['zoom_map'] ); ?>"/>
		</p>

		<p>
			<label><?php _e('Type:', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'type_map' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'type_map' ) ); ?>">
				<option value="ROADMAP" <?php selected( $instance['type_map'], 'ROADMAP' );?>><?php _e('Road Map', 'themify'); ?></option>
				<option value="SATELLITE" <?php selected( $instance['type_map'], 'SATELLITE' );?>><?php _e('Satellite', 'themify'); ?></option>
				<option value="HYBRID" <?php selected( $instance['type_map'], 'HYBRID' );?>><?php _e('Hybrid', 'themify'); ?></option>
				<option value="TERRAIN" <?php selected( $instance['type_map'], 'TERRAIN' );?>><?php _e('Terrain', 'themify'); ?></option>
			</select>
		</p>

		<p>
			<label><?php _e('Scrollwheel:', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'scrollwheel_map' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'scrollwheel_map' ) ); ?>">
				<option value="disable" <?php selected( $instance['scrollwheel_map'], 'disable' );?>><?php _e('Disable', 'themify'); ?></option>
				<option value="enable" <?php selected( $instance['scrollwheel_map'], 'enable' );?>><?php _e('Enable', 'themify'); ?></option>
			</select>
		</p>

		<p>
			<label><?php _e('Draggable:', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'draggable_map' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'draggable_map' ) ); ?>">
				<option value="disable" <?php selected( $instance['draggable_map'], 'disable' );?>><?php _e('Disable', 'themify'); ?></option>
				<option value="enable" <?php selected( $instance['draggable_map'], 'enable' );?>><?php _e('Enable', 'themify'); ?></option>
			</select>
		</p>

		<p>
			<label><?php _e('Disable draggable on mobile:', 'themify'); ?></label>
			<select id="<?php echo esc_attr( $this->get_field_id( 'draggable_disable_mobile_map' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'draggable_disable_mobile_map' ) ); ?>">
				<option value="yes" <?php selected( $instance['draggable_disable_mobile_map'], 'disable' );?>><?php _e('Yes', 'themify'); ?></option>
				<option value="no" <?php selected( $instance['draggable_disable_mobile_map'], 'enable' );?>><?php _e('No', 'themify'); ?></option>
			</select>
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'info_window_map' ) ); ?>"><?php _e('Infowindow:', 'themify'); ?></label><br>
			<textarea id="<?php echo esc_attr( $this->get_field_id( 'info_window_map' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'info_window_map' ) ); ?>" class="widefat"><?php echo esc_attr( $instance['info_window_map'] ); ?></textarea>
		</p>

		<?php
	}
}


///////////////////////////////////////////
// Register Widgets
///////////////////////////////////////////
function themify_register_widgets() {
	register_widget('Themify_Feature_Posts');
	register_widget('Themify_List_Pages');
	register_widget('Themify_List_Categories');
	register_widget('Themify_Recent_Comments');
	if ( get_option( 'link_manager_enabled' ) ) {
		register_widget('Themify_Links');
	}
	register_widget('Themify_Social_Links');
	register_widget('Themify_Twitter');
	register_widget('Themify_Flickr');
	register_widget('Themify_Most_Commented');
	register_widget('Themify_Google_Maps');
}
add_action('widgets_init', 'themify_register_widgets', 1);

?>