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