PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.7.13
Post Views Counter v1.7.13
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 2 weeks ago class-columns-modal.php 2 weeks ago class-columns.php 2 weeks ago class-counter.php 2 weeks ago class-crawler-detect.php 2 weeks ago class-cron.php 2 weeks ago class-dashboard.php 2 weeks ago class-emails-mailer.php 2 weeks ago class-emails-period.php 2 weeks ago class-emails-query.php 2 weeks ago class-emails-scheduler.php 2 weeks ago class-emails-template.php 2 weeks ago class-emails.php 2 weeks ago class-frontend.php 2 weeks ago class-functions.php 2 weeks ago class-import.php 2 weeks ago class-integration-gutenberg.php 2 weeks ago class-integrations.php 2 weeks ago class-query.php 2 weeks ago class-settings-api.php 2 weeks ago class-settings-display.php 2 weeks ago class-settings-emails.php 2 weeks ago class-settings-general.php 2 weeks ago class-settings-integrations.php 2 weeks ago class-settings-other.php 2 weeks ago class-settings-reports.php 2 weeks ago class-settings.php 2 weeks ago class-toolbar.php 2 weeks ago class-traffic-signals.php 2 weeks ago class-update.php 2 weeks ago class-widgets.php 2 weeks ago functions.php 2 weeks ago
class-columns.php
442 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 }
29
30 /**
31 * Sanitize manual post views input.
32 *
33 * @param mixed $value
34 * @return int|null
35 */
36 private function sanitize_post_views_input( $value ) {
37 if ( ! is_scalar( $value ) )
38 return null;
39
40 $value = trim( wp_unslash( (string) $value ) );
41
42 if ( $value === '' || ! is_numeric( $value ) )
43 return null;
44
45 $count = (int) $value;
46
47 return $count < 0 ? 0 : $count;
48 }
49
50 /**
51 * Output post views for single post.
52 *
53 * @global object $post
54 *
55 * @return void
56 */
57 public function submitbox_views() {
58 global $post;
59
60 // get main instance
61 $pvc = Post_Views_Counter();
62 $post_types = (array) $pvc->options['general']['post_types_count'];
63
64 // break if display is not allowed
65 if ( ! $pvc->options['display']['post_views_column'] || ! in_array( $post->post_type, $post_types, true ) )
66 return;
67
68 // check if user can see post stats
69 if ( apply_filters( 'pvc_admin_display_post_views', true, $post->ID ) === false )
70 return;
71
72 // get total post views
73 $count = (int) pvc_get_post_views( $post->ID ); ?>
74
75 <div class="misc-pub-section" id="post-views">
76
77 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
78
79 <span id="post-views-display">
80 <?php echo __( 'Post Views', 'post-views-counter' ) . ': <b>' . number_format_i18n( $count ) . '</b>'; ?>
81 </span>
82
83 <?php
84 // allow editing
85 $allow_edit = (bool) $pvc->options['display']['restrict_edit_views'];
86
87 // allow editing condition
88 $allow_edit_condition = (bool) current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) );
89
90 if ( $allow_edit === true && $allow_edit_condition === true ) {
91 ?>
92 <a href="#post-views" class="edit-post-views hide-if-no-js"><?php _e( 'Edit', 'post-views-counter' ); ?></a>
93
94 <div id="post-views-input-container" class="hide-if-js">
95
96 <p><?php _e( 'Adjust the views count for this post.', 'post-views-counter' ); ?></p>
97 <input type="hidden" name="current_post_views" id="post-views-current" value="<?php echo esc_attr( $count ); ?>" />
98 <input type="text" name="post_views" id="post-views-input" value="<?php echo esc_attr( $count ); ?>"/><br />
99 <p>
100 <a href="#post-views" class="save-post-views hide-if-no-js button"><?php _e( 'OK', 'post-views-counter' ); ?></a>
101 <a href="#post-views" class="cancel-post-views hide-if-no-js"><?php _e( 'Cancel', 'post-views-counter' ); ?></a>
102 </p>
103
104 </div>
105 <?php
106 }
107 ?>
108
109 </div>
110 <?php
111 }
112
113 /**
114 * Save post views data.
115 *
116 * @param int $post_id
117 * @param object $post
118 * @return void
119 */
120 public function save_post( $post_id, $post = null ) {
121 // break if doing autosave
122 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
123 return;
124
125 // break if current user can't edit this post
126 if ( ! current_user_can( 'edit_post', $post_id ) )
127 return;
128
129 // is post views set
130 if ( ! isset( $_POST['post_views'] ) )
131 return;
132
133 $post_views = $this->sanitize_post_views_input( $_POST['post_views'] );
134
135 if ( is_null( $post_views ) )
136 return;
137
138 // get main instance
139 $pvc = Post_Views_Counter();
140
141 // break if post views in not one of the selected
142 $post_types = (array) $pvc->options['general']['post_types_count'];
143
144 // get post type
145 if ( is_null( $post ) )
146 $post_type = get_post_type( $post_id );
147 else
148 $post_type = $post->post_type;
149
150 // invalid post type?
151 if ( ! in_array( $post_type, $post_types, true ) )
152 return;
153
154 // allow editing
155 $allow_edit = (bool) $pvc->options['display']['restrict_edit_views'];
156
157 // allow editing condition
158 $allow_edit_condition = (bool) current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) );
159
160 // break if views editing not allowed or editing condition not met
161 if ( $allow_edit === false || $allow_edit_condition === false )
162 return;
163
164 // validate data
165 if ( ! isset( $_POST['pvc_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['pvc_nonce'] ) ), 'post_views_count' ) )
166 return;
167
168 $current_post_views = null;
169
170 if ( isset( $_POST['current_post_views'] ) )
171 $current_post_views = $this->sanitize_post_views_input( $_POST['current_post_views'] );
172
173 // unchanged post views value?
174 if ( ! is_null( $current_post_views ) && $post_views === $current_post_views )
175 return;
176
177 // update post views
178 pvc_update_post_views( $post_id, $post_views );
179
180 do_action( 'pvc_after_update_post_views_count', $post_id );
181 }
182
183 /**
184 * Register post views column for specific post types.
185 *
186 * @return void
187 */
188 public function register_new_column() {
189 // get main instance
190 $pvc = Post_Views_Counter();
191
192 // is posts views column active?
193 if ( ! $pvc->options['display']['post_views_column'] )
194 return false;
195
196 // get post types
197 $post_types = $pvc->options['general']['post_types_count'];
198
199 // any post types?
200 if ( ! empty( $post_types ) ) {
201 foreach ( $post_types as $post_type ) {
202 if ( $post_type === 'attachment' ) {
203 // actions
204 add_action( 'manage_media_custom_column', [ $this, 'add_new_column_content' ], 10, 2 );
205
206 // filters
207 add_filter( 'manage_media_columns', [ $this, 'add_new_column' ] );
208 add_filter( 'manage_upload_sortable_columns', [ $this, 'register_sortable_custom_column' ] );
209 } else {
210 // actions
211 add_action( 'manage_' . $post_type . '_posts_custom_column', [ $this, 'add_new_column_content' ], 10, 2 );
212
213 // filters
214 add_filter( 'manage_' . $post_type . '_posts_columns', [ $this, 'add_new_column' ] );
215 add_filter( 'manage_edit-' . $post_type . '_columns', [ $this, 'add_new_column' ], 20 );
216 add_filter( 'manage_edit-' . $post_type . '_sortable_columns', [ $this, 'register_sortable_custom_column' ] );
217
218 // bbPress?
219 if ( class_exists( 'bbPress' ) ) {
220 if ( $post_type === 'forum' )
221 add_filter( 'bbp_admin_forums_column_headers', [ $this, 'add_new_column' ] );
222 elseif ( $post_type === 'topic' )
223 add_filter( 'bbp_admin_topics_column_headers', [ $this, 'add_new_column' ] );
224 }
225 }
226 }
227 }
228 }
229
230 /**
231 * Register sortable post views column.
232 *
233 * @param array $columns
234 * @return array
235 */
236 public function register_sortable_custom_column( $columns ) {
237 global $post_type;
238
239 // get main instance
240 $pvc = Post_Views_Counter();
241 $post_types = (array) $pvc->options['general']['post_types_count'];
242
243 // break if display is disabled
244 if ( ! $pvc->options['display']['post_views_column'] || ! in_array( $post_type, $post_types, true ) )
245 return $columns;
246
247 // check if user can see stats
248 if ( apply_filters( 'pvc_admin_display_post_views', true ) === false )
249 return $columns;
250
251 // add new sortable column
252 $columns['post_views'] = 'post_views';
253
254 return $columns;
255 }
256
257 /**
258 * Add post views column.
259 *
260 * @param array $columns
261 * @return array
262 */
263 public function add_new_column( $columns ) {
264 // date column exists?
265 if ( isset( $columns['date'] ) ) {
266 // store date column
267 $date = $columns['date'];
268
269 // unset date column
270 unset( $columns['date'] );
271 }
272
273 // comments column exists?
274 if ( isset( $columns['comments'] ) ) {
275 // store comments column
276 $comments = $columns['comments'];
277
278 // unset comments column
279 unset( $columns['comments'] );
280 }
281
282 // add post views column
283 $columns['post_views'] = '<span class="pvc-views-header" title="' . esc_attr__( 'Post Views', 'post-views-counter' ) . '"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" fill="currentColor"><path d="M12 2a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h1a1 1 0 0 0 1-1V3a1 1 0 0 0-1-1h-1ZM6.5 6a1 1 0 0 1 1-1h1a1 1 0 0 1 1 1v7a1 1 0 0 1-1 1h-1a1 1 0 0 1-1-1V6ZM2 9a1 1 0 0 1 1-1h1a1 1 0 0 1 1 1v4a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V9Z" /></svg><span class="screen-reader-text">' . esc_attr__( 'Post Views', 'post-views-counter' ) . '</span></span>';
284
285 // restore date column
286 if ( isset( $date ) )
287 $columns['date'] = $date;
288
289 // restore comments column
290 if ( isset( $comments ) )
291 $columns['comments'] = $comments;
292
293 return $columns;
294 }
295
296 /**
297 * Add post views column content.
298 *
299 * @param string $column_name
300 * @param int $id
301 * @return void
302 */
303 public function add_new_column_content( $column_name, $id ) {
304 if ( $column_name === 'post_views' ) {
305 // get total post views
306 $count = (int) pvc_get_post_views( $id );
307
308 // check if user can see stats
309 if ( apply_filters( 'pvc_admin_display_post_views', true, $id ) === false ) {
310 echo '';
311 return;
312 }
313
314 // get post title
315 $post_title = get_the_title( $id );
316
317 if ( $post_title === '' )
318 $post_title = __( '(no title)', 'post-views-counter' );
319
320 // get post type labels
321 $post_type_object = get_post_type_object( get_post_type( $id ) );
322
323 if ( $post_type_object ) {
324 $post_type_labels = get_post_type_labels( $post_type_object );
325 }
326
327 if ( $post_type_labels ) {
328 $post_title = $post_type_labels->singular_name . ': ' . $post_title;
329 }
330
331 // clickable link (modal opening handled via JavaScript)
332 echo '<a href="#" class="pvc-view-chart" data-post-id="' . esc_attr( $id ) . '" data-post-title="' . esc_attr( $post_title ) . '" data-post-views="' . esc_attr( $count ) . '">' . esc_html( $count ) . '</a>';
333 }
334 }
335
336 /**
337 * Handle quick edit.
338 *
339 * @global string $pagenow
340 *
341 * @param string $column_name
342 * @param string $post_type
343 * @return void
344 */
345 function quick_edit_custom_box( $column_name, $post_type ) {
346 global $pagenow, $post;
347
348 if ( $pagenow !== 'edit.php' )
349 return;
350
351 if ( $column_name !== 'post_views' )
352 return;
353
354 if ( ! $post )
355 return;
356
357 // get main instance
358 $pvc = Post_Views_Counter();
359 $post_types = (array) $pvc->options['general']['post_types_count'];
360
361 // break if display is not allowed
362 if ( ! $pvc->options['display']['post_views_column'] || ! in_array( $post_type, $post_types, true ) )
363 return;
364
365 // check if user can see stats
366 if ( apply_filters( 'pvc_admin_display_post_views', true, $post->ID ) === false )
367 return;
368
369 // allow editing
370 $allow_edit = (bool) $pvc->options['display']['restrict_edit_views'];
371
372 // allow editing condition
373 $allow_edit_condition = (bool) current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) );
374 ?>
375 <fieldset class="inline-edit-col-left">
376 <div id="inline-edit-post_views" class="inline-edit-col">
377 <label class="inline-edit-group">
378 <span class="title"><?php _e( 'Post Views', 'post-views-counter' ); ?></span>
379 <?php if ( $allow_edit === true && $allow_edit_condition === true ) { ?>
380 <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value=""></span>
381 <input type="hidden" name="current_post_views" value="" />
382 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
383 <?php } else { ?>
384 <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value="" disabled readonly /></span>
385 <?php } ?>
386 </label>
387 </div>
388 </fieldset>
389 <?php
390 }
391
392 /**
393 * Bulk save post views.
394 *
395 * @return void
396 */
397 function save_bulk_post_views() {
398 $pvc = Post_Views_Counter();
399
400 // check nonce
401 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'pvc_save_bulk_post_views' ) )
402 exit;
403
404 $count = isset( $_POST['post_views'] ) ? $this->sanitize_post_views_input( $_POST['post_views'] ) : null;
405
406 // check post ids
407 $post_ids = ( ! empty( $_POST['post_ids'] ) && is_array( $_POST['post_ids'] ) ) ? array_map( 'absint', wp_unslash( $_POST['post_ids'] ) ) : [];
408
409 if ( is_null( $count ) )
410 exit;
411
412 // allow editing
413 $allow_edit = (bool) $pvc->options['display']['restrict_edit_views'];
414
415 // allow editing condition
416 $allow_edit_condition = (bool) current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) );
417
418 // break if views editing not allowed or editing condition not met
419 if ( $allow_edit === false || $allow_edit_condition === false )
420 exit;
421
422 $post_types = (array) $pvc->options['general']['post_types_count'];
423
424 // any post ids?
425 if ( ! empty( $post_ids ) ) {
426 foreach ( $post_ids as $post_id ) {
427 // break if current user can't edit this post
428 if ( ! current_user_can( 'edit_post', $post_id ) )
429 continue;
430
431 if ( ! in_array( get_post_type( $post_id ), $post_types, true ) )
432 continue;
433
434 if ( pvc_update_post_views( $post_id, $count ) !== false )
435 do_action( 'pvc_after_update_post_views_count', $post_id );
436 }
437 }
438
439 exit;
440 }
441 }
442