bainternet
3/23/2012 - 10:32 PM

simple view counter for wordpress plugin

simple view counter for wordpress plugin

<?php
/*
Plugin Name: Simple Post Views
Plugin URI: http://en.bainternet.info
Description: Simple And lightweight plugin to track post views.
Version: 0.2
Author: Bainternet
Author URI: http://en.bainternet.info
*/
if ( !class_exists('PostViews')){
	class PostViews{
		
		static $meta_key = "_post_views_count"
		
		//class constractor
		public function __construct(){
			//auto count view on the_content function.
			add_filter('the_content',array($this,'setPostViews'));
			//add shortcodes
			add_shortcode("TOP_VIEWED",array($this,'top_viewed'));
			add_shortcode("GET_VIEWS"array($this,'top_viewed'));
			//add colums to list view
			add_filter('manage_posts_columns', array($this,'posts_columns_id'), 5);
			add_action('manage_posts_custom_column', array($this,'posts_custom_id_columns'), 5, 2);

			//fixed ajax counts for cached pages
			add_action('wp_ajax_simple_post_views',array($this, 'increment_views')); 
			add_action('wp_ajax_nopriv_simple_post_views',array($this, 'increment_views')); 
		}
		
		//add views column to posts listing
		public function posts_columns_id($defaults){
			$defaults['post_views'] = __('Views');
			return $defaults; 
		}
		
		//render views column
		public function posts_custom_id_columns($column_name, $id){
			if($column_name === 'post_views'){
				echo $this->getPostViews(get_the_ID());
			}
		}
		
		//function to get the views
		public function getPostViews($postID = null){
			if (null == $postID){
				global $post;
				$postID = $post->ID;
			}
			
			$count = get_post_meta($postID, $this->meta_key, true);
			if($count==''){
				delete_post_meta($postID, $this->meta_key);
				add_post_meta($postID, $this->meta_key, '0');
				$re = 0; 
			}else{
				$re = $count;
			}
			return apply_filters("Post_Views_get_views",$re,$postID);
		}
		
		//function to set the view count
		public function setPostViews($content, $postID = null) {
			if (null == $postID){
				global $post;
				$postID = $post->ID;
			}
			if(defined('WP_CACHE') && WP_CACHE) { 
				wp_print_scripts('jquery');                                     
                $content .= '<script type="text/javascript">
                			/* <![CDATA[ */
                			jQuery.ajax({type:"GET",url: "'.admin_url('admin-ajax.php', (is_ssl() ? 'https' : 'http')).'", data: "post_views_id="'.$postID.'"&action=simple_post_views",cache:false});
                			/* ]]> */
                			</script>
                			<!-- End Of Script Generated By Simple Post Views -->';
			}else{
				$count = get_post_meta($postID, $this->meta_key, true);
				if($count==''){
					$count = 1;
					delete_post_meta($postID, $this->meta_key);
					add_post_meta($postID, $this->meta_key, '1');
				}else{
					$count++ ;
					update_post_meta($postID, $this->meta_key, $count); 
				}
			}
			return $content;
		}
		
		//function to get views by shortcode.
		public function top_viewed($atts, $content = null) {
			extract(shortcode_atts(array(
			"id" => null
			), $atts));
			return $this->getPostViews($id);
		}
		
		/**
		 * increment_views  ajax callback for cache enabled sites.
		 * @return void
		 */
		public function increment_views(){
			if (isset($_GET['post_views_id'])){
				$count = get_post_meta($postID, $this->meta_key, true);
				if($count==''){
					$count = 1;
					delete_post_meta($postID, $this->meta_key);
					add_post_meta($postID, $this->meta_key, '1');
				}else{
					$count++ ;
					update_post_meta($postID, $this->meta_key, $count); 
				}
				echo $count;
				die();
			}
			die();
		}


		/**
		 * shortcode handler for top viewed
		 * usage: [TOP_VIEWED count="5"]
		 * this will list top five viewed posts
		 */
		public function top_viewed($atts, $content = null) {
			extract(shortcode_atts(array(
			"count" => '5'
			), $atts));

			global $wp_query,$paged,$post;
			$temp = $wp_query;
			$wp_query= null;
			$wp_query = new WP_Query(array(
				'posts_per_page' => $count,
				'order' => 'DESC',
				'orderby' => 'meta_value_num',
				'meta_key' => $this->meta_key
			));
			ob_start();
			?>
				<ul class="loop">
				<?php have_posts()) : $wp_query->the_post(); ?>
				<li><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
				<?php endwhile; ?>
				</ul>
			<?php
			$wp_query = null;
			$wp_query = $temp;
			wp_reset_query();
			$content = ob_get_contents();
			ob_end_clean();

			return $content;
		}
		
	}//end class
}
$Vcounter = new PostViews();