<?php
/**
* @package Krown
* @subpackage Krown
* @since Krown 1.0
*/

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

class rpwe_widget extends WP_Widget {

	/**
	 * Widget setup
	 */
	function rpwe_widget() {
	
		$widget_ops = array( 
			'classname' => 'rpwe_widget recent-posts-extended', 
			'description' => __( 'KRWN Recent Blog Post Widget for this site.', 'krwn' ) 
		);

		$control_ops = array( 
			'width' => 300, 
			'height' => 350, 
			'id_base' => 'rpwe_widget' 
		);

		$this->WP_Widget( 'rpwe_widget', __( 'KRWN Recent Posts Widget ', 'krwn' ), $widget_ops, $control_ops );

	}

	/**
	 * Display widget
	 */
	function widget( $args, $instance ) {
		extract( $args, EXTR_SKIP );
 
		$title = apply_filters( 'widget_title', $instance['title'] );
		$limit = $instance['limit'];
		$excerpt = $instance['excerpt'];
		$length = (int)( $instance['length'] );
		$thumb = $instance['thumb'];
		$thumb_height = (int)( $instance['thumb_height'] );
		$thumb_width = (int)( $instance['thumb_width'] );
		$cat = $instance['cat'];
		$post_type = $instance['post_type'];
		$date = $instance['date'];
		$css = wp_filter_nohtml_kses( $instance['css'] );

		echo $before_widget;

		if( $css )
		    echo '<style>' . $css . '</style>';
 

		global $post;

		if ( false === ( $rpwewidget = get_transient( 'rpwewidget_' . $widget_id ) ) ) {

			$args = array( 
				'numberposts' => $limit,
				'cat' => $cat,
				'post_type' => $post_type
			);

		    $rpwewidget = get_posts( $args );

		    set_transient( 'rpwewidget_' . $widget_id, $rpwewidget, 60*60*12 );

		} ?>
		<h3><?php echo $instance['title']; ?></h3>
		<ul class="latest-posts">
		
		<?php foreach( $rpwewidget as $post ) :	setup_postdata( $post ); 
			// $dt="";
			// $month=""; $day=""; $year="";$cm="";
			// $dt=get_the_date();
			// //echo $dt;
			// list($month, $day, $year) = split('[ /.-]', $dt);
			// list($day,$cm) = explode(',', $day);
		?>
			<li class="clearfix">
				<?php if( has_post_thumbnail() && $thumb == true ) { ?> 
					<?php the_post_thumbnail(); ?>
				<?php } else {?>
				<img src="http://placehold.it/70x70" alt="Team Member"/>
				<?php } ?>
				<div>
					<a href="<?php the_permalink();?>"><h5><?php echo get_the_title();?></h5></a>
					<?php if( $excerpt == true ) {  ?>
					<p><?php echo rpwe_excerpt( $length ); ?></p>
					<?php } ?>
					<p><?php echo get_the_time('F'); ?> <?php echo get_the_time('j'); ?> <?php echo get_the_time('Y'); ?></p>
				</div>
			</li>
		<?php endforeach; wp_reset_postdata(); ?>
		</ul>
		<?php

		echo $after_widget;
		
	}

	/**
	 * Update widget
	 */
	function update( $new_instance, $old_instance ) {

		$instance = $old_instance;
		$instance['title'] = esc_attr( $new_instance['title'] );
		$instance['limit'] = $new_instance['limit'];
		$instance['excerpt'] = $new_instance['excerpt'];
		$instance['length'] = (int)( $new_instance['length'] );
		$instance['thumb'] = $new_instance['thumb'];
		$instance['thumb_height'] = (int)( $new_instance['thumb_height'] );
		$instance['thumb_width'] = (int)( $new_instance['thumb_width'] );
		$instance['cat'] = $new_instance['cat'];
		$instance['post_type'] = $new_instance['post_type'];
		$instance['date'] = $new_instance['date'];
		$instance['css'] = wp_filter_nohtml_kses( $new_instance['css'] );

		delete_transient( 'rpwewidget_' . $this->id );

		return $instance;

	}

