'. 'if(jQuery)'. '{'. 'jQuery.ajax({'. 'type : "GET",'. 'url : "'.admin_url( 'admin-ajax.php' ).'",'. 'data : '. '{ '. 'action : "ajax-hits-counter-increment",'. 'post_id : '.$post->ID. '}'. '});'. '}'. ''; } function ajax_hits_counter_increment() { if( !isset($_GET['post_id']) || empty($_GET['post_id']) ) { die( '0' ); } $post_id = intval( filter_var( $_GET['post_id'], FILTER_SANITIZE_NUMBER_INT ) ); if( empty($post_id) ) { die( '0' ); } $current_hits = get_post_meta( $post_id, 'hits', true ); if( empty($current_hits) ) { $current_hits = 0; } $current_hits++; update_post_meta( $post_id, 'hits', $current_hits ); die( strval( $current_hits ) ); } /////////////////////////////////////////////////////////////////////////////// function ajax_hits_counter_posts_table_column( $column ) { $column['hits'] = 'Hits'; return $column; } function ajax_hits_counter_posts_table_row( $column_name, $post_id ) { if( $column_name=='hits' ) { $current_hits = get_post_meta( $post_id, 'hits', true ); if( empty($current_hits) ) { $current_hits = 0; } echo( $current_hits ); } } function ajax_hits_counter_posts_table_sortable( $column ) { $column['hits'] = 'hits'; return $column; } function ajax_hits_counter_posts_table_orderby( $vars ) { if( isset($vars['orderby']) && $vars['orderby']=='hits' ) { $vars = array_merge( $vars, array( 'meta_key' => 'hits', 'orderby' => 'meta_value_num' ) ); } return $vars; } /////////////////////////////////////////////////////////////////////////////// add_filter('the_content', 'ajax_hits_counter_init', 100); /////////////////////////////////////////////////////////////////////////////// add_action( 'wp_ajax_nopriv_ajax-hits-counter-increment', 'ajax_hits_counter_increment' ); add_action( 'wp_ajax_ajax-hits-counter-increment', 'ajax_hits_counter_increment' ); /////////////////////////////////////////////////////////////////////////////// add_filter( 'manage_posts_columns', 'ajax_hits_counter_posts_table_column' ); add_filter( 'manage_posts_custom_column', 'ajax_hits_counter_posts_table_row', 10, 2 ); add_filter( 'manage_edit-post_sortable_columns', 'ajax_hits_counter_posts_table_sortable' ); add_filter( 'request', 'ajax_hits_counter_posts_table_orderby' ); /////////////////////////////////////////////////////////////////////////////// /** * Adds ajax_hits_counter_popular_posts_widget widget. */ class AJAX_Hits_Counter_Popular_Posts_Widget extends WP_Widget { /////////////////////////////////////////////////////////////////////////// protected $defaults = array( 'widget_id' => 'ajax_hits_counter_popular_posts_widget', 'count' => 5, 'cache_lifetime' => 86400, ); /////////////////////////////////////////////////////////////////////////// /** * Register widget with WordPress. */ public function __construct() { parent::__construct( $this->defaults['widget_id'], 'AJAX Hits Counter: Popular Posts', array( 'description' => 'Displays popular posts counted by AJAX Hits Counter.', 'classname' => $this->defaults['widget_id'], ) ); } /////////////////////////////////////////////////////////////////////////// /** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) { extract( $args ); $popular_posts = $this->getPopularPosts( array_merge( $instance, array( 'widget_id' => $args['widget_id'] ) ) ); if( empty($popular_posts) ) { return false; } $title = apply_filters( 'widget_title', $instance['title'] ); $output = $before_widget; if ( !empty( $title ) ) { $output .= $before_title.$title.$after_title; } $output .= $this->getHTML( $popular_posts ); $output .= $after_widget; echo( $output ); } /////////////////////////////////////////////////////////////////////////// /** * Returns Popular Posts * * @param array $args * @return array */ public function getPopularPosts( $args = array() ) { global $wpdb; // args $args = array_merge( $this->defaults, $args ); // cache key $cache_key = 'ajax_hits_counter_popular_posts_widget_'.md5( $args['widget_id'] ); // try to get cached data from transient cache $cache = get_transient( $cache_key ); if( !empty($cache) ) { // cache exists, return cached data return $cache; } $q = ' SELECT p.ID, p.post_title, m.meta_value as hits FROM '.$wpdb->postmeta.' m, '.$wpdb->posts.' p WHERE m.post_id = p.ID AND m.meta_key = \'hits\' AND p.post_status = \'publish\' AND p.post_date_gmt < \''.date( 'Y-m-d H:i:s' ).'\' AND p.post_type = \'post\' ORDER BY ( m.meta_value + 0 ) DESC, p.post_date_gmt DESC LIMIT '.$args['count']; $popular_posts = $wpdb->get_results($q); // store result to cache set_transient( $cache_key, $popular_posts, $args['cache_lifetime'] ); return $popular_posts; } /////////////////////////////////////////////////////////////////////////// /** * Returns HTML of Popular Posts * * @param array $popular_posts * @return string */ public function getHTML( $popular_posts = array() ) { if( empty($popular_posts) ) { return false; } $html = ''; return $html; } /////////////////////////////////////////////////////////////////////////// /** * Sanitize widget form values as they are saved. * * @see WP_Widget::update() * * @param array $new_instance Values just sent to be saved. * @param array $old_instance Previously saved values from database. * * @return array Updated safe values to be saved. */ public function update( $new_instance, $old_instance ) { return array( 'title' => trim( strip_tags( $new_instance['title'] ) ), 'count' => intval( preg_replace( '#[^0-9]#', '', $new_instance['count'] ) ), 'cache_lifetime' => intval( preg_replace( '#[^0-9]#', '', $new_instance['cache_lifetime'] ) ), ); } /////////////////////////////////////////////////////////////////////////// /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. */ public function form( $instance ) { // defaults $title = __('Popular Posts'); $count = $this->defaults['count']; $cache_lifetime = $this->defaults['cache_lifetime']; if( isset($instance['title']) && strlen($instance['title'])>1 ) { $title = $instance[ 'title' ]; } if( isset($instance['count']) && intval($instance['count'])>0 ) { $count = intval($instance['count']); } if( isset($instance['cache_lifetime']) && intval($instance['cache_lifetime'])>0 ) { $cache_lifetime = intval($instance['cache_lifetime']); } echo( '

'. ''. ''. '

'. '

'. ''. ''. '

'. '

'. ''. ''. '

' ); } /////////////////////////////////////////////////////////////////////////// } /////////////////////////////////////////////////////////////////////////////// // register AJAX Hits Counter: Popular Posts Widget add_action( 'widgets_init', create_function( '', 'register_widget( "AJAX_Hits_Counter_Popular_Posts_Widget" );' ) ); ///////////////////////////////////////////////////////////////////////////////