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