	/**
	 * Widget setting
	 */
	function form( $instance ) {

		/* Set up some default widget settings. */
        $defaults = array(
            'title' => '',
            'limit' => 5,
            'excerpt' => '',
            'length' => 10,
            'thumb' => true,
            'thumb_height' => 45,
            'thumb_width' => 45,
            'cat' => '',
            'post_type' => '',
            'date' => true,
            'css' => ''
        );
        
		$instance = wp_parse_args( (array) $instance, $defaults );
		$title = esc_attr( $instance['title'] );
		$limit = $instance['limit'];
		$excerpt = $instance['excerpt'];
		$length = (int)($instance['length']);
		$thumb = $instance['thumb'];
		$thumb_height = (int)( $instance['thumb_height'] );
		$thumb_width = (int)( $instance['thumb_width'] );
		$cat = $instance['cat'];
		$post_type = $instance['post_type'];
		$date = $instance['date'];
		$css = wp_filter_nohtml_kses( $instance['css'] );

	?>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'fitness' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo $title; ?>" />
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'limit' ) ); ?>"><?php _e( 'Limit:', 'fitness' ); ?></label>
			<select class="widefat" name="<?php echo $this->get_field_name( 'limit' ); ?>" id="<?php echo $this->get_field_id( 'limit' ); ?>">
				<?php for ( $i=1; $i<=20; $i++ ) { ?>
					<option <?php selected( $limit, $i ) ?> value="<?php echo $i; ?>"><?php echo $i; ?></option>
				<?php } ?>
			</select>
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'date' ) ); ?>"><?php _e( 'Display date?', 'fitness' ); ?></label>
	      	<input id="<?php echo $this->get_field_id( 'date' ); ?>" name="<?php echo $this->get_field_name( 'date' ); ?>" type="checkbox" value="1" <?php checked( '1', $date ); ?> />&nbsp;
        </p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'excerpt' ) ); ?>"><?php _e( 'Display excerpt?', 'fitness' ); ?></label>
	      	<input id="<?php echo $this->get_field_id( 'excerpt' ); ?>" name="<?php echo $this->get_field_name( 'excerpt' ); ?>" type="checkbox" value="1" <?php checked( '1', $excerpt ); ?> />&nbsp;
        </p>
        <p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'length' ) ); ?>"><?php _e( 'Excerpt length:', 'fitness' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'length' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'length' ) ); ?>" type="text" value="<?php echo $length; ?>" />
		</p>

		<?php if( current_theme_supports( 'post-thumbnails' ) ) { ?>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'thumb' ) ); ?>"><?php _e( 'Display thumbnail?', 'fitness' ); ?></label>
		      	<input id="<?php echo $this->get_field_id( 'thumb' ); ?>" name="<?php echo $this->get_field_name( 'thumb' ); ?>" type="checkbox" value="1" <?php checked( '1', $thumb ); ?> />&nbsp;
	        </p>

		<?php } ?>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'cat' ) ); ?>"><?php _e( 'Limit to category: ' , 'fitness' ); ?></label>
			<?php wp_dropdown_categories( array( 'name' => $this->get_field_name( 'cat' ), 'show_option_all' => __( 'All categories' , 'fitness' ), 'hide_empty' => 1, 'hierarchical' => 1, 'selected' => $cat ) ); ?>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'css' ); ?>"><?php _e( 'Custom CSS:', 'fitness' ); ?></label>
			<textarea class="widefat" id="<?php echo $this->get_field_id( 'css' ); ?>" name="<?php echo $this->get_field_name( 'css' ); ?>" style="height:100px;"><?php echo $css; ?></textarea>
		</p>

	<?php
	}

}

/**
 * Register widget.
 *
 * @since 0.1
 */
function rpwe_register_widget() {
	register_widget( 'rpwe_widget' );
}
add_action( 'widgets_init', 'rpwe_register_widget' );

/**
 * Print a custom excerpt.
 * http://bavotasan.com/2009/limiting-the-number-of-words-in-your-excerpt-or-content-in-wordpress/
 *
 * @since 0.1
 */
function rpwe_excerpt( $length ) {

	$excerpt = explode( ' ', get_the_excerpt(), $length );
	if ( count( $excerpt )>=$length ) {
		array_pop( $excerpt );
		$excerpt = implode( " ", $excerpt ) . '&hellip;';
	} else {
		$excerpt = implode( " ", $excerpt );
	} 
		$excerpt = preg_replace( '`\[[^\]]*\]`', '', $excerpt );

	return $excerpt;

}
?>
<?php
/**
 * Social Media Widget
 *
 * @since 1.0
 */
class social_widget extends WP_Widget {
    function __construct() {
        parent::__construct(false, $name = 'KRWN Social Widget', array( 'description' => 'KRWN Social Widget for this site.' ) );
    }

