columns.php
11 years ago
counter.php
11 years ago
cron.php
11 years ago
frontend.php
11 years ago
functions.php
11 years ago
query.php
11 years ago
settings.php
11 years ago
update.php
11 years ago
widgets.php
11 years ago
columns.php
116 lines
| 1 | <?php |
| 2 | if(!defined('ABSPATH')) exit; |
| 3 | |
| 4 | new Post_Views_Counter_Columns(); |
| 5 | |
| 6 | class Post_Views_Counter_Columns { |
| 7 | |
| 8 | public function __construct() { |
| 9 | // actions |
| 10 | add_action('current_screen', array(&$this, 'register_new_column')); |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Register post views column for specific post types |
| 15 | */ |
| 16 | public function register_new_column() { |
| 17 | $screen = get_current_screen(); |
| 18 | |
| 19 | if (Post_Views_Counter()->get_attribute('options', 'general', 'post_views_column') && ($screen->base == 'edit' && in_array($screen->post_type, Post_Views_Counter()->get_attribute('options', 'general', 'post_types_count')))) { |
| 20 | |
| 21 | foreach (Post_Views_Counter()->get_attribute('options', 'general', 'post_types_count') as $post_type) { |
| 22 | |
| 23 | if ($post_type === 'page' && $screen->post_type === 'page') { |
| 24 | // actions |
| 25 | add_action('manage_pages_custom_column', array(&$this, 'add_new_column_content'), 10, 2); |
| 26 | |
| 27 | // filters |
| 28 | add_filter('manage_pages_columns', array(&$this, 'add_new_column')); |
| 29 | add_filter('manage_edit-page_sortable_columns', array(&$this, 'register_sortable_custom_column')); |
| 30 | } |
| 31 | elseif ($post_type === 'post' && $screen->post_type === 'post') { |
| 32 | // actions |
| 33 | add_action('manage_posts_custom_column', array(&$this, 'add_new_column_content'), 10, 2); |
| 34 | |
| 35 | // filters |
| 36 | add_filter('manage_posts_columns', array(&$this, 'add_new_column')); |
| 37 | add_filter('manage_edit-post_sortable_columns', array(&$this, 'register_sortable_custom_column')); |
| 38 | } |
| 39 | elseif ($screen->post_type === $post_type) { |
| 40 | // actions |
| 41 | add_action('manage_'.$post_type.'_posts_custom_column', array(&$this, 'add_new_column_content'), 10, 2); |
| 42 | |
| 43 | // filters |
| 44 | add_filter('manage_'.$post_type.'_posts_columns', array(&$this, 'add_new_column')); |
| 45 | add_filter('manage_edit-'.$post_type.'_sortable_columns', array(&$this, 'register_sortable_custom_column')); |
| 46 | } |
| 47 | |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Register sortable post views column |
| 54 | */ |
| 55 | public function register_sortable_custom_column($columns) { |
| 56 | // add new sortable column |
| 57 | $columns['post_views'] = 'post_views'; |
| 58 | |
| 59 | return $columns; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Add post views column |
| 64 | */ |
| 65 | public function add_new_column($columns) { |
| 66 | $offset = 0; |
| 67 | |
| 68 | if (isset($columns['date'])) |
| 69 | $offset++; |
| 70 | |
| 71 | if (isset($columns['comments'])) |
| 72 | $offset++; |
| 73 | |
| 74 | if ($offset > 0) { |
| 75 | $date = array_slice($columns, -$offset, $offset, true); |
| 76 | |
| 77 | foreach ($date as $column => $name) { |
| 78 | unset($columns[$column]); |
| 79 | } |
| 80 | |
| 81 | $columns['post_views'] = __('Post Views', 'post-views-counter'); |
| 82 | |
| 83 | foreach ($date as $column => $name) { |
| 84 | $columns[$column] = $name; |
| 85 | } |
| 86 | } |
| 87 | else |
| 88 | $columns['post_views'] = __('Post Views', 'post-views-counter'); |
| 89 | |
| 90 | return $columns; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Add post views column content |
| 95 | */ |
| 96 | public function add_new_column_content($column_name, $id) { |
| 97 | |
| 98 | if ($column_name === 'post_views') { |
| 99 | |
| 100 | global $wpdb; |
| 101 | |
| 102 | // get total post views |
| 103 | $views = $wpdb->get_var( |
| 104 | $wpdb->prepare(" |
| 105 | SELECT count |
| 106 | FROM ".$wpdb->prefix."post_views |
| 107 | WHERE id = %d AND type = 4", |
| 108 | $id |
| 109 | ) |
| 110 | ); |
| 111 | |
| 112 | echo (int)$views; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | ?> |