PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.3.11
Post Views Counter v1.3.11
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 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 functions.php 4 years ago
class-columns.php
526 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 *
38 * @return void
39 */
40 public function submitbox_views() {
41 global $post;
42
43 // get main instance
44 $pvc = Post_Views_Counter();
45
46 // incorrect post type?
47 if ( ! in_array( $post->post_type, (array) $pvc->options['general']['post_types_count'] ) )
48 return;
49
50 // break if current user can't edit this post
51 if ( ! current_user_can( 'edit_post', $post->ID ) )
52 return;
53
54 // get total post views
55 $count = (int) pvc_get_post_views( $post->ID ); ?>
56
57 <div class="misc-pub-section" id="post-views">
58
59 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
60
61 <span id="post-views-display">
62 <?php echo __( 'Post Views', 'post-views-counter' ) . ': <b>' . number_format_i18n( $count ) . '</b>'; ?>
63 </span>
64
65 <?php
66 // restrict editing
67 $restrict = (bool) $pvc->options['general']['restrict_edit_views'];
68
69 if ( $restrict === false || ( $restrict === true && current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) ) ) {
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 int
98 */
99 public function save_post( $post_id, $post = null ) {
100 // break if doing autosave
101 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
102 return $post_id;
103
104 // break if current user can't edit this post
105 if ( ! current_user_can( 'edit_post', $post_id ) )
106 return $post_id;
107
108 // is post views set
109 if ( ! isset( $_POST['post_views'] ) )
110 return $post_id;
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 $post_id;
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 $post_id;
134
135 // break if views editing is restricted
136 if ( (bool) $pvc->options['general']['restrict_edit_views'] === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
137 return $post_id;
138
139 // validate data
140 if ( ! isset( $_POST['pvc_nonce'] ) || ! wp_verify_nonce( $_POST['pvc_nonce'], 'post_views_count' ) )
141 return $post_id;
142
143 // update post views
144 pvc_update_post_views( $post_id, $post_views );
145
146 do_action( 'pvc_after_update_post_views_count', $post_id );
147 }
148
149 /**
150 * Register post views column for specific post types.
151 *
152 * @return void
153 */
154 public function register_new_column() {
155 // get main instance
156 $pvc = Post_Views_Counter();
157
158 // is posts views column active?
159 if ( ! $pvc->options['general']['post_views_column'] )
160 return;
161
162 // get post types
163 $post_types = $pvc->options['general']['post_types_count'];
164
165 // any post types?
166 if ( ! empty( $post_types ) ) {
167 foreach ( $post_types as $post_type ) {
168 if ( $post_type === 'attachment' ) {
169 // actions
170 add_action( 'manage_media_custom_column', [ $this, 'add_new_column_content' ], 10, 2 );
171
172 // filters
173 add_filter( 'manage_media_columns', [ $this, 'add_new_column' ] );
174 add_filter( 'manage_upload_sortable_columns', [ $this, 'register_sortable_custom_column' ] );
175 } else {
176 // actions
177 add_action( 'manage_' . $post_type . '_posts_custom_column', [ $this, 'add_new_column_content' ], 10, 2 );
178
179 // filters
180 add_filter( 'manage_' . $post_type . '_posts_columns', [ $this, 'add_new_column' ] );
181 add_filter( 'manage_edit-' . $post_type . '_sortable_columns', [ $this, 'register_sortable_custom_column' ] );
182
183 // bbPress?
184 if ( class_exists( 'bbPress' ) ) {
185 if ( $post_type === 'forum' )
186 add_filter( 'bbp_admin_forums_column_headers', [ $this, 'add_new_column' ] );
187 elseif ( $post_type === 'topic' )
188 add_filter( 'bbp_admin_topics_column_headers', [ $this, 'add_new_column' ] );
189 }
190 }
191 }
192 }
193 }
194
195 /**
196 * Register sortable post views column.
197 *
198 * @param array $columns
199 * @return array
200 */
201 public function register_sortable_custom_column( $columns ) {
202 // add new sortable column
203 $columns['post_views'] = 'post_views';
204
205 return $columns;
206 }
207
208 /**
209 * Add post views column.
210 *
211 * @param array $columns
212 * @return array
213 */
214 public function add_new_column( $columns ) {
215 // date column exists?
216 if ( isset( $columns['date'] ) ) {
217 // store date column
218 $date = $columns['date'];
219
220 // unset date column
221 unset( $columns['date'] );
222 }
223
224 // comments column exists?
225 if ( isset( $columns['comments'] ) ) {
226 // store comments column
227 $comments = $columns['comments'];
228
229 // unset comments column
230 unset( $columns['comments'] );
231 }
232
233 // add post views column
234 $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>';
235
236 // restore date column
237 if ( isset( $date ) )
238 $columns['date'] = $date;
239
240 // restore comments column
241 if ( isset( $comments ) )
242 $columns['comments'] = $comments;
243
244 return $columns;
245 }
246
247 /**
248 * Add post views column content.
249 *
250 * @param string $column_name
251 * @param int $id
252 * @return void
253 */
254 public function add_new_column_content( $column_name, $id ) {
255 if ( $column_name === 'post_views' ) {
256 // get total post views
257 $count = pvc_get_post_views( $id );
258
259 echo esc_html( $count );
260 }
261 }
262
263 /**
264 * Handle quick edit.
265 *
266 * @global string $pagenow
267 *
268 * @param string $column_name
269 * @param string $post_type
270 * @return void
271 */
272 function quick_edit_custom_box( $column_name, $post_type ) {
273 global $pagenow;
274
275 if ( $pagenow !== 'edit.php' )
276 return;
277
278 if ( $column_name !== 'post_views' )
279 return;
280
281 // get main instance
282 $pvc = Post_Views_Counter();
283
284 if ( ! $pvc->options['general']['post_views_column'] || ! in_array( $post_type, $pvc->options['general']['post_types_count'] ) )
285 return;
286
287 // break if views editing is restricted
288 $restrict = (bool) $pvc->options['general']['restrict_edit_views'];
289
290 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
291 return;
292
293 ?>
294 <fieldset class="inline-edit-col-left">
295 <div id="inline-edit-post_views" class="inline-edit-col">
296 <label class="inline-edit-group">
297 <span class="title"><?php _e( 'Post Views', 'post-views-counter' ); ?></span>
298 <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value=""></span>
299 <input type="hidden" name="current_post_views" value="" />
300 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
301 </label>
302 </div>
303 </fieldset>
304 <?php
305 }
306
307 /**
308 * Bulk save post views.
309 *
310 * @global object $wpdb
311 *
312 * @return void
313 */
314 function save_bulk_post_views() {
315 $count = null;
316
317 if ( isset( $_POST['post_views'] ) ) {
318 if ( is_numeric( trim( $_POST['post_views'] ) ) ) {
319 $count = (int) $_POST['post_views'];
320
321 if ( $count < 0 )
322 $count = 0;
323 } else
324 $count = null;
325 }
326
327 // check post IDs
328 $post_ids = ( ! empty( $_POST['post_ids'] ) && is_array( $_POST['post_ids'] ) ) ? array_map( 'absint', $_POST['post_ids'] ) : [];
329
330 if ( is_null( $count ) )
331 exit;
332
333 // break if views editing is restricted
334 $restrict = (bool) Post_Views_Counter()->options['general']['restrict_edit_views'];
335
336 if ( $restrict === true && ! current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) ) )
337 exit;
338
339 // any post IDs?
340 if ( ! empty( $post_ids ) ) {
341 foreach ( $post_ids as $post_id ) {
342
343 // break if current user can't edit this post
344 if ( ! current_user_can( 'edit_post', $post_id ) )
345 continue;
346
347 global $wpdb;
348
349 // insert or update db post views count
350 $wpdb->query(
351 $wpdb->prepare( "
352 INSERT INTO " . $wpdb->prefix . "post_views (id, type, period, count)
353 VALUES (%d, %d, %s, %d)
354 ON DUPLICATE KEY UPDATE count = %d", $post_id, 4, 'total', $count, $count
355 )
356 );
357 }
358 }
359
360 exit;
361 }
362
363 /**
364 * Add admin bar stats to a post.
365 *
366 * @global string $pagenow
367 * @global string $post
368 *
369 * @param object $admin_bar
370 * @return void
371 */
372 public function admin_bar_menu( $admin_bar ) {
373 // get main instance
374 $pvc = Post_Views_Counter();
375
376 // statistics enabled?
377 if ( ! apply_filters( 'pvc_display_toolbar_statistics', $pvc->options['display']['toolbar_statistics'] ) )
378 return;
379
380 $post = null;
381
382 if ( is_admin() && ! wp_doing_ajax() ) {
383 global $pagenow;
384
385 $post = $pagenow == 'post.php' && ! empty( $_GET['post'] ) ? get_post( (int) $_GET['post'] ) : $post;
386 } elseif ( is_singular() )
387 global $post;
388
389 // get countable post types
390 $post_types = $pvc->options['general']['post_types_count'];
391
392 // whether to count this post type or not
393 if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) )
394 return;
395
396 $dt = new DateTime();
397
398 // get post views
399 $views = pvc_get_views(
400 [
401 'post_id' => $post->ID,
402 'post_type' => $post->post_type,
403 'fields' => 'date=>views',
404 'views_query' => [
405 'year' => $dt->format( 'Y' ),
406 'month' => $dt->format( 'm' )
407 ]
408 ]
409 );
410
411 $graph = '';
412
413 // get highest value
414 $views_copy = $views;
415
416 arsort( $views_copy, SORT_NUMERIC );
417
418 $highest = reset( $views_copy );
419
420 // find the multiplier
421 $multiplier = $highest * 0.05;
422
423 // generate ranges
424 $ranges = [];
425
426 for ( $i = 1; $i <= 20; $i ++ ) {
427 $ranges[$i] = round( $multiplier * $i );
428 }
429
430 // create graph
431 foreach ( $views as $date => $count ) {
432 $count_class = 0;
433
434 if ( $count > 0 ) {
435 foreach ( $ranges as $index => $range ) {
436 if ( $count <= $range ) {
437 $count_class = $index;
438 break;
439 }
440 }
441 }
442
443 $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>';
444 }
445
446 $admin_bar->add_menu(
447 [
448 'id' => 'pvc-post-views',
449 'title' => '<span class="pvc-graph-container">' . $graph . '</span>',
450 'href' => false,
451 'meta' => [
452 'title' => false
453 ]
454 ]
455 );
456 }
457
458 /**
459 * Maybe add admin CSS.
460 *
461 * @global string $pagenow
462 * @global string $post
463 *
464 * @return void
465 */
466 public function admin_bar_maybe_add_style() {
467 // get main instance
468 $pvc = Post_Views_Counter();
469
470 // statistics enabled?
471 if ( ! $pvc->options['display']['toolbar_statistics'] )
472 return;
473
474 $post = null;
475
476 if ( is_admin() && ! wp_doing_ajax() ) {
477 global $pagenow;
478
479 $post = ( $pagenow === 'post.php' && ! empty( $_GET['post'] ) ) ? get_post( (int) $_GET['post'] ) : $post;
480 } elseif ( is_singular() )
481 global $post;
482
483 // get countable post types
484 $post_types = $pvc->options['general']['post_types_count'];
485
486 // whether to count this post type or not
487 if ( empty( $post_types ) || empty( $post ) || ! in_array( $post->post_type, $post_types, true ) )
488 return;
489
490 // on backend area
491 add_action( 'admin_head', [ $this, 'admin_bar_css' ] );
492
493 // on frontend area
494 add_action( 'wp_head', [ $this, 'admin_bar_css' ] );
495 }
496
497 /**
498 * Add admin CSS.
499 *
500 * @return void
501 */
502 public function admin_bar_css() {
503 $html = '
504 <style type="text/css">
505 #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; }
506 #wp-admin-bar-pvc-post-views .pvc-line-graph {
507 display: inline-block;
508 width: 1px;
509 margin-right: 1px;
510 background-color: #ccc;
511 vertical-align: baseline;
512 }
513 #wp-admin-bar-pvc-post-views .pvc-line-graph:hover { background-color: #eee; }
514 #wp-admin-bar-pvc-post-views .pvc-line-graph-0 { height: 1% }';
515
516 for ( $i = 1; $i <= 20; $i ++ ) {
517 $html .= '
518 #wp-admin-bar-pvc-post-views .pvc-line-graph-' . $i . ' { height: ' . $i * 5 . '% }';
519 }
520
521 $html .= '
522 </style>';
523
524 echo wp_kses( $html, array( 'style' => array() ) );
525 }
526 }