    function form( $instance ) {
	$widget_title="";
	$fb_url="";
	$twit_url="";
	$dribble_url="";
	$yt_url="";
	$gplus_url="";
	$tumblr_url="";
	$pinterest_url="";
	$flickr_url="";
	$instagram_url="";
	$rss_url="";
		if(isset( $instance['widget_title'] ))
		{
		$widget_title = esc_attr( $instance['widget_title'] );
		}
		if(isset( $instance['fb_url'] ))
		{
        $fb_url = esc_attr($instance['fb_url'] );
		}
		if(isset( $instance['twit_url'] ))
		{
        $twit_url = esc_attr( $instance['twit_url'] );
		}
		if(isset( $instance['dribble_url'] ))
		{
        $dribble_url = esc_attr( $instance['dribble_url'] );
		}
		if(isset( $instance['yt_url'] ))
		{
        $yt_url = esc_attr( $instance['yt_url'] );
		}
		if(isset( $instance['gplus_url'] ))
		{
        $gplus_url = esc_attr( $instance['gplus_url'] );
		}
		if(isset( $instance['tumblr_url'] ))
		{
        $tumblr_url = esc_attr( $instance['tumblr_url'] );
		}
		if(isset( $instance['pinterest_url'] ))
		{
        $pinterest_url = esc_attr( $instance['pinterest_url'] );
		}
		if(isset( $instance['flickr_url'] ))
		{
        $flickr_url = esc_attr( $instance['flickr_url'] );
		}
		if(isset( $instance['instagram_url'] ))
		{
        $instagram_url = esc_attr( $instance['instagram_url'] );
		}
		if(isset( $instance['rss_url'] ))
		{
        $rss_url = esc_attr( $instance['rss_url'] );
		}
        ?>
		
		<p>
            <label for="<?php echo $this->get_field_id( 'widget_title' ); ?>"><?php _e('Widget Title:', 'fitness') ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'widget_title' ); ?>" name="<?php echo $this->get_field_name( 'widget_title' ); ?>" type="text" value="<?php echo $widget_title; ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'fb_url' ); ?>"><?php _e('Facebook URL:', 'krwn') ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'fb_url' ); ?>" name="<?php echo $this->get_field_name( 'fb_url' ); ?>" type="text" value="<?php echo $fb_url; ?>" />
        </p>
		<p>
            <label for="<?php echo $this->get_field_id( 'twit_url' ); ?>"><?php _e('Twitter URL:', 'krwn') ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'twit_url' ); ?>" name="<?php echo $this->get_field_name( 'twit_url' ); ?>" type="text" value="<?php echo $twit_url; ?>" />
        </p>
		<p>
            <label for="<?php echo $this->get_field_id( 'yt_url' ); ?>"><?php _e('Youtube URL:', 'krwn') ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'yt_url' ); ?>" name="<?php echo $this->get_field_name( 'yt_url' ); ?>" type="text" value="<?php echo $yt_url; ?>" />
        </p>
		<p>
            <label for="<?php echo $this->get_field_id( 'gplus_url' ); ?>"><?php _e('Google+ URL:', 'krwn') ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'gplus_url' ); ?>" name="<?php echo $this->get_field_name( 'gplus_url' ); ?>" type="text" value="<?php echo $gplus_url; ?>" />
        </p>
		<p>
            <label for="<?php echo $this->get_field_id( 'dribble_url' ); ?>"><?php _e('Dribble URL:', 'krwn') ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'dribble_url' ); ?>" name="<?php echo $this->get_field_name( 'dribble_url' ); ?>" type="text" value="<?php echo $dribble_url; ?>" />
        </p>
		<p>
            <label for="<?php echo $this->get_field_id( 'tumblr_url' ); ?>"><?php _e('Thumblr URL:', 'krwn') ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'tumblr_url' ); ?>" name="<?php echo $this->get_field_name( 'tumblr_url' ); ?>" type="text" value="<?php echo $tumblr_url; ?>" />
        </p>
		<p>
            <label for="<?php echo $this->get_field_id( 'pinterest_url' ); ?>"><?php _e('Pinterest URL:', 'krwn') ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'pinterest_url' ); ?>" name="<?php echo $this->get_field_name( 'pinterest_url' ); ?>" type="text" value="<?php echo $pinterest_url; ?>" />
        </p>
		<p>
            <label for="<?php echo $this->get_field_id( 'flickr_url' ); ?>"><?php _e('Flickr URL:', 'krwn') ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'flickr_url' ); ?>" name="<?php echo $this->get_field_name( 'flickr_url' ); ?>" type="text" value="<?php echo $flickr_url; ?>" />
        </p>
		<p>
            <label for="<?php echo $this->get_field_id( 'instagram_url' ); ?>"><?php _e('Instagram URL:', 'krwn') ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'instagram_url' ); ?>" name="<?php echo $this->get_field_name( 'instagram_url' ); ?>" type="text" value="<?php echo $instagram_url; ?>" />
        </p>
		<p>
            <label for="<?php echo $this->get_field_id( 'rss_url' ); ?>"><?php _e('RSS URL:', 'krwn') ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'rss_url' ); ?>" name="<?php echo $this->get_field_name( 'rss_url' ); ?>" type="text" value="<?php echo $rss_url; ?>" />
        </p>
		
		<?php
    }

