'. 'function ahc_getXmlHttp(){var e;try{e=new ActiveXObject("Msxml2.XMLHTTP")}catch(t){try{e=new ActiveXObject("Microsoft.XMLHTTP")}catch(n){e=false}}if(!e&&typeof XMLHttpRequest!="undefined"){e=new XMLHttpRequest}return e};'. 'var ahc_xmlhttp=ahc_getXmlHttp();'. 'ahc_xmlhttp.open('. '"GET",'. '"'.admin_url( 'admin-ajax.php' ). '?action=ajax-hits-counter-increment'. '&post_id='.$post->ID. '&t="+(parseInt(new Date().getTime()))+"&r="+(parseInt(Math.random()*100000))'. ');'. 'ahc_xmlhttp.send(null);'. ''; } return $content; } /////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter::incrementHits() * * @return void */ public function incrementHits() { 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 ) ); } /////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter::getHits() * * @param integer $post_id * @return integer */ public function getHits( $post_id ) { $post_id = intval( filter_var( $post_id, FILTER_SANITIZE_NUMBER_INT ) ); if( empty($post_id) ) { return 0; } $hits = get_post_meta( $post_id, 'hits', true ); if( empty($hits) ) { return 0; } return intval($hits); } /////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter::adminInit() * * @return bool */ public function adminInit() { global $current_user; if( isset($current_user->roles) && !empty($current_user->roles) && in_array( 'administrator', $current_user->roles ) ) { // add meta box add_action( 'add_meta_boxes', array( $this, 'adminAddMetaBox' ) ); } } /////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter::adminSavePost() * * @param integer $post_id * @return bool */ public function adminSavePost( $post_id ) { // skip for autosave if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } // update hits count if( isset($_POST['post_type']) && $_POST['post_type']=='post' ) { $hits = ( isset($_POST['hits']) && !empty($_POST['hits']) ? intval( preg_replace( '/[^0-9]/', '', $_POST['hits'] ) ) : 0 ); if( $hits > 0 ) { $hits_exists = get_post_meta( $post_id, 'hits', true ); if( $hits_exists===false ) { add_post_meta( $post_id, 'hits', $hits, true ); } else { update_post_meta( $post_id, 'hits', $hits ); } } } // clear Popular Posts Widget $ahc_ppw = new AJAX_Hits_Counter_Popular_Posts_Widget(); $ahc_ppw->clearCache(); return true; } /////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter::adminPostsTableColumn() * * @param array $column * @return array */ public function adminPostsTableColumn( $column ) { $column['hits'] = 'Hits'; return $column; } /////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter::adminPostsTableRow() * * @param string $column_name * @param integer $post_id * @return string */ public function adminPostsTableRow( $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 ); } } /////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter::adminPostsTableSortable() * * @param array $column * @return array */ public function adminPostsTableSortable( $column ) { $column['hits'] = 'hits'; return $column; } /////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter::adminPostsTableOrderBy() * * @param array $vars * @return array */ public function adminPostsTableOrderBy( $vars ) { if( isset($vars['orderby']) && $vars['orderby']=='hits' ) { $vars = array_merge( $vars, array( 'meta_key' => 'hits', 'orderby' => 'meta_value_num' ) ); } return $vars; } /////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter::adminAddMetaBox() * * @return bool */ public function adminAddMetaBox() { add_meta_box( 'hits', 'Hits count', array( $this, 'adminAddMetaBoxPrint' ), 'post', 'side', 'default' ); return true; } /////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter::adminAddMetaBoxPrint() * * @param string $post * @param string $metabox * @return void */ public function adminAddMetaBoxPrint( $post, $metabox ) { wp_nonce_field( plugin_basename( __FILE__ ), 'ajax_hits_counter_nonce' ); $hits = get_post_meta( $post->ID, 'hits', true ); echo( ' '. '' ); } /////////////////////////////////////////////////////////////////////////// } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter_Popular_Posts_Widget */ class AJAX_Hits_Counter_Popular_Posts_Widget extends WP_Widget { /////////////////////////////////////////////////////////////////////////// protected $defaults = array( 'widget_id' => 'ajax_hits_counter_popular_posts_widget', 'sorting_algorithm' => 1, // hits only 'count' => 5, 'cache_lifetime' => 3600, 'date_range' => 7, 'one_element_html' => "\n {post_title} ({post_hits})\n", 'post_category' => -1, 'post_categories_separator' => ', ', 'post_date_format' => 'd.m.Y', ); /////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter_Popular_Posts_Widget::__construct() * ( Register widget with WordPress ) * * @return void */ 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'], ), array( 'width' => 400, 'height' => 350, ) ); /////////////////////////////////////////////////////////////////////// } /////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter_Popular_Posts_Widget::widget() * ( 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 ) { /////////////////////////////////////////////////////////////////////// // args $args = array_merge( $this->defaults, $args ); // cache key $cache_key = 'ajax_hits_counter_'.dechex(crc32( $args['widget_id'] )); // try to get cached data from transient cache $cache = get_transient( $cache_key ); if( !is_array($cache) && !empty($cache) ) #if( false ) { // cache exists, return cached data echo( $cache ); return true; } // get popular posts $popular_posts = $this->getPopularPosts( $instance ); if( empty($popular_posts) ) { return false; } $title = apply_filters( 'widget_title', $instance['title'] ); $output = $args['before_widget']; if( !empty( $title ) ) { $output .= $args['before_title'].$title.$args['after_title']; } $output .= $this->getHTML( $popular_posts, $instance ); $output .= $args['after_widget']; // store result to cache set_transient( $cache_key, $output, $instance['cache_lifetime'] ); echo( $output ); /////////////////////////////////////////////////////////////////////// } /////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter_Popular_Posts_Widget::getPopularPosts() * ( Returns Popular Posts ) * * @param array $args * @return array */ protected function getPopularPosts( $args = array() ) { /////////////////////////////////////////////////////////////////////// global $wpdb; if( isset($args['sorting_algorithm']) ) { switch( $args['sorting_algorithm'] ) { case 1: // hits only default: $sql_sorting_algorithm = '( m.meta_value + 0 ) DESC,'; break; case 2: // comments only $sql_sorting_algorithm = '( p.comment_count + 0 ) DESC,'; break; case 3: // hits + comments * 10 $sql_sorting_algorithm = '( ( m.meta_value + 0 ) + ( p.comment_count + 0 ) * 10 ) DESC,'; break; } } else { $sql_sorting_algorithm = '( m.meta_value + 0 ) DESC,'; } /////////////////////////////////////////////////////////////////////// $q = ' SELECT DISTINCT p.ID, p.post_title, p.post_content, p.post_author, p.post_date, m.meta_value as post_hits, p.comment_count as post_comments_count FROM '.$wpdb->posts.' p JOIN '.$wpdb->postmeta.' m ON ( p.ID = m.post_id ) WHERE p.post_date_gmt < \''.date( 'Y-m-d H:i:s' ).'\''; if( isset($args['date_range']) && $args['date_range']<7 ) { switch( $args['date_range'] ) { case 1: $temp_post_date_shift = '-1 day'; break; case 2: $temp_post_date_shift = '-1 week'; break; case 3: $temp_post_date_shift = '-1 month'; break; case 4: $temp_post_date_shift = '-3 months'; break; case 5: $temp_post_date_shift = '-6 months'; break; case 6: $temp_post_date_shift = '-1 year'; break; default: $temp_post_date_shift = false; } if( !empty($temp_post_date_shift) ) { $q .= ' AND p.post_date_gmt >= \''.date( 'Y-m-d H:i:s', strtotime( $temp_post_date_shift ) ).'\''; } } $q .= ' AND p.post_status = \'publish\' AND p.post_type = \'post\' AND m.meta_key = \'hits\''; if( isset($args['post_category']) ) { $temp_post_category = false; if( $args['post_category']>0 ) { $temp_post_category = $args['post_category']; } elseif( $args['post_category']==-2 ) { $temp_post_category = intval( get_query_var('cat') ); } if( !empty($temp_post_category) ) { $q .= ' AND p.ID IN ( SELECT DISTINCT t_r.object_id FROM '.$wpdb->term_relationships.' t_r WHERE t_r.term_taxonomy_id = '.$temp_post_category.' )'; } } $q .= ' ORDER BY '. $sql_sorting_algorithm. 'p.post_date_gmt DESC LIMIT '.$args['count']; return $wpdb->get_results($q); /////////////////////////////////////////////////////////////////////// } /////////////////////////////////////////////////////////////////////////// /** * AJAX_Hits_Counter_Popular_Posts_Widget::getHTML() * ( Returns HTML of Popular Posts ) * * @param array $popular_posts * @param array $args * @return string */ protected function getHTML( $popular_posts = array(), $args = array() ) { /////////////////////////////////////////////////////////////////////// if( empty($popular_posts) ) { return false; } /////////////////////////////////////////////////////////////////////// // fix bug in Wordpress :-) global $post; $tmp_post = $post; // args $args = array_merge( $this->defaults, $args ); $excerpt_length_isset = false; /////////////////////////////////////////////////////////////////////// $html = '