PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.8
Post Views Counter v1.3.8
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 / class-columns.php
post-views-counter / includes Last commit date
ajax.php 7 years ago class-admin.php 4 years ago class-columns.php 4 years ago class-counter.php 4 years ago class-crawler-detect.php 4 years ago class-cron.php 4 years ago class-dashboard.php 4 years ago class-frontend.php 4 years ago class-functions.php 4 years ago class-query.php 4 years ago class-settings-api.php 4 years ago class-settings.php 4 years ago class-update.php 4 years ago class-widgets.php 4 years ago columns.php 6 years ago counter.php 6 years ago crawler-detect.php 6 years ago cron.php 6 years ago dashboard.php 6 years ago frontend.php 6 years ago functions.php 4 years ago query.php 6 years ago settings.php 6 years ago update.php 6 years ago widgets.php 6 years ago
class-columns.php
499 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Post_Views_Counter_Columns class.
8 *
9 * @class Post_Views_Counter_Columns
10 */
11 class Post_Views_Counter_Columns {
12
13 /**
14 * Class constructor.
15 *
16 * @return void
17 */
18 public function __construct() {
19 // actions
20 add_action( 'admin_init', [ $this, 'register_new_column' ] );
21 add_action( 'post_submitbox_misc_actions', [ $this, 'submitbox_views' ] );
22 add_action( 'attachment_submitbox_misc_actions', [ $this, 'submitbox_views' ] );
23 add_action( 'save_post', [ $this, 'save_post' ], 10, 2 );
24 add_action( 'edit_attachment', [ $this, 'save_post' ], 10 );
25 add_action( 'bulk_edit_custom_box', [ $this, 'quick_edit_custom_box' ], 10, 2 );
26 add_action( 'quick_edit_custom_box', [ $this, 'quick_edit_custom_box' ], 10, 2 );
27 add_action( 'wp_ajax_save_bulk_post_views', [ $this, 'save_bulk_post_views' ] );
28 add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 100 );
29 add_action( 'wp', [ $this, 'admin_bar_maybe_add_style' ] );
30 add_action( 'admin_init', [ $this, 'admin_bar_maybe_add_style' ] );
31 }
32
33 /**
34 * Output post views for single post.
35 *
36 * @global object $post
37 * @return void
38 */
39 public function submitbox_views() {
40 global $post;
41
42 // incorrect post type?
43 if ( ! in_array( $post->post_type, (array) Post_Views_Counter()->options['general']['post_types_count'] ) )
44 return;
45
46 // break if current user can't edit this post
47 if ( ! current_user_can( 'edit_post', $post->ID ) )
48 return;
49
50 // get total post views
51 $count = (int) pvc_get_post_views( $post->ID ); ?>
52
53 <div class="misc-pub-section" id="post-views">
54
55 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
56
57 <span id="post-views-display">
58 <?php echo __( 'Post Views', 'post-views-counter' ) . ': <b>' . number_format_i18n( $count ) . '</b>'; ?>
59 </span>
60
61 <?php
62 // restrict editing
63 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
64
65 if ( $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) ) {
66 ?>
67 <a href="#post-views" class="edit-post-views hide-if-no-js"><?php _e( 'Edit', 'post-views-counter' ); ?></a>
68
69 <div id="post-views-input-container" class="hide-if-js">
70
71 <p><?php _e( 'Adjust the views count for this post.', 'post-views-counter' ); ?></p>
72 <input type="hidden" name="current_post_views" id="post-views-current" value="<?php echo $count; ?>" />
73 <input type="text" name="post_views" id="post-views-input" value="<?php echo $count; ?>"/><br />
74 <p>
75 <a href="#post-views" class="save-post-views hide-if-no-js button"><?php _e( 'OK', 'post-views-counter' ); ?></a>
76 <a href="#post-views" class="cancel-post-views hide-if-no-js"><?php _e( 'Cancel', 'post-views-counter' ); ?></a>
77 </p>
78
79 </div>
80 <?php
81 }
82 ?>
83
84 </div>
85 <?php
86 }
87
88 /**
89 * Save post views data.
90 *
91 * @param int $post_id
92 * @param object $post
93 * @return int
94 */
95 public function save_post( $post_id, $post = null ) {
96 // break if doing autosave
97 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
98 return $post_id;
99
100 // break if current user can't edit this post
101 if ( ! current_user_can( 'edit_post', $post_id ) )
102 return $post_id;
103
104 // is post views set
105 if ( ! isset( $_POST['post_views'] ) )
106 return $post_id;
107
108 // cast numeric post views
109 $post_views = (int) $_POST['post_views'];
110
111 // unchanged post views value?
112 if ( isset( $_POST['current_post_views'] ) && $post_views === (int) $_POST['current_post_views'] )
113 return $post_id;
114
115 // break if post views in not one of the selected
116 $post_types = (array) Post_Views_Counter()->options['general']['post_types_count'];
117
118 // get post type
119 if ( is_null( $post ) )
120 $post_type = get_post_type( $post_id );
121 else
122 $post_type = $post->post_type;
123
124 // invalid post type?
125 if ( ! in_array( $post_type, $post_types, true ) )
126 return $post_id;
127
128 // break if views editing is restricted
129 if ( (bool) Post_Views_Counter()->options['general']['restrict_edit_views'] === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
130 return $post_id;
131
132 // validate data
133 if ( ! isset( $_POST['pvc_nonce'] ) || ! wp_verify_nonce( $_POST['pvc_nonce'], 'post_views_count' ) )
134 return $post_id;
135
136 // update post views
137 pvc_update_post_views( $post_id, $post_views );
138
139 do_action( 'pvc_after_update_post_views_count', $post_id );
140 }
141
142 /**
143 * Register post views column for specific post types.
144 *
145 * @return void
146 */
147 public function register_new_column() {
148 // get post types
149 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
150
151 // any post types?
152 if ( ! empty( $post_types ) ) {
153 foreach ( $post_types as $post_type ) {
154 if ( $post_type === 'attachment' ) {
155 // actions
156 add_action( 'manage_media_custom_column', [ $this, 'add_new_column_content' ], 10, 2 );
157
158 // filters
159 add_filter( 'manage_media_columns', [ $this, 'add_new_column' ] );
160 add_filter( 'manage_upload_sortable_columns', [ $this, 'register_sortable_custom_column' ] );
161 } else {
162 // actions
163 add_action( 'manage_' . $post_type . '_posts_custom_column', [ $this, 'add_new_column_content' ], 10, 2 );
164
165 // filters
166 add_filter( 'manage_' . $post_type . '_posts_columns', [ $this, 'add_new_column' ] );
167 add_filter( 'manage_edit-' . $post_type . '_sortable_columns', [ $this, 'register_sortable_custom_column' ] );
168
169 // bbPress?
170 if ( class_exists( 'bbPress' ) ) {
171 if ( $post_type === 'forum' )
172 add_filter( 'bbp_admin_forums_column_headers', [ $this, 'add_new_column' ] );
173 elseif ( $post_type === 'topic' )
174 add_filter( 'bbp_admin_topics_column_headers', [ $this, 'add_new_column' ] );
175 }
176 }
177 }
178 }
179 }
180
181 /**
182 * Register sortable post views column.
183 *
184 * @param array $columns
185 * @return array
186 */
187 public function register_sortable_custom_column( $columns ) {
188 // add new sortable column
189 $columns['post_views'] = 'post_views';
190
191 return $columns;
192 }
193
194 /**
195 * Add post views column.
196 *
197 * @param array $columns
198 * @return array
199 */
200 public function add_new_column( $columns ) {
201 $offset = 0;
202
203 // date column?
204 if ( isset( $columns['date'] ) )
205 $offset++;
206
207 // comments column?
208 if ( isset( $columns['comments'] ) )
209 $offset++;
210
211 // any skipped columns?
212 if ( $offset > 0 ) {
213 $data = array_slice( $columns, -$offset, $offset, true );
214
215 // unset columns
216 foreach ( $data as $column => $name ) {
217 unset( $columns[$column] );
218 }
219
220 $columns['post_views'] = '<span class="dash-icon dashicons dashicons-chart-bar" title="' . __( 'Post Views', 'post-views-counter' ) . '"></span><span class="dash-title">' . __( 'Post Views', 'post-views-counter' ) . '</span>';
221
222 // again add columns
223 foreach ( $data as $column => $name ) {
224 $columns[$column] = $name;
225 }
226 } else
227 $columns['post_views'] = '<span class="dash-icon dashicons dashicons-chart-bar" title="' . __( 'Post Views', 'post-views-counter' ) . '"></span><span class="dash-title">' . __( 'Post Views', 'post-views-counter' ) . '</span>';
228
229 return $columns;
230 }
231
232 /**
233 * Add post views column content.
234 *
235 * @param string $column_name
236 * @param int $id
237 * @return void
238 */
239 public function add_new_column_content( $column_name, $id ) {
240 if ( $column_name === 'post_views' ) {
241 // get total post views
242 $count = pvc_get_post_views( $id );
243
244 echo $count;
245 }
246 }
247
248 /**
249 * Handle quick edit.
250 *
251 * @global string $pagenow
252 * @param string $column_name
253 * @param string $post_type
254 * @return void
255 */
256 function quick_edit_custom_box( $column_name, $post_type ) {
257 global $pagenow;
258
259 if ( $pagenow !== 'edit.php' )
260 return;
261
262 if ( $column_name !== 'post_views' )
263 return;
264
265 if ( ! Post_Views_Counter()->options['general']['post_views_column'] || ! in_array( $post_type, Post_Views_Counter()->options['general']['post_types_count'] ) )
266 return;
267
268 // break if views editing is restricted
269 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
270
271 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
272 return;
273
274 ?>
275 <fieldset class="inline-edit-col-left">
276 <div id="inline-edit-post_views" class="inline-edit-col">
277 <label class="inline-edit-group">
278 <span class="title"><?php _e( 'Post Views', 'post-views-counter' ); ?></span>
279 <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value=""></span>
280 <input type="hidden" name="current_post_views" value="" />
281 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
282 </label>
283 </div>
284 </fieldset>
285 <?php
286 }
287
288 /**
289 * Bulk save post views.
290 *
291 * @global object $wpdb;
292 * @return void
293 */
294 function save_bulk_post_views() {
295 $count = null;
296
297 if ( isset( $_POST['post_views'] ) ) {
298 if ( is_numeric( trim( $_POST['post_views'] ) ) ) {
299 $count = (int) $_POST['post_views'];
300
301 if ( $count < 0 )
302 $count = 0;
303 } else
304 $count = null;
305 }
306
307 // check post IDs
308 $post_ids = ( ! empty( $_POST['post_ids'] ) && is_array( $_POST['post_ids'] ) ) ? array_map( 'absint', $_POST['post_ids'] ) : [];
309
310 if ( is_null( $count ) )
311 exit;
312
313 // break if views editing is restricted
314 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
315
316 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
317 exit;
318
319 // any post IDs?
320 if ( ! empty( $post_ids ) ) {
321 foreach ( $post_ids as $post_id ) {
322
323 // break if current user can't edit this post
324 if ( ! current_user_can( 'edit_post', $post_id ) )
325 continue;
326
327 global $wpdb;
328
329 // insert or update db post views count
330 $wpdb->query(
331 $wpdb->prepare( "
332 INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count)
333 VALUES (%d, %d, %s, %d)
334 ON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $count, $count
335 )
336 );
337 }
338 }
339
340 exit;
341 }
342
343 /**
344 * Add admin bar stats to a post.
345 *
346 * @return void
347 */
348 public function admin_bar_menu( $admin_bar ) {
349 // get main instance
350 $pvc = Post_Views_Counter();
351
352 // statistics enabled?
353 if ( ! apply_filters( 'pvc_display_toolbar_statistics', $pvc->options['display']['toolbar_statistics'] ) )
354 return;
355
356 $post = null;
357
358 if ( is_admin() && ! wp_doing_ajax() ) {
359 global $pagenow;
360
361 $post = $pagenow == 'post.php' && ! empty( $_GET['post'] ) ? get_post( (int) $_GET['post'] ) : $post;
362 } elseif ( is_singular() )
363 global $post;
364
365 // get countable post types
366 $post_types = Post_Views_Counter()->options['general']['post_types_count'];
367
368 // whether to count this post type or not
369 if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) )
370 return;
371
372 $dt = new DateTime();
373
374 // get post views
375 $views = pvc_get_views(
376 [
377 'post_id' => $post->ID,
378 'post_type' => $post->post_type,
379 'fields' => 'date=>views',
380 'views_query' => [
381 'year' => $dt->format( 'Y' ),
382 'month' => $dt->format( 'm' )
383 ]
384 ]
385 );
386
387 $graph = '';
388
389 // get highest value
390 $views_copy = $views;
391
392 arsort( $views_copy, SORT_NUMERIC );
393
394 $highest = reset( $views_copy );
395
396 // find the multiplier
397 $multiplier = $highest * 0.05;
398
399 // generate ranges
400 $ranges = [];
401
402 for ( $i = 1; $i <= 20; $i ++ ) {
403 $ranges[$i] = round( $multiplier * $i );
404 }
405
406 // create graph
407 foreach ( $views as $date => $count ) {
408 $count_class = 0;
409
410 if ( $count > 0 ) {
411 foreach ( $ranges as $index => $range ) {
412 if ( $count <= $range ) {
413 $count_class = $index;
414 break;
415 }
416 }
417 }
418
419 $graph .= '<span class="pvc-line-graph pvc-line-graph-' . $count_class . '" title="' . sprintf( _n( '%s post view', '%s post views', $count, 'post-views-counter' ), number_format_i18n( $count ) ) . '"></span>';
420 }
421
422 $admin_bar->add_menu(
423 [
424 'id' => 'pvc-post-views',
425 'title' => '<span class="pvc-graph-container">' . $graph . '</span>',
426 'href' => false,
427 'meta' => [
428 'title' => false
429 ]
430 ]
431 );
432 }
433
434 /**
435 * Maybe add admin CSS.
436 *
437 * @return void
438 */
439 public function admin_bar_maybe_add_style() {
440 // get main instance
441 $pvc = Post_Views_Counter();
442
443 // statistics enabled?
444 if ( ! $pvc->options['display']['toolbar_statistics'] )
445 return;
446
447 $post = null;
448
449 if ( is_admin() && ! wp_doing_ajax() ) {
450 global $pagenow;
451
452 $post = ( $pagenow === 'post.php' && ! empty( $_GET['post'] ) ) ? get_post( (int) $_GET['post'] ) : $post;
453 } elseif ( is_singular() )
454 global $post;
455
456 // get countable post types
457 $post_types = $pvc->options['general']['post_types_count'];
458
459 // whether to count this post type or not
460 if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) )
461 return;
462
463 // on backend area
464 add_action( 'admin_head', [ $this, 'admin_bar_css' ] );
465
466 // on frontend area
467 add_action( 'wp_head', [ $this, 'admin_bar_css' ] );
468 }
469
470 /**
471 * Add admin CSS.
472 *
473 * @return void
474 */
475 public function admin_bar_css() {
476 $html = '
477 <style type="text/css">
478 #wp-admin-bar-pvc-post-views .pvc-graph-container { padding-top: 6px; padding-bottom: 6px; position: relative; display: block; height: 100%; box-sizing: border-box; }
479 #wp-admin-bar-pvc-post-views .pvc-line-graph {
480 display: inline-block;
481 width: 1px;
482 margin-right: 1px;
483 background-color: #ccc;
484 vertical-align: baseline;
485 }
486 #wp-admin-bar-pvc-post-views .pvc-line-graph:hover { background-color: #eee; }
487 #wp-admin-bar-pvc-post-views .pvc-line-graph-0 { height: 1% }';
488
489 for ( $i = 1; $i <= 20; $i ++ ) {
490 $html .= '
491 #wp-admin-bar-pvc-post-views .pvc-line-graph-' . $i . ' { height: ' . $i * 5 . '% }';
492 }
493
494 $html .= '
495 </style>';
496
497 echo $html;
498 }
499 }