function widget( $args, $instance ) {
echo $args['before_widget'];
?>
		<h3><?php echo $instance['widget_title']; ?></h3>
		<div class="social">
			<ul>
				<?php
				if (isset($instance['fb_url']) && $instance['fb_url'] != '' ) { 
				?>
				<li>
					<a href="<?php echo $instance['fb_url']; ?>" title="Facebook">
						<i class="fa fa-facebook"></i>
					</a>
				</li>
				<?php 
				} 
				else 
				{ 
				?>
				<!-- Nothing -->
				<?php
				} 
				?>
				<?php
				if (isset($instance['twit_url']) && $instance['twit_url'] != '' ) { 
				?>
				<li>
					<a href="<?php echo $instance['twit_url']; ?>" title="Twitter">
						<i class="fa fa-twitter"></i>
					</a>
				</li>
				<?php 
				} 
				else 
				{ 
				?>
				<!-- Nothing -->
				<?php
				} 
				?>
				<?php
				if (isset($instance['yt_url']) && $instance['yt_url'] != '' ) { 
				?>
				<li>
					<a href="<?php echo $instance['yt_url']; ?>" title="Youtube">
						<i class="fa fa-youtube"></i>
					</a>
				</li>
				<?php 
				} 
				else 
				{ 
				?>
				<!-- Nothing -->
				<?php
				} 
				?>
				<?php
				if (isset($instance['gplus_url']) && $instance['gplus_url'] != '' ) { 
				?>
				<li>
					<a href="<?php echo $instance['gplus_url']; ?>" title="RSS">
						<i class="fa fa-google-plus"></i>
					</a>
				</li>
				<?php 
				} 
				else 
				{ 
				?>
				<!-- Nothing -->
				<?php
				} 
				?>
				<?php
				if (isset($instance['dribble_url']) && $instance['dribble_url'] != '' ) { 
				?>
				<li>
					<a href="<?php echo $instance['dribble_url']; ?>" class="tip vimeo" title="Vimeo">
						<i class="fa fa-dribbble"></i>
					</a>
				</li>
				<?php 
				} 
				else 
				{ 
				?>
				<!-- Nothing -->
				<?php
				} 
				?>
				<?php
				if (isset($instance['tumblr_url']) && $instance['tumblr_url'] != '' ) { 
				?>
				<li>
					<a href="<?php echo $instance['tumblr_url']; ?>"> 
						<i class="fa fa-tumblr"></i>  
					</a>
				</li>
				<?php 
				} 
				else 
				{ 
				?>
				<!-- Nothing -->
				<?php
				} 
				?>
				<?php
				if (isset($instance['pinterest_url']) && $instance['pinterest_url'] != '' ) { 
				?>
				<li>
					<a href="<?php echo $instance['pinterest_url']; ?>"> 
						<i class="fa fa-pinterest"></i>  
					</a>
				</li>
				<?php 
				} 
				else 
				{ 
				?>
				<!-- Nothing -->
				<?php
				} 
				?>
				<?php
				if (isset($instance['flickr_url']) && $instance['flickr_url'] != '' ) { 
				?>
				<li>
					<a href="<?php echo $instance['flickr_url']; ?>"> 
						<i class="fa fa-flickr"></i>  
					</a>
				</li>
				<?php 
				} 
				else 
				{ 
				?>
				<!-- Nothing -->
				<?php
				} 
				?>
				<?php
				if (isset($instance['instagram_url']) && $instance['instagram_url'] != '' ) { 
				?>
				<li>
					<a href="<?php echo $instance['instagram_url']; ?>"> 
						<i class="fa fa-instagram"></i>  
					</a>
				</li>
				<?php 
				} 
				else 
				{ 
				?>
				<!-- Nothing -->
				<?php
				} 
				?>
				<?php
				if (isset($instance['rss_url']) && $instance['rss_url'] != '' ) { 
				?>
				<li>
					<a href="<?php echo $instance['rss_url']; ?>"> 
						<i class="fa fa-rss"></i>  
					</a>
				</li>
				<?php 
				} 
				else 
				{ 
				?>
				<!-- Nothing -->
				<?php
				} 
				?>
			</ul>
		</div>

<?php
        echo $args['after_widget'];
    }
};

add_action( 'widgets_init', create_function( '', 'return register_widget( "social_widget" );' ) );
?>