<?php

/**
 * List Unread Posts Widget
 */
class mar_unread_posts_widget extends WP_Widget {


    /** constructor */
    function mar_unread_posts_widget() {
        parent::WP_Widget(false, $name = 'User\'s Unread Posts', array('description' => 'Show unread posts for the current user'));	
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {	
        extract( $args );
		
		if(is_user_logged_in()) {
			
			$title  = apply_filters('widget_title', $instance['title']);
			$number  = (int)$instance['number'];
			if($number == '' || !isset($number)) { $number = 5; }
			
			echo $before_widget;
			if ( $title ) {
				echo $before_title . $title . $after_title;
			}
			echo mar_list_unread_posts($number);
			echo $after_widget;
		}
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {		
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['number'] = strip_tags($new_instance['number']);
        return $instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {	
	
        $title = esc_attr($instance['title']);
        $number = esc_attr($instance['number']);
       
        ?>
        <p>
          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>
		<p>
          <input class="widefat" style="width: 30px;" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
          <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number to show:'); ?></label> 
        </p>
        <?php 
    }

} // class mar_unread_posts_widget


/**
 * user Report Widget
 */
class mar_user_report_widget extends WP_Widget {


    /** constructor */
    function mar_user_report_widget() {
        parent::WP_Widget(false, $name = 'User Report', array('description' => 'Show the number of posts the user has read, not read, etc'));	
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {	
        extract( $args );
	
		if(is_user_logged_in()) {
		
			$title  = apply_filters('widget_title', $instance['title']);
			$read_posts  = (bool)$instance['read_posts'];
			$read_label  = $instance['read_label'];
			$unread_posts  = (bool)$instance['unread_posts'];
			$unread_label  = $instance['unread_label'];
			$total_posts  = (bool)$instance['total_posts'];
			$total_label  = $instance['total_label'];
			$percentage  = (bool)$instance['percentage'];
			$percentage_label  = $instance['percentage_label'];
			
			$labels = array(
				'read' => $read_label,
				'unread' => $unread_label,
				'total' => $total_label,
				'percentage' => $percentage_label
			);
			
			echo $before_widget;
			if ( $title ) {
				echo $before_title . $title . $after_title;
			}
			echo mar_list_user_report(
				array(
					'read' => $read_posts,
					'unread' => $unread_posts,
					'total' => $total_posts,
					'percentage' => $percentage,
					'labels' => array(
						'read' => $read_label,
						'unread' => $unread_label,
						'total' => $total_label,
						'percentage' => $percentage_label
					)
				)
			);
			echo $after_widget;
		}
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {		
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['read_posts'] = strip_tags($new_instance['read_posts']);
		$instance['read_label'] = strip_tags($new_instance['read_label']);
		$instance['unread_posts'] = strip_tags($new_instance['unread_posts']);
		$instance['unread_label'] = strip_tags($new_instance['unread_label']);
		$instance['total_posts'] = strip_tags($new_instance['total_posts']);
		$instance['total_label'] = strip_tags($new_instance['total_label']);
		$instance['percentage'] = strip_tags($new_instance['percentage']);
		$instance['percentage_label'] = strip_tags($new_instance['percentage_label']);
        return $instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {	
	
        $title = esc_attr($instance['title']);      
        $read_posts = esc_attr($instance['read_posts']);      
        $read_label = esc_attr($instance['read_label']);      
        $unread_posts = esc_attr($instance['unread_posts']);      
        $unread_label = esc_attr($instance['unread_label']);      
        $total_posts = esc_attr($instance['total_posts']);      
        $total_label = esc_attr($instance['total_label']);      
        $percentage = esc_attr($instance['percentage']);      
        $percentage_label = esc_attr($instance['percentage_label']);      
        ?>
        <p>
          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>
		<p>
          <input id="<?php echo $this->get_field_id('read_posts'); ?>" name="<?php echo $this->get_field_name('read_posts'); ?>" type="checkbox" value="1" <?php checked( '1', $read_posts ); ?>/>
          <label for="<?php echo $this->get_field_id('read_posts'); ?>"><?php _e('Display Number of Posts Read?'); ?></label> 
        </p>
		<p>
          <label for="<?php echo $this->get_field_id('read_label'); ?>"><?php _e('Read Posts Label:'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('read_label'); ?>" name="<?php echo $this->get_field_name('read_label'); ?>" type="text" value="<?php echo $read_label; ?>" />
        </p>
		<p>
          <input id="<?php echo $this->get_field_id('unread_posts'); ?>" name="<?php echo $this->get_field_name('unread_posts'); ?>" type="checkbox" value="1" <?php checked( '1', $unread_posts ); ?>/>
          <label for="<?php echo $this->get_field_id('unread_posts'); ?>"><?php _e('Display Number of Unread Posts?'); ?></label> 
        </p>
		<p>
          <label for="<?php echo $this->get_field_id('unread_label'); ?>"><?php _e('Unread Posts Label:'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('unread_label'); ?>" name="<?php echo $this->get_field_name('unread_label'); ?>" type="text" value="<?php echo $unread_label; ?>" />
        </p>
		<p>
          <input id="<?php echo $this->get_field_id('total_posts'); ?>" name="<?php echo $this->get_field_name('total_posts'); ?>" type="checkbox" value="1" <?php checked( '1', $total_posts ); ?>/>
          <label for="<?php echo $this->get_field_id('total_posts'); ?>"><?php _e('Display Total Number of Posts?'); ?></label> 
        </p>
		<p>
          <label for="<?php echo $this->get_field_id('total_label'); ?>"><?php _e('Total Posts Label:'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('total_label'); ?>" name="<?php echo $this->get_field_name('total_label'); ?>" type="text" value="<?php echo $total_label; ?>" />
        </p>
		<p>
          <input id="<?php echo $this->get_field_id('percentage'); ?>" name="<?php echo $this->get_field_name('percentage'); ?>" type="checkbox" value="1" <?php checked( '1', $percentage ); ?>/>
          <label for="<?php echo $this->get_field_id('percentage'); ?>"><?php _e('Display Percentage of Posts Read?'); ?></label> 
        </p>
		<p>
          <label for="<?php echo $this->get_field_id('percentage_label'); ?>"><?php _e('Percentage Complete Label:'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('percentage_label'); ?>" name="<?php echo $this->get_field_name('percentage_label'); ?>" type="text" value="<?php echo $percentage_label; ?>" />
        </p>
        <?php 
    }

} // class mar_user_report_widget


/**
 * Most Popular Posts Widget
 */
class mar_most_popular extends WP_Widget {


    /** constructor */
    function mar_most_popular() {
        parent::WP_Widget(false, $name = 'Most Read', array('description' => 'Show your most read posts'));	
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {	
        extract( $args );
		
		$title  = apply_filters('widget_title', $instance['title']);
		$number = (int)$instance['number'];
		$read_count = (bool)$instance['read_count'];
		if($number == '' || !isset($number)) { $number = 5; }
		echo $before_widget;
		if ( $title ) {
			echo $before_title . $title . $after_title;
		}
			echo mar_most_read($number, $read_count);
		echo $after_widget;
		
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {		
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['number'] = strip_tags($new_instance['number']);
		$instance['read_count'] = strip_tags($new_instance['read_count']);
        return $instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {	
	
        $title = esc_attr($instance['title']);
        $number = esc_attr($instance['number']);
        $read_count = esc_attr($instance['read_count']);

        ?>
        <p>
          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>
		<p>
          <input class="widefat" style="width: 30px;" id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
          <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number to show:'); ?></label> 
        </p>
		<p>
          <input id="<?php echo $this->get_field_id('read_count'); ?>" name="<?php echo $this->get_field_name('read_count'); ?>" type="checkbox" value="1" <?php checked( '1', $read_count ); ?>/>
          <label for="<?php echo $this->get_field_id('read_count'); ?>"><?php _e('Display Read Count?'); ?></label> 
        </p>
        <?php 
    }


} // class mar_most_popular

/**
 * Mark Post as Read / Unread Widget
 */
class mar_mark_as_read_links extends WP_Widget {


    /** constructor */
    function mar_mark_as_read_links() {
        parent::WP_Widget(false, $name = 'Mark as Read/Unread Links', array('description' => 'Show link to mark current post as read or unread'));	
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {	
        extract( $args );		
		$title  = apply_filters('widget_title', $instance['title']);
		$read  = apply_filters('widget_title', $instance['read']);
		$unread  = apply_filters('widget_title', $instance['unread']);

		if(is_single() && is_user_logged_in()) {
			
			echo $before_widget;
			if ( $title ) {
				echo $before_title . $title . $after_title;
			}
				echo mar_read_unread_links($read, $unread);
			echo $after_widget;
		}
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {		
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['read'] = strip_tags($new_instance['read']);
		$instance['unread'] = strip_tags($new_instance['unread']);
        return $instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {	
	
        $title = esc_attr($instance['title']);
        $read = esc_attr($instance['read']);
        $unread = esc_attr($instance['unread']);

        ?>
        <p>
          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>
		<p>
          <label for="<?php echo $this->get_field_id('read'); ?>"><?php _e('Mark as Read Text:'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('read'); ?>" name="<?php echo $this->get_field_name('read'); ?>" type="text" value="<?php echo $read; ?>" />
        </p>
		<p>
          <label for="<?php echo $this->get_field_id('unread'); ?>"><?php _e('Mark as Unread Text:'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('unread'); ?>" name="<?php echo $this->get_field_name('unread'); ?>" type="text" value="<?php echo $unread; ?>" />
        </p>
        <?php 
    }


} // class mar_mark_as_read_links

function mar_register_widgets() {
	register_widget('mar_unread_posts_widget');
	register_widget('mar_user_report_widget');
	register_widget('mar_most_popular');
	register_widget('mar_mark_as_read_links');
}
add_action('widgets_init', 'mar_register_widgets');