<?php
/*	
	
	WIDGET FILTERS:

    widget_title				- widget title
	wfeatured_args	 			- arguments for get_posts for featured
	wfeatured_post_title		- featured post title
	
*/

// WIDGET CLASS
class Bizz_Widget_Featured extends WP_Widget {

	/**
	 * Set up the widget's unique name, ID, class, description, and other options.
	 * @since 0.6
	 */
	function Bizz_Widget_Featured() {
		$widget_ops = array( 'classname' => 'featured_widget', 'description' => __( 'Displaying featured posts content.', 'bizzthemes' ) );
		$control_ops = array( 'width' => 550, 'height' => 350, 'id_base' => "widgets-reloaded-bizz-featured" );
		$this->WP_Widget( "widgets-reloaded-bizz-featured", __( 'Featured', 'bizzthemes' ), $widget_ops, $control_ops );
	}

	/**
	 * Outputs the widget based on the arguments input through the widget controls.
	 * @since 0.6
	 */
	function widget( $args, $instance ) {
		extract( $args );

		$args = array();
						
		$args['post_type'] = $instance['post_type'];
		$args['include'] = isset( $instance['include'] ) ? $instance['include'] : array();
		$args['include'] = ( !is_array($args['include']) && !empty($args['include']) ) ? explode(",", $args['include']) : $args['include'];
		$args['exclude'] = isset( $instance['exclude'] ) ? $instance['exclude'] : array();
		$args['exclude'] = ( !is_array($args['exclude']) && !empty($args['exclude']) ) ? explode(",", $args['exclude']) : $args['exclude'];
		$args['order'] = $instance['order'];
		$args['orderby'] = $instance['orderby'];
		$args['number'] = intval( $instance['number'] );
				
		$args['display_title'] = isset( $instance['display_title'] ) ? $instance['display_title'] : false;
		
		$widget_id = preg_replace("/[^0-9\.]/", '', $widget_id);
		
		echo $before_widget;
						
		// get included slides		
		$count = 0;
		$slide_args = array(
		    'post_type'     => $args['post_type'],
			'include'       => $args['include'],
			'exclude'       => $args['exclude'],
			'order'         => $args['order'],
			'orderby'       => $args['orderby'],
			'numberposts'   => $args['number']
		); 
		$feat_arr = get_posts( apply_filters( 'wfeatured_args',  $slide_args ) );

		global $post;
		foreach ($feat_arr as $post) {
			setup_postdata($post);
			$count++;
			if ( $post->post_type != 'revision' && $post->post_type != 'nav_menu_item' && $post->post_type != 'bizz_widget' && $post->post_type != 'bizz_grid' ){
?>
				<div id="feat-<?php echo $widget_id .'-'. $count; ?>" class="featured_post">
					<?php 
					if ($instance['display_title'])
						echo apply_filters( 'wfeatured_post_title',  '<h3 class="stitle">'. get_the_title(). '</h3>' ); 
					?>
					<div class="format_text">
					<div class="fix"></div>
					<?php the_content(); ?>
					<div class="fix"></div>
					</div><!-- /.format_text -->
				</div><!-- /.slide -->
<?php										
			}
		}

		echo $after_widget;
	}

	/**
	 * Updates the widget control options for the particular instance of the widget.
	 * @since 0.6
	 */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance = $new_instance;
				
		$instance['post_type'] = $new_instance['post_type'];
		if ( $instance['post_type'] !== $old_instance['post_type'] ) {
			$instance['include'] = array();
			$instance['exclude'] = array();
		}
		$instance['orderby'] = $new_instance['orderby'];
		$instance['order'] = $new_instance['order'];
		$instance['number'] = strip_tags( $new_instance['number'] );

		$instance['display_title'] = ( isset( $new_instance['display_title'] ) ? 1 : 0 );

