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