Skip to content

Instantly share code, notes, and snippets.

@ssovit
Last active August 29, 2015 14:06
Show Gist options
  • Save ssovit/f7b679eb9d11ef7347da to your computer and use it in GitHub Desktop.
Save ssovit/f7b679eb9d11ef7347da to your computer and use it in GitHub Desktop.
Post Views
<?php
class WPPress_Post_Views
{
function __construct() {
add_action('wp_ajax_post_heartbeat', array(&$this,
'increase'
));
add_action('wp_ajax_nopriv_post_heartbeat', array(&$this,
'increase'
));
add_action('wp_footer', array(&$this,
'js'
));
add_action('init', array(&$this,
'init'
));
}
function init() {
add_filter('manage_edit-post_columns', array(&$this,
'manage_posts_columns'
));
add_action('manage_post_posts_custom_column', array(&$this,
'manage_posts_custom_column'
) , 10, 2);
add_filter('manage_edit-post_sortable_columns', array(&$this,
'sortable_columns'
));
add_action('pre_get_posts', array(&$this,
'admin_sort'
));
}
function manage_posts_columns($columns) {
global $current_user;
get_currentuserinfo();
if (current_user_can('edit_posts', $user_id)) {
$columns['video_view'] = __('Video Views');
}
return $columns;
}
function manage_posts_custom_column($column_name, $post_id) {
if ($column_name == 'video_view') {
$views = (int)get_post_meta($post_id, 'post_views', true);
echo "<div title='" . $views . " Views'>" . kmb_format($views) . " Views</div>";
}
}
function sortable_columns($columns) {
$columns['video_view'] = 'video_view';
return $columns;
}
function admin_sort($query) {
if (!is_admin()) return;
$orderby = $query->get('orderby');
if ('video_view' == $orderby) {
$query->set('meta_key', 'post_views');
$query->set('orderby', 'meta_value_num');
}
}
function js() {
if (is_single()) {
wp_reset_query();
echo "<script type='text/javascript'>(function(){var src='" . add_query_arg(array(
'action' => 'post_heartbeat',
'id' => get_the_ID()
) , get_admin_url('', 'admin-ajax.php')) . "';var s = document.createElement('script'); s.async = true; s.type = 'text/javascript'; s.src = src; (document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);})(document);</script>";
}
}
/*
Method: increase()
*/
function increase() {
header('Content-Type: application/javascript');
if ($_GET['id'] > 0) {
$postID = $_GET['id'];
$count_key = 'post_views';
$count = get_post_meta($postID, $count_key, true);
if ($count == '') {
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
} else {
update_post_meta($postID, $count_key, ++$count);
}
}
echo '(function($){})(jQuery);';
die();
}
}
/* Initialize the class */
new WPPress_Post_Views();
?>
<?php
$query = array(
'post_type' => 'post',
'posts_per_page' => 5,
'paged' => 1,
'ignore_sticky_posts' => 1,
'meta_key' => 'post_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post__not_in' => array(
get_the_ID()
)
);
$popular_posts = new WP_Query($query);
if ($popular_posts->have_posts()) {
while ($popular_posts->have_posts()) {
$popular_posts->the_post();
if (has_post_thumbnail()) {
echo "<div class='popular-post'>";
echo "<a href='" . get_permalink() . "?ref=popular' title='" . esc_attr(get_the_title()) . "'>";
the_post_thumbnail('sidebar');
echo "</a>";
echo "<h5><a href='" . get_permalink() . "?ref=popular' title='" . esc_attr(get_the_title()) . "'>" . get_the_title() . "</a></h5>";
echo "</div>";
}
}
}
wp_reset_query();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment