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