PluginProbe ʕ •ᴥ•ʔ
Post Views Counter / 1.7.5
Post Views Counter v1.7.5
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 5 months ago class-columns-modal.php 5 months ago class-columns.php 5 months ago class-counter.php 5 months ago class-crawler-detect.php 5 months ago class-cron.php 5 months ago class-dashboard.php 5 months ago class-frontend.php 5 months ago class-functions.php 5 months ago class-import.php 5 months ago class-integration-gutenberg.php 5 months ago class-integrations.php 5 months ago class-query.php 5 months ago class-settings-api.php 5 months ago class-settings-display.php 5 months ago class-settings-general.php 5 months ago class-settings-integrations.php 5 months ago class-settings-other.php 5 months ago class-settings-reports.php 5 months ago class-settings.php 5 months ago class-toolbar.php 5 months ago class-traffic-signals.php 5 months ago class-update.php 5 months ago class-widgets.php 5 months ago functions.php 5 months ago
class-columns.php
415 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 * Output post views for single post.
31 *
32 * @global object $post
33 *
34 * @return void
35 */
36 public function submitbox_views() {
37 global $post;
38
39 // get main instance
40 $pvc = Post_Views_Counter();
41
42 // break if display is not allowed
43 if ( ! $pvc->options['display']['post_views_column'] || ! in_array( $post->post_type, $pvc->options['general']['post_types_count'] ) )
44 return;
45
46 // check if user can see post stats
47 if ( apply_filters( 'pvc_admin_display_post_views', true, $post->ID ) === false )
48 return;
49
50 // get total post views
51 $count = (int) pvc_get_post_views( $post->ID ); ?>
52
53 <div class="misc-pub-section" id="post-views">
54
55 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
56
57 <span id="post-views-display">
58 <?php echo __( 'Post Views', 'post-views-counter' ) . ': <b>' . number_format_i18n( $count ) . '</b>'; ?>
59 </span>
60
61 <?php
62 // allow editing
63 $allow_edit = (bool) $pvc->options['display']['restrict_edit_views'];
64
65 // allow editing condition
66 $allow_edit_condition = (bool) current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) );
67
68 if ( $allow_edit === true && $allow_edit_condition === true ) {
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 void
97 */
98 public function save_post( $post_id, $post = null ) {
99 // break if doing autosave
100 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
101 return;
102
103 // break if current user can't edit this post
104 if ( ! current_user_can( 'edit_post', $post_id ) )
105 return;
106
107 // is post views set
108 if ( ! isset( $_POST['post_views'] ) )
109 return;
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;
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;
133
134 // allow editing
135 $allow_edit = (bool) $pvc->options['display']['restrict_edit_views'];
136
137 // allow editing condition
138 $allow_edit_condition = (bool) current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) );
139
140 // break if views editing not allowed or editing condition not met
141 if ( $allow_edit === false || $allow_edit_condition === false )
142 return;
143
144 // validate data
145 if ( ! isset( $_POST['pvc_nonce'] ) || ! wp_verify_nonce( $_POST['pvc_nonce'], 'post_views_count' ) )
146 return;
147
148 // update post views
149 pvc_update_post_views( $post_id, $post_views );
150
151 do_action( 'pvc_after_update_post_views_count', $post_id );
152 }
153
154 /**
155 * Register post views column for specific post types.
156 *
157 * @return void
158 */
159 public function register_new_column() {
160 // get main instance
161 $pvc = Post_Views_Counter();
162
163 // is posts views column active?
164 if ( ! $pvc->options['display']['post_views_column'] )
165 return false;
166
167 // get post types
168 $post_types = $pvc->options['general']['post_types_count'];
169
170 // any post types?
171 if ( ! empty( $post_types ) ) {
172 foreach ( $post_types as $post_type ) {
173 if ( $post_type === 'attachment' ) {
174 // actions
175 add_action( 'manage_media_custom_column', [ $this, 'add_new_column_content' ], 10, 2 );
176
177 // filters
178 add_filter( 'manage_media_columns', [ $this, 'add_new_column' ] );
179 add_filter( 'manage_upload_sortable_columns', [ $this, 'register_sortable_custom_column' ] );
180 } else {
181 // actions
182 add_action( 'manage_' . $post_type . '_posts_custom_column', [ $this, 'add_new_column_content' ], 10, 2 );
183
184 // filters
185 add_filter( 'manage_' . $post_type . '_posts_columns', [ $this, 'add_new_column' ] );
186 add_filter( 'manage_edit-' . $post_type . '_columns', [ $this, 'add_new_column' ], 20 );
187 add_filter( 'manage_edit-' . $post_type . '_sortable_columns', [ $this, 'register_sortable_custom_column' ] );
188
189 // bbPress?
190 if ( class_exists( 'bbPress' ) ) {
191 if ( $post_type === 'forum' )
192 add_filter( 'bbp_admin_forums_column_headers', [ $this, 'add_new_column' ] );
193 elseif ( $post_type === 'topic' )
194 add_filter( 'bbp_admin_topics_column_headers', [ $this, 'add_new_column' ] );
195 }
196 }
197 }
198 }
199 }
200
201 /**
202 * Register sortable post views column.
203 *
204 * @param array $columns
205 * @return array
206 */
207 public function register_sortable_custom_column( $columns ) {
208 global $post_type;
209
210 // get main instance
211 $pvc = Post_Views_Counter();
212
213 // break if display is disabled
214 if ( ! $pvc->options['display']['post_views_column'] || ! in_array( $post_type, $pvc->options['general']['post_types_count'] ) )
215 return $columns;
216
217 // check if user can see stats
218 if ( apply_filters( 'pvc_admin_display_post_views', true ) === false )
219 return $columns;
220
221 // add new sortable column
222 $columns['post_views'] = 'post_views';
223
224 return $columns;
225 }
226
227 /**
228 * Add post views column.
229 *
230 * @param array $columns
231 * @return array
232 */
233 public function add_new_column( $columns ) {
234 // date column exists?
235 if ( isset( $columns['date'] ) ) {
236 // store date column
237 $date = $columns['date'];
238
239 // unset date column
240 unset( $columns['date'] );
241 }
242
243 // comments column exists?
244 if ( isset( $columns['comments'] ) ) {
245 // store comments column
246 $comments = $columns['comments'];
247
248 // unset comments column
249 unset( $columns['comments'] );
250 }
251
252 // add post views column
253 $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>';
254
255 // restore date column
256 if ( isset( $date ) )
257 $columns['date'] = $date;
258
259 // restore comments column
260 if ( isset( $comments ) )
261 $columns['comments'] = $comments;
262
263 return $columns;
264 }
265
266 /**
267 * Add post views column content.
268 *
269 * @param string $column_name
270 * @param int $id
271 * @return void
272 */
273 public function add_new_column_content( $column_name, $id ) {
274 if ( $column_name === 'post_views' ) {
275 // get total post views
276 $count = pvc_get_post_views( $id );
277
278 // check if user can see stats
279 if ( apply_filters( 'pvc_admin_display_post_views', true, $id ) === false ) {
280 echo '';
281 return;
282 }
283
284 // get post title
285 $post_title = get_the_title( $id );
286
287 if ( $post_title === '' )
288 $post_title = __( '(no title)', 'post-views-counter' );
289
290 // get post type labels
291 $post_type_object = get_post_type_object( get_post_type( $id ) );
292
293 if ( $post_type_object ) {
294 $post_type_labels = get_post_type_labels( $post_type_object );
295 }
296
297 if ( $post_type_labels ) {
298 $post_title = $post_type_labels->singular_name . ': ' . $post_title;
299 }
300
301 // clickable link (modal opening handled via JavaScript)
302 echo '<a href="#" class="pvc-view-chart" data-post-id="' . esc_attr( $id ) . '" data-post-title="' . esc_attr( $post_title ) . '">' . esc_html( $count ) . '</a>';
303 }
304 }
305
306 /**
307 * Handle quick edit.
308 *
309 * @global string $pagenow
310 *
311 * @param string $column_name
312 * @param string $post_type
313 * @return void
314 */
315 function quick_edit_custom_box( $column_name, $post_type ) {
316 global $pagenow, $post;
317
318 if ( $pagenow !== 'edit.php' )
319 return;
320
321 if ( $column_name !== 'post_views' )
322 return;
323
324 if ( ! $post )
325 return;
326
327 // get main instance
328 $pvc = Post_Views_Counter();
329
330 // break if display is not allowed
331 if ( ! $pvc->options['display']['post_views_column'] || ! in_array( $post_type, $pvc->options['general']['post_types_count'] ) )
332 return;
333
334 // check if user can see stats
335 if ( apply_filters( 'pvc_admin_display_post_views', true, $post->ID ) === false )
336 return;
337
338 // allow editing
339 $allow_edit = (bool) $pvc->options['display']['restrict_edit_views'];
340
341 // allow editing condition
342 $allow_edit_condition = (bool) current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) );
343 ?>
344 <fieldset class="inline-edit-col-left">
345 <div id="inline-edit-post_views" class="inline-edit-col">
346 <label class="inline-edit-group">
347 <span class="title"><?php _e( 'Post Views', 'post-views-counter' ); ?></span>
348 <?php if ( $allow_edit === true && $allow_edit_condition === true ) { ?>
349 <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value=""></span>
350 <input type="hidden" name="current_post_views" value="" />
351 <?php wp_nonce_field( 'post_views_count', 'pvc_nonce' ); ?>
352 <?php } else { ?>
353 <span class="input-text-wrap"><input type="text" name="post_views" class="title text" value="" disabled readonly /></span>
354 <?php } ?>
355 </label>
356 </div>
357 </fieldset>
358 <?php
359 }
360
361 /**
362 * Bulk save post views.
363 *
364 * @global object $wpdb
365 *
366 * @return void
367 */
368 function save_bulk_post_views() {
369 global $wpdb;
370
371 // check nonce
372 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'pvc_save_bulk_post_views' ) )
373 exit;
374
375 $count = null;
376
377 if ( isset( $_POST['post_views'] ) && is_numeric( trim( $_POST['post_views'] ) ) ) {
378 $count = (int) $_POST['post_views'];
379
380 if ( $count < 0 )
381 $count = 0;
382 }
383
384 // check post ids
385 $post_ids = ( ! empty( $_POST['post_ids'] ) && is_array( $_POST['post_ids'] ) ) ? array_map( 'absint', $_POST['post_ids'] ) : [];
386
387 if ( is_null( $count ) )
388 exit;
389
390 // allow editing
391 $allow_edit = (bool) $pvc->options['display']['restrict_edit_views'];
392
393 // allow editing condition
394 $allow_edit_condition = (bool) current_user_can( apply_filters( 'pvc_restrict_edit_capability', 'manage_options' ) );
395
396 // break if views editing not allowed or editing condition not met
397 if ( $allow_edit === false || $allow_edit_condition === false )
398 exit;
399
400 // any post ids?
401 if ( ! empty( $post_ids ) ) {
402 foreach ( $post_ids as $post_id ) {
403 // break if current user can't edit this post
404 if ( ! current_user_can( 'edit_post', $post_id ) )
405 continue;
406
407 // insert or update db post views count
408 $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 ) );
409 }
410 }
411
412 exit;
413 }
414 }
415