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