		return $instance;
	}

	/**
	 * Displays the widget control options in the Widgets admin screen.
	 * @since 0.6
	 */
	function form( $instance ) {

		//Defaults
		$defaults = array(
			'post_type' => 'bizz_featured',
			'include' => '',
			'exclude' => '',
			'order' => 'DESC',
			'orderby' => 'date',
			'number' => 5,
			'display_title' => false
		);
		$instance = wp_parse_args( (array) $instance, $defaults );

		$post_types = get_post_types(array(),'objects');
		$count_posts = wp_count_posts($instance['post_type']);
		if ( $count_posts->publish <= 500 ) {
			$posts = get_posts( array(
				'post_type' => $instance['post_type'], 
				'post_status' => 'publish', 
				'post_mime_type' => '', 
				'orderby' => 'menu_order', 
				'order' => 'ASC', 
				'numberposts' => 500			
			) );
		}
		$order = array( 'ASC' => __( 'Ascending', 'bizzthemes' ), 'DESC' => __( 'Descending', 'bizzthemes' ) );
		$orderby = array(
		    'author' => __( 'Author', 'bizzthemes' ), 
			'category' => __( 'Category', 'bizzthemes' ),
			'content' => __( 'Content', 'bizzthemes' ),
			'date' => __( 'Date', 'bizzthemes' ),
			'ID' => __( 'ID', 'bizzthemes' ),
			'menu_order' => __( 'Menu order', 'bizzthemes' ),
			'mime_type' => __( 'Mime type (attachments)', 'bizzthemes' ),
			'modified' => __( 'Modified date', 'bizzthemes' ),
			'name' => __( 'Name', 'bizzthemes' ),
			'parent' => __( 'Parent ID', 'bizzthemes' ),
			'rand' => __( 'Randomly', 'bizzthemes' ),
			'status' => __( 'Status', 'bizzthemes' ),
			'title' => __( 'Title', 'bizzthemes' ),
			'category' => __( 'Category', 'bizzthemes' ),
		);
?>
		<div class="bizz-widget-controls columns-2">
		<small class="section">Select Posts</small>
		<p>
			<label for="<?php echo $this->get_field_id( 'post_type' ); ?>">Post Type</label> 
			<select class="widefat" id="<?php echo $this->get_field_id( 'post_type' ); ?>" name="<?php echo $this->get_field_name( 'post_type' ); ?>">
				<option value="any" <?php selected( $instance['post_type'], 'any' ); ?>>All post types</option>
<?php 
				foreach ( $post_types as $post_type ) {
					$pt_labels = $post_type->labels;
				    if ( $post_type->name != 'revision' && $post_type->name != 'nav_menu_item' && $post_type->name != 'bizz_widget' && $post_type->name != 'bizz_grid' ){
?>
					<option value="<?php echo $post_type->name; ?>" <?php selected( $instance['post_type'], $post_type->name ); ?>><?php echo $pt_labels->name; ?></option>
<?php
				    }
				}
?>
			</select>
		</p>
<?php
		if ( $count_posts->publish <= 500 ) {
?>
		<p>
			<label for="<?php echo $this->get_field_id( 'include' ); ?>">Include posts</label> 
			<select class="widefat" id="<?php echo $this->get_field_id( 'include' ); ?>" name="<?php echo $this->get_field_name( 'include' ); ?>[]" size="4" multiple="multiple">
<?php 
				foreach ( $posts as $post ) {
				    if ( $post->post_type != 'revision' && $post->post_type != 'nav_menu_item' && $post->post_type != 'bizz_widget' && $post->post_type != 'bizz_grid' ){
					$instance['include'] = ( !is_array($instance['include']) ) ? explode(",", $instance['include']) : $instance['include'];
?>
					<option value="<?php echo $post->ID; ?>" <?php echo ( in_array( $post->ID, (array) $instance['include'] ) ? 'selected="selected"' : '' ); ?>><?php echo esc_attr( $post->post_title ); ?></option>
<?php 
				    }
				} 
?>
			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'exclude' ); ?>">Exclude posts</label> 
			<select class="widefat" id="<?php echo $this->get_field_id( 'exclude' ); ?>" name="<?php echo $this->get_field_name( 'exclude' ); ?>[]" size="4" multiple="multiple">
<?php 
				foreach ( $posts as $post ) {
				    if ( $post->post_type != 'revision' && $post->post_type != 'nav_menu_item' && $post->post_type != 'bizz_widget' && $post->post_type != 'bizz_grid' ){
					$instance['exclude'] = ( !is_array($instance['exclude']) ) ? explode(",", $instance['exclude']) : $instance['exclude'];
?>
					<option value="<?php echo $post->ID; ?>" <?php echo ( in_array( $post->ID, (array) $instance['exclude'] ) ? 'selected="selected"' : '' ); ?>><?php echo esc_attr( $post->post_title ); ?></option>
<?php 
				    }
				} 
?>
			</select>
		</p>
<?php
		} else {
?>
		<p>
			<label for="<?php echo $this->get_field_id( 'include' ); ?>">Include posts (IDs, separated by comma)</label>
			<input type="text" class="widefat code" id="<?php echo $this->get_field_id( 'include' ); ?>" name="<?php echo $this->get_field_name( 'include' ); ?>" value="<?php echo ( is_array($instance['include']) ) ? implode(",", $instance['include']) : $instance['include']; ?>" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'exclude' ); ?>">Exclude posts (IDs, separated by comma)</label>
			<input type="text" class="widefat code" id="<?php echo $this->get_field_id( 'exclude' ); ?>" name="<?php echo $this->get_field_name( 'exclude' ); ?>" value="<?php echo ( is_array($instance['exclude']) ) ? implode(",", $instance['exclude']) : $instance['exclude']; ?>" />
		</p>
<?php
		}
?>
		</div>

		<div class="bizz-widget-controls columns-2 column-last">
		<small class="section">Order Posts</small>
		<p>
			<label for="<?php echo $this->get_field_id( 'orderby' ); ?>">Order by</label> 
			<select class="widefat" id="<?php echo $this->get_field_id( 'orderby' ); ?>" name="<?php echo $this->get_field_name( 'orderby' ); ?>">
				<?php foreach ( $orderby as $option_value => $option_label ) { ?>
					<option value="<?php echo $option_value; ?>" <?php selected( $instance['orderby'], $option_value ); ?>><?php echo $option_label; ?></option>
				<?php } ?>
			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'order' ); ?>">Order</label> 
			<select class="widefat" id="<?php echo $this->get_field_id( 'order' ); ?>" name="<?php echo $this->get_field_name( 'order' ); ?>">
				<?php foreach ( $order as $option_value => $option_label ) { ?>
					<option value="<?php echo $option_value; ?>" <?php selected( $instance['order'], $option_value ); ?>><?php echo $option_label; ?></option>
				<?php } ?>
			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'number' ); ?>">Limit (-1 removes the limit)</label>
			<input type="text" class="widefat code" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>" />
		</p>
		<small class="section">Display Settings</small>
		<p>
			<label for="<?php echo $this->get_field_id( 'display_title' ); ?>">
			<input class="checkbox" type="checkbox" <?php checked( $instance['display_title'], true ); ?> id="<?php echo $this->get_field_id( 'display_title' ); ?>" name="<?php echo $this->get_field_name( 'display_title' ); ?>" /> <?php _e( 'Display post title?', 'bizzthemes'); ?></label>
		</p>
		</div>
		<div style="clear:both;">&nbsp;</div>
	<?php
	}
}

// INITIATE WIDGET
register_widget( 'Bizz_Widget_Featured' );