PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.0.7
Post Views Counter v1.0.7
1.7.13 1.7.12 1.7.11 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.2 1.3.2.1 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.7.10 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9
post-views-counter / includes / columns.php
post-views-counter / includes Last commit date
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
218 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 add_action('post_submitbox_misc_actions', array(&$this, 'submitbox_views'));
12 add_action('save_post', array(&$this, 'save_post'), 10, 2);
13 }
14
15 /**
16 * Output post views for single post.
17 */
18 public function submitbox_views() {
19 global $post;
20
21 $post_types = Post_Views_Counter()->get_attribute('options', 'general', 'post_types_count');
22
23 if (!in_array($post->post_type, (array)$post_types))
24 return;
25
26 global $wpdb;
27
28 // get total post views
29 $views = $wpdb->get_var(
30 $wpdb->prepare("
31 SELECT count
32 FROM ".$wpdb->prefix."post_views
33 WHERE id = %d AND type = 4",
34 absint($post->ID)
35 )
36 );
37 ?>
38
39 <div class="misc-pub-section" id="post-views">
40
41 <?php wp_nonce_field('post_views_count', 'pvc_nonce'); ?>
42
43 <span id="post-views-display">
44
45 <?php echo __('Post Views', 'post-views-counter') . ': <b>' . number_format_i18n((int)$views) . '</b>'; ?>
46
47 </span>
48
49 <a href="#post-views" class="edit-post-views hide-if-no-js"><?php _e('Edit', 'post-views-counter'); ?></a>
50
51 <div id="post-views-input-container" class="hide-if-js">
52
53 <p><?php _e('Adjust the views count for this post.', 'post-views-counter'); ?></p>
54 <input type="hidden" name="current_post_views" id="post-views-current" value="<?php echo (int)$views; ?>" />
55 <input type="text" name="post_views" id="post-views-input" value="<?php echo (int)$views; ?>"/><br />
56 <p>
57 <a href="#post-views" class="save-post-views hide-if-no-js button"><?php _e('OK', 'post-views-counter'); ?></a>
58 <a href="#post-views" class="cancel-post-views hide-if-no-js"><?php _e('Cancel', 'post-views-counter'); ?></a>
59 </p>
60
61 </div>
62
63 </div>
64 <?php
65 }
66
67 /**
68 * Save post views data
69 */
70 public function save_post($post_id, $post) {
71
72 // break if doing autosave
73 if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
74 return $post_id;
75
76 // break if current user can't edit this post
77 if(!current_user_can('edit_post', $post_id))
78 return $post_id;
79
80 // is post views set
81 if(!isset($_POST['post_views']))
82 return $post_id;
83
84 // break if post views in not one of the selected
85 $post_types = Post_Views_Counter()->get_attribute('options', 'general', 'post_types_count');
86
87 if (!in_array($post->post_type, (array)$post_types))
88 return $post_id;
89
90 // validate data
91 if(!isset($_POST['pvc_nonce']) || !wp_verify_nonce($_POST['pvc_nonce'], 'post_views_count'))
92 return $post_id;
93
94 global $wpdb;
95
96 $count = apply_filters('pvc_update_post_views_count', absint($_POST['post_views']), $post_id);
97
98 // insert or update db post views count
99 $wpdb->query(
100 $wpdb->prepare("
101 INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count)
102 VALUES (%d, %d, %s, %d)
103 ON DUPLICATE KEY UPDATE count = %d",
104 $post_id,
105 4,
106 'total',
107 $count,
108 $count
109 )
110 );
111
112 do_action('pvc_after_update_post_views_count', $post_id);
113 }
114
115 /**
116 * Register post views column for specific post types
117 */
118 public function register_new_column() {
119 $screen = get_current_screen();
120
121 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')))) {
122
123 foreach (Post_Views_Counter()->get_attribute('options', 'general', 'post_types_count') as $post_type) {
124
125 if ($post_type === 'page' && $screen->post_type === 'page') {
126 // actions
127 add_action('manage_pages_custom_column', array(&$this, 'add_new_column_content'), 10, 2);
128
129 // filters
130 add_filter('manage_pages_columns', array(&$this, 'add_new_column'));
131 add_filter('manage_edit-page_sortable_columns', array(&$this, 'register_sortable_custom_column'));
132 }
133 elseif ($post_type === 'post' && $screen->post_type === 'post') {
134 // actions
135 add_action('manage_posts_custom_column', array(&$this, 'add_new_column_content'), 10, 2);
136
137 // filters
138 add_filter('manage_posts_columns', array(&$this, 'add_new_column'));
139 add_filter('manage_edit-post_sortable_columns', array(&$this, 'register_sortable_custom_column'));
140 }
141 elseif ($screen->post_type === $post_type) {
142 // actions
143 add_action('manage_'.$post_type.'_posts_custom_column', array(&$this, 'add_new_column_content'), 10, 2);
144
145 // filters
146 add_filter('manage_'.$post_type.'_posts_columns', array(&$this, 'add_new_column'));
147 add_filter('manage_edit-'.$post_type.'_sortable_columns', array(&$this, 'register_sortable_custom_column'));
148 }
149
150 }
151 }
152 }
153
154 /**
155 * Register sortable post views column
156 */
157 public function register_sortable_custom_column($columns) {
158 // add new sortable column
159 $columns['post_views'] = 'post_views';
160
161 return $columns;
162 }
163
164 /**
165 * Add post views column
166 */
167 public function add_new_column($columns) {
168 $offset = 0;
169
170 if (isset($columns['date']))
171 $offset++;
172
173 if (isset($columns['comments']))
174 $offset++;
175
176 if ($offset > 0) {
177 $date = array_slice($columns, -$offset, $offset, true);
178
179 foreach ($date as $column => $name) {
180 unset($columns[$column]);
181 }
182
183 $columns['post_views'] = __('Post Views', 'post-views-counter');
184
185 foreach ($date as $column => $name) {
186 $columns[$column] = $name;
187 }
188 }
189 else
190 $columns['post_views'] = __('Post Views', 'post-views-counter');
191
192 return $columns;
193 }
194
195 /**
196 * Add post views column content
197 */
198 public function add_new_column_content($column_name, $id) {
199
200 if ($column_name === 'post_views') {
201
202 global $wpdb;
203
204 // get total post views
205 $views = $wpdb->get_var(
206 $wpdb->prepare("
207 SELECT count
208 FROM ".$wpdb->prefix."post_views
209 WHERE id = %d AND type = 4",
210 $id
211 )
212 );
213
214 echo number_format_i18n((int)$views);
215 }
216 }
217 }
218 ?>